Skip to content

Instantly share code, notes, and snippets.

@ericntd
Created April 6, 2023 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericntd/8ba8039cd9bf935210f82a09d284c983 to your computer and use it in GitHub Desktop.
Save ericntd/8ba8039cd9bf935210f82a09d284c983 to your computer and use it in GitHub Desktop.
Faster app start scop 3 - Lazy discover

DiscoverFeedMainInteractor

@Override
  protected void didBecomeActive() {
    super.didBecomeActive();
    discoverBagelRefreshStream = BehaviorSubject.create();
    discoverManager.addGiveTakeItemEventListener(this);
    /*
    If something goes wrong witht the download of GiveTakes & RisingGiveTakes,
    discoverMatchRepository.getGiveTakesDownloaded() will never return
    User will be stuck seeing blank or infinite loading screen
    That's why timeout is important here
     */
    AtomicBoolean downloadCompleted = new AtomicBoolean();
    Disposable disposable = discoverMatchRepository.getGiveTakesDownloaded()
            .doOnTerminate(() -> downloadCompleted.set(true))
            .as(AutoDispose.autoDisposable(this))
            .subscribe(unit -> {
              Logger.d(TAG, "GiveTakes & RisingTakes downloaded and saved in local cache, now read from local cache and render");
              discoverManager.refreshGiveTakes();
            });
    /*
    Is there a simpler way to implement time limit with RxJava?
    Probably, need to dig, I don't know of top of my head
    This is probably where Coroutines can be simpler
     */
    Single.timer(30, TimeUnit.SECONDS)
            .as(AutoDispose.autoDisposable(this))
            .subscribe(aLong -> {
              if (!downloadCompleted.get()) {
                final String message = "GiveTakes & RisingGiveTakes download did not completed within reasonable time";
                Logger.e(TAG, message, new TimeoutException(message));
                disposable.dispose();
                getRouter().routeToDiscoverFeedEmpty();
              }
            });

    mainActivity.hideTabLayout();

    observeUpsellConditions();
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment