Created
May 19, 2016 14:14
-
-
Save thedumbtechguy/c5c1dbaa11ce3b0c8329d529ed01ae95 to your computer and use it in GitHub Desktop.
A Folio Page that supports using Glide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.annotation.StringRes; | |
import android.support.v7.widget.Toolbar; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import com.bumptech.glide.RequestManager; | |
import com.bumptech.glide.manager.Lifecycle; | |
import com.bumptech.glide.manager.LifecycleListener; | |
import com.bumptech.glide.manager.RequestManagerTreeNode; | |
import com.bumptech.glide.util.Util; | |
import com.umaplay.android.App; | |
import com.umaplay.folio.Page; | |
import java.util.Collections; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.WeakHashMap; | |
public abstract class FolioGlidePage extends Page implements Lifecycle { | |
private final Set<LifecycleListener> mGlideLifecycleListeners = | |
Collections.newSetFromMap(new WeakHashMap<LifecycleListener, Boolean>()); | |
private RequestManager mGlideInstance; | |
private boolean isStarted; | |
private boolean isDestroyed; | |
public RequestManager getGlide() { | |
if(mGlideInstance == null) { | |
mGlideInstance = new RequestManager(getContext(), this, new RequestManagerTreeNode() { | |
public Set<RequestManager> descendants = new HashSet<>(); | |
@Override | |
public Set<RequestManager> getDescendants() { | |
return descendants; | |
} | |
}); | |
} | |
return mGlideInstance; | |
} | |
/** | |
* Adds the given listener to the list of listeners to be notified on each lifecycle event. | |
* | |
* <p> The latest lifecycle event will be called on the given listener synchronously in this | |
* method. If the activity or fragment is stopped, {@link LifecycleListener#onStop()}} will be | |
* called, and same for onStart and onDestroy. </p> | |
* | |
* <p> Note - {@link com.bumptech.glide.manager.LifecycleListener}s that are added more than once | |
* will have their lifecycle methods called more than once. It is the caller's responsibility to | |
* avoid adding listeners multiple times. </p> | |
*/ | |
@Override | |
public void addListener(LifecycleListener listener) { | |
mGlideLifecycleListeners.add(listener); | |
if (isDestroyed) { | |
listener.onDestroy(); | |
} else if (isStarted) { | |
listener.onStart(); | |
} else { | |
listener.onStop(); | |
} | |
} | |
@Override | |
public void onPageIsVisible() { | |
super.onPageIsVisible(); | |
isStarted = true; | |
for (LifecycleListener lifecycleListener : Util.getSnapshot(mGlideLifecycleListeners)) { | |
lifecycleListener.onStart(); | |
} | |
} | |
@Override | |
public void onPageIsInvisible() { | |
super.onPageIsInvisible(); | |
isStarted = false; | |
for (LifecycleListener lifecycleListener : Util.getSnapshot(mGlideLifecycleListeners)) { | |
lifecycleListener.onStop(); | |
} | |
} | |
@Override | |
public void onDestroy() { | |
isDestroyed = true; | |
for (LifecycleListener lifecycleListener : Util.getSnapshot(mGlideLifecycleListeners)) { | |
lifecycleListener.onDestroy(); | |
} | |
mGlideInstance = null; | |
super.onDestroy(); | |
App.getRefwatcher().watch(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment