Skip to content

Instantly share code, notes, and snippets.

View fvilarino's full-sized avatar

Francesc Vilariño Güell fvilarino

View GitHub Profile
@fvilarino
fvilarino / mvvm_viewmodel_sample.kt
Last active April 12, 2021 23:33
MVVM ViewModel Sample
enum class LoadState {
IDLE,
LOADING,
LOADED,
ERROR
}
@HiltViewModel
class CityViewModel @Inject constructor(
private val getCitiesInteractor: GetCitiesInteractor,
@fvilarino
fvilarino / mvi_viewmodel_sample.kt
Last active April 12, 2021 23:19
Sample MVI ViewModel with LiveData
enum class LoadState {
IDLE,
LOADING,
LOADED,
ERROR
}
data class CitiesState(
val loadState: LoadState,
val cities: List<CityResultModel>,
@fvilarino
fvilarino / mvi_viewmodel.kt
Last active April 12, 2021 23:28
MVI ViewModel
/**
* Marker class for the view state
*/
interface State
/**
* Marker class for View Intents
*/
interface MviIntent
@fvilarino
fvilarino / cities_mvi_viewmodel.kt
Last active April 12, 2021 23:52
Cities MVI ViewModel
enum class LoadState {
IDLE,
LOADING,
LOADED,
ERROR
}
data class CityState(
val loadState: LoadState,
val cities: List<CityResultModel>,
@fvilarino
fvilarino / city_mvi_fragment.kt
Last active April 13, 2021 00:21
City MVI Fragment
@AndroidEntryPoint
class CityFragment : Fragment() {
private lateinit var binding: FragmentCityBinding
private val viewModel: CityViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
@fvilarino
fvilarino / RecyclerViewBindingItem.kt
Created April 19, 2021 20:35
Viewholder item base class
interface Diffable {
val id: Long
}
interface RecyclerViewBindingItem : Diffable {
val type: Int
fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewViewHolder<*>
}
@fvilarino
fvilarino / base_viewholder.kt
Created April 19, 2021 20:40
Base Viewholder class
abstract class RecyclerViewViewHolder<T : Diffable>(
protected val binding: ViewDataBinding
) : RecyclerView.ViewHolder(binding.root) {
abstract fun bind(bindingItem: T)
}
@fvilarino
fvilarino / based_typed_adapter.kt
Last active April 19, 2021 23:05
Base typed adapter
class TypedBaseAdapter<T : RecyclerViewBindingItem> :
RecyclerView.Adapter<RecyclerViewViewHolder<T>>() {
private val differ: AsyncListDiffer<T> by lazy(LazyThreadSafetyMode.NONE) {
AsyncListDiffer(this, Differ<T>())
}
private var items: List<T> by Delegates.observable(emptyList()) { _, _, new ->
differ.submitList(new)
}
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="state"
type="com.francescsoftware.weathersample.presentation.feature.weather.tabs.forecast.ForecastHeaderState" />
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="state"