Skip to content

Instantly share code, notes, and snippets.

class SearchShows @Inject
constructor(private val showRepository: ShowRepository, private val resourceRepository: ResourceRepository) :
UseCase<Show>() {
var id: String? = null
override suspend fun executeOnBackground(): Show {
id?.let {
val showDetail = background{
showRepository.showDetail(it)
class SearchShows @Inject
constructor(private val showRepository: ShowRepository, private val resourceRepository: ResourceRepository) :
UseCase<String>() {
var query: String? = null
override suspend fun executeOnBackground(): String {
query?.let {
val showsInfo = showRepository.searchShow(it)
val showName: String? = showsInfo?.getOrNull(0)?.show?.title
/**
* (C) Copyright 2018 Paulo Vitor Sato Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
class QueryViewModelArc @Inject
constructor(private val searchShows: SearchShows?) : ViewModel() {
val result = MutableLiveData<String>()
val query = MutableLiveData<String>()
val showLoading = MutableLiveData<Boolean>()
val searchEnabled = MutableLiveData<Boolean>()
class QueryFragment : BaseFragment() {
private var binding: FragmentQueryMvvmBinding? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_query_mvvm, container, false)
binding = DataBindingUtil.bind(view)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
class QueryViewModelArc @Inject
constructor(private val mSearchShows: SearchShows?) : ViewModel() {
val query = MutableLiveData<String>()
val searchEnabled = MutableLiveData<Boolean>()
init {
searchEnabled.value = false
query.value = ""
@Module(subcomponents = arrayOf(ViewModelSubComponent::class))
class ApplicationModule() {
@Singleton
@Provides
internal fun provideViewModelFactory(
viewModelSubComponent: ViewModelSubComponent.Builder): ProjectViewModelFactory {
return ProjectViewModelFactory(viewModelSubComponent.build())
}
@Singleton
@Component(modules = arrayOf(ApplicationModule::class, NetworkModule::class, ShowRepositoryModule::class, ResourceRepositoryModule::class))
interface ApplicationComponent {
// expose to sub graphs
fun showRepository(): ShowRepository
fun resourceRepository(): ResourceRepository
fun viewModelFactory(): ProjectViewModelFactory
@Subcomponent
interface ViewModelSubComponent {
@Subcomponent.Builder
interface Builder {
fun build(): ViewModelSubComponent
}
fun homeFragmentViewModel(): HomeFragmentViewModel
fun queryViewModelArc(): QueryViewModelArc
}
class ProjectViewModelFactory(viewModelSubComponent: ViewModelSubComponent) : ViewModelProvider.Factory {
private val creators: ArrayMap<Class<*>,() ->ViewModel> = ArrayMap()
init {
creators[HomeFragmentViewModel::class.java] = { viewModelSubComponent.homeFragmentViewModel() }
creators[QueryViewModelArc::class.java] = { viewModelSubComponent.queryViewModelArc() }
}
override fun <T : ViewModel> create(modelClass: Class<T>): T {
var creator: (() ->ViewModel)? = creators[modelClass]