Skip to content

Instantly share code, notes, and snippets.

@Abhityagi16
Created October 21, 2018 10:26
Show Gist options
  • Save Abhityagi16/8e4303253c5048c2074bf33aa2dd7e9f to your computer and use it in GitHub Desktop.
Save Abhityagi16/8e4303253c5048c2074bf33aa2dd7e9f to your computer and use it in GitHub Desktop.
import clean.architecture.example.UseCase
import clean.architecture.example.data.model.Post
import clean.architecture.example.data.source.PostDataRepository
import clean.architecture.example.data.source.PostDataSource
class GetPosts(private val mDataRepository: PostDataRepository) : UseCase<GetPosts.RequestValues, GetPosts.ResponseValue>() {
protected override fun executeUseCase(requestValues: GetPosts.RequestValues?) {
mDataRepository.getPosts(requestValues?.userId ?: -1, object : PostDataSource.LoadPostsCallback {
override fun onPostsLoaded(posts: List<Post>) {
val responseValue = ResponseValue(posts)
useCaseCallback?.onSuccess(responseValue)
}
override fun onError(t: Throwable) {
// Never use generic exceptions. Create proper exceptions. Since our usecase is different we will go with generic throwable
useCaseCallback?.onError(Throwable("Data not found"))
}
})
}
class RequestValues(val userId: Int) : UseCase.RequestValues
class ResponseValue(val posts: List<Post>) : UseCase.ResponseValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment