mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-18 16:25:27 +02:00
Backup calculator history and system font
This commit is contained in:
parent
f5fdf181a2
commit
04f3351741
@ -24,6 +24,7 @@ import androidx.core.content.FileProvider
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import com.sadellie.unitto.data.database.CalculatorHistoryDao
|
||||
import com.sadellie.unitto.data.database.TimeZoneDao
|
||||
import com.sadellie.unitto.data.database.UnitsDao
|
||||
import com.sadellie.unitto.data.userprefs.PrefsKeys
|
||||
@ -46,6 +47,7 @@ import com.sadellie.unitto.data.userprefs.getRpnMode
|
||||
import com.sadellie.unitto.data.userprefs.getSeparator
|
||||
import com.sadellie.unitto.data.userprefs.getShownUnitGroups
|
||||
import com.sadellie.unitto.data.userprefs.getStartingScreen
|
||||
import com.sadellie.unitto.data.userprefs.getSystemFont
|
||||
import com.sadellie.unitto.data.userprefs.getThemingMode
|
||||
import com.sadellie.unitto.data.userprefs.getUnitConverterFavoritesOnly
|
||||
import com.sadellie.unitto.data.userprefs.getUnitConverterFormatTime
|
||||
@ -66,10 +68,9 @@ import javax.inject.Inject
|
||||
class BackupManager @Inject constructor(
|
||||
@ApplicationContext private val mContext: Context,
|
||||
private val dataStore: DataStore<Preferences>,
|
||||
private val calculatorHistoryDao: CalculatorHistoryDao,
|
||||
private val unitsDao: UnitsDao,
|
||||
private val timeZoneDao: TimeZoneDao,
|
||||
// Not planned at the moment
|
||||
// private val calculatorHistoryDao: CalculatorHistoryDao,
|
||||
) {
|
||||
private val moshi: Moshi = Moshi.Builder()
|
||||
.add(UserDataTableAdapter())
|
||||
@ -102,6 +103,7 @@ class BackupManager @Inject constructor(
|
||||
val userData: UserData = jsonAdapter.fromJson(jsonContent.toString()) ?: return@withContext IllegalArgumentException("Can't parse: $jsonContent")
|
||||
|
||||
// Apply tables
|
||||
updateCalculatorHistoryTable(userData)
|
||||
updateUnitsTable(userData)
|
||||
updateTimeZonesTable(userData)
|
||||
|
||||
@ -113,6 +115,7 @@ class BackupManager @Inject constructor(
|
||||
|
||||
internal suspend fun userDataFromApp(): UserData {
|
||||
val data = dataStore.data.first()
|
||||
val calculatorHistoryTable = calculatorHistoryDao.getAllDescending().first()
|
||||
val unitsTableData = unitsDao.getAllFlow().first()
|
||||
val timeZoneTableData = timeZoneDao.getFavorites().first()
|
||||
|
||||
@ -124,6 +127,7 @@ class BackupManager @Inject constructor(
|
||||
monetMode = data.getMonetMode(),
|
||||
startingScreen = data.getStartingScreen(),
|
||||
enableToolsExperiment = data.getEnableToolsExperiment(),
|
||||
systemFont = data.getSystemFont(),
|
||||
enableVibrations = data.getEnableVibrations(),
|
||||
middleZero = data.getMiddleZero(),
|
||||
acButton = data.getAcButton(),
|
||||
@ -140,6 +144,7 @@ class BackupManager @Inject constructor(
|
||||
unitConverterFavoritesOnly = data.getUnitConverterFavoritesOnly(),
|
||||
unitConverterFormatTime = data.getUnitConverterFormatTime(),
|
||||
unitConverterSorting = data.getUnitConverterSorting().name,
|
||||
calculatorHistoryTable = calculatorHistoryTable,
|
||||
unitsTable = unitsTableData,
|
||||
timeZoneTable = timeZoneTableData,
|
||||
)
|
||||
@ -155,6 +160,7 @@ class BackupManager @Inject constructor(
|
||||
it[PrefsKeys.MONET_MODE] = userData.monetMode
|
||||
it[PrefsKeys.STARTING_SCREEN] = userData.startingScreen
|
||||
it[PrefsKeys.ENABLE_TOOLS_EXPERIMENT] = userData.enableToolsExperiment
|
||||
it[PrefsKeys.SYSTEM_FONT] = userData.systemFont
|
||||
it[PrefsKeys.ENABLE_VIBRATIONS] = userData.enableVibrations
|
||||
it[PrefsKeys.MIDDLE_ZERO] = userData.middleZero
|
||||
it[PrefsKeys.AC_BUTTON] = userData.acButton
|
||||
@ -180,6 +186,11 @@ class BackupManager @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
internal suspend fun updateCalculatorHistoryTable(userData: UserData) {
|
||||
calculatorHistoryDao.clear()
|
||||
userData.calculatorHistoryTable.forEach { calculatorHistoryDao.insert(it) }
|
||||
}
|
||||
|
||||
internal suspend fun updateUnitsTable(userData: UserData) {
|
||||
unitsDao.clear()
|
||||
userData.unitsTable.forEach { unitsDao.insertUnit(it) }
|
||||
|
@ -18,6 +18,7 @@
|
||||
|
||||
package com.sadellie.unitto.data.backup
|
||||
|
||||
import com.sadellie.unitto.data.database.CalculatorHistoryEntity
|
||||
import com.sadellie.unitto.data.database.TimeZoneEntity
|
||||
import com.sadellie.unitto.data.database.UnitsEntity
|
||||
import com.squareup.moshi.Json
|
||||
@ -33,6 +34,7 @@ internal data class UserData(
|
||||
@Json(name = "monetMode") val monetMode: String,
|
||||
@Json(name = "startingScreen") val startingScreen: String,
|
||||
@Json(name = "enableToolsExperiment") val enableToolsExperiment: Boolean,
|
||||
@Json(name = "systemFont") val systemFont: Boolean,
|
||||
@Json(name = "enableVibrations") val enableVibrations: Boolean,
|
||||
@Json(name = "middleZero") val middleZero: Boolean,
|
||||
@Json(name = "acButton") val acButton: Boolean,
|
||||
@ -53,6 +55,7 @@ internal data class UserData(
|
||||
@Json(name = "unitConverterFormatTime") val unitConverterFormatTime: Boolean,
|
||||
@Json(name = "unitConverterSorting") val unitConverterSorting: String,
|
||||
|
||||
@Json(name = "calculatorHistoryTable") val calculatorHistoryTable: List<CalculatorHistoryEntity>,
|
||||
@Json(name = "unitsTable") val unitsTable: List<UnitsEntity>,
|
||||
@Json(name = "timeZoneTable") val timeZoneTable: List<TimeZoneEntity>,
|
||||
)
|
||||
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Unitto is a unit converter for Android
|
||||
* Copyright (c) 2023 Elshan Agaev
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sadellie.unitto.data.backup
|
||||
|
||||
import com.squareup.moshi.JsonClass
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
internal data class UserDataCalculatorHistory(
|
||||
val entityId: Int,
|
||||
val timestamp: Long,
|
||||
val expression: String,
|
||||
val result: String,
|
||||
)
|
@ -18,6 +18,7 @@
|
||||
|
||||
package com.sadellie.unitto.data.backup
|
||||
|
||||
import com.sadellie.unitto.data.database.CalculatorHistoryEntity
|
||||
import com.sadellie.unitto.data.database.TimeZoneEntity
|
||||
import com.sadellie.unitto.data.database.UnitsEntity
|
||||
import com.squareup.moshi.FromJson
|
||||
@ -26,6 +27,24 @@ import com.squareup.moshi.ToJson
|
||||
// Have to use this wrapper since entity classes are in database module
|
||||
@Suppress("UNUSED")
|
||||
internal class UserDataTableAdapter {
|
||||
@ToJson
|
||||
fun toJson(calculatorHistoryEntity: CalculatorHistoryEntity): UserDataCalculatorHistory =
|
||||
UserDataCalculatorHistory(
|
||||
entityId = calculatorHistoryEntity.entityId,
|
||||
timestamp = calculatorHistoryEntity.timestamp,
|
||||
expression = calculatorHistoryEntity.expression,
|
||||
result = calculatorHistoryEntity.result
|
||||
)
|
||||
|
||||
@FromJson
|
||||
fun fromJson(userDataCalculatorHistory: UserDataCalculatorHistory): CalculatorHistoryEntity =
|
||||
CalculatorHistoryEntity(
|
||||
entityId = userDataCalculatorHistory.entityId,
|
||||
timestamp = userDataCalculatorHistory.timestamp,
|
||||
expression = userDataCalculatorHistory.expression,
|
||||
result = userDataCalculatorHistory.result
|
||||
)
|
||||
|
||||
@ToJson
|
||||
fun toJson(unitsEntity: UnitsEntity): UserDataUnit =
|
||||
UserDataUnit(
|
||||
|
Loading…
x
Reference in New Issue
Block a user