Beispiel #1
0
 public Subscription registerUserAccountUpdate(Action1<UserAccountData> user) {
   return userAccountDataBehaviorSubject.subscribe(user);
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final MainPresenter presenter = getRetentionFragment(savedInstanceState).presenter();

    setContentView(R.layout.main_activity);
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_activity_recycler_view);
    final LinearLayoutManager layout =
        new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layout);
    final MainAdapter adapter = new MainAdapter();
    recyclerView.setAdapter(adapter);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    final BehaviorSubject<Boolean> isLastSubject = BehaviorSubject.create(true);
    recyclerView.setOnScrollListener(
        new RecyclerView.OnScrollListener() {
          boolean manualScrolling = false;

          @Override
          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
              manualScrolling = true;
            }
            if (manualScrolling && newState == RecyclerView.SCROLL_STATE_IDLE) {
              manualScrolling = false;

              final int lastVisibleItemPosition = layout.findLastVisibleItemPosition();
              final int previousItemsCount = adapter.getItemCount();
              final boolean isLast = previousItemsCount - 1 == lastVisibleItemPosition;
              isLastSubject.onNext(isLast);
            }
          }
        });

    subs =
        new CompositeSubscription(
            isLastSubject.subscribe(presenter.lastItemInViewObserver()),
            presenter
                .itemsWithScrollObservable()
                .subscribe(
                    new Action1<MainPresenter.ItemsWithScroll>() {
                      @Override
                      public void call(final MainPresenter.ItemsWithScroll itemsWithScroll) {
                        adapter.call(itemsWithScroll.items());
                        if (itemsWithScroll.shouldScroll()) {
                          recyclerView.post(
                              new Runnable() {
                                @Override
                                public void run() {
                                  recyclerView.smoothScrollToPosition(
                                      itemsWithScroll.scrollToPosition());
                                }
                              });
                        }
                      }
                    }),
            presenter
                .connectButtonEnabledObservable()
                .subscribe(ViewActions.setEnabled(findViewById(R.id.main_activity_connect_button))),
            presenter
                .disconnectButtonEnabledObservable()
                .subscribe(
                    ViewActions.setEnabled(findViewById(R.id.macin_activity_disconnect_button))),
            presenter
                .sendButtonEnabledObservable()
                .subscribe(ViewActions.setEnabled(findViewById(R.id.main_activity_send_button))),
            ViewObservable.clicks(findViewById(R.id.main_activity_connect_button))
                .subscribe(presenter.connectClickObserver()),
            ViewObservable.clicks(findViewById(R.id.macin_activity_disconnect_button))
                .subscribe(presenter.disconnectClickObserver()),
            ViewObservable.clicks(findViewById(R.id.main_activity_send_button))
                .subscribe(presenter.sendClickObserver()));
  }