Skip to content

Commit 0f50bd6

Browse files
author
Sothearith Sreang
committed
Implements MainPresenter to interact with both data source and MainActivity
1 parent b6265b1 commit 0f50bd6

File tree

1 file changed

+76
-0
lines changed
  • GitHub-Search/app/src/main/java/thearith/github/com/github_search/presentation/presenter/search

1 file changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package thearith.github.com.github_search.presentation.presenter.search
2+
3+
import android.text.TextUtils
4+
import io.reactivex.Observable
5+
import thearith.github.com.github_search.data.search.repository.GitHubSearchRepository
6+
import thearith.github.com.github_search.presentation.executor.PostExecutionThread
7+
import thearith.github.com.github_search.presentation.executor.ThreadExecutor
8+
import thearith.github.com.github_search.presentation.presenter.base.BasePresenter
9+
import thearith.github.com.github_search.view.model.SearchFeedResponse
10+
import thearith.github.com.github_search.view.model.Status
11+
import thearith.github.com.github_search.view.activities.search.MainContract
12+
import javax.inject.Inject
13+
14+
/**
15+
* Created by Thearith on 10/31/17.
16+
*/
17+
class MainPresenter : BasePresenter, MainContract.Presenter {
18+
19+
private val mGitHubSearchRepository : GitHubSearchRepository
20+
21+
private var mPageNumber : Int = 1
22+
23+
@Inject
24+
constructor(threadExecutor: ThreadExecutor,
25+
postExecutionThread: PostExecutionThread,
26+
gitHubSearchRepository: GitHubSearchRepository) : super(threadExecutor, postExecutionThread) {
27+
mGitHubSearchRepository = gitHubSearchRepository
28+
}
29+
30+
31+
/**
32+
* UI states
33+
* */
34+
35+
private fun initializePageNumber() {
36+
mPageNumber = 1
37+
}
38+
39+
private fun incrementPageNumber() {
40+
mPageNumber++
41+
}
42+
43+
44+
override fun loadNewSearch(searchParam: String) : Observable<SearchFeedResponse> {
45+
initializePageNumber()
46+
return loadSearch(searchParam, mPageNumber)
47+
.startWith(SearchFeedResponse(Status.IN_PROGRESS_WITH_REFRESH))
48+
.applySchedulers()
49+
}
50+
51+
override fun loadNextSearch(searchParam: String) : Observable<SearchFeedResponse> {
52+
incrementPageNumber()
53+
return loadSearch(searchParam, mPageNumber)
54+
.startWith(SearchFeedResponse(Status.IN_PROGRESS))
55+
.applySchedulers()
56+
}
57+
58+
private fun loadSearch(searchParam : String, pageNumber : Int) =
59+
if(TextUtils.isEmpty(searchParam))
60+
loadEmptyQuerySearch()
61+
else
62+
loadNonEmptyQuerySearch(searchParam, pageNumber)
63+
64+
private fun loadEmptyQuerySearch() =
65+
Observable.just(SearchFeedResponse(Status.IDLE))
66+
67+
private fun loadNonEmptyQuerySearch(searchParam : String, pageNumber : Int) =
68+
mGitHubSearchRepository.searchGitHubRepo(searchParam, pageNumber)
69+
.map {
70+
val isNotEmpty = it.items?.isNotEmpty()
71+
val mode = if(isNotEmpty == true) Status.COMPLETE else Status.NO_RESULT
72+
73+
SearchFeedResponse(mode, it)
74+
}
75+
.onErrorReturn { SearchFeedResponse(Status.ERROR) }
76+
}

0 commit comments

Comments
 (0)