Skip to content

Commit ceca5f5

Browse files
committed
refactor:migrate K9.isHideTimeZone to PreferenceDataStore
1 parent 9af37fa commit ceca5f5

File tree

18 files changed

+342
-178
lines changed

18 files changed

+342
-178
lines changed

core/preference/api/src/commonMain/kotlin/net/thunderbird/core/preference/GeneralSettings.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.thunderbird.core.preference
22

3+
import net.thunderbird.core.preference.privacy.PrivacySettings
4+
35
/**
46
* Stores a snapshot of the app's general settings.
57
*
@@ -33,6 +35,7 @@ data class GeneralSettings(
3335
val isThreadedViewEnabled: Boolean,
3436
val isUseMessageViewFixedWidthFont: Boolean,
3537
val isAutoFitWidth: Boolean,
38+
val privacy: PrivacySettings,
3639
)
3740

3841
enum class BackgroundSync {

core/preference/api/src/commonMain/kotlin/net/thunderbird/core/preference/GeneralSettingsManager.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package net.thunderbird.core.preference
22

33
import kotlinx.coroutines.flow.Flow
4+
import net.thunderbird.core.preference.privacy.PrivacySettingsManager
45

56
/**
67
* Retrieve and modify general settings.
78
*
89
* TODO: Add more settings as needed.
910
*/
10-
interface GeneralSettingsManager {
11+
interface GeneralSettingsManager : PrivacySettingsManager {
1112
fun getSettings(): GeneralSettings
1213
fun getSettingsFlow(): Flow<GeneralSettings>
1314

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.thunderbird.core.preference.privacy
2+
3+
class DefaultPrivacySettingsManager(
4+
private val preferenceManager: PrivacySettingsPreferenceManager,
5+
) : PrivacySettingsManager {
6+
override val privacySettings: PrivacySettings
7+
get() = preferenceManager.getConfig()
8+
9+
override fun setIsHideTimeZone(isHideTimeZone: Boolean) {
10+
val privacySettings = preferenceManager.getConfig()
11+
preferenceManager.save(privacySettings.copy(isHideTimeZone = isHideTimeZone))
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package net.thunderbird.core.preference.privacy
2+
3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.SupervisorJob
7+
import kotlinx.coroutines.channels.awaitClose
8+
import kotlinx.coroutines.flow.Flow
9+
import kotlinx.coroutines.flow.MutableStateFlow
10+
import kotlinx.coroutines.flow.callbackFlow
11+
import kotlinx.coroutines.flow.launchIn
12+
import kotlinx.coroutines.flow.onEach
13+
import kotlinx.coroutines.flow.update
14+
import kotlinx.coroutines.launch
15+
import kotlinx.coroutines.sync.Mutex
16+
import kotlinx.coroutines.sync.withLock
17+
import net.thunderbird.core.preference.PreferenceChangeBroker
18+
import net.thunderbird.core.preference.PreferenceChangeSubscriber
19+
import net.thunderbird.core.preference.storage.Storage
20+
import net.thunderbird.core.preference.storage.StorageEditor
21+
22+
class DefaultPrivacySettingsPreferenceManager(
23+
private val storage: Storage,
24+
private val storageEditor: StorageEditor,
25+
private val changeBroker: PreferenceChangeBroker,
26+
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO,
27+
private var scope: CoroutineScope = CoroutineScope(SupervisorJob()),
28+
) : PrivacySettingsPreferenceManager {
29+
private val configState: MutableStateFlow<PrivacySettings> = MutableStateFlow(value = loadConfig())
30+
private val mutex = Mutex()
31+
32+
init {
33+
asSettingsFlow()
34+
.onEach { config ->
35+
configState.update { config }
36+
}
37+
.launchIn(scope)
38+
}
39+
40+
override fun save(config: PrivacySettings) {
41+
configState.update { config }
42+
writeConfig(config)
43+
}
44+
45+
override fun getConfig(): PrivacySettings = configState.value
46+
override fun getConfigFlow(): Flow<PrivacySettings> = configState
47+
48+
// Not 100% sure if this method is really required, but applied the same logic
49+
// present in the RealDrawerConfigManager implementation
50+
private fun asSettingsFlow(): Flow<PrivacySettings> {
51+
return callbackFlow {
52+
send(loadConfig())
53+
54+
val subscriber = PreferenceChangeSubscriber {
55+
configState.update { loadConfig() }
56+
}
57+
58+
changeBroker.subscribe(subscriber)
59+
60+
awaitClose {
61+
changeBroker.unsubscribe(subscriber)
62+
}
63+
}
64+
}
65+
66+
private fun loadConfig(): PrivacySettings = PrivacySettings(
67+
isHideTimeZone = storage.getBoolean(KEY_HIDE_TIME_ZONE, false),
68+
)
69+
70+
private fun writeConfig(config: PrivacySettings) {
71+
scope.launch(ioDispatcher) {
72+
mutex.withLock {
73+
storageEditor.putBoolean(KEY_HIDE_TIME_ZONE, config.isHideTimeZone)
74+
storageEditor.commit()
75+
}
76+
}
77+
}
78+
79+
companion object {
80+
private const val KEY_HIDE_TIME_ZONE = "hideTimeZone"
81+
}
82+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.thunderbird.core.preference.privacy
2+
3+
data class PrivacySettings(
4+
val isHideTimeZone: Boolean,
5+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.thunderbird.core.preference.privacy
2+
3+
interface PrivacySettingsManager {
4+
val privacySettings: PrivacySettings
5+
6+
fun setIsHideTimeZone(isHideTimeZone: Boolean)
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.thunderbird.core.preference.privacy
2+
3+
import net.thunderbird.core.preference.PreferenceManager
4+
5+
interface PrivacySettingsPreferenceManager : PreferenceManager<PrivacySettings>

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,6 @@ object K9 : KoinComponent {
205205
@JvmStatic
206206
var isHideUserAgent = false
207207

208-
@JvmStatic
209-
var isHideTimeZone = false
210-
211208
@get:Synchronized
212209
@set:Synchronized
213210
@JvmStatic
@@ -319,7 +316,6 @@ object K9 : KoinComponent {
319316
messageViewPostMarkAsUnreadNavigation =
320317
storage.getEnum("messageViewPostMarkAsUnreadAction", PostMarkAsUnreadNavigation.ReturnToMessageList)
321318
isHideUserAgent = storage.getBoolean("hideUserAgent", false)
322-
isHideTimeZone = storage.getBoolean("hideTimeZone", false)
323319

324320
isConfirmDelete = storage.getBoolean("confirmDelete", false)
325321
isConfirmDiscardMessage = storage.getBoolean("confirmDiscardMessage", true)
@@ -397,7 +393,6 @@ object K9 : KoinComponent {
397393
editor.putEnum("messageViewPostDeleteAction", messageViewPostRemoveNavigation)
398394
editor.putEnum("messageViewPostMarkAsUnreadAction", messageViewPostMarkAsUnreadNavigation)
399395
editor.putBoolean("hideUserAgent", isHideUserAgent)
400-
editor.putBoolean("hideTimeZone", isHideTimeZone)
401396

402397
editor.putString("language", k9Language)
403398

legacy/core/src/main/java/com/fsck/k9/autocrypt/AutocryptTransferMessageCreator.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fsck.k9.autocrypt
22

3-
import com.fsck.k9.K9
43
import com.fsck.k9.mail.Address
54
import com.fsck.k9.mail.Flag
65
import com.fsck.k9.mail.Message
@@ -13,8 +12,12 @@ import com.fsck.k9.mail.internet.MimeMultipart
1312
import com.fsck.k9.mail.internet.TextBody
1413
import com.fsck.k9.mailstore.BinaryMemoryBody
1514
import java.util.Date
15+
import net.thunderbird.core.preference.GeneralSettingsManager
1616

17-
class AutocryptTransferMessageCreator(private val stringProvider: AutocryptStringProvider) {
17+
class AutocryptTransferMessageCreator(
18+
private val stringProvider: AutocryptStringProvider,
19+
private val generalSettingsManager: GeneralSettingsManager,
20+
) {
1821
fun createAutocryptTransferMessage(data: ByteArray, address: Address): Message {
1922
try {
2023
val subjectText = stringProvider.transferMessageSubject()
@@ -41,7 +44,10 @@ class AutocryptTransferMessageCreator(private val stringProvider: AutocryptStrin
4144
message.subject = subjectText
4245
message.setHeader("Autocrypt-Setup-Message", "v1")
4346
message.internalDate = nowDate
44-
message.addSentDate(nowDate, K9.isHideTimeZone)
47+
message.addSentDate(
48+
nowDate,
49+
generalSettingsManager.getSettings().privacy.isHideTimeZone,
50+
)
4551
message.setFrom(address)
4652
message.setHeader("To", address.toEncodedString())
4753

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package com.fsck.k9.autocrypt
33
import org.koin.dsl.module
44

55
val autocryptModule = module {
6-
single { AutocryptTransferMessageCreator(get()) }
6+
single {
7+
AutocryptTransferMessageCreator(
8+
stringProvider = get(),
9+
generalSettingsManager = get(),
10+
)
11+
}
712
single { AutocryptDraftStateHeaderParser() }
813
}

0 commit comments

Comments
 (0)