Browse Source

Implement resetTo

Jason Skuby 8 years ago
parent
commit
4a7672805c

+ 10
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java View File

242
         return null;
242
         return null;
243
     }
243
     }
244
 
244
 
245
+    @CallSuper
246
+    public Screen resetTo(Screen screen) {
247
+        StyleHelper.updateStyles(mToolbar, screen);
248
+        if (mToolbar != null) {
249
+            mToolbar.hideBackButton();
250
+        }
251
+
252
+        return null;
253
+    }
254
+
245
     protected abstract String getCurrentNavigatorId();
255
     protected abstract String getCurrentNavigatorId();
246
 
256
 
247
     @CallSuper
257
     @CallSuper

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java View File

156
         return mScreenStacks != null ? mScreenStacks.get(mCurrentStackPosition).peek() : null;
156
         return mScreenStacks != null ? mScreenStacks.get(mCurrentStackPosition).peek() : null;
157
     }
157
     }
158
 
158
 
159
+    public Screen resetTo(Screen screen) {
160
+        super.resetTo(screen);
161
+        return mScreenStacks.get(mCurrentStackPosition).resetTo(screen);
162
+    }
163
+
159
     @Override
164
     @Override
160
     protected String getCurrentNavigatorId() {
165
     protected String getCurrentNavigatorId() {
161
         return mScreenStacks.get(mCurrentStackPosition).peek().navigatorId;
166
         return mScreenStacks.get(mCurrentStackPosition).peek().navigatorId;

+ 7
- 0
android/app/src/main/java/com/reactnativenavigation/activities/SingleScreenActivity.java View File

72
         return screen;
72
         return screen;
73
     }
73
     }
74
 
74
 
75
+    @Override
76
+    public Screen resetTo(Screen screen) {
77
+        super.resetTo(screen);
78
+        Screen popped = mScreenStack.resetTo(screen);
79
+        return popped;
80
+    }
81
+
75
     @Override
82
     @Override
76
     public String getCurrentNavigatorId() {
83
     public String getCurrentNavigatorId() {
77
         return mNavigatorId;
84
         return mNavigatorId;

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/activities/TabActivity.java View File

75
     @Override
75
     @Override
76
     public Screen pop(String navigatorId) {
76
     public Screen pop(String navigatorId) {
77
         super.pop(navigatorId);
77
         super.pop(navigatorId);
78
-        Screen screen = mAdapter.pop(navigatorId);
78
+        Screen popped = mAdapter.pop(navigatorId);
79
         setNavigationStyle(getCurrentScreen());
79
         setNavigationStyle(getCurrentScreen());
80
-        return screen;
80
+        return popped;
81
     }
81
     }
82
 
82
 
83
     @Override
83
     @Override

+ 15
- 0
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java View File

177
                 context.popToRoot(navigatorID);
177
                 context.popToRoot(navigatorID);
178
             }
178
             }
179
         });
179
         });
180
+    }
181
+
182
+    @ReactMethod
183
+    public void navigatorResetTo(final ReadableMap skreen) {
184
+        final BaseReactActivity context = ContextProvider.getActivityContext();
185
+        if (context == null || context.isFinishing()) {
186
+            return;
187
+        }
180
 
188
 
189
+        final Screen screen = new Screen(skreen);
190
+        context.runOnUiThread(new Runnable() {
191
+            @Override
192
+            public void run() {
193
+                context.resetTo(screen);
194
+            }
195
+        });
181
     }
196
     }
182
 
197
 
183
     @ReactMethod
198
     @ReactMethod

+ 39
- 15
android/app/src/main/java/com/reactnativenavigation/views/ScreenStack.java View File

68
     }
68
     }
69
 
69
 
