Skip to content

Commit 6d415cf

Browse files
committed
Make ControllerFactory an interface
1 parent 9d7445e commit 6d415cf

7 files changed

Lines changed: 60 additions & 78 deletions

File tree

conductor/src/main/java/com/bluelinelabs/conductor/Controller.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
*/
4545
public abstract class Controller {
4646

47-
static final String KEY_CLASS_NAME = "Controller.className";
47+
private static final String KEY_CLASS_NAME = "Controller.className";
4848
private static final String KEY_VIEW_STATE = "Controller.viewState";
4949
private static final String KEY_CHILD_ROUTERS = "Controller.childRouters";
5050
private static final String KEY_SAVED_STATE = "Controller.savedState";
5151
private static final String KEY_INSTANCE_ID = "Controller.instanceId";
5252
private static final String KEY_TARGET_INSTANCE_ID = "Controller.target.instanceId";
53-
static final String KEY_ARGS = "Controller.args";
53+
private static final String KEY_ARGS = "Controller.args";
5454
private static final String KEY_NEEDS_ATTACH = "Controller.needsAttach";
5555
private static final String KEY_REQUESTED_PERMISSIONS = "Controller.requestedPermissions";
5656
private static final String KEY_OVERRIDDEN_PUSH_HANDLER = "Controller.overriddenPushHandler";
@@ -92,32 +92,34 @@ public abstract class Controller {
9292
private boolean isPerformingExitTransition;
9393
private boolean isContextAvailable;
9494

95-
static volatile ControllerFactory FACTORY = new ControllerFactory.DefaultControllerFactory();
95+
static volatile ControllerFactory FACTORY = DefaultControllerFactory.INSTANCE;
9696

9797
@NonNull
9898
static Controller newInstance(@NonNull Bundle bundle) {
9999
final String className = bundle.getString(KEY_CLASS_NAME);
100100
//noinspection ConstantConditions
101101
Class cls = ClassUtils.classForName(className, false);
102-
Constructor[] constructors = cls.getConstructors();
103-
Constructor bundleConstructor = getBundleConstructor(constructors);
104-
105102
Bundle args = bundle.getBundle(KEY_ARGS);
106103
if (args != null) {
107104
args.setClassLoader(cls.getClassLoader());
108105
}
109106

110107
Controller controller;
111108
try {
112-
if (bundleConstructor != null) {
113-
controller = (Controller)bundleConstructor.newInstance(args);
114-
} else {
115-
//noinspection ConstantConditions
116-
controller = (Controller)getDefaultConstructor(constructors).newInstance();
117-
118-
// Restore the args that existed before the last process death
119-
if (args != null) {
120-
controller.args.putAll(args);
109+
controller = FACTORY.create(className, args);
110+
if (controller == null) {
111+
Constructor[] constructors = cls.getConstructors();
112+
Constructor bundleConstructor = getBundleConstructor(constructors);
113+
if (bundleConstructor != null) {
114+
controller = (Controller) bundleConstructor.newInstance(args);
115+
} else {
116+
//noinspection ConstantConditions
117+
controller = (Controller) getDefaultConstructor(constructors).newInstance();
118+
119+
// Restore the args that existed before the last process death
120+
if (args != null) {
121+
controller.args.putAll(args);
122+
}
121123
}
122124
}
123125
} catch (Exception e) {
@@ -1323,7 +1325,7 @@ final void setParentController(@Nullable Controller controller) {
13231325
private void ensureRequiredConstructor() {
13241326
Constructor[] constructors = getClass().getConstructors();
13251327
if (getBundleConstructor(constructors) == null && getDefaultConstructor(constructors) == null
1326-
&& FACTORY instanceof ControllerFactory.DefaultControllerFactory) {
1328+
&& FACTORY == DefaultControllerFactory.INSTANCE) {
13271329
throw new RuntimeException(getClass() + " does not have a constructor that takes a Bundle argument or a default constructor. Controllers must have one of these in order to restore their states.");
13281330
}
13291331
}
Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,10 @@
11
package com.bluelinelabs.conductor;
22

33
import android.os.Bundle;
4-
5-
import com.bluelinelabs.conductor.internal.ClassUtils;
6-
74
import androidx.annotation.NonNull;
85
import androidx.annotation.Nullable;
96

10-
import static com.bluelinelabs.conductor.Controller.KEY_ARGS;
11-
import static com.bluelinelabs.conductor.Controller.KEY_CLASS_NAME;
12-
13-
public abstract class ControllerFactory {
14-
15-
@NonNull final Controller create(@NonNull Bundle bundle) {
16-
final String className = bundle.getString(KEY_CLASS_NAME);
17-
//noinspection ConstantConditions
18-
Class cls = ClassUtils.classForName(className, false);
19-
Bundle args = bundle.getBundle(KEY_ARGS);
20-
if (args != null) {
21-
args.setClassLoader(cls.getClassLoader());
22-
}
23-
24-
final Controller result = create(className, args);
25-
if (result == null) {
26-
return Controller.newInstance(bundle);
27-
} else {
28-
return result;
29-
}
30-
}
31-
7+
public interface ControllerFactory {
328
@Nullable
33-
public abstract Controller create(@NonNull String controllerName, @NonNull Bundle args);
34-
35-
static final class DefaultControllerFactory extends ControllerFactory {
36-
@Nullable
37-
@Override
38-
public Controller create(@NonNull String controllerName, @NonNull Bundle args) {
39-
return null;
40-
}
41-
}
9+
Controller create(@NonNull String controllerName, @Nullable Bundle args);
4210
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.bluelinelabs.conductor;
2+
3+
import android.os.Bundle;
4+
import androidx.annotation.NonNull;
5+
import androidx.annotation.Nullable;
6+
7+
final class DefaultControllerFactory implements ControllerFactory {
8+
9+
static final DefaultControllerFactory INSTANCE = new DefaultControllerFactory();
10+
11+
@Nullable
12+
@Override
13+
public final Controller create(@NonNull String controllerName, @Nullable Bundle args) {
14+
return null;
15+
}
16+
}

conductor/src/main/java/com/bluelinelabs/conductor/RouterTransaction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private RouterTransaction(@NonNull Controller controller) {
3838
}
3939

4040
RouterTransaction(@NonNull Bundle bundle) {
41-
controller = Controller.FACTORY.create(bundle.getBundle(KEY_VIEW_CONTROLLER_BUNDLE));
41+
controller = Controller.newInstance(bundle.getBundle(KEY_VIEW_CONTROLLER_BUNDLE));
4242
pushControllerChangeHandler = ControllerChangeHandler.fromBundle(bundle.getBundle(KEY_PUSH_TRANSITION));
4343
popControllerChangeHandler = ControllerChangeHandler.fromBundle(bundle.getBundle(KEY_POP_TRANSITION));
4444
tag = bundle.getString(KEY_TAG);
Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
11
package com.bluelinelabs.conductor;
22

3-
import android.content.Context;
43
import android.os.Bundle;
5-
import android.view.View;
6-
import android.view.ViewGroup;
7-
8-
import com.bluelinelabs.conductor.Controller.LifecycleListener;
9-
import com.bluelinelabs.conductor.Controller.RetainViewMode;
10-
import com.bluelinelabs.conductor.changehandler.SimpleSwapChangeHandler;
114
import com.bluelinelabs.conductor.util.ActivityProxy;
12-
import com.bluelinelabs.conductor.util.CallState;
13-
import com.bluelinelabs.conductor.util.ChangeHandlerHistory;
14-
import com.bluelinelabs.conductor.util.MockChangeHandler;
15-
import com.bluelinelabs.conductor.util.MockChangeHandler.ChangeHandlerListener;
165
import com.bluelinelabs.conductor.util.TestController;
176
import com.bluelinelabs.conductor.util.TestControllerFactory;
187
import com.bluelinelabs.conductor.util.TestDependenciesController;
19-
import com.bluelinelabs.conductor.util.ViewUtils;
20-
218
import org.junit.Before;
229
import org.junit.Test;
2310
import org.junit.runner.RunWith;
2411
import org.robolectric.RobolectricTestRunner;
2512
import org.robolectric.annotation.Config;
2613

27-
import androidx.annotation.NonNull;
28-
2914
import static org.junit.Assert.assertEquals;
30-
import static org.junit.Assert.assertFalse;
31-
import static org.junit.Assert.assertNotNull;
32-
import static org.junit.Assert.assertNull;
33-
import static org.junit.Assert.assertTrue;
3415

3516
@RunWith(RobolectricTestRunner.class)
3617
@Config(manifest = Config.NONE)
@@ -39,7 +20,6 @@ public class ControllerFactoryTests {
3920
private Router router;
4021

4122
private ActivityProxy activityProxy;
42-
private CallState currentCallState;
4323

4424
public void createActivityController(Bundle savedInstanceState, boolean includeStartAndResume) {
4525
activityProxy = new ActivityProxy().create(savedInstanceState);
@@ -57,7 +37,6 @@ public void createActivityController(Bundle savedInstanceState, boolean includeS
5737
public void setup() {
5838
Conductor.setControllerFactory(new TestControllerFactory());
5939
createActivityController(null, true);
60-
currentCallState = new CallState(false);
6140
}
6241

6342
@Test
@@ -67,6 +46,9 @@ public void testControllerWithDependenciesRecreated() {
6746
.tag("root"));
6847
activityProxy.getActivity().isChangingConfigurations = true;
6948

49+
assertEquals(false, controller.wasCreatedByFactory);
50+
assertEquals(false, controller.wasInstanceStateRestored);
51+
7052
Bundle bundle = new Bundle();
7153
activityProxy.saveInstanceState(bundle);
7254
activityProxy.pause();
@@ -75,6 +57,7 @@ public void testControllerWithDependenciesRecreated() {
7557

7658
createActivityController(bundle, false);
7759
controller = (TestDependenciesController)router.getControllerWithTag("root");
78-
assertEquals(true, controller.wasCreatedInFactory);
60+
assertEquals(true, controller.wasCreatedByFactory);
61+
assertEquals(true, controller.wasInstanceStateRestored);
7962
}
8063
}

conductor/src/test/java/com/bluelinelabs/conductor/util/TestControllerFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import androidx.annotation.NonNull;
99
import androidx.annotation.Nullable;
1010

11-
public class TestControllerFactory extends ControllerFactory {
11+
public class TestControllerFactory implements ControllerFactory {
1212
@Nullable
1313
@Override
14-
public Controller create(@NonNull String controllerName, @NonNull Bundle args) {
14+
public Controller create(@NonNull String controllerName, @Nullable Bundle args) {
1515
if (controllerName.equals(TestDependenciesController.class.getName())) {
1616
return new TestDependenciesController(true);
1717
}
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bluelinelabs.conductor.util;
22

3+
import android.os.Bundle;
34
import android.view.LayoutInflater;
45
import android.view.View;
56
import android.view.ViewGroup;
@@ -10,16 +11,28 @@
1011

1112
public class TestDependenciesController extends Controller {
1213

13-
public final Boolean wasCreatedInFactory;
14+
private static final String INSTANCE_STATE = "INSTANCE_STATE";
15+
public final Boolean wasCreatedByFactory;
16+
public Boolean wasInstanceStateRestored = false;
1417

15-
public TestDependenciesController(Boolean wasCreatedInFactory) {
18+
public TestDependenciesController(Boolean wasCreatedByFactory) {
1619
super();
17-
this.wasCreatedInFactory = wasCreatedInFactory;
20+
this.wasCreatedByFactory = wasCreatedByFactory;
1821
}
1922

2023
@NonNull
2124
@Override
2225
protected View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
2326
return new AttachFakingFrameLayout(inflater.getContext());
2427
}
28+
29+
@Override protected void onSaveInstanceState(@NonNull Bundle outState) {
30+
super.onSaveInstanceState(outState);
31+
outState.putBoolean(INSTANCE_STATE, true);
32+
}
33+
34+
@Override protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
35+
super.onRestoreInstanceState(savedInstanceState);
36+
wasInstanceStateRestored = savedInstanceState.getBoolean(INSTANCE_STATE);
37+
}
2538
}

0 commit comments

Comments
 (0)