Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#176: master/detail controller implementation #564

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 @@ -51,6 +51,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 @@ -79,6 +80,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 @@ -159,10 +161,21 @@ protected Controller(@Nullable Bundle args) {
protected abstract View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, @Nullable Bundle savedViewState);

/**
* 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 @@ -273,6 +286,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 @@ -320,6 +340,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 @@ -614,7 +645,7 @@ public boolean handleBack() {
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 @@ -1069,7 +1100,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 @@ -1182,6 +1213,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 @@ -1224,6 +1256,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