Created
October 21, 2018 10:26
-
-
Save Abhityagi16/8e4303253c5048c2074bf33aa2dd7e9f 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
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