-
Notifications
You must be signed in to change notification settings - Fork 114
Implement Conjugate App Scribe-Server data download functionality (#565) #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prince-0408
wants to merge
3
commits into
scribe-org:main
Choose a base branch
from
prince-0408:feature/conjugate-app-data-download-565
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
acac249
Implement Conjugate App Scribe-Server data download functionality (#565)
prince-0408 d39cd03
Merge branch 'main' into feature/conjugate-app-data-download-565
prince-0408 9bc17ee
Merge branch 'main' into feature/conjugate-app-data-download-565
angrezichatterbox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
app/src/main/java/be/scri/data/remote/ConjugateDynamicDbHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| package be.scri.data.remote | ||
|
|
||
| import android.content.ContentValues | ||
| import android.content.Context | ||
| import android.database.sqlite.SQLiteDatabase | ||
| import android.database.sqlite.SQLiteException | ||
| import android.database.sqlite.SQLiteOpenHelper | ||
| import android.util.Log | ||
| import be.scri.data.model.DataResponse | ||
|
|
||
| /** | ||
| * Helper class for managing dynamic SQLite databases for conjugate data (verbs only). | ||
| * It creates only the verbs table and inserts verb data according to the provided DataResponse. | ||
| */ | ||
| class ConjugateDynamicDbHelper( | ||
| context: Context, | ||
| language: String, | ||
| ) : SQLiteOpenHelper(context, "${language.uppercase()}ConjugateData.sqlite", null, 1) { | ||
| override fun onCreate(db: SQLiteDatabase) { | ||
| // Tables are created dynamically via syncConjugateDatabase from API contract. | ||
| } | ||
|
|
||
| override fun onUpgrade( | ||
| db: SQLiteDatabase, | ||
| old: Int, | ||
| new: Int, | ||
| ) { | ||
| // Dynamic schema updates are handled via syncConjugateDatabase. | ||
| } | ||
|
|
||
| /** | ||
| * Synchronizes the conjugate database schema and data based on the provided DataResponse. | ||
| * Only creates the verbs table and inserts verb data, ignoring other data types. | ||
| * @param response The data response containing the contract and data to be inserted. | ||
| */ | ||
| fun syncConjugateDatabase(response: DataResponse) { | ||
| val db = writableDatabase | ||
| try { | ||
| db.beginTransaction() | ||
|
|
||
| // Check if verbs table exists in the contract | ||
| val verbsColumns = response.contract.fields["verbs"] | ||
| if (verbsColumns != null) { | ||
| // Create verbs table | ||
| val colDefinition = verbsColumns.keys.joinToString(", ") { "$it TEXT" } | ||
| db.execSQL("DROP TABLE IF EXISTS verbs") | ||
| db.execSQL( | ||
| "CREATE TABLE verbs " + | ||
| "(id INTEGER PRIMARY KEY AUTOINCREMENT, $colDefinition)", | ||
| ) | ||
|
|
||
| // Insert verbs data | ||
| val verbsData = response.data["verbs"] | ||
| if (verbsData != null) { | ||
| val cv = ContentValues() | ||
| verbsData.forEach { row -> | ||
| cv.clear() | ||
| row.forEach { (key, value) -> | ||
| cv.put(key, value?.toString() ?: "") | ||
| } | ||
| db.insert("verbs", null, cv) | ||
| } | ||
| Log.i("CONJUGATE_DB", "Successfully synced ${verbsData.size} verb records for ${response.language}") | ||
| } else { | ||
| Log.w("CONJUGATE_DB", "No verbs data found in response for ${response.language}") | ||
| } | ||
| } else { | ||
| Log.e("CONJUGATE_DB", "No verbs table found in contract for ${response.language}") | ||
| } | ||
|
|
||
| db.setTransactionSuccessful() | ||
| } catch (e: SQLiteException) { | ||
| Log.e("CONJUGATE_DB", "Error during conjugate database sync: ${e.message}") | ||
| throw e | ||
| } finally { | ||
| db.endTransaction() | ||
| db.close() | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.