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

Unverified Commit fb32122c authored by Sunik Kupfer's avatar Sunik Kupfer Committed by GitHub
Browse files

Send setting value straight away (#438)

parent 0b411ea9
Loading
Loading
Loading
Loading
+16 −17
Original line number Diff line number Diff line
@@ -11,7 +11,9 @@ import android.content.Context
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.provider.CalendarContract
import android.util.Log
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.callbackFlow

object AppAccount {
@@ -47,7 +49,7 @@ object AppAccount {
        if (am.addAccountExplicitly(newAccount, null, null)) {
            account = newAccount
            ContentResolver.setIsSyncable(newAccount, CalendarContract.AUTHORITY, 1)
            syncInterval(context, DEFAULT_SYNC_INTERVAL)
            setSyncInterval(context, DEFAULT_SYNC_INTERVAL)
            return newAccount
        }

@@ -55,29 +57,26 @@ object AppAccount {
    }


    fun syncInterval(context: Context) =
        preferences(context).getLong(KEY_SYNC_INTERVAL, SYNC_INTERVAL_MANUALLY)

    fun syncIntervalFlow(context: Context) = callbackFlow {
        val preferences = preferences(context)
    fun getSyncIntervalFlow(context: Context) = callbackFlow {
        val preferences = getPreferences(context)
        val listener = OnSharedPreferenceChangeListener { _, key ->
            if (key == KEY_SYNC_INTERVAL) {
                trySend(preferences.getLong(KEY_SYNC_INTERVAL, SYNC_INTERVAL_MANUALLY))
            }
            if (key == KEY_SYNC_INTERVAL)
                trySend(preferences.getLong(KEY_SYNC_INTERVAL, DEFAULT_SYNC_INTERVAL))
        }
        preferences.registerOnSharedPreferenceChangeListener(listener)

        if (preferences.contains(KEY_SYNC_INTERVAL))
            send(preferences.getLong(KEY_SYNC_INTERVAL, DEFAULT_SYNC_INTERVAL))
        awaitClose {
            preferences.unregisterOnSharedPreferenceChangeListener(listener)
        }
    }
    }.buffer(Channel.UNLIMITED)

    fun syncInterval(context: Context, syncInterval: Long) {
    fun setSyncInterval(context: Context, syncInterval: Long) {
        // don't use the sync framework anymore (legacy)
        ContentResolver.setSyncAutomatically(get(context), CalendarContract.AUTHORITY, false)

        // remember sync interval so that it can be checked/restored later
        preferences(context).edit()
        getPreferences(context).edit()
            .putLong(KEY_SYNC_INTERVAL, syncInterval)
            .apply()

@@ -94,7 +93,7 @@ object AppAccount {

    // helpers

    private fun preferences(context: Context) =
    private fun getPreferences(context: Context) =
        context.getSharedPreferences(PREF_ACCOUNT, 0)

}
+2 −2
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ class SubscriptionsModel(application: Application): AndroidViewModel(application
    val forceDarkMode = settings.forceDarkModeFlow()
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), false)

    val syncInterval = AppAccount.syncIntervalFlow(application)
    val syncInterval = AppAccount.getSyncIntervalFlow(application)
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), AppAccount.DEFAULT_SYNC_INTERVAL)

    init {
@@ -128,7 +128,7 @@ class SubscriptionsModel(application: Application): AndroidViewModel(application
    }

    fun onSyncIntervalChange(interval: Long) {
        AppAccount.syncInterval(getApplication(), interval)
        AppAccount.setSyncInterval(getApplication(), interval)
    }

    @SuppressLint("BatteryLife")