Move crash toggle to about

This commit is contained in:
Rafał Kobyłko 2023-05-15 18:29:40 +02:00
parent 739eb1004a
commit 6148592e2b
6 changed files with 55 additions and 19 deletions

View File

@ -86,6 +86,7 @@ class Strings(c: Context) {
val aboutTitle = c.getString(R.string.settings__about)
val aboutGeneral = c.getString(R.string.settings__general)
val aboutShare = c.getString(R.string.settings__share_app)
val aboutSendCrashes = c.getString(R.string.settings__about_crash_optout_title)
val aboutWriteReview = c.getString(R.string.settings__write_a_review)
val aboutPrivacyPolicy = c.getString(R.string.settings__privacy_policy)
val aboutTerms = c.getString(R.string.settings__terms_of_service)

View File

@ -6,12 +6,14 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.core.app.ShareCompat
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.twofasapp.designsystem.R
import com.twofasapp.designsystem.TwIcons
import com.twofasapp.designsystem.TwTheme
@ -20,6 +22,7 @@ import com.twofasapp.designsystem.ktx.openSafely
import com.twofasapp.designsystem.settings.SettingsDivider
import com.twofasapp.designsystem.settings.SettingsHeader
import com.twofasapp.designsystem.settings.SettingsLink
import com.twofasapp.designsystem.settings.SettingsSwitch
import com.twofasapp.locale.TwLocale
import org.koin.androidx.compose.koinViewModel
@ -28,18 +31,25 @@ internal fun AboutRoute(
openLicenses: () -> Unit,
viewModel: AboutViewModel = koinViewModel()
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
AboutScreen(
uiState = uiState,
versionName = viewModel.versionName,
onLicensesClick = openLicenses,
onReviewClick = { viewModel.reviewDone() }
onReviewClick = { viewModel.reviewDone() },
onSendCrashLogsToggle = { viewModel.toggleSendCrashLogs() }
)
}
@Composable
private fun AboutScreen(
uiState: AboutUiState,
versionName: String,
onLicensesClick: () -> Unit,
onReviewClick: () -> Unit,
onSendCrashLogsToggle: () -> Unit,
) {
val activity = LocalContext.current as Activity
val uriHandler = LocalUriHandler.current
@ -91,6 +101,20 @@ private fun AboutScreen(
.startChooser()
}
}
item { SettingsDivider() }
item { SettingsHeader(title = TwLocale.strings.aboutSendCrashes) }
item {
SettingsSwitch(
title = TwLocale.strings.settingsSendCrashes,
subtitle = TwLocale.strings.settingsSendCrashesBody,
icon = TwIcons.Settings,
checked = uiState.appSettings.sendCrashLogs,
onCheckedChange = { onSendCrashLogsToggle() }
)
}
}
SettingsLink(

View File

@ -0,0 +1,7 @@
package com.twofasapp.feature.about.ui.about
import com.twofasapp.data.session.domain.AppSettings
data class AboutUiState(
val appSettings: AppSettings = AppSettings(),
)

View File

@ -6,15 +6,31 @@ import com.twofasapp.common.coroutines.Dispatchers
import com.twofasapp.common.environment.AppBuild
import com.twofasapp.common.environment.BuildVariant
import com.twofasapp.common.ktx.camelCaseBeginLower
import com.twofasapp.common.ktx.launchScoped
import com.twofasapp.data.session.SessionRepository
import com.twofasapp.data.session.SettingsRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
internal class AboutViewModel(
private val dispatchers: Dispatchers,
private val appBuild: AppBuild,
private val sessionRepository: SessionRepository,
private val settingsRepository: SettingsRepository,
) : ViewModel() {
val uiState = MutableStateFlow(AboutUiState())
init {
launchScoped {
settingsRepository.observeAppSettings()
.collect { appSettings ->
uiState.update { it.copy(appSettings = appSettings) }
}
}
}
val versionName =
if (appBuild.buildVariant != BuildVariant.Release) {
"${appBuild.versionName} (${appBuild.buildVariant.name.camelCaseBeginLower()})"
@ -31,4 +47,10 @@ internal class AboutViewModel(
// rateAppStatusPreference.get().copy(counterStarted = Instant.now(), counterReached = Instant.now())
// )
}
fun toggleSendCrashLogs() {
launchScoped {
settingsRepository.setSendCrashLogs(uiState.value.appSettings.sendCrashLogs.not())
}
}
}

View File

@ -40,7 +40,6 @@ internal fun AppSettingsRoute(
onShowNextTokenToggle = { viewModel.toggleShowNextToken() },
onShowBackupNoticeToggle = { viewModel.toggleShowBackupNotice() },
onAutoFocusSearchToggle = { viewModel.toggleAutoFocusSearch() },
onSendCrashLogsToggle = { viewModel.toggleSendCrashLogs() },
)
}
@ -53,7 +52,6 @@ private fun AppSettingsScreen(
onShowNextTokenToggle: () -> Unit,
onShowBackupNoticeToggle: () -> Unit,
onAutoFocusSearchToggle: () -> Unit,
onSendCrashLogsToggle: () -> Unit,
) {
val activity = LocalContext.currentActivity
var showThemeDialog by remember { mutableStateOf(false) }
@ -125,16 +123,6 @@ private fun AppSettingsScreen(
icon = TwIcons.CloudOff,
)
}
item {
SettingsSwitch(
title = TwLocale.strings.settingsSendCrashes,
subtitle = TwLocale.strings.settingsSendCrashesBody,
icon = TwIcons.Settings,
checked = uiState.appSettings.sendCrashLogs,
onCheckedChange = { onSendCrashLogsToggle() }
)
}
}
if (showThemeDialog) {

View File

@ -61,12 +61,6 @@ internal class AppSettingsViewModel(
}
}
fun toggleSendCrashLogs() {
launchScoped {
settingsRepository.setSendCrashLogs(uiState.value.appSettings.sendCrashLogs.not())
}
}
fun consumeEvent(event: AppSettingsUiEvent) {
uiState.update { it.copy(events = it.events.minus(event)) }
}