Skip to content

Instantly share code, notes, and snippets.

View attilakruchio's full-sized avatar

Attila attilakruchio

View GitHub Profile
@attilakruchio
attilakruchio / BaseFragment.kt
Created June 25, 2022 12:58
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
abstract class BaseFragment : Fragment() {
abstract val viewModel: BaseViewModel
@CallSuper
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.events.collect { event ->
onEvent(event)
@attilakruchio
attilakruchio / BaseViewModel.kt
Created June 25, 2022 13:01
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
open class BaseViewModel : ViewModel() {
private val _events = Channel<SingleEvent>(Channel.UNLIMITED)
val events = _events.receiveAsFlow()
}
@attilakruchio
attilakruchio / SingleEvent.kt
Created June 25, 2022 13:01
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
sealed interface SingleEvent
data class NavigateTo(
val navDirections: NavDirections
) : SingleEvent
@attilakruchio
attilakruchio / Navigator.kt
Created June 25, 2022 13:02
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
@ActivityRetainedScoped
class Navigator @Inject constructor() {
private val _navEvents = Channel<Direction>(Channel.UNLIMITED)
val navEvents: Flow<Direction> = _navEvents.receiveAsFlow()
fun navigateTo(navDirections: NavDirections) {
_navEvents.trySend(Direction.NavigateTo(navDirections))
}
@attilakruchio
attilakruchio / NavigatorExt.kt
Created June 25, 2022 13:03
Gist for 'Easy and elegant navigation with Jetpack Navigation and Hilt' Medium article (https://medium.com/@kruchio98/6f93a0461298)
fun Navigator.setupWithNavController(
activity: ComponentActivity, navController: NavController
) {
activity.lifecycleScope.launch {
activity.repeatOnLifecycle(Lifecycle.State.STARTED) {
navEvents.collect { direction ->
when (direction) {
is Navigator.Direction.NavigateTo -> {
navController.navigate(direction.navDirections)
}
@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"
@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 / 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
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 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,
)