move extensive setter logic out of Account into saveAccount()

This commit is contained in:
Vincent Breitmoser 2018-11-29 14:51:53 +01:00
parent 4a386b6c9b
commit a42d1111ae
5 changed files with 51 additions and 71 deletions

View file

@ -24,11 +24,9 @@ import com.fsck.k9.mailstore.LocalStore;
import com.fsck.k9.mailstore.StorageManager;
import com.fsck.k9.mailstore.StorageManager.StorageProvider;
import org.jetbrains.annotations.NotNull;
import timber.log.Timber;
/**
* Account stores all of the settings for a single account defined by the user. It is able to save
* and delete itself given a Preferences to work with. Each account is defined by a UUID.
* Account stores all of the settings for a single account defined by the user. Each account is defined by a UUID.
*/
public class Account implements BaseAccount, StoreConfig {
/**
@ -185,6 +183,8 @@ public class Account implements BaseAccount, StoreConfig {
private int remoteSearchNumResults;
private boolean uploadSentMessages;
private boolean changedVisibleLimits = false;
private boolean changedLocalStorageProviderId = false;
/**
* Indicates whether this account is enabled, i.e. ready for use, or not.
@ -238,15 +238,6 @@ public class Account implements BaseAccount, StoreConfig {
this.accountUuid = uuid;
}
private void resetVisibleLimits() {
try {
getLocalStore().resetVisibleLimits(getDisplayCount());
} catch (MessagingException e) {
Timber.e(e, "Unable to reset visible limits");
}
}
public synchronized void setChipColor(int color) {
chipColor = color;
}
@ -346,25 +337,10 @@ public class Account implements BaseAccount, StoreConfig {
}
public void setLocalStorageProviderId(String id) {
if (localStorageProviderId == null || !localStorageProviderId.equals(id)) {
boolean successful = false;
try {
switchLocalStorage(id);
successful = true;
} catch (MessagingException e) {
Timber.e(e, "Switching local storage provider from %s to %s failed.", localStorageProviderId, id);
}
// if migration to/from SD-card failed once, it will fail again.
if (!successful) {
return;
}
localStorageProviderId = id;
this.localStorageProviderId = id;
changedLocalStorageProviderId = true;
}
}
/**
@ -394,7 +370,8 @@ public class Account implements BaseAccount, StoreConfig {
} else {
this.displayCount = K9.DEFAULT_VISIBLE_LIMIT;
}
resetVisibleLimits();
changedVisibleLimits = true;
}
public synchronized long getLatestOldMessageSeenTime() {
@ -801,26 +778,6 @@ public class Account implements BaseAccount, StoreConfig {
this.pushPollOnConnect = pushPollOnConnect;
}
/**
* Are we storing out localStore on the SD-card instead of the local device
* memory?<br/>
* Only to be called during initial account-setup!<br/>
* Side-effect: changes {@link #localStorageProviderId}.
*
* @param newStorageProviderId
* Never <code>null</code>.
* @throws MessagingException
*/
private void switchLocalStorage(final String newStorageProviderId) throws MessagingException {
boolean isInMemoryStorage = localStorageProviderId == null;
if (isInMemoryStorage) {
return;
}
if (!localStorageProviderId.equals(newStorageProviderId)) {
getLocalStore().switchLocalStorage(newStorageProviderId);
}
}
public synchronized boolean isGoToUnreadMessageSearch() {
return goToUnreadMessageSearch;
}
@ -1105,4 +1062,17 @@ public class Account implements BaseAccount, StoreConfig {
remoteSearchFullText = val;
}
boolean isChangedVisibleLimits() {
return changedVisibleLimits;
}
boolean isChangedLocalStorageProviderId() {
return changedLocalStorageProviderId;
}
void resetChangeMarkers() {
changedVisibleLimits = false;
changedLocalStorageProviderId = false;
}
}

View file

