Parcourir la source

Implement popToRoot

Jason Skuby il y a 8 ans
Parent
révision
b80b8d29e1

+ 9
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java Voir le fichier

233
         return null;
233
         return null;
234
     }
234
     }
235
 
235
 
236
+    @CallSuper
237
+    public Screen popToRoot(String navigatorId) {
238
+        if (mToolbar != null) {
239
+            mToolbar.hideBackButton();
240
+        }
241
+
242
+        return null;
243
+    }
244
+
236
     protected abstract String getCurrentNavigatorId();
245
     protected abstract String getCurrentNavigatorId();
237
 
246
 
238
     @CallSuper
247
     @CallSuper

+ 13
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java Voir le fichier

133
         return null;
133
         return null;
134
     }
134
     }
135
 
135
 
136
+    @Override
137
+    public Screen popToRoot(String navigatorId) {
138
+        super.popToRoot(navigatorId);
139
+        for (ScreenStack stack: mScreenStacks) {
140
+            if (stack.peek().navigatorId.equals(navigatorId)) {
141
+                Screen popped = stack.popToRoot();
142
+                setNavigationStyle(getCurrentScreen());
143
+                return popped;
144
+            }
145
+        }
146
+        return null;
147
+    }
148
+
136
     @Override
149
     @Override
137
     protected Screen getCurrentScreen() {
150
     protected Screen getCurrentScreen() {
138
         Screen currentScreen = super.getCurrentScreen();
151
         Screen currentScreen = super.getCurrentScreen();

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/activities/SingleScreenActivity.java Voir le fichier

64
         return popped;
64
         return popped;
65
     }
65
     }
66
 
66
 
67
+    @Override
68
+    public Screen popToRoot(String navigatorId) {
69
+        super.popToRoot(navigatorId);
70
+        Screen screen = mScreenStack.popToRoot();
71
+        setNavigationStyle(getCurrentScreen());
72
+        return screen;
73
+    }
74
+
67
     @Override
75
     @Override
68
     public String getCurrentNavigatorId() {
76
     public String getCurrentNavigatorId() {
69
         return mNavigatorId;
77
         return mNavigatorId;

+ 18
- 1
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java Voir le fichier

27
  */
27
  */
28
 public class RctActivityModule extends ReactContextBaseJavaModule {
28
 public class RctActivityModule extends ReactContextBaseJavaModule {
29
     public static final String REACT_CLASS = "RctActivity";
29
     public static final String REACT_CLASS = "RctActivity";
30
+    private static final String KEY_NAVIGATOR_ID = "navigatorID";
30
 
31
 
31
     public RctActivityModule(ReactApplicationContext reactContext) {
32
     public RctActivityModule(ReactApplicationContext reactContext) {
32
         super(reactContext);
33
         super(reactContext);
133
 
134
 
134
     @ReactMethod
135
     @ReactMethod
135
     public void navigatorPop(final ReadableMap navigator) {
136
     public void navigatorPop(final ReadableMap navigator) {
136
-        final String navigatorId = navigator.getString("navigatorID");
137
+        final String navigatorId = navigator.getString(KEY_NAVIGATOR_ID);
137
         final BaseReactActivity context = ContextProvider.getActivityContext();
138
         final BaseReactActivity context = ContextProvider.getActivityContext();
138
         if (context == null || context.isFinishing()) {
139
         if (context == null || context.isFinishing()) {
139
             return;
140
             return;
160
                 }
161
                 }
161
             });
162
             });
162
         }
163
         }
164
+    }
165
+
166
+    @ReactMethod
167
+    public void navigatorPopToRoot(final ReadableMap params) {
168
+        final BaseReactActivity context = ContextProvider.getActivityContext();
169
+        if (context == null || context.isFinishing()) {
170
+            return;
171
+        }
172
+
173
+        final String navigatorID = params.getString(KEY_NAVIGATOR_ID);
174
+        context.runOnUiThread(new Runnable() {
175
+            @Override
176
+            public void run() {
177
+                context.popToRoot(navigatorID);
178
+            }
179
+        });
163
 
180
 
164
     }
181
     }
165
 
182
 

+ 27
- 0
android/app/src/main/java/com/reactnativenavigation/views/ScreenStack.java Voir le fichier

81
         return popped.screen;
81
         return popped.screen;
82
     }
82
     }
83
 
83
 
84
+    public Screen popToRoot() {
85
+        if (mStack.isEmpty()) {
86
+            return null;
87
+        }
88
+
89
+        int stackSize = getStackSize();
90
+        if (stackSize < 2) {
91
+            return null;
92
+        }
93
+
94
+        ScreenView lastView = null;
95
+        while (getStackSize() >= 2) {
96
+            ScreenView popped = mStack.pop();
97
+            ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
98
+            removeView(popped.view);
99
+            if (lastView == null) {
100
+                lastView = popped;
101
+            }
102
+        }
103
+
104
+        if (!mStack.isEmpty()) {
105
+            addView(mStack.peek().view, 0);
106
+        }
107
+
108
+        return lastView.screen;
109
+    }
110
+
84
     public boolean isEmpty() {
111
     public boolean isEmpty() {
85
         return mStack.isEmpty();
112
         return mStack.isEmpty();
86
     }
113
     }

+ 8
- 0
src/platformSpecific.android.js Voir le fichier

69
   RctActivity.navigatorPop(navigator);
69
   RctActivity.navigatorPop(navigator);
70
 }
70
 }
71
 
71
 
72
+function navigatorPopToRoot(navigator, params) {
73
+  RctActivity.navigatorPopToRoot({
74
+    navigatorID: navigator.navigatorID,
75
+    animated: !(params.animated !== false)
76
+  });
77
+}
78
+
72
 function showModal(params) {
79
 function showModal(params) {
73
   addNavigatorParams(params);
80
   addNavigatorParams(params);
74
   addNavigatorButtons(params);
81
   addNavigatorButtons(params);
117
   startSingleScreenApp,
124
   startSingleScreenApp,
118
   navigatorPush,
125
   navigatorPush,
119
   navigatorPop,
126
   navigatorPop,
127
+  navigatorPopToRoot,
120
   showModal,
128
   showModal,
121
   dismissModal,
129
   dismissModal,
122
   dismissAllModals,
130
   dismissAllModals,