Skip to content

Commit

Permalink
counter module: Add counter ViewModel to handle actions
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza94max committed Dec 7, 2023
1 parent 7ebf965 commit e9c9c60
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package com.islamey.hamza.wazaker.ui.CounterFragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.islamey.hamza.wazaker.utils.RandomColor.getRandomColor
import com.islamey.hamza.wazaker.utils.Utils.getTotalCounts
import com.islamey.hamza.wazaker.utils.Utils.saveTotalCountsInSharedPreference
import com.islamey.wazkar.R
import com.islamey.wazkar.databinding.FragmentCounterBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class CounterFragment : Fragment() {

private lateinit var binding: FragmentCounterBinding

private var counts = 0
private var totalCounts = 0
private val counterViewModel: CounterViewModel by viewModels()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand All @@ -33,49 +35,34 @@ class CounterFragment : Fragment() {
initUi()
}

@SuppressLint("SetTextI18n")
private fun initUi() {

getDataFromSharedPreferences()
lifecycleScope.launch {
counterViewModel.counts.collect { counts ->
binding.countertextview.text = counts.toString()
checkCounts(counts)
}
}

lifecycleScope.launch {
counterViewModel.totalCounts.collect { totalCounts ->
binding.totalCountstextview.text =
getString(R.string.totalzeker) + " " + totalCounts.toString()
}
}

binding.counterScreenLayout.setOnClickListener {
incrementCounter()
counterViewModel.incrementCounter()
}

binding.resetbtn.setOnClickListener {
resetCounter()
counterViewModel.resetCounter()
}
}

@SuppressLint("SetTextI18n")
fun getDataFromSharedPreferences() {
totalCounts = context?.let { getTotalCounts(it) }!!
setTotalZekerCountsText(totalCounts)
}

@SuppressLint("SetTextI18n")
fun setTotalZekerCountsText(TotalZekerCounts: Int) {
binding.totalCountstextview.text = getString(R.string.totalzeker) + " " + TotalZekerCounts
}

private fun incrementCounter() {
counts++
totalCounts++

checkCounts(counts)
binding.countertextview.text = counts.toString()
setTotalZekerCountsText(totalCounts)

saveTotalCountsInSharedPreference(requireContext(), totalCounts)
}

private fun resetCounter() {
counts = 0
binding.countertextview.text = counts.toString()
binding.countertextview.setTextColor(Color.WHITE)
}

private fun checkCounts(counts: Int) {
if (counts % 33 == 0) binding.countertextview.setTextColor(getRandomColor())
if (counts != 0 && counts % 33 == 0) binding.countertextview.setTextColor(getRandomColor())
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Expand All @@ -87,18 +74,11 @@ class CounterFragment : Fragment() {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == R.id.delete) {
resetTotalCounter()
counterViewModel.resetTotalCounter()
Toast.makeText(requireContext(), R.string.TotalDeleted, Toast.LENGTH_SHORT).show()
return true
}
return super.onOptionsItemSelected(item)
}

private fun resetTotalCounter() {
resetCounter()
totalCounts = 0
setTotalZekerCountsText(totalCounts)
saveTotalCountsInSharedPreference(requireContext(), totalCounts)
Toast.makeText(context, getString(R.string.TotalDeleted), Toast.LENGTH_SHORT).show()
}

}
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
)
}


}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
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

0 comments on commit e9c9c60

Please sign in to comment.