70
     public Screen pop() {
70
     public Screen pop() {
71
-        if (mStack.isEmpty()) {
71
+        if (mStack.isEmpty() || getStackSize() == 1) {
72
             return null;
72
             return null;
73
         }
73
         }
74
+
74
         ScreenView popped = mStack.pop();
75
         ScreenView popped = mStack.pop();
75
-        if (!mStack.isEmpty()) {
76
-            addView(mStack.peek().view, 0);
77
-        }
76
+        addView(mStack.peek().view, 0);
78
 
77
 
79
         ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
78
         ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
80
         removeView(popped.view);
79
         removeView(popped.view);
82
     }
81
     }
83
 
82
 
84
     public Screen popToRoot() {
83
     public Screen popToRoot() {
85
-        if (mStack.isEmpty()) {
86
-            return null;
87
-        }
88
-
89
-        int stackSize = getStackSize();
90
-        if (stackSize < 2) {
84
+        if (mStack.isEmpty() || getStackSize() <= 1) {
91
             return null;
85
             return null;
92
         }
86
         }
93
 
87
 
94
-        ScreenView lastView = null;
95
-        while (getStackSize() >= 2) {
88
+        ScreenView oldScreenView = null;
89
+        while (getStackSize() > 1) {
96
             ScreenView popped = mStack.pop();
90
             ScreenView popped = mStack.pop();
97
             ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
91
             ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
98
             removeView(popped.view);
92
             removeView(popped.view);
99
-            if (lastView == null) {
100
-                lastView = popped;
93
+            if (oldScreenView == null) {
94
+                oldScreenView = popped;
101
             }
95
             }
102
         }
96
         }
103
 
97
 
105
             addView(mStack.peek().view, 0);
99
             addView(mStack.peek().view, 0);
106
         }
100
         }
107
 
101
 
108
-        return lastView.screen;
102
+        return oldScreenView.screen;
103
+    }
104
+
105
+    public Screen resetTo(Screen screen) {
106
+        return resetTo(screen, null);
107
+    }
108
+
109
+    public Screen resetTo(Screen screen, RctView.OnDisplayedListener onDisplayed) {
110
+        RctView view = new RctView(mReactActivity, mReactInstanceManager, screen, onDisplayed);
111
+        addView(view, MATCH_PARENT, MATCH_PARENT);
112
+
113
+        ScreenView oldScreenView = null;
114
+        if (!mStack.isEmpty()) {
115
+            while (getStackSize() > 0) {
116
+                ScreenView screenView = mStack.pop();
117
+                ReflectionUtils.setBooleanField(screenView.view.getReactRootView(), "mAttachScheduled", false);
118
+                removeView(screenView.view);
119
+                if (oldScreenView == null) {
120
+                    oldScreenView = screenView;
121
+                }
122
+            }
123
+        }
124
+
125
+        // Add screen to stack after it's clear
126
+        mStack.push(new ScreenView(screen, view));
127
+
128
+        if (oldScreenView == null) {
129
+            return null;
130
+        }
131
+
132
+        return oldScreenView.screen;
109
     }
133
     }
110
 
134
 
111
     public boolean isEmpty() {
135
     public boolean isEmpty() {

+ 8
- 0
src/platformSpecific.android.js View File

77
   });
77
   });
78
 }
78
 }
79
 
79
 
80
+function navigatorResetTo(navigator, params) {
81
+  addNavigatorParams(params, navigator);
82
+  addNavigatorButtons(params);
83
+  addNavigationStyleParams(params);
84
+  RctActivity.navigatorResetTo(params);
85
+}
86
+
80
 function showModal(params) {
87
 function showModal(params) {
81
   addNavigatorParams(params);
88
   addNavigatorParams(params);
82
   addNavigatorButtons(params);
89
   addNavigatorButtons(params);
126
   navigatorPush,
133
   navigatorPush,
127
   navigatorPop,
134
   navigatorPop,
128
   navigatorPopToRoot,
135
   navigatorPopToRoot,
136
+  navigatorResetTo,
129
   showModal,
137
   showModal,
130
   dismissModal,
138
   dismissModal,
131
   dismissAllModals,
139
   dismissAllModals,