我有一个简单的应用程序SearchView
,用户可以查询并根据我显示/隐藏一些视图小部件的状态。我很难找出为什么ProgressBar
永远不可见。有人能说为什么加载指示器永远不会出现吗?
这是代码:
trait MainView {
def searchIntent(): Observable[String]
def render(viewState: PartialMainViewState)
}
主持人:
class MainPresenter(...) {
def bind(mainView: MainView) = {
val searchQueryObservable = mainView.searchIntent()
.flatMap(s => mainInteractor.search(s))
searchQueryObservable.observeOn(AndroidSchedulers.mainThread()).subscribe { state => mainView.render(state) }
}
}
带有实际渲染的 Activity:
class SimpleActivity extends BaseSearchActivity with MainView {
override def searchIntent(): Observable[String] = {
Observable.defer {
RxSearchView.queryTextChanges(vh.searchView)
.filter((ev: CharSequence) => ev.length >= 2 || ev.length == 0)
.debounce(500.milliseconds)
.map[String]((ev: CharSequence) => ev.toString())
}
}
override def render(viewState: PartialMainViewState): Unit =
viewState match {
case SearchNotStartedYet => renderSearchNotStarted()
case Loading => renderLoading()
case EmptyResult(_) => renderEmptyResult()
case SearchResult(_, res) => renderResult(res)
...
}
def renderSearchNotStarted(): Unit = {
vh.list.setVisibility(View.GONE)
vh.progressBar.setVisibility(View.GONE)
vh.emptyView.setVisibility(View.GONE)
}
def renderLoading(): Unit = {
vh.list.setVisibility(View.GONE)
vh.progressBar.setVisibility(View.VISIBLE)
vh.emptyView.setVisibility(View.GONE)
}
def renderEmptyResult(): Unit = {
vh.list.setVisibility(View.GONE)
vh.progressBar.setVisibility(View.GONE)
vh.emptyView.setVisibility(View.VISIBLE)
}
def renderResult(result: Array[String]): Unit = {
vh.list.setVisibility(View.VISIBLE)
vh.progressBar.setVisibility(View.GONE)
vh.emptyView.setVisibility(View.GONE)
//setting the adapter etc
}
}
这里我模拟一个网络请求:
class SimpleInteractor(...) {
def search(query: String): Observable[PartialMainViewState] = {
Thread.sleep(1500) // to simulate "Network latency"
if(query.isEmpty) Observable.just(SearchNotStartedYet)
else Observable.just(persons.filter(_.toLowerCase().contains(query.toLowerCase())))
.map(array => {
if(array.isEmpty) EmptyResult(query) else SearchResult(query, array)
}).+:(Loading).onErrorReturn(error => ErrorState(query, error))
}
}