Skip to content

Instantly share code, notes, and snippets.

@jraska
jraska / RealDeepLinkLauncher.kt
Last active August 23, 2018 20:28
File RealDeepLinkLauncher.kt
class RealDeepLinkLauncher(val topActivityProvider: Provider<Activity>) : DeepLinkLauncher {
override fun launch(deepLink: HttpUrl) {
if (deepLink.pathSize() == 2) {
val fullRepoPath = deepLink.pathSegments()[0] + "/" + deepLink.pathSegments()[1]
RepoDetailActivity.start(topActivityProvider.get(), fullRepoPath)
return
}
if (deepLink.pathSize() == 1) {
val login = deepLink.pathSegments()[0]
@jraska
jraska / DeepLinkNavigator.kt
Last active August 26, 2018 12:04
DeepLinkNavigator.kt
class DeepLinkNavigator(val deepLinkLauncher: DeepLinkLauncher) : Navigator {
override fun startUserDetail(login: String) {
val url = HttpUrl.get("https://github.com/$login")
deepLinkLauncher.launch(url)
}
override fun startRepoDetail(fullPath: String) {
val url = HttpUrl.get("https://github.com/$fullPath")
deepLinkLauncher.launch(url)
}
@jraska
jraska / Navigator.kt
Created August 23, 2018 20:30
Navigator.kt
interface Navigator {
fun startUserDetail(login: String)
fun startRepoDetail(fullPath: String)
}
@jraska
jraska / ViewModelFactory.kt
Last active October 7, 2018 12:51
ViewModelFactory.kt
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
}
}
@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 }
}
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?
class ContactsViewModel(val getContactsUseCase: GetContactsUseCase) : ViewModel() {
private val contactsLiveData = MutableLiveData<Contacts>()
init {
getContactsUseCase.loadContacts(Parameters()) { contactsLiveData.value = it }
}
fun contacts(): LiveData<Contacts> = contactsLiveData
}
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
}
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()
}
}