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
val fetchDetailsModule = module { | |
viewModel { FetchDetailsViewModel(get(), get(), get()) } | |
single { ResourceProvider(androidApplication()) } | |
single { CoroutinesManager() } | |
} |
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 FetchDetailsViewModel( | |
private val resourceProvider: ResourceProvider, | |
private val coroutinesManager: CoroutinesManager, | |
private val fetchDetailsRepo: FetchDetailsRepo | |
) : ViewModel() { | |
companion object { | |
private const val logTag = "SkeletonRepository" | |
} |
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 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) |
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
interface ViewType<out T> { | |
@LayoutRes | |
fun layoutId(): Int | |
fun data(): T | |
fun isUserInteractionEnabled() = true | |
} |
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
data class SingleChoiceViewType(private val model: SingleChoiceQuestion) : ViewType<SingleChoiceQuestion> { | |
override fun layoutId(): Int = R.layout.layout_single_choice | |
override fun data(): SingleChoiceQuestion = model | |
} |
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
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( |
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 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) |
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
<?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" |
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
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") |
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
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}") | |
} |
OlderNewer