-
-
Save cloudbank/1a68649c110bde6dc69441dd87a61dab to your computer and use it in GitHub Desktop.
LiveData meets RxJava
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
compile "android.arch.lifecycle:reactivestreams:$archVersion" |
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 PostRepo(private val api: Api) { | |
private sealed class Command { | |
class Reset(val size: Int) : Command() | |
class LoadMore(val position: Int, val size: Int) : Command() | |
} | |
private var itemIds = emptyList<Long>() | |
private val commands = MutableLiveData<Command>() | |
val posts: LiveData<List<Item>> = commands.switchMap { | |
... | |
} | |
fun load(size: Int) { | |
itemIds = emptyList() | |
commands.value = Command.Reset(size) | |
} | |
fun loadMore(size: Int) { | |
commands.value = Command.LoadMore(posts.value?.size ?: 0, size) | |
} | |
} |
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 <T, R> LiveData<T>.switchMap(f: (T) -> LiveData<R>): LiveData<R> = Transformations.switchMap(this, f) |
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 posts: LiveData<List<Item>> = commands.switchMap { | |
val command = it | |
LiveDataReactiveStreams.fromPublisher<List<Item>> { | |
when (command) { | |
is Command.Reset -> { | |
getFirst(command.size) | |
} | |
is PostRepo.Command.LoadMore -> { | |
getMore(command.position, command.size) | |
} | |
} | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.toFlowable() | |
.subscribe(it) | |
} | |
} |
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 MainViewModel(app: Application) : AndroidViewModel(app) { | |
private val postRepo = getApplication<App>().postRepo | |
private val LOAD_ITEM_COUNT = 15 | |
fun getPosts(): LiveData<List<Post>> { | |
return postRepo.posts.map { | |
it.map { Post(it.id, it.title, it.url) } | |
} | |
} | |
fun refresh() { | |
postRepo.load(LOAD_ITEM_COUNT) | |
} | |
fun loadMore() { | |
postRepo.loadMore(LOAD_ITEM_COUNT) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment