-
Notifications
You must be signed in to change notification settings - Fork 19
Import and export data #34
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
odweta
wants to merge
27
commits into
LibreFitOrg:main
Choose a base branch
from
odweta:feat/import-and-export-data
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
27 commits
Select commit
Hold shift + click to select a range
760813f
add: import & export buttons in SettingsScreen
mnjxclone c17da6d
update: add import and export functionality
mnjxclone daed80b
progress on a migration from raw SQL backup/restore to a JSON arch.
mnjxclone 1d61653
update: JSON data export/import
mnjxclone e8bb260
fix: remove temporary logging
mnjxclone 8e2aed6
fix: remove to-be-deprecated content
mnjxclone 3bc0c36
fix: proper use of ioDispatcher
mnjxclone a6b6f97
fix: remove unused imports
mnjxclone eb46859
fix: remove redundant lib in libs.versions.toml
mnjxclone f1ee9b5
fix: repair build.gradle.kts
mnjxclone 70c7dc0
fix: use standard naming conventions
mnjxclone cab0cc9
fix: use correct serializer for LocalDateTime
mnjxclone aab175e
fix: remove unused variables
mnjxclone 703cfb7
fix: revert prev commit
mnjxclone 3d0f7aa
fix: remove unnecessary upsert methods
mnjxclone 3ecf5e0
fix: correct export/import pipeline (but allows duplicates)
mnjxclone e7f57bc
fix: convert toasts to snackbars
mnjxclone 0d1f0c6
resolve merge conflicts with upstream
mnjxclone 7d6d1cf
add: missing generic savePreferences function
mnjxclone 3988156
add: datastore dependency
mnjxclone 2240b70
fix: correct jvm version
mnjxclone 67efb57
update: use dialogs instead of snackbars
mnjxclone d7000c9
fix: fix linted errors
mnjxclone 29d6a42
fix: add proper migration logic
mnjxclone 01a3c42
fix: fix typo and add animations to loading
mnjxclone edb8a62
fix: separated concerns regarding SettingsEvents
mnjxclone 48d9855
fix: remove redundant datastore dependency
mnjxclone 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
Some comments aren't visible on the classic Files Changed page.
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
67 changes: 67 additions & 0 deletions
67
app/src/main/java/org/librefit/db/repository/ImportExportRepository.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,67 @@ | ||
| package org.librefit.db.repository | ||
|
|
||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.net.Uri | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import org.librefit.db.AppDatabase | ||
| import java.io.File | ||
| import javax.inject.Inject | ||
| import kotlin.system.exitProcess | ||
|
|
||
| class ImportExportRepository @Inject constructor( | ||
| private val db: AppDatabase, | ||
| @ApplicationContext private val context: Context | ||
| ) { | ||
| suspend fun exportTo(uri: Uri) = withContext(Dispatchers.IO) { | ||
| val sqliteDb = db.openHelper.writableDatabase | ||
|
|
||
| val tempFile = File(context.cacheDir, "backup.db") | ||
|
|
||
| val path = tempFile.absolutePath.replace("'", "''") | ||
| sqliteDb.execSQL("VACUUM INTO '$path'") | ||
|
|
||
| context.contentResolver.openOutputStream(uri)?.use { out -> | ||
| tempFile.inputStream().use { input -> | ||
| input.copyTo(out) | ||
| } | ||
| } | ||
|
|
||
| tempFile.delete() | ||
| } | ||
|
|
||
| suspend fun importFrom(uri: Uri): Nothing = withContext(Dispatchers.IO) { | ||
| val dbFile = context.getDatabasePath(AppDatabase.NAME) | ||
|
|
||
| AppDatabase.closeInstance() | ||
|
|
||
| val tempFile = File(context.cacheDir, "restore.db") | ||
|
|
||
| context.contentResolver.openInputStream(uri)?.use { input -> | ||
| tempFile.outputStream().use { output -> | ||
| input.copyTo(output) | ||
| } | ||
| } | ||
|
|
||
| val wal = File(dbFile.path + "-wal") | ||
| val shm = File(dbFile.path + "-shm") | ||
|
|
||
| wal.delete() | ||
| shm.delete() | ||
|
|
||
| tempFile.copyTo(dbFile, overwrite = true) | ||
| tempFile.delete() | ||
|
|
||
| AppDatabase.getInstance(context) // Reinitialize with the new DB file | ||
| // restart app process cleanly | ||
| val intent = context.packageManager | ||
| .getLaunchIntentForPackage(context.packageName) | ||
|
|
||
| intent?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) | ||
|
|
||
| context.startActivity(intent) | ||
| exitProcess(0) | ||
| } | ||
| } | ||
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,7 @@ | ||
| package org.librefit.ui.models | ||
|
|
||
| sealed interface BackupEvent { | ||
| object BackupSuccess : BackupEvent | ||
| object RestoreSuccess : BackupEvent | ||
| data class Error(val message: String) : BackupEvent | ||
| } |
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
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,24 @@ | ||
| <!-- | ||
| ~ Copyright (C) 2026 The Android Open Source Project | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ you may not use this file except in compliance with the License. | ||
| ~ You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="960" | ||
| android:viewportHeight="960"> | ||
| <path | ||
| android:pathData="M260,800q-91,0 -155.5,-63T40,583q0,-78 47,-139t123,-78q25,-92 100,-149t170,-57q117,0 198.5,81.5T760,440q69,8 114.5,59.5T920,620q0,75 -52.5,127.5T740,800L520,800q-33,0 -56.5,-23.5T440,720v-206l-64,62 -56,-56 160,-160 160,160 -56,56 -64,-62v206h220q42,0 71,-29t29,-71q0,-42 -29,-71t-71,-29h-60v-80q0,-83 -58.5,-141.5T480,240q-83,0 -141.5,58.5T280,440h-20q-58,0 -99,41t-41,99q0,58 41,99t99,41h100v80L260,800ZM480,520Z" | ||
| android:fillColor="#000000"/> | ||
| </vector> |
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,24 @@ | ||
| <!-- | ||
| ~ Copyright (C) 2026 The Android Open Source Project | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ you may not use this file except in compliance with the License. | ||
| ~ You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="960" | ||
| android:viewportHeight="960"> | ||
| <path | ||
| android:pathData="M260,800q-91,0 -155.5,-63T40,583q0,-78 47,-139t123,-78q17,-72 85,-137t145,-65q33,0 56.5,23.5T520,244v242l64,-62 56,56 -160,160 -160,-160 56,-56 64,62v-242q-76,14 -118,73.5T280,440h-20q-58,0 -99,41t-41,99q0,58 41,99t99,41h480q42,0 71,-29t29,-71q0,-42 -29,-71t-71,-29h-60v-80q0,-48 -22,-89.5T600,280v-93q74,35 117,103.5T760,440q69,8 114.5,59.5T920,620q0,75 -52.5,127.5T740,800L260,800ZM480,442Z" | ||
| android:fillColor="#000000"/> | ||
| </vector> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.