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,6 +242,16 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
242 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 255
     protected abstract String getCurrentNavigatorId();
246 256
 
247 257
     @CallSuper

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

@@ -156,6 +156,11 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
156 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 164
     @Override
160 165
     protected String getCurrentNavigatorId() {
161 166
         return mScreenStacks.get(mCurrentStackPosition).peek().navigatorId;

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

@@ -72,6 +72,13 @@ public class SingleScreenActivity extends BaseReactActivity {
72 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 82
     @Override
76 83
     public String getCurrentNavigatorId() {
77 84
         return mNavigatorId;

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

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

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

@@ -177,7 +177,22 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
177 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 198
     @ReactMethod

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

@@ -68,13 +68,12 @@ public class ScreenStack extends FrameLayout {
68 68
     }
69 69
 
70 70
     public Screen pop() {
71
-        if (mStack.isEmpty()) {
71
+        if (mStack.isEmpty() || getStackSize() == 1) {
72 72
             return null;
73 73
         }
74
+
74 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 78
         ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
80 79
         removeView(popped.view);
@@ -82,22 +81,17 @@ public class ScreenStack extends FrameLayout {
82 81
     }
83 82
 
84 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 85
             return null;
92 86
         }
93 87
 
94
-        ScreenView lastView = null;
95
-        while (getStackSize() >= 2) {
88
+        ScreenView oldScreenView = null;
89
+        while (getStackSize() > 1) {
96 90
             ScreenView popped = mStack.pop();
97 91
             ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
98 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,7 +99,37 @@ public class ScreenStack extends FrameLayout {
105 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 135
     public boolean isEmpty() {

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

@@ -77,6 +77,13 @@ function navigatorPopToRoot(navigator, params) {
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 87
 function showModal(params) {
81 88
   addNavigatorParams(params);
82 89
   addNavigatorButtons(params);
@@ -126,6 +133,7 @@ export default {
126 133
   navigatorPush,
127 134
   navigatorPop,
128 135
   navigatorPopToRoot,
136
+  navigatorResetTo,
129 137
   showModal,
130 138
   dismissModal,
131 139
   dismissAllModals,