Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b8051992 authored by Michael Enoma's avatar Michael Enoma
Browse files

Remove AccountsLiveData

parent fa0377d3
Loading
Loading
Loading
Loading
+0 −40
Original line number Diff line number Diff line
package com.fsck.k9.ui.account

import androidx.lifecycle.LiveData
import com.fsck.k9.Account
import com.fsck.k9.AccountsChangeListener
import com.fsck.k9.Preferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class AccountsLiveData(val preferences: Preferences) : LiveData<List<Account>>(), AccountsChangeListener {

    private fun loadAccountsAsync() {
        GlobalScope.launch(Dispatchers.Main) {
            value = withContext(Dispatchers.IO) {
                loadAccounts()
            }
        }
    }

    override fun onAccountsChanged() {
        loadAccountsAsync()
    }

    private fun loadAccounts(): List<Account> {
        return preferences.accounts
    }

    override fun onActive() {
        super.onActive()
        preferences.addOnAccountsChangeListener(this)
        loadAccountsAsync()
    }

    override fun onInactive() {
        super.onInactive()
        preferences.removeOnAccountsChangeListener(this)
    }
}
+2 −3
Original line number Diff line number Diff line
package com.fsck.k9.ui.settings

import com.fsck.k9.helper.NamedThreadFactory
import com.fsck.k9.ui.account.AccountsLiveData
import com.fsck.k9.ui.settings.account.AccountSettingsDataStoreFactory
import com.fsck.k9.ui.settings.account.AccountSettingsViewModel
import com.fsck.k9.ui.settings.export.SettingsExportViewModel
@@ -15,8 +14,8 @@ import org.koin.core.qualifier.named
import org.koin.dsl.module

val settingsUiModule = module {
    factory { AccountsLiveData(get()) }
    viewModel { SettingsViewModel(accountManager = get(), accounts = get()) }
    viewModel { SettingsViewModel(accountManager = get()) }


    factory { GeneralSettingsDataStore(jobManager = get(), themeManager = get(), appLanguageManager = get()) }
    single(named("SaveSettingsExecutorService")) {
+2 −2
Original line number Diff line number Diff line
package com.fsck.k9.ui.settings

import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import com.fsck.k9.Account
import com.fsck.k9.preferences.AccountManager
import com.fsck.k9.ui.account.AccountsLiveData
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -15,8 +15,8 @@ internal class SettingsViewModel(
    private val accountManager: AccountManager,
    private val coroutineScope: CoroutineScope = GlobalScope,
    private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO,
    val accounts: AccountsLiveData
) : ViewModel() {
    val accounts = accountManager.getAccountsFlow().asLiveData()

    fun moveAccount(account: Account, newPosition: Int) {
        coroutineScope.launch(coroutineDispatcher) {
+6 −11
Original line number Diff line number Diff line
@@ -3,24 +3,24 @@ package com.fsck.k9.ui.settings.account
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import com.fsck.k9.Account
import com.fsck.k9.Preferences
import com.fsck.k9.mailstore.FolderRepository
import com.fsck.k9.mailstore.FolderType
import com.fsck.k9.mailstore.RemoteFolder
import com.fsck.k9.mailstore.SpecialFolderSelectionStrategy
import com.fsck.k9.ui.account.AccountsLiveData
import com.fsck.k9.preferences.AccountManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class AccountSettingsViewModel(
    private val preferences: Preferences,
    private val accountManager: AccountManager,
    private val folderRepository: FolderRepository,
    private val specialFolderSelectionStrategy: SpecialFolderSelectionStrategy
) : ViewModel() {
    val accounts = AccountsLiveData(preferences)
    val accounts = accountManager.getAccountsFlow().asLiveData()
    private val accountLiveData = MutableLiveData<Account>()
    private val foldersLiveData = MutableLiveData<RemoteFolderInfo>()

@@ -49,17 +49,15 @@ class AccountSettingsViewModel(
    }

    private fun loadAccount(accountUuid: String): Account {
        return preferences.getAccount(accountUuid) ?: error("Account $accountUuid not found")
        return accountManager.getAccount(accountUuid) ?: error("Account $accountUuid not found")
    }

    fun getFolders(account: Account): LiveData<RemoteFolderInfo> {
        if (foldersLiveData.value == null) {
            loadFolders(account)
        }

        return foldersLiveData
    }

    private fun loadFolders(account: Account) {
        GlobalScope.launch(Dispatchers.Main) {
            val remoteFolderInfo = withContext(Dispatchers.IO) {
@@ -70,8 +68,6 @@ class AccountSettingsViewModel(
            foldersLiveData.value = remoteFolderInfo
        }
    }
    }

    private fun getAutomaticSpecialFolders(folders: List<RemoteFolder>): Map<FolderType, RemoteFolder?> {
        return mapOf(
            FolderType.ARCHIVE to specialFolderSelectionStrategy.selectSpecialFolder(folders, FolderType.ARCHIVE),
@@ -82,7 +78,6 @@ class AccountSettingsViewModel(
        )
    }
}

data class RemoteFolderInfo(
    val folders: List<RemoteFolder>,
    val automaticSpecialFolders: Map<FolderType, RemoteFolder?>