Пример #1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LocaleUtil.initLocale(this);

    boolean shouldRefresh = getIntent().getBooleanExtra(EXTRA_SHOULD_REFRESH, false);

    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    DataBindingUtil.bind(binding.navView.getHeaderView(0));

    MainApplication.getComponent(this).inject(this);

    subscription.add(
        brokerProvider
            .get()
            .observe()
            .subscribe(
                page -> {
                  toggleToolbarElevation(page.shouldToggleToolbar());
                  changePage(page.getTitleResId(), page.createFragment());
                  binding.navView.setCheckedItem(page.getMenuId());
                }));

    initView();
    AppUtil.setTaskDescription(
        this, getString(R.string.all_sessions), AppUtil.getThemeColorPrimary(this));

    if (savedInstanceState == null) {
      replaceFragment(SessionsFragment.newInstance(shouldRefresh));
    } else {
      binding.toolbar.setTitle(savedInstanceState.getString(EXTRA_TITLE));
    }
    getSupportFragmentManager().addOnBackStackChangedListener(this);
  }
 private Subscription fetchAndSave() {
   return client
       .getSessions(LocaleUtil.getCurrentLanguageId(getActivity()))
       .subscribeOn(Schedulers.io())
       .observeOn(AndroidSchedulers.mainThread())
       .subscribe(
           dao::updateAllAsync, throwable -> Log.e(TAG, "Failed to fetchAndSave.", throwable));
 }
 protected Subscription loadData() {
   Observable<List<Session>> cachedSessions = dao.findAll();
   return cachedSessions
       .flatMap(
           sessions -> {
             if (shouldRefresh || sessions.isEmpty()) {
               return client
                   .getSessions(LocaleUtil.getCurrentLanguageId(getActivity()))
                   .doOnNext(dao::updateAllAsync);
             } else {
               return Observable.just(sessions);
             }
           })
       .subscribeOn(Schedulers.io())
       .observeOn(AndroidSchedulers.mainThread())
       .subscribe(this::onLoadDataSuccess, this::onLoadDataFailure);
 }