Browse Source

Added pop

Yedidya Kennard 8 years ago
parent
commit
2bcc122807

+ 5
- 3
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java View File

@@ -202,6 +202,8 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
202 202
 
203 203
     public abstract void push(Screen screen);
204 204
 
205
+    public abstract Screen pop(String navID);
206
+
205 207
     @Override
206 208
     public boolean onCreateOptionsMenu(Menu menu) {
207 209
         mMenu = menu;
@@ -250,11 +252,11 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
250 252
 
251 253
     @Override
252 254
     public void onBackPressed() {
253
-        if (mReactInstanceManager != null) {
255
+        if (mReactInstanceManager != null)
254 256
             mReactInstanceManager.onBackPressed();
255
-        } else {
257
+        //TODO uncomment and add current navID
258
+         else //if(pop() == null)
256 259
             super.onBackPressed();
257
-        }
258 260
     }
259 261
 
260 262
     @Override

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/activities/RootActivity.java View File

@@ -59,4 +59,10 @@ public class RootActivity extends BaseReactActivity {
59 59
     public void push(Screen screen) {
60 60
         //TODO
61 61
     }
62
+
63
+    @Override
64
+    public Screen pop(String navID) {
65
+        return null;
66
+        //TODO
67
+    }
62 68
 }

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

@@ -43,4 +43,9 @@ public class SingleScreenActivity extends BaseReactActivity {
43 43
     public void push(Screen screen) {
44 44
         screenStack.push(screen);
45 45
     }
46
+
47
+    @Override
48
+    public Screen pop(String navID) {
49
+        return screenStack.pop();
50
+    }
46 51
 }

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

@@ -45,6 +45,11 @@ public class TabActivity extends BaseReactActivity {
45 45
         adapter.pushScreen(screen);
46 46
     }
47 47
 
48
+    @Override
49
+    public Screen pop(String navID) {
50
+        return adapter.pop(navID);
51
+    }
52
+
48 53
     private void setupToolbar() {
49 54
         setSupportActionBar(mToolbar);
50 55
         mToolbar.setScreens(mScreens);

+ 10
- 2
android/app/src/main/java/com/reactnativenavigation/adapters/ViewPagerAdapter.java View File

@@ -3,6 +3,7 @@ package com.reactnativenavigation.adapters;
3 3
 import android.support.design.widget.TabLayout;
4 4
 import android.support.v4.view.PagerAdapter;
5 5
 import android.support.v4.view.ViewPager;
6
+import android.support.v7.widget.Toolbar;
6 7
 import android.view.View;
7 8
 import android.view.ViewGroup;
8 9
 
@@ -33,7 +34,7 @@ public class ViewPagerAdapter extends PagerAdapter implements TabLayout.OnTabSel
33 34
     private final Map<String, ScreenStack> stacksByNavId;
34 35
 
35 36
 
36
-    public ViewPagerAdapter(BaseReactActivity context, ViewPager viewPager, RnnToolBar toolbar,
37
+    public ViewPagerAdapter(BaseReactActivity context, ViewPager viewPager, Toolbar toolbar,
37 38
                             ArrayList<Screen> screens) {
38 39
         mContext = context;
39 40
         mViewPager = viewPager;
@@ -54,6 +55,13 @@ public class ViewPagerAdapter extends PagerAdapter implements TabLayout.OnTabSel
54 55
         stack.push(screen);
55 56
     }
56 57
 
58
+    public Screen pop(String navID){
59
+        ScreenStack stack = stacksByNavId.get(navID);
60
+        if(stack != null)
61
+            return stack.pop();
62
+        return null;
63
+    }
64
+
57 65
     @Override
58 66
     public Object instantiateItem(ViewGroup container, int position) {
59 67
         ScreenStack view = screenStacks.get(position);
@@ -92,7 +100,7 @@ public class ViewPagerAdapter extends PagerAdapter implements TabLayout.OnTabSel
92 100
         Screen screen = screenStacks.get(position).peek();
93 101
         params.putString(Screen.KEY_NAVIGATOR_EVENT_ID, screen.navigatorEventId);
94 102
 
95
-        mToolbar.setupToolbarButtonsAsync(mScreens.get(position));
103
+//        mToolbar.setupToolbarButtonsAsync(mScreens.get(position));
96 104
 
97 105
         RctManager.getInstance().sendEvent(EVENT_ON_TAB_SELECTED, screen.navigatorEventId, params);
98 106
     }

+ 22
- 3
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java View File

@@ -65,10 +65,29 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
65 65
     }
66 66
 
67 67
     @ReactMethod
68
-    public void navigatorPush(ReadableMap screen) {
69
-        BaseReactActivity context = ContextProvider.getActivityContext();
68
+    public void navigatorPush(final ReadableMap screen) {
69
+        final BaseReactActivity context = ContextProvider.getActivityContext();
70 70
          if (context != null && !context.isFinishing()) {
71
-             context.push(new Screen(screen));
71
+             context.runOnUiThread(new Runnable() {
72
+                 @Override
73
+                 public void run() {
74
+                     context.push(new Screen(screen));
75
+                 }
76
+             });
77
+        }
78
+    }
79
+
80
+    @ReactMethod
81
+    public void navigatorPop(final ReadableMap navigator) {
82
+        final String navID = navigator.getString("navigatorID");
83
+        final BaseReactActivity context = ContextProvider.getActivityContext();
84
+        if (context != null && !context.isFinishing()) {
85
+            context.runOnUiThread(new Runnable() {
86
+                @Override
87
+                public void run() {
88
+                    context.pop(navID);
89
+                }
90
+            });
72 91
         }
73 92
     }
74 93
 }

+ 11
- 0
android/app/src/main/java/com/reactnativenavigation/views/ScreenStack.java View File

@@ -1,6 +1,7 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3 3
 import android.animation.LayoutTransition;
4
+import android.view.View;
4 5
 import android.widget.FrameLayout;
5 6
 
6 7
 import com.facebook.react.ReactInstanceManager;
@@ -35,14 +36,24 @@ public class ScreenStack extends FrameLayout {
35 36
     }
36 37
 
37 38
     public void push(Screen screen){
39
+        View oldView = null;
40
+        if(!stack.isEmpty())
41
+            oldView = stack.peek().view;
38 42
         RctView view = new RctView(reactActivity, mReactInstanceManager, screen);
39 43
         addView(view, MATCH_PARENT, MATCH_PARENT);
44
+        if(oldView!=null)
45
+            removeView(oldView);
40 46
         stack.push(new ScreenView(screen, view));
41 47
     }
42 48
 
43 49
     public Screen pop(){
50
+        //TODO maybe return null if size is 1?
51
+        if(stack.isEmpty())
52
+            return null;
44 53
         ScreenView popped = stack.pop();
45 54
         removeView(popped.view);
55
+        if(!stack.isEmpty());
56
+        addView(stack.peek().view);
46 57
         return popped.screen;
47 58
     }
48 59
 

+ 6
- 1
src/platformSpecific.android.js View File

@@ -56,6 +56,10 @@ function navigatorPush(navigator, params) {
56 56
   RctActivity.navigatorPush(params);
57 57
 }
58 58
 
59
+function navigatorPop(navigator, params) {
60
+  RctActivity.navigatorPop(navigator);
61
+}
62
+
59 63
 function addNavigatorParams(screen, navigator = null, idx = '') {
60 64
   screen.navigatorID = navigator ? navigator.navigatorID : utils.getRandomId() + '_nav' + idx;
61 65
   screen.screenInstanceID = utils.getRandomId();
@@ -83,5 +87,6 @@ function addNavigatorButtons(screen) {
83 87
 export default {
84 88
   startSingleScreenApp,
85 89
   startTabBasedApp,
86
-  navigatorPush
90
+  navigatorPush,
91
+  navigatorPop
87 92
 }