Move code to configure special folders to SpecialFolderUpdater

This commit is contained in:
cketti 2020-01-02 22:07:40 +01:00
parent b504e6a83a
commit 495d8c0521
3 changed files with 48 additions and 24 deletions

View file

@ -113,6 +113,30 @@ class FolderRepository(
RemoteFolderType.SPAM -> FolderType.SPAM
RemoteFolderType.ARCHIVE -> FolderType.ARCHIVE
}
fun setIncludeInUnifiedInbox(serverId: String, includeInUnifiedInbox: Boolean) {
val localStore = localStoreProvider.getInstance(account)
val folder = localStore.getFolder(serverId)
folder.isIntegrate = includeInUnifiedInbox
}
fun setDisplayClass(serverId: String, folderClass: FolderClass) {
val localStore = localStoreProvider.getInstance(account)
val folder = localStore.getFolder(serverId)
folder.displayClass = folderClass
}
fun setSyncClass(serverId: String, folderClass: FolderClass) {
val localStore = localStoreProvider.getInstance(account)
val folder = localStore.getFolder(serverId)
folder.syncClass = folderClass
}
fun setNotificationClass(serverId: String, folderClass: FolderClass) {
val localStore = localStoreProvider.getInstance(account)
val folder = localStore.getFolder(serverId)
folder.notifyClass = folderClass
}
}
data class Folder(val id: Long, val serverId: String, val name: String, val type: FolderType)

View file

@ -914,29 +914,6 @@ public class LocalStore {
}
final LocalFolder.PreferencesHolder prefHolder = folder.new PreferencesHolder();
// When created, special folders should always be displayed
// inbox should be integrated
// and the inbox and drafts folders should be syncced by default
if (account.isSpecialFolder(serverId)) {
prefHolder.inTopGroup = true;
prefHolder.displayClass = LocalFolder.FolderClass.FIRST_CLASS;
if (serverId.equals(account.getInboxFolder())) {
prefHolder.integrate = true;
prefHolder.notifyClass = LocalFolder.FolderClass.FIRST_CLASS;
prefHolder.pushClass = LocalFolder.FolderClass.FIRST_CLASS;
} else {
prefHolder.pushClass = LocalFolder.FolderClass.INHERITED;
}
if (serverId.equals(account.getInboxFolder()) || serverId.equals(account.getDraftsFolder())) {
prefHolder.syncClass = LocalFolder.FolderClass.FIRST_CLASS;
} else {
prefHolder.syncClass = LocalFolder.FolderClass.NO_CLASS;
}
}
folder.refresh(serverId, prefHolder); // Recover settings from Preferences
db.execSQL("INSERT INTO folders (name, visible_limit, top_group, display_class, poll_class, notify_class, push_class, integrate, server_id, local_only, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[] {
name,
visibleLimit,

View file

@ -3,6 +3,7 @@ package com.fsck.k9.mailstore
import com.fsck.k9.Account
import com.fsck.k9.Account.SpecialFolderSelection
import com.fsck.k9.Preferences
import com.fsck.k9.mail.Folder.FolderClass
/**
* Updates special folders in [Account] if they are marked as [SpecialFolderSelection.AUTOMATIC] or if they are marked
@ -28,7 +29,22 @@ class SpecialFolderUpdater(
}
private fun updateInbox(folders: List<Folder>) {
account.inboxFolder = folders.firstOrNull { it.type == FolderType.INBOX }?.serverId
val oldInboxServerId = account.inboxFolder
val newInboxServerId = folders.firstOrNull { it.type == FolderType.INBOX }?.serverId
if (newInboxServerId == oldInboxServerId) return
account.inboxFolder = newInboxServerId
if (oldInboxServerId != null && folders.any { it.serverId == oldInboxServerId }) {
folderRepository.setIncludeInUnifiedInbox(oldInboxServerId, false)
}
if (newInboxServerId != null) {
folderRepository.setIncludeInUnifiedInbox(newInboxServerId, true)
folderRepository.setDisplayClass(newInboxServerId, FolderClass.FIRST_CLASS)
folderRepository.setSyncClass(newInboxServerId, FolderClass.FIRST_CLASS)
folderRepository.setNotificationClass(newInboxServerId, FolderClass.FIRST_CLASS)
}
}
private fun updateSpecialFolder(type: FolderType, folders: List<Folder>) {
@ -64,6 +80,8 @@ class SpecialFolderUpdater(
}
private fun setSpecialFolder(type: FolderType, folder: String?, selection: SpecialFolderSelection) {
if (getSpecialFolder(type) == folder) return
when (type) {
FolderType.ARCHIVE -> account.setArchiveFolder(folder, selection)
FolderType.DRAFTS -> account.setDraftsFolder(folder, selection)
@ -72,6 +90,11 @@ class SpecialFolderUpdater(
FolderType.TRASH -> account.setTrashFolder(folder, selection)
else -> throw AssertionError("Unsupported: $type")
}
if (folder != null) {
folderRepository.setDisplayClass(folder, FolderClass.FIRST_CLASS)
folderRepository.setSyncClass(folder, FolderClass.NO_CLASS)
}
}
private fun saveAccount() {