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 <K, V> lazyMap(initializer: (K) -> V): Map<K, V> { | |
val map = mutableMapOf<K, V>() | |
return map.withDefault { key -> | |
val newValue = initializer(key) | |
map[key] = newValue | |
return@withDefault newValue | |
} | |
} |
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 ContactsViewModel(val getContactsUseCase: GetContactsUseCase) : ViewModel() { | |
private val contactsLiveData: Map<Parameters, LiveData<Contacts>> = lazyMap { parameters -> | |
val liveData = MutableLiveData<Contacts>() | |
getContactsUseCase.loadContacts(parameters) { liveData.value = it } | |
return@lazyMap liveData | |
} | |
fun contacts(parameters: Parameters): LiveData<Contacts> = contactsLiveData.getValue(parameters) | |
} |
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 ContactsViewModel(val getContactsUseCase: GetContactsUseCase) : ViewModel() { | |
private val contactsLiveData = MutableLiveData<Contacts>() | |
fun loadContacts(parameters: Parameters) { | |
getContactsUseCase.loadContacts(parameters) { contactsLiveData.value = it } | |
} | |
fun contacts(): LiveData<Contacts> = contactsLiveData | |
} |
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 GetContactsUseCase { | |
fun loadContacts(parameters: Parameters): Flowable<Contacts> { /* Implementation detail */ } | |
} | |
class ContactsViewModel(val getContactsUseCase: GetContactsUseCase) : ViewModel() { | |
fun contacts(parameters: Parameters): LiveData<Contacts> { | |
return getContactsUseCase.loadContacts(parameters).toLiveData() | |
} | |
} |
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 ContactsViewModel(val getContactsUseCase: GetContactsUseCase) : ViewModel() { | |
private val contactsLiveData by lazy { | |
val liveData = MutableLiveData<Contacts>() | |
getContactsUseCase.loadContacts(Parameters()) { liveData.value = it } | |
return@lazy liveData | |
} | |
fun contacts(): LiveData<Contacts> = contactsLiveData | |
} |
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 ContactsViewModel(val getContactsUseCase: GetContactsUseCase) : ViewModel() { | |
private val contactsLiveData = MutableLiveData<Contacts>() | |
init { | |
getContactsUseCase.loadContacts(Parameters()) { contactsLiveData.value = it } | |
} | |
fun contacts(): LiveData<Contacts> = contactsLiveData | |
} |
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 Contacts(val names: List<String>) | |
data class Parameters(val namePrefix: String = "") | |
class GetContactsUseCase { | |
fun loadContacts(parameters: Parameters, onLoad: (Contacts) -> Unit) { /* Implementation detail */ } | |
} | |
class ContactsViewModel(val getContactsUseCase: GetContactsUseCase) : ViewModel() { | |
// TODO When to call getContactsUseCase.loadContacts? |
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
@Test | |
fun writePostAndReadInList() { | |
val testObserver = postDao.getAll().test() | |
testObserver.assertValue { it.isEmpty() } | |
val post = TestUtil.createPost(id = 42) | |
postDao.insert(post) | |
testObserver.assertValue { it.size == 1 } | |
} |
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 ViewModelFactory @Inject constructor(val providersMap: Map<Class<*>, Provider<ViewModel>>) | |
: ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(aClass: Class<T>): T { | |
val provider = providersMap[aClass] ?: throw IllegalArgumentException("No $aClass provider") | |
return provider.get() as T | |
} | |
} |