Skip to content

Commit

Permalink
bluelinelabs#176: master/detail controller implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelaysam committed Sep 10, 2019
1 parent 7a50f12 commit 28cfa34
Show file tree
Hide file tree
Showing 33 changed files with 2,664 additions and 478 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ boolean isEmpty() {
return backstack.isEmpty();
}

void clear() {
backstack.clear();
}

int size() {
return backstack.size();
}
Expand Down
41 changes: 37 additions & 4 deletions conductor/src/main/java/com/bluelinelabs/conductor/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public abstract class Controller {
private static final String KEY_TARGET_INSTANCE_ID = "Controller.target.instanceId";
private static final String KEY_ARGS = "Controller.args";
private static final String KEY_NEEDS_ATTACH = "Controller.needsAttach";
private static final String KEY_IS_DETAIL = "Controller.isDetail";
private static final String KEY_REQUESTED_PERMISSIONS = "Controller.requestedPermissions";
private static final String KEY_OVERRIDDEN_PUSH_HANDLER = "Controller.overriddenPushHandler";
private static final String KEY_OVERRIDDEN_POP_HANDLER = "Controller.overriddenPopHandler";
Expand Down Expand Up @@ -80,6 +81,7 @@ public abstract class Controller {
private boolean awaitingParentAttach;
private boolean hasSavedViewState;
boolean isDetachFrozen;
boolean isDetail;
private ControllerChangeHandler overriddenPushHandler;
private ControllerChangeHandler overriddenPopHandler;
private RetainViewMode retainViewMode = RetainViewMode.RELEASE_DETACH;
Expand Down Expand Up @@ -158,10 +160,21 @@ protected Controller(@Nullable Bundle args) {
protected abstract View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container);

/**
* Returns the {@link Router} object that can be used for pushing or popping other Controllers
* Returns the {@link Router} object that can be used for pushing or popping other Controllers.
* NOTE: If this Controller is pushed to {@link MasterDetailController},
* method will return either {@link MasterDetailController#getMasterRouter()} or
* {@link MasterDetailController#getDetailRouter()}
*/
public final Router getRouter() {
return router;
if (getMasterDetailController() != null) {
if (isDetail) {
return getMasterDetailController().getDetailRouter();
} else {
return getMasterDetailController().getMasterRouter();
}
} else {
return router;
}
}

/**
Expand Down Expand Up @@ -272,6 +285,13 @@ public final boolean isAttached() {
return attached;
}

/**
* Returns whether or not this Controller is currently shown in master/detail as detail.
*/
public final boolean isDetail() {
return isDetail;
}

/**
* Return this Controller's View or {@code null} if it has not yet been created or has been
* destroyed.
Expand Down Expand Up @@ -319,6 +339,17 @@ public final Controller getParentController() {
return parentController;
}

/**
* Returns this Controller's parent master/details Controller or {@code null} if
* it has no parent or parent is not a master/detail Controller.
*/
@Nullable
public final MasterDetailController getMasterDetailController() {
return parentController instanceof MasterDetailController
? (MasterDetailController) parentController
: null;
}

/**
* Returns this Controller's instance ID, which is generated when the instance is created and
* retained across restarts.
Expand Down Expand Up @@ -610,7 +641,7 @@ public int compare(RouterTransaction o1, RouterTransaction o2) {
for (RouterTransaction transaction : childTransactions) {
Controller childController = transaction.controller;

if (childController.isAttached() && childController.getRouter().handleBack()) {
if (childController.isAttached() && childController.router.handleBack()) {
return true;
}
}
Expand Down Expand Up @@ -1060,7 +1091,7 @@ private void restoreChildControllerHosts() {
if (!childRouter.hasHost()) {
View containerView = view.findViewById(childRouter.getHostId());

if (containerView != null && containerView instanceof ViewGroup) {
if (containerView instanceof ViewGroup) {
childRouter.setHost(this, (ViewGroup)containerView);
childRouter.rebindIfNeeded();
}
Expand Down Expand Up @@ -1173,6 +1204,7 @@ final Bundle saveInstanceState() {
outState.putString(KEY_TARGET_INSTANCE_ID, targetInstanceId);
outState.putStringArrayList(KEY_REQUESTED_PERMISSIONS, requestedPermissions);
outState.putBoolean(KEY_NEEDS_ATTACH, needsAttach || attached);
outState.putBoolean(KEY_IS_DETAIL, isDetail);
outState.putInt(KEY_RETAIN_VIEW_MODE, retainViewMode.ordinal());

if (overriddenPushHandler != null) {
Expand Down Expand Up @@ -1215,6 +1247,7 @@ private void restoreInstanceState(@NonNull Bundle savedInstanceState) {
overriddenPushHandler = ControllerChangeHandler.fromBundle(savedInstanceState.getBundle(KEY_OVERRIDDEN_PUSH_HANDLER));
overriddenPopHandler = ControllerChangeHandler.fromBundle(savedInstanceState.getBundle(KEY_OVERRIDDEN_POP_HANDLER));
needsAttach = savedInstanceState.getBoolean(KEY_NEEDS_ATTACH);
isDetail = savedInstanceState.getBoolean(KEY_IS_DETAIL);
retainViewMode = RetainViewMode.values()[savedInstanceState.getInt(KEY_RETAIN_VIEW_MODE, 0)];

List<Bundle> childBundles = savedInstanceState.getParcelableArrayList(KEY_CHILD_ROUTERS);
Expand Down
Loading

0 comments on commit 28cfa34

Please sign in to comment.