Created
March 18, 2020 09:51
-
-
Save stoichoandreev/5a9f0a39afd2ea141c8662b292a97843 to your computer and use it in GitHub Desktop.
Gist 4 - Android Example
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
data class Ad(val id: String) |
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
object SearchAdsApi { | |
fun searchAds(search: String): Observable<List<Ad>> { | |
return Observable.just(listOf(Ad("1"), Ad("2"))) | |
} | |
} |
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 SearchAdsFragment : Fragment(R.layout.fragment_search_ads), SearchAdsPresenter.SearchAdsView, | |
TextWatcher { | |
private val presenter = SearchAdsPresenter(this) | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
searchAdsEditText.addTextChangedListener(this) | |
} | |
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { | |
presenter.onSearchChanges(s.toString()) | |
} | |
override fun afterTextChanged(s: Editable?) { | |
// nothing | |
} | |
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { | |
// nothing | |
} | |
override fun setProgressVisibility(visible: Int) { | |
searchAdsProgressBar.visibility = visible | |
} | |
override fun showAds(ads: List<Ad>) { | |
searchAdsResultTextView.text = ads.toString() | |
} | |
} |
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 SearchAdsPresenter(private val view: SearchAdsView) { | |
private val api = SearchAdsApi | |
private val search: BehaviorSubject<String> = BehaviorSubject.create() | |
init { | |
search | |
.subscribeOn(AndroidSchedulers.mainThread()) | |
.doOnNext { view.setProgressVisibility(View.VISIBLE) } | |
.flatMap { searchAds(it) } | |
.subscribe { | |
view.setProgressVisibility(View.GONE) | |
view.showAds(it) | |
} | |
} | |
fun onSearchChanges(text: String) { | |
search.onNext(text) | |
} | |
private fun searchAds(search: String): Observable<List<Ad>> { | |
return api.searchAds(search) | |
.subscribeOn(Schedulers.io()) | |
.doOnError { view.setProgressVisibility(View.GONE) } | |
} | |
interface SearchAdsView { | |
fun setProgressVisibility(visible: Int) | |
fun showAds(ads: List<Ad>) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment