Skip to content

Commit 6004e72

Browse files
committed
refactor: migrate K9.isSyncLoggingEnabled to PreferenceDataStore
1 parent 016da71 commit 6004e72

File tree

11 files changed

+52
-32
lines changed

11 files changed

+52
-32
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/job/KoinModule.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ val jobModule = module {
5050
baseLogger = get<Logger>(),
5151
fileLogSink = get<FileLogSink>(named("syncDebug")),
5252
syncDebugCompositeSink = get<CompositeLogSink>(named("syncDebug")),
53-
parameters,
53+
generalSettingsManager = get(),
54+
parameters = parameters,
5455
)
5556
}
5657
}

legacy/core/src/main/java/com/fsck/k9/job/SyncDebugWorker.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ package com.fsck.k9.job
33
import android.content.Context
44
import androidx.work.CoroutineWorker
55
import androidx.work.WorkerParameters
6-
import com.fsck.k9.K9
76
import java.io.IOException
87
import net.thunderbird.core.logging.Logger
98
import net.thunderbird.core.logging.composite.CompositeLogSink
109
import net.thunderbird.core.logging.file.FileLogSink
10+
import net.thunderbird.core.preference.GeneralSettingsManager
11+
import net.thunderbird.core.preference.update
1112

1213
class SyncDebugWorker(
1314
context: Context,
1415
val baseLogger: Logger,
1516
val fileLogSink: FileLogSink,
1617
val syncDebugCompositeSink: CompositeLogSink,
1718
parameters: WorkerParameters,
19+
val generalSettingsManager: GeneralSettingsManager,
1820
) : CoroutineWorker(context, parameters) {
1921

2022
override suspend fun doWork(): Result {
@@ -25,8 +27,9 @@ class SyncDebugWorker(
2527
return Result.failure()
2628
}
2729
syncDebugCompositeSink.manager.remove(fileLogSink)
28-
K9.isSyncLoggingEnabled = false
29-
K9.saveSettingsAsync()
30+
generalSettingsManager.update { settings ->
31+
settings.copy(debugging = settings.debugging.copy(isSyncLoggingEnabled = false))
32+
}
3033
return Result.success()
3134
}
3235
}

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(

0 commit comments

Comments
 (0)