AppStateRepo.kt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.baseTag
  10. import com.example.modifier.data.AppState
  11. import com.example.modifier.enums.ReqState
  12. import com.example.modifier.utils.getContext
  13. import kotlinx.coroutines.CoroutineScope
  14. import kotlinx.coroutines.Dispatchers
  15. import kotlinx.coroutines.flow.MutableStateFlow
  16. import kotlinx.coroutines.flow.update
  17. import kotlinx.coroutines.launch
  18. val Context.appStateDataStore by preferencesDataStore(name = "${BuildConfig.APPLICATION_ID}.appState")
  19. class AppStateRepo private constructor(private val context: Context) {
  20. companion object {
  21. private const val TAG = "${baseTag}/AppStateRepo"
  22. val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { AppStateRepo(getContext()) }
  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. val GROUP_EXECUTED_NUM = intPreferencesKey("group_executed_num")
  30. val GROUP_SUCCESS_NUM = intPreferencesKey("group_success_num")
  31. }
  32. val appState = MutableStateFlow(
  33. AppState(
  34. send = false,
  35. singleExecuted = 0,
  36. singleSuccess = 0,
  37. groupExecuted = 0,
  38. groupSuccess = 0,
  39. requestedNum = 0,
  40. running = false,
  41. preparing = false,
  42. checkingConnection = false,
  43. suspended = false,
  44. reqState = ReqState.NONE
  45. )
  46. )
  47. init {
  48. CoroutineScope(Dispatchers.IO).launch {
  49. context.appStateDataStore.data.collect { preferences ->
  50. appState.update {
  51. it.copy(
  52. send = preferences[PreferencesKeys.SEND] ?: false,
  53. singleExecuted = preferences[PreferencesKeys.EXECUTED_NUM] ?: 0,
  54. singleSuccess = preferences[PreferencesKeys.SUCCESS_NUM] ?: 0,
  55. groupExecuted = preferences[PreferencesKeys.GROUP_EXECUTED_NUM] ?: 0,
  56. groupSuccess = preferences[PreferencesKeys.GROUP_SUCCESS_NUM] ?: 0,
  57. requestedNum = preferences[PreferencesKeys.REQUESTED_NUM] ?: 0
  58. )
  59. }
  60. }
  61. }
  62. }
  63. suspend fun incrementExecutedNum(num: Int = 1, success: Boolean) {
  64. context.appStateDataStore.edit { preferences ->
  65. val executedNum = (preferences[PreferencesKeys.EXECUTED_NUM] ?: 0) + num
  66. preferences[PreferencesKeys.EXECUTED_NUM] = executedNum
  67. if (success) {
  68. val successNum = (preferences[PreferencesKeys.SUCCESS_NUM] ?: 0) + num
  69. preferences[PreferencesKeys.SUCCESS_NUM] = successNum
  70. }
  71. }
  72. }
  73. suspend fun incrementGroupExecutedNum(num: Int = 1, success: Boolean) {
  74. context.appStateDataStore.edit { preferences ->
  75. val executedNum = (preferences[PreferencesKeys.GROUP_EXECUTED_NUM] ?: 0) + num
  76. preferences[PreferencesKeys.GROUP_EXECUTED_NUM] = executedNum
  77. if (success) {
  78. val successNum = (preferences[PreferencesKeys.GROUP_SUCCESS_NUM] ?: 0) + num
  79. preferences[PreferencesKeys.GROUP_SUCCESS_NUM] = successNum
  80. }
  81. }
  82. }
  83. suspend fun incrementRequestedNum() {
  84. context.appStateDataStore.edit { preferences ->
  85. val requestedNum = (preferences[PreferencesKeys.REQUESTED_NUM] ?: 0) + 1
  86. Log.i(TAG, "incrementRequestedNum: $requestedNum")
  87. preferences[PreferencesKeys.REQUESTED_NUM] = requestedNum
  88. }
  89. }
  90. suspend fun setRequestedNum(num: Int) {
  91. context.appStateDataStore.edit { preferences ->
  92. val requestedNum = (preferences[PreferencesKeys.REQUESTED_NUM] ?: 0) + 1
  93. Log.i(TAG, "incrementRequestedNum: $requestedNum")
  94. preferences[PreferencesKeys.REQUESTED_NUM] = requestedNum
  95. }
  96. }
  97. suspend fun resetExecutedNum() {
  98. context.appStateDataStore.edit { preferences ->
  99. preferences[PreferencesKeys.EXECUTED_NUM] = 0
  100. }
  101. }
  102. suspend fun resetSuccessNum() {
  103. context.appStateDataStore.edit { preferences ->
  104. preferences[PreferencesKeys.SUCCESS_NUM] = 0
  105. }
  106. }
  107. suspend fun resetGroupExecutedNum() {
  108. context.appStateDataStore.edit { preferences ->
  109. preferences[PreferencesKeys.GROUP_EXECUTED_NUM] = 0
  110. }
  111. }
  112. suspend fun resetGroupSuccessNum() {
  113. context.appStateDataStore.edit { preferences ->
  114. preferences[PreferencesKeys.GROUP_SUCCESS_NUM] = 0
  115. }
  116. }
  117. suspend fun resetRequestedNum() {
  118. context.appStateDataStore.edit { preferences ->
  119. preferences[PreferencesKeys.REQUESTED_NUM] = 0
  120. }
  121. }
  122. suspend fun updateSend(send: Boolean) {
  123. context.appStateDataStore.edit { preferences ->
  124. preferences[PreferencesKeys.SEND] = send
  125. }
  126. }
  127. fun updateRuntimeFlags(
  128. running: Boolean? = null,
  129. preparing: Boolean? = null,
  130. checkingConnection: Boolean? = null,
  131. suspended: Boolean? = null,
  132. reqState: ReqState? = null,
  133. storing: Boolean? = null
  134. ) {
  135. appState.update {
  136. it.copy(
  137. running = running ?: it.running,
  138. preparing = preparing ?: it.preparing,
  139. checkingConnection = checkingConnection ?: it.checkingConnection,
  140. suspended = suspended ?: it.suspended,
  141. reqState = reqState ?: it.reqState,
  142. storing = storing ?: it.storing
  143. )
  144. }
  145. }
  146. fun incrementFailure() {
  147. appState.update {
  148. it.copy(failure = it.failure + 1)
  149. }
  150. }
  151. fun resetFailure() {
  152. appState.update {
  153. it.copy(failure = 0)
  154. }
  155. }
  156. }