@ -16,6 +16,7 @@ import android.support.annotation.RestrictTo;
import android.support.annotation.RestrictTo.Scope;
import com.fsck.k9.backend.BackendManager;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mailstore.LocalStore;
import com.fsck.k9.preferences.Storage;
import com.fsck.k9.preferences.StorageEditor;
@ -203,9 +204,31 @@ public class Preferences {
account.setAccountNumber(accountNumber);
}
processChangedValues(account);
accountPreferenceSerializer.save(storage, editor, account);
}
private void processChangedValues(Account account) {
if (account.isChangedVisibleLimits()) {
try {
account.getLocalStore().resetVisibleLimits(account.getDisplayCount());
} catch (MessagingException e) {
Timber.e(e, "Failed to load LocalStore!");
}
}
if (account.isChangedLocalStorageProviderId()) {
try {
account.getLocalStore().switchLocalStorage(account.getLocalStorageProviderId());
} catch (MessagingException e) {
Timber.e(e, "Failed to load LocalStore!");
}
}
account.resetChangeMarkers();
}
public int generateAccountNumber() {
List<Integer> accountNumbers = getExistingAccountNumbers();
return findNewAccountNumber(accountNumbers);

View file

@ -9,9 +9,8 @@ import com.fsck.k9.mail.Message
import com.fsck.k9.mail.Message.RecipientType
import com.fsck.k9.mail.internet.MimeMessage
import com.google.common.truth.Truth.assertThat
import com.nhaarman.mockito_kotlin.mock
import org.junit.Test
import org.robolectric.RuntimeEnvironment
import java.util.*
class IdentityHelperTest : RobolectricTest() {
private val account = createDummyAccount()
@ -110,7 +109,7 @@ class IdentityHelperTest : RobolectricTest() {
}
private fun createDummyAccount() = DummyAccount().apply {
private fun createDummyAccount() = Account(UUID.randomUUID().toString()).apply {
identities = listOf(
newIdentity("Default", DEFAULT_ADDRESS),
newIdentity("Identity 1", IDENTITY_1_ADDRESS),
@ -142,7 +141,4 @@ class IdentityHelperTest : RobolectricTest() {
const val IDENTITY_4_ADDRESS = "identity4@example.org"
const val IDENTITY_5_ADDRESS = "identity5@example.org"
}
class DummyAccount : Account("dummy-uuid")
}

View file

@ -8,13 +8,15 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.UUID;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.fsck.k9.Account;
import com.fsck.k9.AccountPreferenceSerializer;
import com.fsck.k9.DI;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
import com.fsck.k9.mail.BodyPart;
import com.fsck.k9.mail.FetchProfile;
import com.fsck.k9.mail.Multipart;
@ -721,14 +723,9 @@ public class MigrationTest extends K9RobolectricTest {
}
private Account getNewAccount() {
Preferences preferences = Preferences.getPreferences(RuntimeEnvironment.application);
//FIXME: This is a hack to get Preferences into a state where it's safe to call newAccount()
preferences.clearAccounts();
Account account = preferences.newAccount();
Account account = new Account(UUID.randomUUID().toString());
DI.get(AccountPreferenceSerializer.class).loadDefaults(account);
account.setStoreUri("imap+tls+://user:password@imap.example.org");
return account;
}
}

View file

@ -194,13 +194,7 @@ class AccountSettingsDataStore(
"account_vibrate_pattern" -> account.notificationSetting.vibratePattern = value.toInt()
"account_vibrate_times" -> account.notificationSetting.vibrateTimes = value.toInt()
"account_remote_search_num_results" -> account.remoteSearchNumResults = value.toInt()
"local_storage_provider" -> {
executorService.execute {
account.localStorageProviderId = value
saveSettings()
}
return
}
"local_storage_provider" -> account.localStorageProviderId = value
"account_ringtone" -> with(account.notificationSetting) {
isRingEnabled = true
ringtone = value