Skip to content

Instantly share code, notes, and snippets.

val fetchDetailsModule = module {
viewModel { FetchDetailsViewModel(get(), get(), get()) }
single { ResourceProvider(androidApplication()) }
single { CoroutinesManager() }
}
class FetchDetailsViewModel(
private val resourceProvider: ResourceProvider,
private val coroutinesManager: CoroutinesManager,
private val fetchDetailsRepo: FetchDetailsRepo
) : ViewModel() {
companion object {
private const val logTag = "SkeletonRepository"
}
class ViewTypeAdapter<E : ViewType<*>>(
private var list: MutableList<E> = mutableListOf(),
private val onItemActionListener: OnItemActionListener? = null
) : RecyclerView.Adapter<ViewTypeHolder>(), BindableAdapter<List<E>> {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewTypeHolder {
val binding: ViewDataBinding =
DataBindingUtil.inflate(LayoutInflater.from(parent.context), viewType, parent, false)
return ViewTypeHolder(binding, onItemActionListener)
interface ViewType<out T> {
@LayoutRes
fun layoutId(): Int
fun data(): T
fun isUserInteractionEnabled() = true
}
data class SingleChoiceViewType(private val model: SingleChoiceQuestion) : ViewType<SingleChoiceQuestion> {
override fun layoutId(): Int = R.layout.layout_single_choice
override fun data(): SingleChoiceQuestion = model
}
private val list = ArrayList<ViewType<*>>()
fun getList(): List<ViewType<*>> {
list.clear()
val singleChoiceQuestion = SingleChoiceQuestion(
question = resourceProvider.getString(R.string.txt_question_car),
optionOne = resourceProvider.getString(R.string.txt_yes),
optionTwo = resourceProvider.getString(R.string.txt_no)
)
list.add(SingleChoiceViewType(singleChoiceQuestion))
val multipleChoiceQuestion = MultipleChoiceQuestion(
class ViewTypeHolder(
private val binding: ViewDataBinding,
private val onItemActionListener: OnItemActionListener?
) :
RecyclerView.ViewHolder(binding.root) {
fun bindItem(item: ViewType<*>) {
binding.setVariable(BR.model, item.data())
if (item.isUserInteractionEnabled()) {
binding.setVariable(BR.position, adapterPosition)
binding.setVariable(BR.actionItemListener, onItemActionListener)
<?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="model"
type="com.grvexample.data.model.SingleChoiceQuestion" />
<variable
name="actionItemListener"
private val ioScope = CoroutineScope(Dispatchers.IO + Job() )
fun fireAndForgetNetworkCall() {
Log.i(logTag, "-----Async network calls without error handling-----")
ioScope.launch {
val job = ArrayList<Job>()
Log.i(logTag, "Making 10 asynchronous network calls")
for (i in 0..10) {
job.add(launch {
Log.i(logTag, "Network Call ID: $i")
fun tryExceptionHandler() {
val deferredList = ArrayList<Deferred<*>>()
val errorHandler = CoroutineExceptionHandler { _, error ->
when (error) {
is CustomException -> {
Log.i(logTag, "Network call ${error.errorDescription} failed. Canceling all pending jobs")
Log.i(logTag, "Update UI with Error handling")
}
else -> Log.i(logTag, "Exception ${error.localizedMessage}")
}