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

Unverified Commit c07d601d authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

feat(core-preference): add LegacyAvatarDtoStorageHandler to legacy module

parent 3eb6ec42
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -69,7 +69,10 @@ class TestStoragePersister(
        }

        private fun writeValues(currentStorage: Storage): Storage {
            return InMemoryStorage(currentStorage.getAll() - removals + changes, logger)
            val updatedValues = currentStorage.getAll() - removals + changes
            values.clear()
            values.putAll(updatedValues.mapValues { (_, value) -> value })
            return InMemoryStorage(updatedValues, logger)
        }
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -13,8 +13,10 @@ class AccountKeyGenerator(
     * Creates a key by combining account ID with the specified key.
     *
     * @param key The key to combine with the account ID.
     * @throws IllegalArgumentException if the key is empty.
     */
    fun create(key: String): String {
        require(key.isNotEmpty()) { "Key must not be empty" }
        return "${id.asRaw()}.$key"
    }
}
+6 −1
Original line number Diff line number Diff line
@@ -25,12 +25,17 @@ val featureAccountStorageLegacyModule = module {

    factory { ServerSettingsDtoSerializer() }

    factory<AvatarDtoStorageHandler> {
        LegacyAvatarDtoStorageHandler()
    }

    factory<ProfileDtoStorageHandler> {
        LegacyProfileDtoStorageHandler(
            avatarDtoStorageHandler = get(),
        )
    }

    single<StorageHandler<LegacyAccount>> {
    single<AccountDtoStorageHandler> {
        LegacyAccountStorageHandler(
            serverSettingsDtoSerializer = get(),
            profileDtoStorageHandler = get(),
+6 −17
Original line number Diff line number Diff line
@@ -24,9 +24,9 @@ import net.thunderbird.feature.notification.VibratePattern

class LegacyAccountStorageHandler(
    private val serverSettingsDtoSerializer: ServerSettingsDtoSerializer,
    private val profileDtoStorageHandler: LegacyProfileDtoStorageHandler,
    private val profileDtoStorageHandler: ProfileDtoStorageHandler,
    private val logger: Logger,
) : StorageHandler<LegacyAccount> {
) : AccountDtoStorageHandler {

    @Suppress("LongMethod", "MagicNumber")
    @Synchronized
@@ -307,6 +307,8 @@ class LegacyAccountStorageHandler(
    override fun save(data: LegacyAccount, storage: Storage, editor: StorageEditor) {
        val keyGen = AccountKeyGenerator(data.id)

        profileDtoStorageHandler.save(data, storage, editor)

        if (!storage.getStringOrDefault("accountUuids", "").contains(data.uuid)) {
            var accountUuids = storage.getStringOrDefault("accountUuids", "")
            accountUuids += (if (accountUuids.isNotEmpty()) "," else "") + data.uuid
@@ -421,6 +423,8 @@ class LegacyAccountStorageHandler(
        val keyGen = AccountKeyGenerator(data.id)
        val accountUuid = data.uuid

        profileDtoStorageHandler.delete(data, storage, editor)

        // Get the list of account UUIDs
        val uuids = storage
            .getStringOrDefault("accountUuids", "")
@@ -576,21 +580,6 @@ class LegacyAccountStorageHandler(
        } while (gotOne)
    }

    fun move(data: LegacyAccount, storage: Storage, editor: StorageEditor, newPosition: Int) {
        val accountUuids = storage.getStringOrDefault("accountUuids", "").split(",").filter { it.isNotEmpty() }
        val oldPosition = accountUuids.indexOf(data.uuid)
        if (oldPosition == -1 || oldPosition == newPosition) return

        val newAccountUuidsString = accountUuids.toMutableList()
            .apply {
                removeAt(oldPosition)
                add(newPosition, data.uuid)
            }
            .joinToString(separator = ",")

        editor.putString("accountUuids", newAccountUuidsString)
    }

    private inline fun <reified T : Enum<T>> getEnumStringPref(storage: Storage, key: String, defaultEnum: T): T {
        return try {
            storage.getEnumOrDefault<T>(key, defaultEnum)
+63 −0
Original line number Diff line number Diff line
package net.thunderbird.feature.account.storage.legacy

import net.thunderbird.core.android.account.LegacyAccount
import net.thunderbird.core.preference.storage.Storage
import net.thunderbird.core.preference.storage.StorageEditor
import net.thunderbird.core.preference.storage.getEnumOrDefault
import net.thunderbird.core.preference.storage.putEnum
import net.thunderbird.feature.account.storage.profile.AvatarDto
import net.thunderbird.feature.account.storage.profile.AvatarTypeDto

class LegacyAvatarDtoStorageHandler : AvatarDtoStorageHandler {

    override fun load(
        data: LegacyAccount,
        storage: Storage,
    ) {
        val keyGen = AccountKeyGenerator(data.id)

        with(data) {
            avatar = AvatarDto(
                avatarType = storage.getEnumOrDefault(keyGen.create(KEY_AVATAR_TYPE), AvatarTypeDto.MONOGRAM),
                avatarMonogram = storage.getStringOrNull(keyGen.create(KEY_AVATAR_MONOGRAM)),
                avatarImageUri = storage.getStringOrNull(keyGen.create(KEY_AVATAR_IMAGE_URI)),
                avatarIconName = storage.getStringOrNull(keyGen.create(KEY_AVATAR_ICON_NAME)),
            )
        }
    }

    override fun save(
        data: LegacyAccount,
        storage: Storage,
        editor: StorageEditor,
    ) {
        val keyGen = AccountKeyGenerator(data.id)

        with(data.avatar) {
            editor.putEnum(keyGen.create(KEY_AVATAR_TYPE), avatarType)
            editor.putString(keyGen.create(KEY_AVATAR_MONOGRAM), avatarMonogram)
            editor.putString(keyGen.create(KEY_AVATAR_IMAGE_URI), avatarImageUri)
            editor.putString(keyGen.create(KEY_AVATAR_ICON_NAME), avatarIconName)
        }
    }

    override fun delete(
        data: LegacyAccount,
        storage: Storage,
        editor: StorageEditor,
    ) {
        val keyGen = AccountKeyGenerator(data.id)

        editor.remove(keyGen.create(KEY_AVATAR_TYPE))
        editor.remove(keyGen.create(KEY_AVATAR_MONOGRAM))
        editor.remove(keyGen.create(KEY_AVATAR_IMAGE_URI))
        editor.remove(keyGen.create(KEY_AVATAR_ICON_NAME))
    }

    private companion object Companion {
        const val KEY_AVATAR_TYPE = "avatarType"
        const val KEY_AVATAR_MONOGRAM = "avatarMonogram"
        const val KEY_AVATAR_IMAGE_URI = "avatarImageUri"
        const val KEY_AVATAR_ICON_NAME = "avatarIconName"
    }
}
Loading