Android Open Source - rxloader Base Rx Loader






From Project

Back to project page rxloader.

License

The source code is released under:

Apache License

If you think the Android project rxloader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.tatarka.rxloader;
//from  w ww . j a  v  a2 s.  c o m
import java.lang.ref.WeakReference;

import rx.Observable;
import rx.Scheduler;
import rx.android.schedulers.AndroidSchedulers;

abstract class BaseRxLoader<T> {
    private RxLoaderBackend manager;
    private String tag;
    private RxLoaderObserver<T> observer;
    private Scheduler scheduler;
    private SaveCallback<T> saveCallback;

    BaseRxLoader(RxLoaderBackend manager, String tag, RxLoaderObserver<T> observer) {
        scheduler = AndroidSchedulers.mainThread();
        this.manager = manager;
        this.tag = tag;
        this.observer = observer;

        CachingWeakRefSubscriber<T> subscription = manager.get(tag);
        if (subscription != null) {
            subscription.set(observer);
        }
    }

    protected BaseRxLoader<T> start(Observable<T> observable) {
        CachingWeakRefSubscriber<T> subscriber = manager.get(tag);
        if (subscriber == null) {
            manager.put(tag, this, createSubscriber(observable));
        }
        return this;
    }

    protected BaseRxLoader<T> restart(Observable<T> observable) {
        CachingWeakRefSubscriber<T> subscriber = manager.get(tag);
        if (subscriber != null) {
            subscriber.unsubscribe();
        }
        manager.put(tag, this, createSubscriber(observable));
        if (saveCallback != null) {
            manager.setSave(tag, observer, new WeakReference<SaveCallback<T>>(saveCallback));
        }
        return this;
    }

    protected BaseRxLoader<T> save(SaveCallback<T> saveCallback) {
        this.saveCallback = saveCallback;
        manager.setSave(tag, observer, new WeakReference<SaveCallback<T>>(saveCallback));
        return this;
    }

    protected BaseRxLoader<T> save() {
        return save(new ParcelableSaveCallback<T>());
    }

    private CachingWeakRefSubscriber<T> createSubscriber(Observable<T> observable) {
        CachingWeakRefSubscriber<T> subscriber = new CachingWeakRefSubscriber<T>(observer);
        subscriber.setSubscription(observable.observeOn(scheduler).subscribe(subscriber));
        return subscriber;
    }

    /**
     * Cancels the task.
     *
     * @return true if the task was started, false otherwise
     */
    public boolean unsubscribe() {
        CachingWeakRefSubscriber<T> subscriber = manager.get(tag);
        if (subscriber != null) {
            subscriber.unsubscribe();
            return true;
        }
        return false;
    }

    /**
     * Clears the loader's state. After a configuration change you will no longer received cached
     * values and {@link #start(rx.Observable)} will cause the observable to be executed again.
     * This is useful if the loader is handling transient state (showing a Toast for example).
     */
    public void clear() {
        CachingWeakRefSubscriber<T> subscriber = manager.get(tag);
        if (subscriber != null) {
            subscriber.clear();
        }
    }
}




Java Source Code List

me.tatarka.rxloader.BaseRxLoader.java
me.tatarka.rxloader.CachingWeakRefSubscriber.java
me.tatarka.rxloader.ParcelableSaveCallback.java
me.tatarka.rxloader.RxLoader1.java
me.tatarka.rxloader.RxLoader2.java
me.tatarka.rxloader.RxLoaderBackendFragmentCompat.java
me.tatarka.rxloader.RxLoaderBackendFragmentHelper.java
me.tatarka.rxloader.RxLoaderBackendFragment.java
me.tatarka.rxloader.RxLoaderBackendNestedFragmentCompat.java
me.tatarka.rxloader.RxLoaderBackendNestedFragment.java
me.tatarka.rxloader.RxLoaderBackend.java
me.tatarka.rxloader.RxLoaderManagerCompat.java
me.tatarka.rxloader.RxLoaderManager.java
me.tatarka.rxloader.RxLoaderObserver.java
me.tatarka.rxloader.RxLoader.java
me.tatarka.rxloader.SaveCallback.java
me.tatarka.rxloader.sample.MainActivityWithFragment.java
me.tatarka.rxloader.sample.MainActivityWithViewPager.java
me.tatarka.rxloader.sample.MainActivity.java
me.tatarka.rxloader.sample.MainFragment.java
me.tatarka.rxloader.sample.SampleObservables.java