Remove FolderRepositoryManager

Instead we use one FolderRepository instance and pass Account as parameter to individual functions.
This commit is contained in:
cketti 2021-08-26 22:28:36 +02:00
parent e0fe1a2dfa
commit f2f8e4d0f3
28 changed files with 102 additions and 133 deletions

View file

@ -7,7 +7,7 @@ import com.fsck.k9.backend.BackendManager
import com.fsck.k9.backend.api.BackendPusher
import com.fsck.k9.backend.api.BackendPusherCallback
import com.fsck.k9.controller.MessagingController
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -20,11 +20,10 @@ internal class AccountPushController(
private val backendManager: BackendManager,
private val messagingController: MessagingController,
private val preferences: Preferences,
folderRepositoryManager: FolderRepositoryManager,
private val folderRepository: FolderRepository,
backgroundDispatcher: CoroutineDispatcher = Dispatchers.IO,
private val account: Account
) {
private val folderRepository = folderRepositoryManager.getFolderRepository(account)
private val coroutineScope = CoroutineScope(backgroundDispatcher)
@Volatile
@ -76,7 +75,7 @@ internal class AccountPushController(
private fun startListeningForPushFolders() {
coroutineScope.launch {
folderRepository.getPushFoldersFlow().collect { remoteFolders ->
folderRepository.getPushFoldersFlow(account).collect { remoteFolders ->
val folderServerIds = remoteFolders.map { it.serverId }
updatePushFolders(folderServerIds)
}

View file

@ -4,12 +4,12 @@ import com.fsck.k9.Account
import com.fsck.k9.Preferences
import com.fsck.k9.backend.BackendManager
import com.fsck.k9.controller.MessagingController
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
internal class AccountPushControllerFactory(
private val backendManager: BackendManager,
private val messagingController: MessagingController,
private val folderRepositoryManager: FolderRepositoryManager,
private val folderRepository: FolderRepository,
private val preferences: Preferences
) {
fun create(account: Account): AccountPushController {
@ -17,7 +17,7 @@ internal class AccountPushControllerFactory(
backendManager,
messagingController,
preferences,
folderRepositoryManager,
folderRepository,
account = account
)
}

View file

@ -10,7 +10,7 @@ internal val controllerPushModule = module {
AccountPushControllerFactory(
backendManager = get(),
messagingController = get(),
folderRepositoryManager = get(),
folderRepository = get(),
preferences = get()
)
}

View file

@ -22,14 +22,14 @@ class AutoExpandFolderBackendFoldersRefreshListener(
}
private fun checkAutoExpandFolder() {
val folderId = account.importedAutoExpandFolder?.let { folderRepository.getFolderId(it) }
val folderId = account.importedAutoExpandFolder?.let { folderRepository.getFolderId(account, it) }
if (folderId != null) {
account.autoExpandFolderId = folderId
return
}
account.autoExpandFolderId?.let { autoExpandFolderId ->
if (!folderRepository.isFolderPresent(autoExpandFolderId)) {
if (!folderRepository.isFolderPresent(account, autoExpandFolderId)) {
account.autoExpandFolderId = null
}
}

View file

@ -23,7 +23,6 @@ import com.fsck.k9.mail.FolderType as RemoteFolderType
class FolderRepository(
private val messageStoreManager: MessageStoreManager,
private val accountManager: AccountManager,
private val account: Account,
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
) {
private val sortForDisplay =
@ -33,7 +32,7 @@ class FolderRepository(
.thenByDescending { it.isInTopGroup }
.thenBy(String.CASE_INSENSITIVE_ORDER) { it.folder.name }
fun getDisplayFolders(displayMode: FolderMode?): List<DisplayFolder> {
fun getDisplayFolders(account: Account, displayMode: FolderMode?): List<DisplayFolder> {
val messageStore = messageStoreManager.getMessageStore(account)
return messageStore.getDisplayFolders(
displayMode = displayMode ?: account.folderDisplayMode,
@ -43,7 +42,7 @@ class FolderRepository(
folder = Folder(
id = folder.id,
name = folder.name,
type = folderTypeOf(folder.id),
type = folderTypeOf(account, folder.id),
isLocalOnly = folder.isLocalOnly
),
isInTopGroup = folder.isInTopGroup,
@ -53,26 +52,26 @@ class FolderRepository(
}.sortedWith(sortForDisplay)
}
fun getFolder(folderId: Long): Folder? {
fun getFolder(account: Account, folderId: Long): Folder? {
val messageStore = messageStoreManager.getMessageStore(account)
return messageStore.getFolder(folderId) { folder ->
Folder(
id = folder.id,
name = folder.name,
type = folderTypeOf(folder.id),
type = folderTypeOf(account, folder.id),
isLocalOnly = folder.isLocalOnly
)
}
}
fun getFolderDetails(folderId: Long): FolderDetails? {
fun getFolderDetails(account: Account, folderId: Long): FolderDetails? {
val messageStore = messageStoreManager.getMessageStore(account)
return messageStore.getFolder(folderId) { folder ->
FolderDetails(
folder = Folder(
id = folder.id,
name = folder.name,
type = folderTypeOf(folder.id),
type = folderTypeOf(account, folder.id),
isLocalOnly = folder.isLocalOnly
),
isInTopGroup = folder.isInTopGroup,
@ -85,7 +84,7 @@ class FolderRepository(
}
}
fun getRemoteFolders(): List<RemoteFolder> {
fun getRemoteFolders(account: Account): List<RemoteFolder> {
val messageStore = messageStoreManager.getMessageStore(account)
return messageStore.getFolders(excludeLocalOnly = true) { folder ->
RemoteFolder(
@ -97,7 +96,7 @@ class FolderRepository(
}
}
fun getRemoteFolderDetails(): List<RemoteFolderDetails> {
fun getRemoteFolderDetails(account: Account): List<RemoteFolderDetails> {
val messageStore = messageStoreManager.getMessageStore(account)
return messageStore.getFolders(excludeLocalOnly = true) { folder ->
RemoteFolderDetails(
@ -117,21 +116,21 @@ class FolderRepository(
}
}
fun getPushFoldersFlow(): Flow<List<RemoteFolder>> {
fun getPushFoldersFlow(account: Account): Flow<List<RemoteFolder>> {
return account.getFolderPushModeFlow()
.flatMapLatest { pushMode ->
getPushFoldersFlow(pushMode)
getPushFoldersFlow(account, pushMode)
}
}
private fun getPushFoldersFlow(folderMode: FolderMode): Flow<List<RemoteFolder>> {
private fun getPushFoldersFlow(account: Account, folderMode: FolderMode): Flow<List<RemoteFolder>> {
val messageStore = messageStoreManager.getMessageStore(account)
return callbackFlow {
send(getPushFolders(folderMode))
send(getPushFolders(account, folderMode))
val listener = FolderSettingsChangedListener {
launch {
send(getPushFolders(folderMode))
send(getPushFolders(account, folderMode))
}
}
messageStore.addFolderSettingsChangedListener(listener)
@ -144,10 +143,10 @@ class FolderRepository(
.flowOn(ioDispatcher)
}
private fun getPushFolders(folderMode: FolderMode): List<RemoteFolder> {
private fun getPushFolders(account: Account, folderMode: FolderMode): List<RemoteFolder> {
if (folderMode == FolderMode.NONE) return emptyList()
return getRemoteFolderDetails()
return getRemoteFolderDetails(account)
.asSequence()
.filter { folderDetails ->
val pushClass = folderDetails.effectivePushClass
@ -165,54 +164,54 @@ class FolderRepository(
.toList()
}
fun getFolderServerId(folderId: Long): String? {
fun getFolderServerId(account: Account, folderId: Long): String? {
val messageStore = messageStoreManager.getMessageStore(account)
return messageStore.getFolder(folderId) { folder ->
folder.serverId
}
}
fun getFolderId(folderServerId: String): Long? {
fun getFolderId(account: Account, folderServerId: String): Long? {
val messageStore = messageStoreManager.getMessageStore(account)
return messageStore.getFolderId(folderServerId)
}
fun isFolderPresent(folderId: Long): Boolean {
fun isFolderPresent(account: Account, folderId: Long): Boolean {
val messageStore = messageStoreManager.getMessageStore(account)
return messageStore.getFolder(folderId) { true } ?: false
}
fun updateFolderDetails(folderDetails: FolderDetails) {
fun updateFolderDetails(account: Account, folderDetails: FolderDetails) {
val messageStore = messageStoreManager.getMessageStore(account)
messageStore.updateFolderSettings(folderDetails)
}
fun setIncludeInUnifiedInbox(folderId: Long, includeInUnifiedInbox: Boolean) {
fun setIncludeInUnifiedInbox(account: Account, folderId: Long, includeInUnifiedInbox: Boolean) {
val messageStore = messageStoreManager.getMessageStore(account)
messageStore.setIncludeInUnifiedInbox(folderId, includeInUnifiedInbox)
}
fun setDisplayClass(folderId: Long, folderClass: FolderClass) {
fun setDisplayClass(account: Account, folderId: Long, folderClass: FolderClass) {
val messageStore = messageStoreManager.getMessageStore(account)
messageStore.setDisplayClass(folderId, folderClass)
}
fun setSyncClass(folderId: Long, folderClass: FolderClass) {
fun setSyncClass(account: Account, folderId: Long, folderClass: FolderClass) {
val messageStore = messageStoreManager.getMessageStore(account)
messageStore.setSyncClass(folderId, folderClass)
}
fun setPushClass(folderId: Long, folderClass: FolderClass) {
fun setPushClass(account: Account, folderId: Long, folderClass: FolderClass) {
val messageStore = messageStoreManager.getMessageStore(account)
messageStore.setPushClass(folderId, folderClass)
}
fun setNotificationClass(folderId: Long, folderClass: FolderClass) {
fun setNotificationClass(account: Account, folderId: Long, folderClass: FolderClass) {
val messageStore = messageStoreManager.getMessageStore(account)
messageStore.setNotificationClass(folderId, folderClass)
}
private fun folderTypeOf(folderId: Long) = when (folderId) {
private fun folderTypeOf(account: Account, folderId: Long) = when (folderId) {
account.inboxFolderId -> FolderType.INBOX
account.outboxFolderId -> FolderType.OUTBOX
account.sentFolderId -> FolderType.SENT

View file

@ -1,13 +0,0 @@
package com.fsck.k9.mailstore
import com.fsck.k9.Account
import com.fsck.k9.preferences.AccountManager
class FolderRepositoryManager(
private val messageStoreManager: MessageStoreManager,
private val accountManager: AccountManager
) {
fun getFolderRepository(account: Account): FolderRepository {
return FolderRepository(messageStoreManager, accountManager, account)
}
}

View file

@ -5,13 +5,12 @@ import com.fsck.k9.Preferences
class K9BackendStorageFactory(
private val preferences: Preferences,
private val folderRepositoryManager: FolderRepositoryManager,
private val folderRepository: FolderRepository,
private val messageStoreManager: MessageStoreManager,
private val specialFolderSelectionStrategy: SpecialFolderSelectionStrategy,
private val saveMessageDataCreator: SaveMessageDataCreator
) {
fun createBackendStorage(account: Account): K9BackendStorage {
val folderRepository = folderRepositoryManager.getFolderRepository(account)
val messageStore = messageStoreManager.getMessageStore(account)
val folderSettingsProvider = FolderSettingsProvider(preferences, account)
val specialFolderUpdater = SpecialFolderUpdater(

View file

@ -6,7 +6,7 @@ import com.fsck.k9.message.extractors.MessagePreviewCreator
import org.koin.dsl.module
val mailStoreModule = module {
single { FolderRepositoryManager(messageStoreManager = get(), accountManager = get()) }
single { FolderRepository(messageStoreManager = get(), accountManager = get()) }
single { MessageViewInfoExtractorFactory(get(), get(), get()) }
single { StorageManager.getInstance(get()) }
single { SearchStatusManager() }
@ -14,7 +14,7 @@ val mailStoreModule = module {
single {
K9BackendStorageFactory(
preferences = get(),
folderRepositoryManager = get(),
folderRepository = get(),
messageStoreManager = get(),
specialFolderSelectionStrategy = get(),
saveMessageDataCreator = get()

View file

@ -18,7 +18,7 @@ class SpecialFolderUpdater(
private val account: Account
) {
fun updateSpecialFolders() {
val folders = folderRepository.getRemoteFolders()
val folders = folderRepository.getRemoteFolders(account)
updateInbox(folders)
@ -42,15 +42,15 @@ class SpecialFolderUpdater(
account.inboxFolderId = newInboxId
if (oldInboxId != null && folders.any { it.id == oldInboxId }) {
folderRepository.setIncludeInUnifiedInbox(oldInboxId, false)
folderRepository.setIncludeInUnifiedInbox(account, oldInboxId, false)
}
if (newInboxId != null) {
folderRepository.setIncludeInUnifiedInbox(newInboxId, true)
folderRepository.setDisplayClass(newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setSyncClass(newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setPushClass(newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setNotificationClass(newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setIncludeInUnifiedInbox(account, newInboxId, true)
folderRepository.setDisplayClass(account, newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setSyncClass(account, newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setPushClass(account, newInboxId, FolderClass.FIRST_CLASS)
folderRepository.setNotificationClass(account, newInboxId, FolderClass.FIRST_CLASS)
}
}
@ -117,8 +117,8 @@ class SpecialFolderUpdater(
}
if (folderId != null) {
folderRepository.setDisplayClass(folderId, FolderClass.FIRST_CLASS)
folderRepository.setSyncClass(folderId, FolderClass.NO_CLASS)
folderRepository.setDisplayClass(account, folderId, FolderClass.FIRST_CLASS)
folderRepository.setSyncClass(account, folderId, FolderClass.NO_CLASS)
}
}

View file

@ -2,13 +2,12 @@ package com.fsck.k9.preferences
import com.fsck.k9.Account
import com.fsck.k9.mail.FolderClass
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.mailstore.RemoteFolderDetails
class FolderSettingsProvider(private val folderRepositoryManager: FolderRepositoryManager) {
class FolderSettingsProvider(private val folderRepository: FolderRepository) {
fun getFolderSettings(account: Account): List<FolderSettings> {
val folderRepository = folderRepositoryManager.getFolderRepository(account)
return folderRepository.getRemoteFolderDetails()
return folderRepository.getRemoteFolderDetails(account)
.filterNot { it.containsOnlyDefaultValues() }
.map { it.toFolderSettings() }
}

View file

@ -11,10 +11,10 @@ val preferencesModule = module {
contentResolver = get(),
preferences = get(),
folderSettingsProvider = get(),
folderRepositoryManager = get()
folderRepository = get()
)
}
factory { FolderSettingsProvider(folderRepositoryManager = get()) }
factory { FolderSettingsProvider(folderRepository = get()) }
factory<AccountManager> { get<Preferences>() }
single {
RealGeneralSettingsManager(

View file

@ -10,7 +10,6 @@ import com.fsck.k9.AccountPreferenceSerializer.Companion.IDENTITY_EMAIL_KEY
import com.fsck.k9.AccountPreferenceSerializer.Companion.IDENTITY_NAME_KEY
import com.fsck.k9.Preferences
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.preferences.ServerTypeConverter.fromServerSettingsType
import com.fsck.k9.preferences.Settings.InvalidSettingValueException
import com.fsck.k9.preferences.Settings.SettingsDescription
@ -24,7 +23,7 @@ class SettingsExporter(
private val contentResolver: ContentResolver,
private val preferences: Preferences,
private val folderSettingsProvider: FolderSettingsProvider,
private val folderRepositoryManager: FolderRepositoryManager
private val folderRepository: FolderRepository
) {
@Throws(SettingsImportExportException::class)
fun exportToUri(includeGlobals: Boolean, accountUuids: Set<String>, uri: Uri) {
@ -211,7 +210,6 @@ class SettingsExporter(
}
}
val folderRepository = folderRepositoryManager.getFolderRepository(account)
writeFolderNameSettings(account, folderRepository, serializer)
serializer.endTag(null, SETTINGS_ELEMENT)
@ -272,7 +270,7 @@ class SettingsExporter(
) {
fun writeFolderNameSetting(key: String, folderId: Long?, importedFolderServerId: String?) {
val folderServerId = folderId?.let {
folderRepository.getFolderServerId(folderId)
folderRepository.getFolderServerId(account, folderId)
} ?: importedFolderServerId
if (folderServerId != null) {

View file

@ -2,7 +2,7 @@ package com.fsck.k9.preferences
import com.fsck.k9.K9RobolectricTest
import com.fsck.k9.Preferences
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
import java.io.ByteArrayOutputStream
import org.jdom2.Document
import org.jdom2.input.SAXBuilder
@ -17,12 +17,12 @@ class SettingsExporterTest : K9RobolectricTest() {
private val contentResolver = RuntimeEnvironment.application.contentResolver
private val preferences: Preferences by inject()
private val folderSettingsProvider: FolderSettingsProvider by inject()
private val folderRepositoryManager: FolderRepositoryManager by inject()
private val folderRepository: FolderRepository by inject()
private val settingsExporter = SettingsExporter(
contentResolver,
preferences,
folderSettingsProvider,
folderRepositoryManager
folderRepository
)
@Test

View file

@ -8,13 +8,13 @@ import com.fsck.k9.mail.ssl.TrustManagerFactory
import com.fsck.k9.mail.store.webdav.DraftsFolderProvider
import com.fsck.k9.mail.store.webdav.WebDavStore
import com.fsck.k9.mail.transport.WebDavTransport
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.mailstore.K9BackendStorageFactory
class WebDavBackendFactory(
private val backendStorageFactory: K9BackendStorageFactory,
private val trustManagerFactory: TrustManagerFactory,
private val folderRepositoryManager: FolderRepositoryManager
private val folderRepository: FolderRepository
) : BackendFactory {
override fun createBackend(account: Account): Backend {
val accountName = account.displayName
@ -27,10 +27,9 @@ class WebDavBackendFactory(
}
private fun createDraftsFolderProvider(account: Account): DraftsFolderProvider {
val folderRepository = folderRepositoryManager.getFolderRepository(account)
return DraftsFolderProvider {
val draftsFolderId = account.draftsFolderId ?: error("No Drafts folder configured")
folderRepository.getFolderServerId(draftsFolderId) ?: error("Couldn't find local Drafts folder")
folderRepository.getFolderServerId(account, draftsFolderId) ?: error("Couldn't find local Drafts folder")
}
}
}

View file

@ -8,13 +8,13 @@ import com.fsck.k9.mail.ssl.TrustManagerFactory
import com.fsck.k9.mail.store.webdav.DraftsFolderProvider
import com.fsck.k9.mail.store.webdav.WebDavStore
import com.fsck.k9.mail.transport.WebDavTransport
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.mailstore.K9BackendStorageFactory
class WebDavBackendFactory(
private val backendStorageFactory: K9BackendStorageFactory,
private val trustManagerFactory: TrustManagerFactory,
private val folderRepositoryManager: FolderRepositoryManager
private val folderRepository: FolderRepository
) : BackendFactory {
override fun createBackend(account: Account): Backend {
val accountName = account.displayName
@ -27,10 +27,9 @@ class WebDavBackendFactory(
}
private fun createDraftsFolderProvider(account: Account): DraftsFolderProvider {
val folderRepository = folderRepositoryManager.getFolderRepository(account)
return DraftsFolderProvider {
val draftsFolderId = account.draftsFolderId ?: error("No Drafts folder configured")
folderRepository.getFolderServerId(draftsFolderId) ?: error("Couldn't find local Drafts folder")
folderRepository.getFolderServerId(account, draftsFolderId) ?: error("Couldn't find local Drafts folder")
}
}
}

View file

@ -10,11 +10,11 @@ val unreadWidgetModule = module {
preferences = get(),
messagingController = get(),
defaultFolderProvider = get(),
folderRepositoryManager = get(),
folderRepository = get(),
folderNameFormatterFactory = get()
)
}
single { UnreadWidgetUpdater(context = get()) }
single { UnreadWidgetUpdateListener(unreadWidgetUpdater = get()) }
single { UnreadWidgetMigrations(accountRepository = get(), folderRepositoryManager = get()) }
single { UnreadWidgetMigrations(accountRepository = get(), folderRepository = get()) }
}

View file

@ -7,7 +7,7 @@ import com.fsck.k9.Preferences
import com.fsck.k9.R
import com.fsck.k9.activity.MessageList
import com.fsck.k9.controller.MessagingController
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.search.LocalSearch
import com.fsck.k9.search.SearchAccount
import com.fsck.k9.ui.folders.FolderNameFormatterFactory
@ -19,7 +19,7 @@ class UnreadWidgetDataProvider(
private val preferences: Preferences,
private val messagingController: MessagingController,
private val defaultFolderProvider: DefaultFolderProvider,
private val folderRepositoryManager: FolderRepositoryManager,
private val folderRepository: FolderRepository,
private val folderNameFormatterFactory: FolderNameFormatterFactory
) {
fun loadUnreadWidgetData(configuration: UnreadWidgetConfiguration): UnreadWidgetData? = with(configuration) {
@ -78,8 +78,7 @@ class UnreadWidgetDataProvider(
}
private fun getFolderDisplayName(account: Account, folderId: Long): String {
val folderRepository = folderRepositoryManager.getFolderRepository(account)
val folder = folderRepository.getFolder(folderId)
val folder = folderRepository.getFolder(account, folderId)
return if (folder != null) {
val folderNameFormatter = folderNameFormatterFactory.create(context)
folderNameFormatter.displayName(folder)

View file

@ -3,13 +3,13 @@ package com.fsck.k9.widget.unread
import android.content.SharedPreferences
import androidx.core.content.edit
import com.fsck.k9.Preferences
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.widget.unread.UnreadWidgetRepository.Companion.PREFS_VERSION
import com.fsck.k9.widget.unread.UnreadWidgetRepository.Companion.PREF_VERSION_KEY
internal class UnreadWidgetMigrations(
private val accountRepository: Preferences,
private val folderRepositoryManager: FolderRepositoryManager
private val folderRepository: FolderRepository
) {
fun upgradePreferences(preferences: SharedPreferences, version: Int) {
if (version < 2) rewriteFolderNameToFolderId(preferences)
@ -33,8 +33,7 @@ internal class UnreadWidgetMigrations(
val folderServerId = preferences.getString("unread_widget.$widgetId.folder_name", null)
if (folderServerId != null) {
val folderRepository = folderRepositoryManager.getFolderRepository(account)
val folderId = folderRepository.getFolderId(folderServerId)
val folderId = folderRepository.getFolderId(account, folderServerId)
putString("unread_widget.$widgetId.folder_id", folderId?.toString())
}

View file

@ -7,7 +7,6 @@ import com.fsck.k9.Preferences
import com.fsck.k9.controller.MessagingController
import com.fsck.k9.mailstore.Folder
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderType
import com.fsck.k9.search.SearchAccount
import com.fsck.k9.ui.folders.FolderNameFormatter
@ -27,11 +26,11 @@ class UnreadWidgetDataProviderTest : AppRobolectricTest() {
val preferences = createPreferences()
val messagingController = createMessagingController()
val defaultFolderStrategy = createDefaultFolderStrategy()
val folderRepositoryManager = createFolderRepositoryManager()
val folderRepository = createFolderRepository()
val folderNameFormatterFactory = createFolderNameFormatterFactory()
val provider = UnreadWidgetDataProvider(
context, preferences, messagingController, defaultFolderStrategy,
folderRepositoryManager, folderNameFormatterFactory
folderRepository, folderNameFormatterFactory
)
@Test
@ -102,16 +101,9 @@ class UnreadWidgetDataProviderTest : AppRobolectricTest() {
on { getDefaultFolder(account) } doReturn FOLDER_ID
}
fun createFolderRepositoryManager(): FolderRepositoryManager {
val folderRepository = createFolderRepository()
return mock {
on { getFolderRepository(account) } doReturn folderRepository
}
}
fun createFolderRepository(): FolderRepository {
return mock {
on { getFolder(FOLDER_ID) } doReturn FOLDER
on { getFolder(account, FOLDER_ID) } doReturn FOLDER
}
}

View file

@ -11,7 +11,7 @@ class ChooseFolderViewModel(private val foldersLiveDataFactory: FoldersLiveDataF
fun getFolders(account: Account, displayMode: FolderMode): FoldersLiveData {
val liveData = foldersLiveData
if (liveData != null && liveData.accountUuid == account.uuid && liveData.displayMode == displayMode) {
if (liveData != null && liveData.account.uuid == account.uuid && liveData.displayMode == displayMode) {
return liveData
}

View file

@ -18,16 +18,13 @@ class FoldersLiveData(
private val folderRepository: FolderRepository,
private val messagingController: MessagingController,
private val preferences: Preferences,
val accountUuid: String,
val account: Account,
val displayMode: FolderMode?
) : LiveData<List<DisplayFolder>>() {
private val messagingListener = object : SimpleMessagingListener() {
override fun folderStatusChanged(
account: Account,
folderId: Long
) {
if (account?.uuid == accountUuid) {
override fun folderStatusChanged(account: Account, folderId: Long) {
if (account.uuid == this@FoldersLiveData.account.uuid) {
loadFoldersAsync()
}
}
@ -39,7 +36,10 @@ class FoldersLiveData(
private fun loadFoldersAsync() {
GlobalScope.launch(Dispatchers.Main) {
value = withContext(Dispatchers.IO) { folderRepository.getDisplayFolders(displayMode) }
val displayFolders = withContext(Dispatchers.IO) {
folderRepository.getDisplayFolders(account, displayMode)
}
value = displayFolders
}
}

View file

@ -4,15 +4,14 @@ import com.fsck.k9.Account
import com.fsck.k9.Account.FolderMode
import com.fsck.k9.Preferences
import com.fsck.k9.controller.MessagingController
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
class FoldersLiveDataFactory(
private val folderRepositoryManager: FolderRepositoryManager,
private val folderRepository: FolderRepository,
private val messagingController: MessagingController,
private val preferences: Preferences
) {
fun create(account: Account, displayMode: FolderMode? = null): FoldersLiveData {
val folderRepository = folderRepositoryManager.getFolderRepository(account)
return FoldersLiveData(folderRepository, messagingController, preferences, account.uuid, displayMode)
return FoldersLiveData(folderRepository, messagingController, preferences, account, displayMode)
}
}

View file

@ -15,7 +15,7 @@ class FoldersViewModel(private val foldersLiveDataFactory: FoldersLiveDataFactor
}
fun loadFolders(account: Account) {
if (currentFoldersLiveData?.accountUuid == account.uuid) return
if (currentFoldersLiveData?.account?.uuid == account.uuid) return
removeCurrentFoldersLiveData()

View file

@ -1,6 +1,7 @@
package com.fsck.k9.ui.managefolders
import androidx.preference.PreferenceDataStore
import com.fsck.k9.Account
import com.fsck.k9.mail.FolderClass
import com.fsck.k9.mailstore.FolderDetails
import com.fsck.k9.mailstore.FolderRepository
@ -11,6 +12,7 @@ import kotlinx.coroutines.launch
class FolderSettingsDataStore(
private val folderRepository: FolderRepository,
private val account: Account,
private var folder: FolderDetails
) : PreferenceDataStore() {
private val saveScope = CoroutineScope(GlobalScope.coroutineContext + Dispatchers.IO)
@ -64,7 +66,7 @@ class FolderSettingsDataStore(
private fun updateFolder(newFolder: FolderDetails) {
folder = newFolder
saveScope.launch {
folderRepository.updateFolderDetails(newFolder)
folderRepository.updateFolderDetails(account, newFolder)
}
}
}

View file

@ -11,7 +11,6 @@ import com.fsck.k9.helper.SingleLiveEvent
import com.fsck.k9.mailstore.Folder
import com.fsck.k9.mailstore.FolderDetails
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.mailstore.FolderRepositoryManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
@ -20,7 +19,7 @@ private const val NO_FOLDER_ID = 0L
class FolderSettingsViewModel(
private val preferences: Preferences,
private val folderRepositoryManager: FolderRepositoryManager,
private val folderRepository: FolderRepository,
private val messagingController: MessagingController
) : ViewModel() {
private val actionLiveData = SingleLiveEvent<Action>()
@ -44,8 +43,7 @@ class FolderSettingsViewModel(
): LiveData<FolderSettingsResult> {
return liveData(context = viewModelScope.coroutineContext) {
val account = loadAccount(accountUuid)
val folderRepository = folderRepositoryManager.getFolderRepository(account)
val folderDetails = folderRepository.loadFolderDetails(folderId)
val folderDetails = folderRepository.loadFolderDetails(account, folderId)
if (folderDetails == null) {
Timber.w("Folder with ID $folderId not found")
emit(FolderNotFound)
@ -57,7 +55,7 @@ class FolderSettingsViewModel(
val folderSettingsData = FolderSettingsData(
folder = folderDetails.folder,
dataStore = FolderSettingsDataStore(folderRepository, folderDetails)
dataStore = FolderSettingsDataStore(folderRepository, account, folderDetails)
)
emit(folderSettingsData)
}
@ -69,9 +67,9 @@ class FolderSettingsViewModel(
}
}
private suspend fun FolderRepository.loadFolderDetails(folderId: Long): FolderDetails? {
private suspend fun FolderRepository.loadFolderDetails(account: Account, folderId: Long): FolderDetails? {
return withContext(Dispatchers.IO) {
getFolderDetails(folderId)
getFolderDetails(account, folderId)
}
}

View file

@ -5,5 +5,5 @@ import org.koin.dsl.module
val manageFoldersUiModule = module {
viewModel { ManageFoldersViewModel(foldersLiveDataFactory = get()) }
viewModel { FolderSettingsViewModel(preferences = get(), folderRepositoryManager = get(), messagingController = get()) }
viewModel { FolderSettingsViewModel(preferences = get(), folderRepository = get(), messagingController = get()) }
}

View file

@ -10,7 +10,7 @@ class ManageFoldersViewModel(private val foldersLiveDataFactory: FoldersLiveData
fun getFolders(account: Account): FoldersLiveData {
val liveData = foldersLiveData
if (liveData != null && liveData.accountUuid == account.uuid) {
if (liveData != null && liveData.account.uuid == account.uuid) {
return liveData
}

View file

@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.fsck.k9.Account
import com.fsck.k9.Preferences
import com.fsck.k9.mailstore.FolderRepositoryManager
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.mailstore.FolderType
import com.fsck.k9.mailstore.RemoteFolder
import com.fsck.k9.mailstore.SpecialFolderSelectionStrategy
@ -17,7 +17,7 @@ import kotlinx.coroutines.withContext
class AccountSettingsViewModel(
private val preferences: Preferences,
private val folderRepositoryManager: FolderRepositoryManager,
private val folderRepository: FolderRepository,
private val specialFolderSelectionStrategy: SpecialFolderSelectionStrategy
) : ViewModel() {
val accounts = AccountsLiveData(preferences)
@ -28,9 +28,10 @@ class AccountSettingsViewModel(
if (accountLiveData.value == null) {
GlobalScope.launch(Dispatchers.Main) {
accountLiveData.value = withContext(Dispatchers.IO) {
val account = withContext(Dispatchers.IO) {
loadAccount(accountUuid)
}
accountLiveData.value = account
}
}
@ -60,13 +61,13 @@ class AccountSettingsViewModel(
}
private fun loadFolders(account: Account) {
val folderRepository = folderRepositoryManager.getFolderRepository(account)
GlobalScope.launch(Dispatchers.Main) {
foldersLiveData.value = withContext(Dispatchers.IO) {
val folders = folderRepository.getRemoteFolders()
val remoteFolderInfo = withContext(Dispatchers.IO) {
val folders = folderRepository.getRemoteFolders(account)
val automaticSpecialFolders = getAutomaticSpecialFolders(folders)
RemoteFolderInfo(folders, automaticSpecialFolders)
}
foldersLiveData.value = remoteFolderInfo
}
}