-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
counter module: Add counter ViewModel to handle actions
- Loading branch information
1 parent
7ebf965
commit e9c9c60
Showing
3 changed files
with
104 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
app/src/main/java/com/islamey/hamza/wazaker/ui/CounterFragment/CounterViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.islamey.hamza.wazaker.ui.CounterFragment | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class CounterViewModel @Inject constructor( | ||
private val application: Application | ||
) : ViewModel() { | ||
|
||
private val _counts = MutableStateFlow(0) | ||
val counts: StateFlow<Int> get() = _counts | ||
|
||
private val _totalCounts = MutableStateFlow(0) | ||
val totalCounts: StateFlow<Int> get() = _totalCounts | ||
|
||
init { | ||
viewModelScope.launch { | ||
_totalCounts.value = getTotalCountsFromSharedPreference() | ||
} | ||
} | ||
|
||
fun incrementCounter() { | ||
viewModelScope.launch { | ||
_counts.value++ | ||
_totalCounts.value++ | ||
saveTotalCountsInSharedPreference() | ||
} | ||
} | ||
|
||
fun resetCounter() { | ||
viewModelScope.launch { | ||
_counts.value = 0 | ||
} | ||
} | ||
|
||
fun resetTotalCounter() { | ||
viewModelScope.launch { | ||
_totalCounts.value = 0 | ||
saveTotalCountsInSharedPreference() | ||
} | ||
} | ||
|
||
private suspend fun saveTotalCountsInSharedPreference() { | ||
withContext(Dispatchers.IO) { | ||
val sharedPref = getSharedPreferences() | ||
val editor: SharedPreferences.Editor = sharedPref.edit() | ||
editor.putInt("totalCounts", _totalCounts.value) | ||
editor.apply() | ||
} | ||
} | ||
|
||
private suspend fun getTotalCountsFromSharedPreference(): Int { | ||
return withContext(Dispatchers.IO) { | ||
val sharedPref = getSharedPreferences() | ||
sharedPref.getInt("totalCounts", 0) | ||
} | ||
} | ||
|
||
private fun getSharedPreferences(): SharedPreferences { | ||
return application.getSharedPreferences( | ||
"pref", Context.MODE_PRIVATE | ||
) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Sun Mar 06 18:47:16 EET 2022 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip | ||
distributionPath=wrapper/dists | ||
zipStorePath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME |