Skip to content

Instantly share code, notes, and snippets.

View plusmobileapps's full-sized avatar

Andrew Steinmetz plusmobileapps

View GitHub Profile
sealed class DataWrapper {
abstract val id: Int
data class CarouselData(override val id: Int = UUID.randomUUID().hashCode(), val items: List<CarouselItem>) :
DataWrapper()
data class CardData(override val id: Int, val header: String, val imageUrl: String, val body: String) :
DataWrapper()
}
class RecyclerViewDiffUtil : DiffUtil.ItemCallback<DataWrapper>() {
override fun areItemsTheSame(oldItem: DataWrapper, newItem: DataWrapper): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: DataWrapper, newItem: DataWrapper): Boolean {
return oldItem == newItem
}
}
class RecyclerViewListAdapter(
private val carouselItemClickListener: (CarouselItem) -> Unit,
private val cardClickListener: (DataWrapper.CardData) -> Unit,
private val cardDeleteListener: (DataWrapper.CardData) -> Unit,
private val glide: RequestManager
) : ListAdapter<DataWrapper, BaseViewHolder<*>>(
RecyclerViewDiffUtil()
) {
override fun getItemViewType(position: Int): Int {
abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun bind(data: T)
}
class CardViewHolder(itemView: View,
private val glide: RequestManager,
private val clickListener: (DataWrapper.CardData) -> Unit,
private val deleteListener: (DataWrapper.CardData) -> Unit) : BaseViewHolder<DataWrapper.CardData>(itemView) {
class MainFragment : Fragment() {
val viewModel: MainViewModel by viewModel()
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val adapter = RecyclerViewListAdapter(
carouselItemClickListener = { viewModel.onCarouselItemClicked(it) },
cardClickListener = { viewModel.onCardClicked(it) },
class MainViewModel (private val stateReducer: StateReducer,
private val countryRepository: CountryRepository
) : ViewModel(), MainView {
private val bigCards: LiveData<List<Card>> = Transformations.map(countryRepository.getAll()) { countries ->
return@map countries.map { country ->
Card(country.id!!, country.name, country.imageUrl, country.description)
}
}
@plusmobileapps
plusmobileapps / MutableSaveStateFlow.kt
Last active December 22, 2021 18:09
MutableSaveStateFlow - a workaround of using the SavedStateHandle with a StateFlow
class MutableSaveStateFlow<T>(
private val savedStateHandle: SavedStateHandle,
private val key: String,
defaultValue: T
) {
private val _state: MutableStateFlow<T> =
MutableStateFlow(savedStateHandle.get<T>(key) ?: defaultValue)
var value: T
get() = _state.value
@plusmobileapps
plusmobileapps / CustomTextField.kt
Created June 9, 2022 15:48
CustomTextField with non standard hint not embedded in outline
/**
* MIT License
*
* Copyright (c) 2022 Andrew Steinmetz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is