mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-11-22 07:58:20 -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.Injector;
|
||||||
import org.bspeice.minimalbible.MinimalBible;
|
import org.bspeice.minimalbible.MinimalBible;
|
||||||
|
import org.bspeice.minimalbible.OGHolder;
|
||||||
import org.bspeice.minimalbible.R;
|
import org.bspeice.minimalbible.R;
|
||||||
import org.bspeice.minimalbible.activity.BaseActivity;
|
import org.bspeice.minimalbible.activity.BaseActivity;
|
||||||
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment;
|
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment;
|
||||||
@ -26,6 +27,8 @@ public class DownloadActivity extends BaseActivity implements
|
|||||||
BaseNavigationDrawerFragment.NavigationDrawerCallbacks,
|
BaseNavigationDrawerFragment.NavigationDrawerCallbacks,
|
||||||
Injector {
|
Injector {
|
||||||
|
|
||||||
|
private final String TAG = "DownloadActivity";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fragment managing the behaviors, interactions and presentation of the
|
* Fragment managing the behaviors, interactions and presentation of the
|
||||||
* navigation drawer.
|
* navigation drawer.
|
||||||
@ -48,19 +51,15 @@ public class DownloadActivity extends BaseActivity implements
|
|||||||
*/
|
*/
|
||||||
private void buildObjGraph() {
|
private void buildObjGraph() {
|
||||||
if (daObjectGraph == null) {
|
if (daObjectGraph == null) {
|
||||||
FragmentManager fM = getSupportFragmentManager();
|
OGHolder holder = OGHolder.get(this);
|
||||||
DownloadOGHolder ogHolder = (DownloadOGHolder) fM.findFragmentByTag("OG_HOLDER");
|
ObjectGraph holderGraph = holder.fetchGraph();
|
||||||
if (ogHolder == null) {
|
if (holderGraph == null) {
|
||||||
Log.e("DownloadActivity", "Creating new holder...");
|
Log.i(TAG, "Rebuilding ObjectGraph...");
|
||||||
ogHolder = new DownloadOGHolder();
|
|
||||||
daObjectGraph = MinimalBible.get(this)
|
daObjectGraph = MinimalBible.get(this)
|
||||||
.plus(new DownloadActivityModules(this));
|
.plus(new DownloadActivityModules(this));
|
||||||
ogHolder.persistObjectGraph(daObjectGraph);
|
holder.persistGraph(daObjectGraph);
|
||||||
fM.beginTransaction().add(ogHolder, "OG_HOLDER").commit();
|
|
||||||
} else {
|
} else {
|
||||||
Log.e("DownloadActivity", "Found existing holder...");
|
daObjectGraph = holderGraph;
|
||||||
daObjectGraph = ogHolder.fetchObjectGraph();
|
|
||||||
daObjectGraph.inject(this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
daObjectGraph.inject(this);
|
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