This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |