Permalink
75 lines (68 sloc) 2.5 KB
/*
* Copyright (C) 2017 The Android 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
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.example.github.ui.repo
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.android.example.github.repository.RepoRepository
import com.android.example.github.testing.OpenForTesting
import com.android.example.github.util.AbsentLiveData
import com.android.example.github.vo.Contributor
import com.android.example.github.vo.Repo
import com.android.example.github.vo.Resource
import javax.inject.Inject
@OpenForTesting
class RepoViewModel @Inject constructor(repository: RepoRepository) : ViewModel() {
private val _repoId: MutableLiveData<RepoId> = MutableLiveData()
val repoId: LiveData<RepoId>
get() = _repoId
val repo: LiveData<Resource<Repo>> = Transformations
.switchMap(_repoId) { input ->
input.ifExists { owner, name ->
repository.loadRepo(owner, name)
}
}
val contributors: LiveData<Resource<List<Contributor>>> = Transformations
.switchMap(_repoId) { input ->
input.ifExists { owner, name ->
repository.loadContributors(owner, name)
}
}
fun retry() {
val owner = _repoId.value?.owner
val name = _repoId.value?.name
if (owner != null && name != null) {
_repoId.value = RepoId(owner, name)
}
}
fun setId(owner: String, name: String) {
val update = RepoId(owner, name)
if (_repoId.value == update) {
return
}
_repoId.value = update
}
data class RepoId(val owner: String, val name: String) {
fun <T> ifExists(f: (String, String) -> LiveData<T>): LiveData<T> {
return if (owner.isBlank() || name.isBlank()) {
AbsentLiveData.create()
} else {
f(owner, name)
}
}
}
}