mirror of
https://github.com/PhilKes/NotallyX.git
synced 2025-06-28 20:29:54 +00:00
Trigger moving database if dataOnExternalStorage changed by import
This commit is contained in:
parent
b55c01c94a
commit
02e97768ce
4 changed files with 31 additions and 5 deletions
|
@ -135,7 +135,7 @@ class SettingsFragment : Fragment() {
|
||||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||||
if (result.resultCode == RESULT_OK) {
|
if (result.resultCode == RESULT_OK) {
|
||||||
result.data?.data?.let { uri ->
|
result.data?.data?.let { uri ->
|
||||||
if (model.preferences.import(requireContext(), uri)) {
|
if (model.importPreferences(requireContext(), uri)) {
|
||||||
showToast(R.string.import_settings_success)
|
showToast(R.string.import_settings_success)
|
||||||
} else {
|
} else {
|
||||||
showToast(R.string.import_settings_failure)
|
showToast(R.string.import_settings_failure)
|
||||||
|
@ -446,7 +446,7 @@ class SettingsFragment : Fragment() {
|
||||||
}
|
}
|
||||||
ResetSettings.setOnClickListener {
|
ResetSettings.setOnClickListener {
|
||||||
showDialog(R.string.reset_settings_message, R.string.reset_settings) { _, _ ->
|
showDialog(R.string.reset_settings_message, R.string.reset_settings) { _, _ ->
|
||||||
model.preferences.reset()
|
model.resetPreferences()
|
||||||
showToast(R.string.reset_settings_success)
|
showToast(R.string.reset_settings_success)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.philkes.notallyx.presentation.viewmodel
|
package com.philkes.notallyx.presentation.viewmodel
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
@ -187,7 +188,7 @@ class BaseNoteModel(private val app: Application) : AndroidViewModel(app) {
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
val database = NotallyDatabase.getDatabase(app, observePreferences = false).value
|
val database = NotallyDatabase.getDatabase(app, observePreferences = false).value
|
||||||
database.checkpoint()
|
database.checkpoint()
|
||||||
NotallyDatabase.getCurrentDatabaseFile(app)
|
NotallyDatabase.getInternalDatabaseFile(app)
|
||||||
.copyTo(NotallyDatabase.getExternalDatabaseFile(app), overwrite = true)
|
.copyTo(NotallyDatabase.getExternalDatabaseFile(app), overwrite = true)
|
||||||
}
|
}
|
||||||
savePreference(preferences.dataOnExternalStorage, true)
|
savePreference(preferences.dataOnExternalStorage, true)
|
||||||
|
@ -199,7 +200,7 @@ class BaseNoteModel(private val app: Application) : AndroidViewModel(app) {
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
val database = NotallyDatabase.getDatabase(app, observePreferences = false).value
|
val database = NotallyDatabase.getDatabase(app, observePreferences = false).value
|
||||||
database.checkpoint()
|
database.checkpoint()
|
||||||
NotallyDatabase.getCurrentDatabaseFile(app)
|
NotallyDatabase.getExternalDatabaseFile(app)
|
||||||
.copyTo(NotallyDatabase.getInternalDatabaseFile(app), overwrite = true)
|
.copyTo(NotallyDatabase.getInternalDatabaseFile(app), overwrite = true)
|
||||||
NotallyDatabase.getExternalDatabaseFiles(app).forEach {
|
NotallyDatabase.getExternalDatabaseFiles(app).forEach {
|
||||||
if (it.exists()) {
|
if (it.exists()) {
|
||||||
|
@ -464,6 +465,30 @@ class BaseNoteModel(private val app: Application) : AndroidViewModel(app) {
|
||||||
viewModelScope.launch(Dispatchers.IO) { function() }
|
viewModelScope.launch(Dispatchers.IO) { function() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun resetPreferences() {
|
||||||
|
preferences.reset()
|
||||||
|
refreshDataOnExternalStorage()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun importPreferences(context: Context, uri: Uri): Boolean {
|
||||||
|
val success = preferences.import(context, uri)
|
||||||
|
refreshDataOnExternalStorage()
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun refreshDataOnExternalStorage() {
|
||||||
|
val dataOnExternalStorageBefore = preferences.dataOnExternalStorage.value
|
||||||
|
val dataOnExternalStorageAfter = preferences.dataOnExternalStorage.getFreshValue()
|
||||||
|
if (dataOnExternalStorageBefore != dataOnExternalStorageAfter) {
|
||||||
|
if (dataOnExternalStorageAfter) {
|
||||||
|
enableExternalData()
|
||||||
|
} else {
|
||||||
|
disableExternalData()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
preferences.dataOnExternalStorage.refresh()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
fun transform(list: List<BaseNote>, pinned: Header, others: Header): List<Item> {
|
fun transform(list: List<BaseNote>, pinned: Header, others: Header): List<Item> {
|
||||||
|
|
|
@ -234,7 +234,6 @@ class NotallyXPreferences private constructor(private val app: Application) {
|
||||||
listItemSorting,
|
listItemSorting,
|
||||||
labelsHiddenInNavigation,
|
labelsHiddenInNavigation,
|
||||||
autoBackup,
|
autoBackup,
|
||||||
dataOnExternalStorage,
|
|
||||||
dateFormat,
|
dateFormat,
|
||||||
maxItems,
|
maxItems,
|
||||||
maxTitle,
|
maxTitle,
|
||||||
|
|
|
@ -88,6 +88,8 @@ abstract class BasePreference<T>(
|
||||||
cachedValue = null
|
cachedValue = null
|
||||||
getData().postValue(value)
|
getData().postValue(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getFreshValue() = getValue(sharedPreferences)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> BasePreference<T>.observeForeverSkipFirst(observer: Observer<T>) {
|
fun <T> BasePreference<T>.observeForeverSkipFirst(observer: Observer<T>) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue