| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package com.example.modifier.repo
- import android.content.Context
- import android.util.Log
- import androidx.datastore.preferences.core.booleanPreferencesKey
- import androidx.datastore.preferences.core.edit
- import androidx.datastore.preferences.core.intPreferencesKey
- import androidx.datastore.preferences.preferencesDataStore
- import com.example.modifier.BuildConfig
- import com.example.modifier.data.AppRuntimeFlags
- import com.example.modifier.data.AppState
- import com.example.modifier.enums.RequestNumberState
- import kotlinx.coroutines.CoroutineScope
- import kotlinx.coroutines.MainScope
- import kotlinx.coroutines.flow.MutableStateFlow
- import kotlinx.coroutines.flow.combine
- import kotlinx.coroutines.flow.stateIn
- import kotlinx.coroutines.flow.update
- import kotlinx.coroutines.launch
- import kotlin.coroutines.coroutineContext
- val Context.appStateDataStore by preferencesDataStore(name = "${BuildConfig.APPLICATION_ID}.appState")
- class AppStateRepository(
- val context: Context
- ) {
- private object PreferencesKeys {
- val SEND = booleanPreferencesKey("send")
- val EXECUTED_NUM = intPreferencesKey("executed_num")
- val SUCCESS_NUM = intPreferencesKey("success_num")
- val REQUESTED_NUM = intPreferencesKey("requested_num")
- }
- private val appRuntimeFlags = MutableStateFlow(AppRuntimeFlags())
- private val appStateFlow =
- context.appStateDataStore.data.combine(appRuntimeFlags) { preferences, runtimeFlags ->
- val sPrefs =
- context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE)
- val busy =
- runtimeFlags.running || runtimeFlags.requesting || runtimeFlags.preparing || runtimeFlags.checkingConnection
- Log.i("AppStateRepository.appStateFlow", "running: ${runtimeFlags.running}, \nrequesting: ${runtimeFlags.requesting}, \npreparing: ${runtimeFlags.preparing}, \ncheckingConnection: ${runtimeFlags.checkingConnection}, \nbusy: $busy")
- AppState(
- send = preferences[PreferencesKeys.SEND] ?: false,
- executedNum = preferences[PreferencesKeys.EXECUTED_NUM] ?: 0,
- successNum = preferences[PreferencesKeys.SUCCESS_NUM]
- ?: sPrefs.getInt("sendCount", 0),
- requestedNum = preferences[PreferencesKeys.REQUESTED_NUM]
- ?: sPrefs.getInt("requestNumberCount", 0),
- running = runtimeFlags.running,
- requesting = runtimeFlags.requesting,
- preparing = runtimeFlags.preparing,
- checkingConnection = runtimeFlags.checkingConnection,
- busy = runtimeFlags.running || runtimeFlags.requesting || runtimeFlags.preparing || runtimeFlags.checkingConnection,
- suspended = runtimeFlags.suspended,
- requestNumberState = runtimeFlags.requestNumberState
- )
- }
- init {
- MainScope().launch {
- }
- }
- suspend fun stateFlow() = appStateFlow.stateIn(CoroutineScope(coroutineContext))
- suspend fun incrementExecutedNum(success: Boolean) {
- context.appStateDataStore.edit { preferences ->
- val executedNum = (preferences[PreferencesKeys.EXECUTED_NUM] ?: 0) + 1
- preferences[PreferencesKeys.EXECUTED_NUM] = executedNum
- if (success) {
- val successNum = (preferences[PreferencesKeys.SUCCESS_NUM] ?: 0) + 1
- preferences[PreferencesKeys.SUCCESS_NUM] = successNum
- }
- }
- }
- suspend fun incrementRequestedNum() {
- context.appStateDataStore.edit { preferences ->
- val requestedNum = (preferences[PreferencesKeys.REQUESTED_NUM] ?: 0) + 1
- preferences[PreferencesKeys.REQUESTED_NUM] = requestedNum
- }
- }
- suspend fun resetExecutedNum() {
- context.appStateDataStore.edit { preferences ->
- preferences[PreferencesKeys.EXECUTED_NUM] = 0
- }
- }
- suspend fun resetSuccessNum() {
- context.appStateDataStore.edit { preferences ->
- preferences[PreferencesKeys.SUCCESS_NUM] = 0
- }
- }
- suspend fun resetRequestedNum() {
- context.appStateDataStore.edit { preferences ->
- preferences[PreferencesKeys.REQUESTED_NUM] = 0
- }
- }
- suspend fun updateSend(send: Boolean) {
- context.appStateDataStore.edit { preferences ->
- preferences[PreferencesKeys.SEND] = send
- }
- }
- fun updateRuntimeFlags(
- running: Boolean? = null,
- requesting: Boolean? = null,
- preparing: Boolean? = null,
- checkingConnection: Boolean? = null,
- suspended: Boolean? = null,
- requestNumberState: RequestNumberState? = null
- ) {
- Log.i(
- "AppStateRepository.updateRuntimeFlags",
- "running: $running, \nrequesting: $requesting, \npreparing: $preparing, \ncheckingConnection: $checkingConnection, \nsuspended: $suspended, \nrequestNumberState: $requestNumberState"
- )
- val value = appRuntimeFlags.value
- appRuntimeFlags.update {
- it.copy(
- running = running ?: value.running,
- requesting = requesting ?: value.requesting,
- preparing = preparing ?: value.preparing,
- checkingConnection = checkingConnection ?: value.checkingConnection,
- suspended = suspended ?: value.suspended,
- requestNumberState = requestNumberState ?: value.requestNumberState
- )
- }
- }
- }
|