| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 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.baseTag
- import com.example.modifier.data.AppState
- import com.example.modifier.enums.ReqState
- import com.example.modifier.utils.getContext
- import kotlinx.coroutines.CoroutineScope
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.flow.MutableStateFlow
- import kotlinx.coroutines.flow.update
- import kotlinx.coroutines.launch
- val Context.appStateDataStore by preferencesDataStore(name = "${BuildConfig.APPLICATION_ID}.appState")
- class AppStateRepo private constructor(private val context: Context) {
- companion object {
- private const val TAG = "${baseTag}/AppStateRepo"
- val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { AppStateRepo(getContext()) }
- }
- 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")
- val GROUP_EXECUTED_NUM = intPreferencesKey("group_executed_num")
- val GROUP_SUCCESS_NUM = intPreferencesKey("group_success_num")
- }
- val appState = MutableStateFlow(
- AppState(
- send = false,
- singleExecuted = 0,
- singleSuccess = 0,
- groupExecuted = 0,
- groupSuccess = 0,
- requestedNum = 0,
- running = false,
- preparing = false,
- checkingConnection = false,
- suspended = false,
- reqState = ReqState.NONE
- )
- )
- init {
- CoroutineScope(Dispatchers.IO).launch {
- context.appStateDataStore.data.collect { preferences ->
- appState.update {
- it.copy(
- send = preferences[PreferencesKeys.SEND] ?: false,
- singleExecuted = preferences[PreferencesKeys.EXECUTED_NUM] ?: 0,
- singleSuccess = preferences[PreferencesKeys.SUCCESS_NUM] ?: 0,
- groupExecuted = preferences[PreferencesKeys.GROUP_EXECUTED_NUM] ?: 0,
- groupSuccess = preferences[PreferencesKeys.GROUP_SUCCESS_NUM] ?: 0,
- requestedNum = preferences[PreferencesKeys.REQUESTED_NUM] ?: 0
- )
- }
- }
- }
- }
- suspend fun incrementExecutedNum(num: Int = 1, success: Boolean) {
- context.appStateDataStore.edit { preferences ->
- val executedNum = (preferences[PreferencesKeys.EXECUTED_NUM] ?: 0) + num
- preferences[PreferencesKeys.EXECUTED_NUM] = executedNum
- if (success) {
- val successNum = (preferences[PreferencesKeys.SUCCESS_NUM] ?: 0) + num
- preferences[PreferencesKeys.SUCCESS_NUM] = successNum
- }
- }
- }
- suspend fun incrementGroupExecutedNum(num: Int = 1, success: Boolean) {
- context.appStateDataStore.edit { preferences ->
- val executedNum = (preferences[PreferencesKeys.GROUP_EXECUTED_NUM] ?: 0) + num
- preferences[PreferencesKeys.GROUP_EXECUTED_NUM] = executedNum
- if (success) {
- val successNum = (preferences[PreferencesKeys.GROUP_SUCCESS_NUM] ?: 0) + num
- preferences[PreferencesKeys.GROUP_SUCCESS_NUM] = successNum
- }
- }
- }
- suspend fun incrementRequestedNum() {
- context.appStateDataStore.edit { preferences ->
- val requestedNum = (preferences[PreferencesKeys.REQUESTED_NUM] ?: 0) + 1
- Log.i(TAG, "incrementRequestedNum: $requestedNum")
- preferences[PreferencesKeys.REQUESTED_NUM] = requestedNum
- }
- }
- suspend fun setRequestedNum(num: Int) {
- context.appStateDataStore.edit { preferences ->
- val requestedNum = (preferences[PreferencesKeys.REQUESTED_NUM] ?: 0) + 1
- Log.i(TAG, "incrementRequestedNum: $requestedNum")
- 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 resetGroupExecutedNum() {
- context.appStateDataStore.edit { preferences ->
- preferences[PreferencesKeys.GROUP_EXECUTED_NUM] = 0
- }
- }
- suspend fun resetGroupSuccessNum() {
- context.appStateDataStore.edit { preferences ->
- preferences[PreferencesKeys.GROUP_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,
- preparing: Boolean? = null,
- checkingConnection: Boolean? = null,
- suspended: Boolean? = null,
- reqState: ReqState? = null,
- storing: Boolean? = null
- ) {
- appState.update {
- it.copy(
- running = running ?: it.running,
- preparing = preparing ?: it.preparing,
- checkingConnection = checkingConnection ?: it.checkingConnection,
- suspended = suspended ?: it.suspended,
- reqState = reqState ?: it.reqState,
- storing = storing ?: it.storing
- )
- }
- }
- fun incrementFailure() {
- appState.update {
- it.copy(failure = it.failure + 1)
- }
- }
- fun resetFailure() {
- appState.update {
- it.copy(failure = 0)
- }
- }
- }
|