Skip to content

Instantly share code, notes, and snippets.

@makorowy
makorowy / activity_main.xml
Created March 2, 2019 14:07
Sample for article needs - How to create a compound view?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
@makorowy
makorowy / Interactions.kt
Last active October 18, 2020 09:37
Example of not handling interactions via ViewModel, part 1
sealed class Interaction {
object BackButtonClick : Interaction()
object SkipButtonClick : Interaction()
object SaveButtonClick : Interaction()
data class NameChange(val name: String) : Interaction()
}
@makorowy
makorowy / Repository.kt
Last active October 18, 2020 09:49
Example of not handling interactions via ViewModel, part 3
object RepositoryData
class Repository {
fun observeChanges(observer: (RepositoryData) -> Unit) {
//some logic
observer.invoke(RepositoryData)
}
}
@makorowy
makorowy / ViewModel.kt
Last active October 18, 2020 09:46
Example of not handling interactions via ViewModel, part 4
data class ModelData(
val progressVisible: Boolean,
)
class ViewModel(
private val repository: Repository,
private val navigator: Navigator
) : ViewModel() {
private val _liveData = MutableLiveData<ModelData>()
@makorowy
makorowy / Fragment.kt
Last active October 18, 2020 09:46
Example of not handling interactions via ViewModel, part 5
class Fragment : Fragment() {
@Inject
lateinit var viewModel: ViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.liveData.observe(this, Observer { data -> /* update the view */ })
@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()
@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 / 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 / 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 / SettingsRepository.kt
Created November 22, 2020 14:36
[BLOG] Example of some repository, implementation not important
class SettingsRepository {
val settings: Settings get() = /* retrieving settings */
}