Update Preferences

This commit is contained in:
Ash 2022-05-18 03:59:39 +08:00
parent 140cb3ed0a
commit 27e8780d82
13 changed files with 222 additions and 82 deletions

View File

@ -0,0 +1,23 @@
package me.ash.reader.data.preference
import android.content.Context
import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.ash.reader.ui.ext.DataStoreKeys
import me.ash.reader.ui.ext.dataStore
import me.ash.reader.ui.ext.put
object NewVersionDownloadUrlPreference {
const val default = ""
fun put(context: Context, scope: CoroutineScope, value: String) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(DataStoreKeys.NewVersionDownloadUrl, value)
}
}
fun fromPreferences(preferences: Preferences) =
preferences[DataStoreKeys.NewVersionDownloadUrl.key] ?: default
}

View File

@ -0,0 +1,23 @@
package me.ash.reader.data.preference
import android.content.Context
import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.ash.reader.ui.ext.DataStoreKeys
import me.ash.reader.ui.ext.dataStore
import me.ash.reader.ui.ext.put
object NewVersionLogPreference {
const val default = ""
fun put(context: Context, scope: CoroutineScope, value: String) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(DataStoreKeys.NewVersionLog, value)
}
}
fun fromPreferences(preferences: Preferences) =
preferences[DataStoreKeys.NewVersionLog.key] ?: default
}

View File

@ -0,0 +1,25 @@
package me.ash.reader.data.preference
import android.content.Context
import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.ash.reader.data.model.Version
import me.ash.reader.data.model.toVersion
import me.ash.reader.ui.ext.DataStoreKeys
import me.ash.reader.ui.ext.dataStore
import me.ash.reader.ui.ext.put
object NewVersionNumberPreference {
val default = Version()
fun put(context: Context, scope: CoroutineScope, value: String) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(DataStoreKeys.NewVersionNumber, value)
}
}
fun fromPreferences(preferences: Preferences) =
preferences[DataStoreKeys.NewVersionNumber.key].toVersion()
}

View File

@ -0,0 +1,23 @@
package me.ash.reader.data.preference
import android.content.Context
import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.ash.reader.ui.ext.DataStoreKeys
import me.ash.reader.ui.ext.dataStore
import me.ash.reader.ui.ext.put
object NewVersionPublishDatePreference {
const val default = ""
fun put(context: Context, scope: CoroutineScope, value: String) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(DataStoreKeys.NewVersionPublishDate, value)
}
}
fun fromPreferences(preferences: Preferences) =
preferences[DataStoreKeys.NewVersionPublishDate.key] ?: default
}

View File

@ -0,0 +1,28 @@
package me.ash.reader.data.preference
import android.content.Context
import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.ash.reader.ui.ext.DataStoreKeys
import me.ash.reader.ui.ext.dataStore
import me.ash.reader.ui.ext.put
object NewVersionSizePreference {
const val default = ""
fun Int.formatSize(): String =
(this / 1024f / 1024f)
.takeIf { it > 0f }
?.run { " ${String.format("%.2f", this)} MB" } ?: ""
fun put(context: Context, scope: CoroutineScope, value: String) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(DataStoreKeys.NewVersionSize, value)
}
}
fun fromPreferences(preferences: Preferences) =
preferences[DataStoreKeys.NewVersionSize.key] ?: default
}

View File

