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
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) |
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
open class BaseViewModel : ViewModel() { | |
private val _events = Channel<SingleEvent>(Channel.UNLIMITED) | |
val events = _events.receiveAsFlow() | |
} |
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
sealed interface SingleEvent | |
data class NavigateTo( | |
val navDirections: NavDirections | |
) : SingleEvent |
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
<?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" |
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
@HiltViewModel | |
class HomeViewModel @Inject constructor( | |
private val navigator: Navigator | |
) : ViewModel() { | |
fun navigateToSettings() { | |
navigator.navigateTo( | |
HomeFragmentDirections.showSettings() | |
) | |
} |
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
class HomeViewModel : ViewModel() { | |
private val _state = MutableStateFlow(State()) | |
val state = _state.asStateFlow() | |
init { | |
loadContacts() | |
loadDeliveryList() | |
} |
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
class HomeViewModel : ViewModel() { | |
private val _state = MutableStateFlow(State()) | |
val state = _state.asStateFlow() | |
init { | |
loadContacts() | |
loadDeliveryList() | |
} |
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
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, | |
) |
OlderNewer