AppStateRepository.kt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.example.modifier.repo
  2. import android.content.Context
  3. import android.util.Log
  4. import androidx.datastore.preferences.core.booleanPreferencesKey
  5. import androidx.datastore.preferences.core.edit
  6. import androidx.datastore.preferences.core.intPreferencesKey
  7. import androidx.datastore.preferences.preferencesDataStore
  8. import com.example.modifier.BuildConfig
  9. import com.example.modifier.data.AppRuntimeFlags
  10. import com.example.modifier.data.AppState
  11. import com.example.modifier.enums.RequestNumberState
  12. import kotlinx.coroutines.CoroutineScope
  13. import kotlinx.coroutines.MainScope
  14. import kotlinx.coroutines.flow.MutableStateFlow
  15. import kotlinx.coroutines.flow.combine
  16. import kotlinx.coroutines.flow.stateIn
  17. import kotlinx.coroutines.flow.update
  18. import kotlinx.coroutines.launch
  19. import kotlin.coroutines.coroutineContext
  20. val Context.appStateDataStore by preferencesDataStore(name = "${BuildConfig.APPLICATION_ID}.appState")
  21. class AppStateRepository(
  22. val context: Context
  23. ) {
  24. private object PreferencesKeys {
  25. val SEND = booleanPreferencesKey("send")
  26. val EXECUTED_NUM = intPreferencesKey("executed_num")
  27. val SUCCESS_NUM = intPreferencesKey("success_num")
  28. val REQUESTED_NUM = intPreferencesKey("requested_num")
  29. }
  30. private val appRuntimeFlags = MutableStateFlow(AppRuntimeFlags())
  31. private val appStateFlow =
  32. context.appStateDataStore.data.combine(appRuntimeFlags) { preferences, runtimeFlags ->
  33. val sPrefs =
  34. context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE)
  35. val busy =
  36. runtimeFlags.running || runtimeFlags.requesting || runtimeFlags.preparing || runtimeFlags.checkingConnection
  37. Log.i("AppStateRepository.appStateFlow", "running: ${runtimeFlags.running}, \nrequesting: ${runtimeFlags.requesting}, \npreparing: ${runtimeFlags.preparing}, \ncheckingConnection: ${runtimeFlags.checkingConnection}, \nbusy: $busy")
  38. AppState(
  39. send = preferences[PreferencesKeys.SEND] ?: false,
  40. executedNum = preferences[PreferencesKeys.EXECUTED_NUM] ?: 0,
  41. successNum = preferences[PreferencesKeys.SUCCESS_NUM]
  42. ?: sPrefs.getInt("sendCount", 0),
  43. requestedNum = preferences[PreferencesKeys.REQUESTED_NUM]
  44. ?: sPrefs.getInt("requestNumberCount", 0),
  45. running = runtimeFlags.running,
  46. requesting = runtimeFlags.requesting,
  47. preparing = runtimeFlags.preparing,
  48. checkingConnection = runtimeFlags.checkingConnection,
  49. busy = runtimeFlags.running || runtimeFlags.requesting || runtimeFlags.preparing || runtimeFlags.checkingConnection,
  50. suspended = runtimeFlags.suspended,
  51. requestNumberState = runtimeFlags.requestNumberState
  52. )
  53. }
  54. init {
  55. MainScope().launch {
  56. }
  57. }
  58. suspend fun stateFlow() = appStateFlow.stateIn(CoroutineScope(coroutineContext))
  59. suspend fun incrementExecutedNum(success: Boolean) {
  60. context.appStateDataStore.edit { preferences ->
  61. val executedNum = (preferences[PreferencesKeys.EXECUTED_NUM] ?: 0) + 1
  62. preferences[PreferencesKeys.EXECUTED_NUM] = executedNum
  63. if (success) {
  64. val successNum = (preferences[PreferencesKeys.SUCCESS_NUM] ?: 0) + 1
  65. preferences[PreferencesKeys.SUCCESS_NUM] = successNum
  66. }
  67. }
  68. }
  69. suspend fun incrementRequestedNum() {
  70. context.appStateDataStore.edit { preferences ->
  71. val requestedNum = (preferences[PreferencesKeys.REQUESTED_NUM] ?: 0) + 1
  72. preferences[PreferencesKeys.REQUESTED_NUM] = requestedNum
  73. }
  74. }
  75. suspend fun resetExecutedNum() {
  76. context.appStateDataStore.edit { preferences ->
  77. preferences[PreferencesKeys.EXECUTED_NUM] = 0
  78. }
  79. }
  80. suspend fun resetSuccessNum() {
  81. context.appStateDataStore.edit { preferences ->
  82. preferences[PreferencesKeys.SUCCESS_NUM] = 0
  83. }
  84. }
  85. suspend fun resetRequestedNum() {
  86. context.appStateDataStore.edit { preferences ->
  87. preferences[PreferencesKeys.REQUESTED_NUM] = 0
  88. }
  89. }
  90. suspend fun updateSend(send: Boolean) {
  91. context.appStateDataStore.edit { preferences ->
  92. preferences[PreferencesKeys.SEND] = send
  93. }
  94. }
  95. fun updateRuntimeFlags(
  96. running: Boolean? = null,
  97. requesting: Boolean? = null,
  98. preparing: Boolean? = null,
  99. checkingConnection: Boolean? = null,
  100. suspended: Boolean? = null,
  101. requestNumberState: RequestNumberState? = null
  102. ) {
  103. Log.i(
  104. "AppStateRepository.updateRuntimeFlags",
  105. "running: $running, \nrequesting: $requesting, \npreparing: $preparing, \ncheckingConnection: $checkingConnection, \nsuspended: $suspended, \nrequestNumberState: $requestNumberState"
  106. )
  107. val value = appRuntimeFlags.value
  108. appRuntimeFlags.update {
  109. it.copy(
  110. running = running ?: value.running,
  111. requesting = requesting ?: value.requesting,
  112. preparing = preparing ?: value.preparing,
  113. checkingConnection = checkingConnection ?: value.checkingConnection,
  114. suspended = suspended ?: value.suspended,
  115. requestNumberState = requestNumberState ?: value.requestNumberState
  116. )
  117. }
  118. }
  119. }