@ -10,6 +10,13 @@ sealed class Preference {
fun Preferences.toSettings(): Settings {
return Settings(
newVersionNumber = NewVersionNumberPreference.fromPreferences(this),
skipVersionNumber = SkipVersionNumberPreference.fromPreferences(this),
newVersionPublishDate = NewVersionPublishDatePreference.fromPreferences(this),
newVersionLog = NewVersionLogPreference.fromPreferences(this),
newVersionSize = NewVersionSizePreference.fromPreferences(this),
newVersionDownloadUrl = NewVersionDownloadUrlPreference.fromPreferences(this),
themeIndex = ThemeIndexPreference.fromPreferences(this),
customPrimaryColor = CustomPrimaryColorPreference.fromPreferences(this),
darkTheme = DarkThemePreference.fromPreferences(this),

View File

@ -7,10 +7,18 @@ import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.flow.map
import me.ash.reader.data.model.Version
import me.ash.reader.ui.ext.collectAsStateValue
import me.ash.reader.ui.ext.dataStore
data class Settings(
val newVersionNumber: Version = NewVersionNumberPreference.default,
val skipVersionNumber: Version = SkipVersionNumberPreference.default,
val newVersionPublishDate: String = NewVersionPublishDatePreference.default,
val newVersionLog: String = NewVersionLogPreference.default,
val newVersionSize: String = NewVersionSizePreference.default,
val newVersionDownloadUrl: String = NewVersionDownloadUrlPreference.default,
val themeIndex: Int = ThemeIndexPreference.default,
val customPrimaryColor: String = CustomPrimaryColorPreference.default,
val darkTheme: DarkThemePreference = DarkThemePreference.default,
@ -53,6 +61,13 @@ fun SettingsProvider(
}.collectAsStateValue(initial = Settings())
CompositionLocalProvider(
LocalNewVersionNumber provides settings.newVersionNumber,
LocalSkipVersionNumber provides settings.skipVersionNumber,
LocalNewVersionPublishDate provides settings.newVersionPublishDate,
LocalNewVersionLog provides settings.newVersionLog,
LocalNewVersionSize provides settings.newVersionSize,
LocalNewVersionDownloadUrl provides settings.newVersionDownloadUrl,
LocalThemeIndex provides settings.themeIndex,
LocalCustomPrimaryColor provides settings.customPrimaryColor,
LocalDarkTheme provides settings.darkTheme,
@ -85,6 +100,13 @@ fun SettingsProvider(
}
}
val LocalNewVersionNumber = compositionLocalOf { NewVersionNumberPreference.default }
val LocalSkipVersionNumber = compositionLocalOf { SkipVersionNumberPreference.default }
val LocalNewVersionPublishDate = compositionLocalOf { NewVersionPublishDatePreference.default }
val LocalNewVersionLog = compositionLocalOf { NewVersionLogPreference.default }
val LocalNewVersionSize = compositionLocalOf { NewVersionSizePreference.default }
val LocalNewVersionDownloadUrl = compositionLocalOf { NewVersionDownloadUrlPreference.default }
val LocalThemeIndex =
compositionLocalOf { ThemeIndexPreference.default }
val LocalCustomPrimaryColor =

View File

@ -0,0 +1,25 @@
package me.ash.reader.data.preference
import android.content.Context
import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.ash.reader.data.model.Version
import me.ash.reader.data.model.toVersion
import me.ash.reader.ui.ext.DataStoreKeys
import me.ash.reader.ui.ext.dataStore
import me.ash.reader.ui.ext.put
object SkipVersionNumberPreference {
val default = Version()
fun put(context: Context, scope: CoroutineScope, value: String) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(DataStoreKeys.SkipVersionNumber, value)
}
}
fun fromPreferences(preferences: Preferences) =
preferences[DataStoreKeys.SkipVersionNumber.key].toVersion()
}

View File

@ -11,10 +11,15 @@ import me.ash.reader.R
import me.ash.reader.data.model.toVersion
import me.ash.reader.data.module.DispatcherIO
import me.ash.reader.data.module.DispatcherMain
import me.ash.reader.data.preference.*
import me.ash.reader.data.preference.NewVersionSizePreference.formatSize
import me.ash.reader.data.source.AppNetworkDataSource
import me.ash.reader.data.source.Download
import me.ash.reader.data.source.downloadToFileWithProgress
import me.ash.reader.ui.ext.*
import me.ash.reader.ui.ext.getCurrentVersion
import me.ash.reader.ui.ext.getLatestApk
import me.ash.reader.ui.ext.showToast
import me.ash.reader.ui.ext.skipVersionNumber
import javax.inject.Inject
class AppRepository @Inject constructor(
@ -46,7 +51,7 @@ class AppRepository @Inject constructor(
}
val latest = response.body()!!
val latestVersion = latest.tag_name.toVersion()
// val latestVersion = "0.7.3".toVersion()
// val latestVersion = "1.0.0".toVersion()
val skipVersion = context.skipVersionNumber.toVersion()
val currentVersion = context.getCurrentVersion()
val latestLog = latest.body ?: ""
@ -57,14 +62,11 @@ class AppRepository @Inject constructor(
Log.i("RLog", "current version $currentVersion")
if (latestVersion.whetherNeedUpdate(currentVersion, skipVersion)) {
Log.i("RLog", "new version $latestVersion")
context.dataStore.put(
DataStoreKeys.NewVersionNumber,
latestVersion.toString()
)
context.dataStore.put(DataStoreKeys.NewVersionLog, latestLog)
context.dataStore.put(DataStoreKeys.NewVersionPublishDate, latestPublishDate)
context.dataStore.put(DataStoreKeys.NewVersionSize, latestSize)
context.dataStore.put(DataStoreKeys.NewVersionDownloadUrl, latestDownloadUrl)
NewVersionNumberPreference.put(context, this, latestVersion.toString())
NewVersionLogPreference.put(context, this, latestLog)
NewVersionPublishDatePreference.put(context, this, latestPublishDate)
NewVersionSizePreference.put(context, this, latestSize.formatSize())
NewVersionDownloadUrlPreference.put(context, this, latestDownloadUrl)
true
} else {
false

View File

@ -15,16 +15,6 @@ import java.io.IOException
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
val Context.newVersionPublishDate: String
get() = this.dataStore.get(DataStoreKeys.NewVersionPublishDate) ?: ""
val Context.newVersionLog: String
get() = this.dataStore.get(DataStoreKeys.NewVersionLog) ?: ""
val Context.newVersionSize: Int
get() = this.dataStore.get(DataStoreKeys.NewVersionSize) ?: 0
val Context.newVersionDownloadUrl: String
get() = this.dataStore.get(DataStoreKeys.NewVersionDownloadUrl) ?: ""
val Context.newVersionNumber: String
get() = this.dataStore.get(DataStoreKeys.NewVersionNumber) ?: ""
val Context.skipVersionNumber: String
get() = this.dataStore.get(DataStoreKeys.SkipVersionNumber) ?: ""
val Context.isFirstLaunch: Boolean
@ -93,9 +83,9 @@ sealed class DataStoreKeys<T> {
get() = stringPreferencesKey("newVersionLog")
}
object NewVersionSize : DataStoreKeys<Int>() {
override val key: Preferences.Key<Int>
get() = intPreferencesKey("newVersionSize")
object NewVersionSize : DataStoreKeys<String>() {
override val key: Preferences.Key<String>
get() = stringPreferencesKey("newVersionSize")
}
object NewVersionDownloadUrl : DataStoreKeys<String>() {

View File

@ -1,5 +1,6 @@
package me.ash.reader.ui.page.home.feeds
import android.annotation.SuppressLint
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
@ -25,19 +26,20 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import kotlinx.coroutines.flow.map
import me.ash.reader.R
import me.ash.reader.data.model.getName
import me.ash.reader.data.model.toVersion
import me.ash.reader.data.preference.*
import me.ash.reader.data.repository.SyncWorker.Companion.getIsSyncing
import me.ash.reader.ui.component.FilterBar
import me.ash.reader.ui.component.base.Banner
import me.ash.reader.ui.component.base.DisplayText
import me.ash.reader.ui.component.base.FeedbackIconButton
import me.ash.reader.ui.component.base.Subtitle
import me.ash.reader.ui.ext.*
import me.ash.reader.ui.ext.collectAsStateValue
import me.ash.reader.ui.ext.findActivity
import me.ash.reader.ui.ext.getCurrentVersion
import me.ash.reader.ui.ext.surfaceColorAtElevation
import me.ash.reader.ui.page.common.RouteName
import me.ash.reader.ui.component.FilterBar
import me.ash.reader.ui.page.home.FilterState
import me.ash.reader.ui.page.home.HomeViewAction
import me.ash.reader.ui.page.home.HomeViewModel
@ -48,6 +50,7 @@ import me.ash.reader.ui.page.home.feeds.subscribe.SubscribeViewAction
import me.ash.reader.ui.page.home.feeds.subscribe.SubscribeViewModel
import me.ash.reader.ui.theme.palette.onDark
@SuppressLint("FlowOperatorInvokedInComposition")
@OptIn(
ExperimentalMaterial3Api::class, com.google.accompanist.pager.ExperimentalPagerApi::class,
androidx.compose.foundation.ExperimentalFoundationApi::class
@ -71,16 +74,8 @@ fun FeedsPage(
val feedsViewState = feedsViewModel.viewState.collectAsStateValue()
val filterState = homeViewModel.filterState.collectAsStateValue()
val skipVersion = context.dataStore.data
.map { it[DataStoreKeys.SkipVersionNumber.key] ?: "" }
.collectAsState(initial = "")
.value
.toVersion()
val latestVersion = context.dataStore.data
.map { it[DataStoreKeys.NewVersionNumber.key] ?: "" }
.collectAsState(initial = "")
.value
.toVersion()
val newVersion = LocalNewVersionNumber.current
val skipVersion = LocalSkipVersionNumber.current
val currentVersion by remember { mutableStateOf(context.getCurrentVersion()) }
val owner = LocalLifecycleOwner.current
@ -145,7 +140,7 @@ fun FeedsPage(
imageVector = Icons.Outlined.Settings,
contentDescription = stringResource(R.string.settings),
tint = MaterialTheme.colorScheme.onSurface,
showBadge = latestVersion.whetherNeedUpdate(currentVersion, skipVersion),
showBadge = newVersion.whetherNeedUpdate(currentVersion, skipVersion),
) {
navController.navigate(RouteName.SETTINGS) {
launchSingleTop = true

View File

@ -9,7 +9,10 @@ import androidx.compose.material.icons.outlined.*
import androidx.compose.material.icons.rounded.ArrowBack
import androidx.compose.material.icons.rounded.Close
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
@ -17,14 +20,12 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import kotlinx.coroutines.flow.map
import me.ash.reader.R
import me.ash.reader.data.model.toVersion
import me.ash.reader.data.preference.LocalNewVersionNumber
import me.ash.reader.data.preference.LocalSkipVersionNumber
import me.ash.reader.ui.component.base.Banner
import me.ash.reader.ui.component.base.DisplayText
import me.ash.reader.ui.component.base.FeedbackIconButton
import me.ash.reader.ui.ext.DataStoreKeys
import me.ash.reader.ui.ext.dataStore
import me.ash.reader.ui.ext.getCurrentVersion
import me.ash.reader.ui.page.common.RouteName
import me.ash.reader.ui.page.settings.tips.UpdateDialog
@ -40,16 +41,8 @@ fun SettingsPage(
updateViewModel: UpdateViewModel = hiltViewModel(),
) {
val context = LocalContext.current
val skipVersion = context.dataStore.data
.map { it[DataStoreKeys.SkipVersionNumber.key] ?: "" }
.collectAsState(initial = "")
.value
.toVersion()
val latestVersion = context.dataStore.data
.map { it[DataStoreKeys.NewVersionNumber.key] ?: "" }
.collectAsState(initial = "")
.value
.toVersion()
val newVersion = LocalNewVersionNumber.current
val skipVersion = LocalSkipVersionNumber.current
val currentVersion by remember { mutableStateOf(context.getCurrentVersion()) }
Scaffold(
@ -81,13 +74,13 @@ fun SettingsPage(
}
item {
Box {
if (latestVersion.whetherNeedUpdate(currentVersion, skipVersion)) {
if (newVersion.whetherNeedUpdate(currentVersion, skipVersion)) {
Banner(
modifier = Modifier.zIndex(1f),
title = stringResource(R.string.get_new_updates),
desc = stringResource(
R.string.get_new_updates_desc,
latestVersion.toString(),
newVersion.toString(),
),
icon = Icons.Outlined.Lightbulb,
action = {

View File

@ -31,12 +31,12 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import me.ash.reader.R
import me.ash.reader.data.preference.*
import me.ash.reader.data.source.Download
import me.ash.reader.ui.component.base.Dialog
import me.ash.reader.ui.ext.*
import me.ash.reader.ui.ext.collectAsStateValue
import me.ash.reader.ui.ext.installLatestApk
@SuppressLint("FlowOperatorInvokedInComposition")
@Composable
@ -47,24 +47,10 @@ fun UpdateDialog(
val viewState = updateViewModel.viewState.collectAsStateValue()
val downloadState = viewState.downloadFlow.collectAsState(initial = Download.NotYet).value
val scope = rememberCoroutineScope { Dispatchers.IO }
val newVersionNumber = context.dataStore.data
.map { it[DataStoreKeys.NewVersionNumber.key] ?: "" }
.collectAsState(initial = "")
.value
val newVersionPublishDate = context.dataStore.data
.map { it[DataStoreKeys.NewVersionPublishDate.key] ?: "" }
.collectAsState(initial = "")
.value
val newVersionLog = context.dataStore.data
.map { it[DataStoreKeys.NewVersionLog.key] ?: "" }
.collectAsState(initial = "")
.value
val newVersionSize = " " + context.dataStore.data
.map { it[DataStoreKeys.NewVersionSize.key] ?: 0 }
.map { it / 1024f / 1024f }
.map { if (it > 0f) " ${String.format("%.2f", it)} MB" else "" }
.collectAsState(initial = 0)
.value
val newVersionNumber = LocalNewVersionNumber.current
val newVersionPublishDate = LocalNewVersionPublishDate.current
val newVersionLog = LocalNewVersionLog.current
val newVersionSize = LocalNewVersionSize.current
val settings = rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult()
@ -104,7 +90,7 @@ fun UpdateDialog(
Text(text = stringResource(R.string.change_log))
Spacer(modifier = Modifier.height(16.dp))
Text(
text = "$newVersionPublishDate$newVersionSize",
text = "$newVersionPublishDate $newVersionSize",
color = MaterialTheme.colorScheme.outline.copy(alpha = 0.7f),
style = MaterialTheme.typography.bodyMedium,
)
@ -125,7 +111,7 @@ fun UpdateDialog(
context.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse(context.getString(R.string.github_link)),
Uri.parse("${context.getString(R.string.github_link)}/releases/latest"),
)
)
// Disable automatic updates in F-Droid
@ -162,10 +148,8 @@ fun UpdateDialog(
if (downloadState !is Download.Progress) {
TextButton(
onClick = {
scope.launch {
context.dataStore.put(DataStoreKeys.SkipVersionNumber, newVersionNumber)
updateViewModel.dispatch(UpdateViewAction.Hide)
}
SkipVersionNumberPreference.put(context, scope, newVersionNumber.toString())
updateViewModel.dispatch(UpdateViewAction.Hide)
}
) {
Text(text = stringResource(R.string.skip_this_version))