mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-12-22 14:48:23 -05:00
Permanent implementation of the OG holder pattern
This commit is contained in:
parent
7c483695d3
commit
915467c465
50
app/src/main/java/org/bspeice/minimalbible/OGHolder.java
Normal file
50
app/src/main/java/org/bspeice/minimalbible/OGHolder.java
Normal file
@ -0,0 +1,50 @@
|
||||
package org.bspeice.minimalbible;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import dagger.ObjectGraph;
|
||||
|
||||
/**
|
||||
* Holding location for activity object graphs.
|
||||
* This technique could be extended to other things, but honestly,
|
||||
* everything it could be extended to likely needs to be in
|
||||
* an ObjectGraph anyway.
|
||||
* This works because getSupportFragmentManager() is scoped to each activity.
|
||||
* TODO: Prove the above claim.
|
||||
*/
|
||||
public class OGHolder extends Fragment {
|
||||
private final static String TAG = "OGHolder";
|
||||
|
||||
private ObjectGraph mObjectGraph;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setRetainInstance(true);
|
||||
}
|
||||
|
||||
public void persistGraph(ObjectGraph graph) {
|
||||
mObjectGraph = graph;
|
||||
}
|
||||
|
||||
public ObjectGraph fetchGraph() {
|
||||
return mObjectGraph;
|
||||
}
|
||||
|
||||
public static OGHolder get(FragmentActivity activity) {
|
||||
FragmentManager manager = activity.getSupportFragmentManager();
|
||||
OGHolder holder = (OGHolder) manager.findFragmentByTag(TAG);
|
||||
if (holder == null) {
|
||||
holder = new OGHolder();
|
||||
manager.beginTransaction().add(holder, TAG).commit();
|
||||
}
|
||||
return holder;
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ import android.view.MenuItem;
|
||||
|
||||
import org.bspeice.minimalbible.Injector;
|
||||
import org.bspeice.minimalbible.MinimalBible;
|
||||
import org.bspeice.minimalbible.OGHolder;
|
||||
import org.bspeice.minimalbible.R;
|
||||
import org.bspeice.minimalbible.activity.BaseActivity;
|
||||
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment;
|
||||
@ -26,6 +27,8 @@ public class DownloadActivity extends BaseActivity implements
|
||||
BaseNavigationDrawerFragment.NavigationDrawerCallbacks,
|
||||
Injector {
|
||||
|
||||
private final String TAG = "DownloadActivity";
|
||||
|
||||
/**
|
||||
* Fragment managing the behaviors, interactions and presentation of the
|
||||
* navigation drawer.
|
||||
@ -48,19 +51,15 @@ public class DownloadActivity extends BaseActivity implements
|
||||
*/
|
||||
private void buildObjGraph() {
|
||||
if (daObjectGraph == null) {
|
||||
FragmentManager fM = getSupportFragmentManager();
|
||||
DownloadOGHolder ogHolder = (DownloadOGHolder) fM.findFragmentByTag("OG_HOLDER");
|
||||
if (ogHolder == null) {
|
||||
Log.e("DownloadActivity", "Creating new holder...");
|
||||
ogHolder = new DownloadOGHolder();
|
||||
OGHolder holder = OGHolder.get(this);
|
||||
ObjectGraph holderGraph = holder.fetchGraph();
|
||||
if (holderGraph == null) {
|
||||
Log.i(TAG, "Rebuilding ObjectGraph...");
|
||||
daObjectGraph = MinimalBible.get(this)
|
||||
.plus(new DownloadActivityModules(this));
|
||||
ogHolder.persistObjectGraph(daObjectGraph);
|
||||
fM.beginTransaction().add(ogHolder, "OG_HOLDER").commit();
|
||||
holder.persistGraph(daObjectGraph);
|
||||
} else {
|
||||
Log.e("DownloadActivity", "Found existing holder...");
|
||||
daObjectGraph = ogHolder.fetchObjectGraph();
|
||||
daObjectGraph.inject(this);
|
||||
daObjectGraph = holderGraph;
|
||||
}
|
||||
}
|
||||
daObjectGraph.inject(this);
|
||||
|
@ -1,28 +0,0 @@
|
||||
package org.bspeice.minimalbible.activity.downloader;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
||||
import dagger.ObjectGraph;
|
||||
|
||||
/**
|
||||
* Created by bspeice on 7/21/14.
|
||||
*/
|
||||
public class DownloadOGHolder extends Fragment {
|
||||
|
||||
ObjectGraph holder;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setRetainInstance(true);
|
||||
}
|
||||
|
||||
public void persistObjectGraph(ObjectGraph holder) {
|
||||
this.holder = holder;
|
||||
}
|
||||
|
||||
public ObjectGraph fetchObjectGraph() {
|
||||
return this.holder;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user