Skip to content

Instantly share code, notes, and snippets.

View attilakruchio's full-sized avatar

Attila attilakruchio

View GitHub Profile
@attilakruchio
attilakruchio / EventChannelFlow.kt
Last active September 14, 2023 07:49
EventChannelFlow implementation, which requires that the emission and collection happens on Main.immediate dispatcher, preventing any event losses.
/*
* Created by Attila Kruchió on 2023. 09. 13. 22:24
* attila.kruchio.dev@gmail.com
*
* Copyright (c) 2023.
* All rights reserved.
*/
package com.attila.kruchio.android.flow
@attilakruchio
attilakruchio / MainActivity.kt
Last active September 13, 2023 18:32
MutableSharedFlow vs. Unlimited Channel for one-shot events
package com.attila.kruchio.test
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlin.coroutines.CoroutineContext
import kotlin.random.Random
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
/*
* Created by Attila Kruchió on 2022. 01. 09. 18:24
* attila.kruchio.dev@gmail.com
*
* Copyright (c) 2022.
* All rights reserved.
*/
package com.attila.kruchio.android.core.ext
@HiltViewModel
class EventViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle,
// ...
) : ViewModel() {
private val _selectedEventId = savedStateHandle.getStateFlow<Int?>(
KEY_SELECTED_EVENT_ID, null
)
private val _eventFilter = savedStateHandle.getStateFlow<EventFilter>(
@attilakruchio
attilakruchio / MainViewModel.kt
Created July 3, 2022 14:14
Gist for 'Taking care of process death with minimal pain using ViewModel' Medium article
@HiltViewModel
class MainViewModel @Inject constructor(
private val hotelRepository: HotelRepository
) : ViewModel() {
private val _filterBySustainability = MutableStateFlow(false)
private val _hotels = MutableStateFlow<List<Hotel>>(emptyList())
val state = combine(
@attilakruchio
attilakruchio / HomeViewModel.kt
Created June 26, 2022 19:07
Gist for 'Assigning value to a StateFlow with '.value = .value.copy()' introduces a race condition. Do this now!' Medium article
class HomeViewModel : ViewModel() {
private val _contacts = MutableStateFlow<List<String>>(emptyList())
private val _deliveryList = MutableStateFlow<List<String>>(emptyList())
val state = combine(_contacts, _deliveryList) { contacts, deliveryList ->
State(
contactList = contacts,
deliveryList = deliveryList,
)
@attilakruchio
attilakruchio / HomeViewModel.kt
Created June 26, 2022 17:36
Gist for 'Assigning value to a StateFlow with '.value = .value.copy()' introduces a race condition. Do this now!' Medium article
class HomeViewModel : ViewModel() {
private val _state = MutableStateFlow(State())
val state = _state.asStateFlow()
init {
loadContacts()
loadDeliveryList()
}
@attilakruchio
attilakruchio / HomeViewModel.kt
Created June 26, 2022 16:47
Gist for 'Assigning value to a StateFlow with '.value = .value.copy()' introduces a race condition. Do this now!' Medium article
class HomeViewModel : ViewModel() {
private val _state = MutableStateFlow(State())
val state = _state.asStateFlow()
init {
loadContacts()
loadDeliveryList()
}
@attilakruchio
attilakruchio / HomeViewModel.kt
Last active June 25, 2022 15:01
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
@HiltViewModel
class HomeViewModel @Inject constructor(
private val navigator: Navigator
) : ViewModel() {
fun navigateToSettings() {
navigator.navigateTo(
HomeFragmentDirections.showSettings()
)
}
@attilakruchio
attilakruchio / activity_main.xml
Created June 25, 2022 13:03
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"