Skip to content

Instantly share code, notes, and snippets.

@makorowy
makorowy / SettingsFragment.kt
Created November 22, 2020 14:44
[BLOG] Example of Fragment handling ViewModel's ViewState and Interactions
class SettingsFragment : Fragment() {
private val viewModel: SettingsViewModel2 by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setInteractions()
observeViewState()
}
@makorowy
makorowy / SettingsViewModel.kt
Created November 22, 2020 14:43
[BLOG] Example of ViewModel implementation with ViewState and Interactions
class SettingsViewModel(
settingsRepository: SettingsRepository
) : ViewModel() {
private var currentViewState = ViewState(
firstName = "",
lastName = "",
notificationsChecked = false,
notificationsEnabled = false
)
@makorowy
makorowy / SettinngsScreen.kt
Created November 22, 2020 14:39
[BLOG] Example of contract between ViewModel and View
data class ViewState(
val firstName: String,
val lastName: String,
val notificationsChecked: Boolean,
val notificationsEnabled: Boolean
)
sealed class Interaction {
data class NotificationSwitchChanged(val enabled: Boolean) : Interaction()
data class FirstNameChange(val firstName: String) : Interaction()
@makorowy
makorowy / SettingsFragment.kt
Last active November 22, 2020 14:53
[BLOG] Example of common fragment implementation
class SettingsFragment : Fragment() {
private val viewModel: SettingsViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setInteractions()
observeViewModel()
}
@makorowy
makorowy / Settings.kt
Created November 22, 2020 14:36
[BLOG] Settings data structure
data class Settings(
val firstName: String,
val lastName: String,
val notificationsChecked: Boolean
)
@makorowy
makorowy / SettingsRepository.kt
Created November 22, 2020 14:36
[BLOG] Example of some repository, implementation not important
class SettingsRepository {
val settings: Settings get() = /* retrieving settings */
}
@makorowy
makorowy / SettingsViewModel.kt
Last active November 22, 2020 14:45
Example of common ViewModel implementation
class SettingsViewModel(
settingsRepository: SettingsRepository
) : ViewModel() {
private val _firstName = MutableLiveData<String>()
val firstName: LiveData<String> = _firstName
private val _lastName = MutableLiveData<String>()
val lastName: LiveData<String> = _lastName
@makorowy
makorowy / Fragment2.kt
Last active October 18, 2020 09:46
Example of not handling interactions via ViewModel, part 8
class Fragment : Fragment() {
@Inject
lateinit var viewModel: ViewModel
@Inject
lateinit var interactor: Interactor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@makorowy
makorowy / ViewModel2.kt
Last active October 18, 2020 09:48
Example of not handling interactions via ViewModel, part 7
data class ModelData(
val progressVisible: Boolean,
)
class ViewModel(
repository: Repository
) : ViewModel() {
private val _liveData = MutableLiveData<ModelData>()
val liveData: LiveData<ModelData> = _liveData
@makorowy
makorowy / Interactor.kt
Last active October 18, 2020 09:48
Example of not handling interactions via ViewModel, part 6
class Interactor(
private val repository: Repository,
private val navigator: Navigator,
private val viewModel: ViewModel
) {
fun onInteraction(interaction: Interaction) {
when (interaction) {
is Interaction.BackButtonClick -> onBackButtonClicked()
is Interaction.SkipButtonClick -> onSkipButtonClicked()