Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / ConsentsView.kt
Created March 2, 2019 13:59
Sample for article needs - How to create a compound view?
package com.makor.compoundviewexample
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.consents_view.view.*
@makorowy
makorowy / attrs.xml
Created March 2, 2019 13:54
Sample for article needs - How to create a compound view?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ConsentsView">
<attr name="separatorVisibility" format="boolean"/>
</declare-styleable>
</resources>
@makorowy
makorowy / consents_view.xml
Created March 2, 2019 13:21
Sample for article needs - How to create a compound view?
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:parentTag="android.widget.LinearLayout"
tools:orientation="vertical"
tools:gravity="center">
<TextView
@makorowy
makorowy / activity_main.xml
Created March 2, 2019 12:52
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: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">
<com.makor.compoundviewexample.ConsentsView
@makorowy
makorowy / MainActivity.kt
Last active March 2, 2019 12:53
Sample for article needs - How to create a compound view?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
consentsView.onConsentsCheckedChangeListener = { allConsentsChecked ->
confirmButton.isEnabled = allConsentsChecked
}
}