Skip to content

Instantly share code, notes, and snippets.

View putrarifin's full-sized avatar
🌴
On vacation

Muh Arif Saputra putrarifin

🌴
On vacation
View GitHub Profile
# QuotaShop
Protocol: https
Host: quotashop.ml
Title: QuotaShop
# Eevee Save Project
Protocol: https
Host: tiny.cc
Path: eeveesaveproject
Title: Eevee Save Project
suspend fun setUiMode(uiMode: UiMode) {
dataStore.edit { preferences ->
preferences[IS_DARK_MODE] = when (uiMode) {
UiMode.LIGHT -> false
UiMode.DARK -> true
}
}
}
companion object {
val IS_DARK_MODE = preferencesKey<Boolean>("dark_mode")
}
class SettingsManager(context: Context) {
private val dataStore = context.createDataStore(name = "settings_pref")
...
dependencies {
// Preferences DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha01"
}
private fun initCountObserver() {
lifecycleScope.launch {
viewModel.countState.collect { value ->
textview_count.text = "$value"
}
}
}
private fun initView() {
button_plus.setOnClickListener(::incrementCounter)
button_minus.setOnClickListener(::decrementCounter)
}
private fun incrementCounter(view: View) {
viewModel.incrementCount()
}
private fun decrementCounter(view: View) {
class MainActivity : AppCompatActivity() {
private val viewModel by lazy {
ViewModelProvider(this)[MainViewModel::class.java]
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
val stateFlow = MutableStateFlow<Int>(0)
// Observe values
val job = launch {
stateFlow.collect {
print("$it ")
@ExperimentalCoroutinesApi
class MainViewModel : ViewModel() {
private val _countState = MutableStateFlow(0)
val countState: StateFlow<Int> = _countState
fun incrementCount() {
_countState.value++
}