如何创建一个Rx Search视图,该视图将使用查询过滤回收器视图

时间:2016-04-02 16:13:15

标签: android rx-java

我想实现一个Rx Search视图,它将使用字符串过滤回收器视图,并检查本地Realm数据库并进行改进请求,然后将所有结果组合而不重复?所以,换句话说:我想在搜索视图中进行文本更改事件,使用输入字符串进行网络请求,使用数据库查询并结合不带重复的结果

这是我的初始代码:

RxSearchView.queryTextChanges(searchView)
            .filter(charSequence -> !TextUtils.isEmpty(charSequence))
            .throttleLast(100, TimeUnit.MILLISECONDS)
            .debounce(200, TimeUnit.MILLISECONDS)
            .onBackpressureLatest()
            .flatMap(charSequence -> userListPresenter.search(charSequence))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .onErrorResumeNext(Observable.empty())
            .subscribe((Action1) response -> userListPresenter.showSearchResult((List<UserModel>) response));

userListPresenter.search(charSequence))应该返回一个没有重复的连接响应的observable,谢谢:)

userListPresenter.search(CharSequence中)):

Observable.create(subscriber -> {
        if (Utils.isNetworkAvailable(getContext())) {
            if (!subscriber.isUnsubscribed()) {
                subscriber.onNext(restApi.search(query));
                subscriber.onCompleted();
            }
        }).mergeWith(
                    Observable.create(subscriber -> {
        if (!subscriber.isUnsubscribed()) {
            subscriber.onNext(realmManager.getWhere(query));
            subscriber.onCompleted();
        }
    })).collect(HashSet::new, HashSet::add)
            .flatMap(Observable::from);

1 个答案:

答案 0 :(得分:3)

如果我正确理解你的问题。结合多个Observable的排放有很好的方法,请看zip method。 所以,你的搜索方法看起来像这样:

library(shiny)
library(shinydashboard)

ui <- fluidPage(
         titlePanel("censusVis"),

sidebarLayout(
sidebarPanel(
  helpText("Create demographic maps with 
           information from the 2010 US Census."),
  selectInput("var", 
              label = "Choose a variable to display",
              choices = list("Percent White" = 1,
                             "Percent Black" = 2,
                             "Percent Hispanic" = 3),
              selected = 1
  ),
  conditionalPanel(condition = "input.var == 1",
                   sliderInput("range1", label = "Range of interest:", min = 0, 
                               max = 10, value = c(0, 10))
  ),
  conditionalPanel(condition = "input.var == 2",
                   sliderInput("range2", label = "Range of interest:", min = 0, 
                               max = 20, value = c(0, 20))
  ),
  conditionalPanel(condition = "input.var == 3",
                   sliderInput("range3", label = "Range of interest:", min = 0, 
                               max = 30, value = c(0, 30))
  )
),
mainPanel(
  textOutput("text1"),
  textOutput("text2")
)))

server <- function(input, output) {

   someName <- reactive({
                   switch(input$var,
                          "1" = input$range1,
                          "2" = input$range2,
                          "3" = input$range3)
                        })

output$text1 <- renderText({ 
paste("You have selected", input$var)
})

output$text2 <- renderText({ 
paste("You have chosen a range that goes from",
      someName()[1], "to", someName()[2])
})


}

shinyApp(ui, server)