Skip to content

Instantly share code, notes, and snippets.

@cuber5566
Created December 27, 2017 09:36
Show Gist options
  • Save cuber5566/c0983805c5af75cdceaf9a759949258b to your computer and use it in GitHub Desktop.
Save cuber5566/c0983805c5af75cdceaf9a759949258b to your computer and use it in GitHub Desktop.
class PostDetailViewModel(
application: Application,
private var schedulerProvider: AppScheduler,
private var postRepository: PostSource,
private var commentRepository: CommentRepository
) : AndroidViewModel(application) {
companion object {
private val DELAY_LIKE_EVENT = 2_000L
}
private val compositeDisposable: CompositeDisposable = CompositeDisposable()
val post = MutableLiveData<Post>()
val postError = SingleLiveEvent<Throwable>()
val commentList = MutableLiveData<List<Comment>>()
val commentListError = SingleLiveEvent<Throwable>()
fun updatePost(postId: Long) {
compositeDisposable.add(postRepository
.getPostById(postId)
.observeOn(schedulerProvider.ui())
.subscribe({ post.value = it }, { postError.value = it }))
}
fun updateComment(postId: Long) {
compositeDisposable.add(commentRepository
.getCommentListByPostId(postId)
.observeOn(schedulerProvider.ui())
.subscribe({ commentList.value = it }, { commentListError.value = it }))
}
override fun onCleared() {
compositeDisposable.clear()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment