Skip to content

Commit cf4d6a4

Browse files
committed
refactor: migrate K9.isSyncLoggingEnabled to PreferenceDataStore
1 parent 8e9a3b2 commit cf4d6a4

File tree

9 files changed

+44
-28
lines changed

9 files changed

+44
-28
lines changed

core/logging/impl-legacy/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ kotlin {
1010
sourceSets {
1111
androidMain.dependencies {
1212
implementation(libs.timber)
13+
implementation(projects.core.logging.implComposite)
14+
implementation(projects.core.logging.implFile)
1315
}
1416

1517
commonMain.dependencies {
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
package net.thunderbird.core.logging.legacy
22

3+
import net.thunderbird.core.logging.composite.CompositeLogSink
4+
import net.thunderbird.core.logging.file.FileLogSink
35
import timber.log.Timber
6+
import timber.log.Timber.DebugTree
47

58
// TODO: Implementation https://github.com/thunderbird/thunderbird-android/issues/9573
6-
class DebugLogConfigurator {
7-
9+
class DebugLogConfigurator(
10+
private val syncDebugCompositeSink: CompositeLogSink,
11+
private val syncDebugFileLogSink: FileLogSink,
12+
) {
813
fun updateLoggingStatus(isDebugLoggingEnabled: Boolean) {
914
Timber.uprootAll()
1015
if (isDebugLoggingEnabled) {
11-
Timber.plant(Timber.DebugTree())
16+
Timber.plant(DebugTree())
17+
}
18+
}
19+
20+
fun updateSyncLogging(isSyncLoggingEnabled: Boolean) {
21+
if (isSyncLoggingEnabled) {
22+
syncDebugCompositeSink.manager.add(syncDebugFileLogSink)
23+
} else {
24+
syncDebugCompositeSink.manager.remove(syncDebugFileLogSink)
1225
}
1326
}
1427
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package net.thunderbird.core.preference.debugging
22

3+
const val DEBUGGING_SETTINGS_DEFAULT_IS_SYNC_LOGGING_ENABLED = false
34
val DEBUGGING_SETTINGS_DEFAULT_IS_DEBUGGING_LOGGING_ENABLED = isDebug
45

56
data class DebuggingSettings(
67
val isDebugLoggingEnabled: Boolean = DEBUGGING_SETTINGS_DEFAULT_IS_DEBUGGING_LOGGING_ENABLED,
8+
val isSyncLoggingEnabled: Boolean = DEBUGGING_SETTINGS_DEFAULT_IS_SYNC_LOGGING_ENABLED,
79
)

core/preference/api/src/commonMain/kotlin/net/thunderbird/core/preference/debugging/DebuggingSettingsPreferenceManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package net.thunderbird.core.preference.debugging
33
import net.thunderbird.core.preference.PreferenceManager
44

55
const val KEY_ENABLE_DEBUG_LOGGING = "enableDebugLogging"
6+
const val KEY_ENABLE_SYNC_DEBUG_LOGGING = "enableSyncDebugLogging"
67

78
interface DebuggingSettingsPreferenceManager : PreferenceManager<DebuggingSettings>

core/preference/impl/src/commonMain/kotlin/net/thunderbird/core/preference/debugging/DefaultDebuggingSettingsPreferenceManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ class DefaultDebuggingSettingsPreferenceManager(
4040
KEY_ENABLE_DEBUG_LOGGING,
4141
DEBUGGING_SETTINGS_DEFAULT_IS_DEBUGGING_LOGGING_ENABLED,
4242
),
43+
isSyncLoggingEnabled = storage.getBoolean(
44+
KEY_ENABLE_SYNC_DEBUG_LOGGING,
45+
DEBUGGING_SETTINGS_DEFAULT_IS_SYNC_LOGGING_ENABLED,
46+
),
4347
)
4448

4549
private fun writeConfig(config: DebuggingSettings) {
4650
logger.debug(TAG) { "writeConfig() called with: config = $config" }
4751
scope.launch(ioDispatcher) {
4852
mutex.withLock {
4953
storageEditor.putBoolean(KEY_ENABLE_DEBUG_LOGGING, config.isDebugLoggingEnabled)
54+
storageEditor.putBoolean(KEY_ENABLE_SYNC_DEBUG_LOGGING, config.isSyncLoggingEnabled)
5055
storageEditor.commit().also { commited ->
5156
logger.verbose(TAG) { "writeConfig: storageEditor.commit() resulted in: $commited" }
5257
}

legacy/core/src/main/java/com/fsck/k9/K9.kt

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,18 @@ import net.thunderbird.core.common.action.SwipeAction
1616
import net.thunderbird.core.common.action.SwipeActions
1717
import net.thunderbird.core.featureflag.FeatureFlagProvider
1818
import net.thunderbird.core.featureflag.toFeatureFlagKey
19-
import net.thunderbird.core.logging.composite.CompositeLogSink
20-
import net.thunderbird.core.logging.file.FileLogSink
2119
import net.thunderbird.core.preference.storage.Storage
2220
import net.thunderbird.core.preference.storage.StorageEditor
2321
import net.thunderbird.core.preference.storage.getEnumOrDefault
2422
import org.koin.core.component.KoinComponent
2523
import org.koin.core.component.inject
26-
import org.koin.core.qualifier.named
2724
import timber.log.Timber
2825

2926
// TODO "Use GeneralSettingsManager and GeneralSettings instead"
3027
object K9 : KoinComponent {
3128
private val generalSettingsManager: DefaultGeneralSettingsManager by inject()
3229
private val telemetryManager: TelemetryManager by inject()
3330
private val featureFlagProvider: FeatureFlagProvider by inject()
34-
private val syncDebugCompositeSink: CompositeLogSink by inject(named("syncDebug"))
35-
private val syncDebugFileLogSink: FileLogSink by inject(named("syncDebug"))
3631

3732
/**
3833
* Name of the [SharedPreferences] file used to store the last known version of the
@@ -118,13 +113,6 @@ object K9 : KoinComponent {
118113
}
119114
}
120115

121-
@JvmStatic
122-
var isSyncLoggingEnabled: Boolean = false
123-
set(debug) {
124-
field = debug
125-
updateSyncLogging()
126-
}
127-
128116
@JvmStatic
129117
var isSensitiveDebugLoggingEnabled: Boolean = false
130118

@@ -261,7 +249,6 @@ object K9 : KoinComponent {
261249
@JvmStatic
262250
@Suppress("LongMethod")
263251
fun loadPrefs(storage: Storage) {
264-
isSyncLoggingEnabled = storage.getBoolean("enableSyncDebugLogging", false)
265252
isSensitiveDebugLoggingEnabled = storage.getBoolean("enableSensitiveLogging", false)
266253
isUseVolumeKeysForNavigation = storage.getBoolean("useVolumeKeysForNavigation", false)
267254
isShowAccountSelector = storage.getBoolean("showAccountSelector", true)
@@ -330,7 +317,6 @@ object K9 : KoinComponent {
330317

331318
@Suppress("LongMethod")
332319
internal fun save(editor: StorageEditor) {
333-
editor.putBoolean("enableSyncDebugLogging", isSyncLoggingEnabled)
334320
editor.putBoolean("enableSensitiveLogging", isSensitiveDebugLoggingEnabled)
335321
editor.putBoolean("useVolumeKeysForNavigation", isUseVolumeKeysForNavigation)
336322
editor.putBoolean("notificationDuringQuietTimeEnabled", isNotificationDuringQuietTimeEnabled)
@@ -379,14 +365,6 @@ object K9 : KoinComponent {
379365
fontSizes.save(editor)
380366
}
381367

382-
private fun updateSyncLogging() {
383-
if (isSyncLoggingEnabled) {
384-
syncDebugCompositeSink.manager.add(syncDebugFileLogSink)
385-
} else {
386-
syncDebugCompositeSink.manager.remove(syncDebugFileLogSink)
387-
}
388-
}
389-
390368
@JvmStatic
391369
fun saveSettingsAsync() {
392370
generalSettingsManager.saveSettingsAsync()

legacy/core/src/main/java/com/fsck/k9/preferences/DefaultGeneralSettingsManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ internal class DefaultGeneralSettingsManager(
7676
debugging = debuggingSettings,
7777
).also {
7878
debugLogConfigurator.updateLoggingStatus(debuggingSettings.isDebugLoggingEnabled)
79+
debugLogConfigurator.updateSyncLogging(debuggingSettings.isSyncLoggingEnabled)
7980
}
8081
}
8182
.stateIn(

legacy/core/src/main/java/com/fsck/k9/preferences/KoinModule.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ val preferencesModule = module {
7474
)
7575
}
7676
single<DebugLogConfigurator> {
77-
DebugLogConfigurator()
77+
DebugLogConfigurator(
78+
syncDebugCompositeSink = get(named("syncDebug")),
79+
syncDebugFileLogSink = get(named("syncDebug")),
80+
)
7881
}
7982
single {
8083
DefaultGeneralSettingsManager(

legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/general/GeneralSettingsDataStore.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class GeneralSettingsDataStore(
7171
"privacy_hide_useragent" -> generalSettingsManager.getConfig().privacy.isHideUserAgent
7272
"privacy_hide_timezone" -> generalSettingsManager.getConfig().privacy.isHideTimeZone
7373
"debug_logging" -> generalSettingsManager.getConfig().debugging.isDebugLoggingEnabled
74-
"sync_debug_logging" -> K9.isSyncLoggingEnabled
74+
"sync_debug_logging" -> generalSettingsManager.getConfig().debugging.isSyncLoggingEnabled
7575
"sensitive_logging" -> K9.isSensitiveDebugLoggingEnabled
7676
"volume_navigation" -> K9.isUseVolumeKeysForNavigation
7777
"enable_telemetry" -> K9.isTelemetryEnabled
@@ -111,7 +111,7 @@ class GeneralSettingsDataStore(
111111
"privacy_hide_useragent" -> setIsHideUserAgent(isHideUserAgent = value)
112112
"privacy_hide_timezone" -> setIsHideTimeZone(isHideTimeZone = value)
113113
"debug_logging" -> setIsDebugLoggingEnabled(isDebugLoggingEnabled = value)
114-
"sync_debug_logging" -> K9.isSyncLoggingEnabled = value
114+
"sync_debug_logging" -> setIsSyncLoggingEnabled(isSyncLoggingEnabled = value)
115115
"sensitive_logging" -> K9.isSensitiveDebugLoggingEnabled = value
116116
"volume_navigation" -> K9.isUseVolumeKeysForNavigation = value
117117
"enable_telemetry" -> setTelemetryEnabled(value)
@@ -479,6 +479,17 @@ class GeneralSettingsDataStore(
479479
}
480480
}
481481

482+
private fun setIsSyncLoggingEnabled(isSyncLoggingEnabled: Boolean) {
483+
skipSaveSettings = true
484+
generalSettingsManager.update { settings ->
485+
settings.copy(
486+
debugging = settings.debugging.copy(
487+
isSyncLoggingEnabled = isSyncLoggingEnabled,
488+
),
489+
)
490+
}
491+
}
492+
482493
private fun setIsHideUserAgent(isHideUserAgent: Boolean) {
483494
skipSaveSettings = true
484495
generalSettingsManager.update { settings ->

0 commit comments

Comments
 (0)