Created
December 27, 2017 09:36
-
-
Save cuber5566/c0983805c5af75cdceaf9a759949258b to your computer and use it in GitHub Desktop.
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 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