Browse Source

Add selectedTabChanged event

Guy Carmeli 8 years ago
parent
commit
a177b65d0c

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

89
         navigationReactInstance.getReactEventEmitter().sendNavigatorEvent(eventId, navigatorEventId);
89
         navigationReactInstance.getReactEventEmitter().sendNavigatorEvent(eventId, navigatorEventId);
90
     }
90
     }
91
 
91
 
92
+    public void sendNavigatorEvent(String eventId, String navigatorEventId, WritableMap data) {
93
+        if (navigationReactInstance == null) {
94
+            return;
95
+        }
96
+        navigationReactInstance.getReactEventEmitter().sendNavigatorEvent(eventId, navigatorEventId, data);
97
+    }
98
+
92
     public void sendEvent(String eventId, String navigatorEventId) {
99
     public void sendEvent(String eventId, String navigatorEventId) {
93
         if (navigationReactInstance == null) {
100
         if (navigationReactInstance == null) {
94
             return;
101
             return;

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactEventEmitter.java View File

25
         eventEmitter.emit(navigatorEventId, data);
25
         eventEmitter.emit(navigatorEventId, data);
26
     }
26
     }
27
 
27
 
28
+    public void sendNavigatorEvent(String eventId, String navigatorEventId, WritableMap data) {
29
+        data.putString(KEY_NAVIGATOR_EVENT_ID, navigatorEventId);
30
+        data.putString(KEY_EVENT_ID, eventId);
31
+        eventEmitter.emit(navigatorEventId, data);
32
+    }
33
+
28
     public void sendEvent(String eventId, String data) {
34
     public void sendEvent(String eventId, String data) {
29
         eventEmitter.emit(eventId, data);
35
         eventEmitter.emit(eventId, data);
30
     }
36
     }

+ 0
- 1
android/app/src/main/java/com/reactnativenavigation/params/TopTabParams.java View File

5
 public class TopTabParams {
5
 public class TopTabParams {
6
     public String screenId;
6
     public String screenId;
7
     public String title;
7
     public String title;
8
-    public String screenInstanceId;
9
     public Bundle passProps;
8
     public Bundle passProps;
10
 }
9
 }

+ 0
- 2
android/app/src/main/java/com/reactnativenavigation/params/parsers/TopTabParamsParser.java View File

10
 
10
 
11
 public class TopTabParamsParser extends Parser {
11
 public class TopTabParamsParser extends Parser {
12
     private static final String KEY_SCREEN_ID = "screenId";
12
     private static final String KEY_SCREEN_ID = "screenId";
13
-    private static final String KEY_SCREEN_INSTANCE_ID = "screenInstanceID";
14
     private static final String KEY_TITLE = "title";
13
     private static final String KEY_TITLE = "title";
15
     private static final String KEY_PROPS = "passProps";
14
     private static final String KEY_PROPS = "passProps";
16
 
15
 
27
     private static TopTabParams parseItem(Bundle params) {
26
     private static TopTabParams parseItem(Bundle params) {
28
         TopTabParams result = new TopTabParams();
27
         TopTabParams result = new TopTabParams();
29
         result.screenId = params.getString(KEY_SCREEN_ID);
28
         result.screenId = params.getString(KEY_SCREEN_ID);
30
-        result.screenInstanceId = params.getString(KEY_SCREEN_INSTANCE_ID);
31
         result.title = params.getString(KEY_TITLE);
29
         result.title = params.getString(KEY_TITLE);
32
         result.passProps = params.getBundle(KEY_PROPS);
30
         result.passProps = params.getBundle(KEY_PROPS);
33
         return result;
31
         return result;

+ 61
- 0
android/app/src/main/java/com/reactnativenavigation/screens/ContentViewPagerAdapter.java View File

1
+package com.reactnativenavigation.screens;
2
+
3
+import android.support.v4.view.PagerAdapter;
4
+import android.view.View;
5
+import android.view.ViewGroup;
6
+
7
+import com.facebook.react.bridge.Arguments;
8
+import com.facebook.react.bridge.WritableMap;
9
+import com.reactnativenavigation.NavigationApplication;
10
+import com.reactnativenavigation.params.TopTabParams;
11
+import com.reactnativenavigation.views.ContentView;
12
+
13
+import java.util.List;
14
+
15
+public class ContentViewPagerAdapter extends PagerAdapter {
16
+
17
+    private List<ContentView> contentViews;
18
+    private List<TopTabParams> topTabParams;
19
+    private int currentPosition = 0;
20
+
21
+    public ContentViewPagerAdapter(List<ContentView> contentViews, List<TopTabParams> topTabParams) {
22
+        this.contentViews = contentViews;
23
+        this.topTabParams = topTabParams;
24
+    }
25
+
26
+    @Override
27
+    public Object instantiateItem(ViewGroup container, int position) {
28
+        return contentViews.get(position);
29
+    }
30
+
31
+    @Override
32
+    public void setPrimaryItem(ViewGroup container, int position, Object object) {
33
+        super.setPrimaryItem(container, position, object);
34
+        if (position != currentPosition) {
35
+            currentPosition = position;
36
+            sendPageChangeEvent();
37
+        }
38
+    }
39
+
40
+    private void sendPageChangeEvent() {
41
+        WritableMap data = Arguments.createMap();
42
+        String navigatorEventId = contentViews.get(currentPosition).getNavigatorEventId();
43
+        data.putInt("position", currentPosition);
44
+        NavigationApplication.instance.sendNavigatorEvent("selectedTabChanged", navigatorEventId, data);
45
+    }
46
+
47
+    @Override
48
+    public int getCount() {
49
+        return contentViews.size();
50
+    }
51
+
52
+    @Override
53
+    public boolean isViewFromObject(View view, Object object) {
54
+        return view == object;
55
+    }
56
+
57
+    @Override
58
+    public CharSequence getPageTitle(int position) {
59
+        return topTabParams.get(position).title;
60
+    }
61
+}

+ 1
- 4
android/app/src/main/java/com/reactnativenavigation/screens/FragmentScreen.java View File

38
     }
38
     }
39
 
39
 
40
     private void addContent() {
40
     private void addContent() {
41
-        ContentView contentView = new ContentView(getContext(),
42
-                screenParams.screenId,
43
-                screenParams.passProps,
44
-                screenParams.navigationParams);
41
+        ContentView contentView = new ContentView(getContext(), screenParams, screenParams.screenId);
45
         addView(contentView, 0, 0);
42
         addView(contentView, 0, 0);
46
         LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
43
         LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
47
         if (screenParams.styleParams.drawScreenBelowTopBar) {
44
         if (screenParams.styleParams.drawScreenBelowTopBar) {

+ 1
- 4
android/app/src/main/java/com/reactnativenavigation/screens/SingleScreen.java View File

19
 
19
 
20
     @Override
20
     @Override
21
     protected void createContent() {
21
     protected void createContent() {
22
-        contentView = new ContentView(getContext(),
23
-                screenParams.screenId,
24
-                screenParams.passProps,
25
-                screenParams.navigationParams);
22
+        contentView = new ContentView(getContext(), screenParams, screenParams.screenId);
26
         LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
23
         LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
27
         if (screenParams.styleParams.drawScreenBelowTopBar) {
24
         if (screenParams.styleParams.drawScreenBelowTopBar) {
28
             params.addRule(BELOW, topBar.getId());
25
             params.addRule(BELOW, topBar.getId());

+ 4
- 79
android/app/src/main/java/com/reactnativenavigation/screens/ViewPagerScreen.java View File

1
 package com.reactnativenavigation.screens;
1
 package com.reactnativenavigation.screens;
2
 
2
 
3
 import android.support.design.widget.TabLayout;
3
 import android.support.design.widget.TabLayout;
4
-import android.support.v4.view.PagerAdapter;
5
 import android.support.v4.view.ViewPager;
4
 import android.support.v4.view.ViewPager;
6
 import android.support.v7.app.AppCompatActivity;
5
 import android.support.v7.app.AppCompatActivity;
7
-import android.view.View;
8
-import android.view.ViewGroup;
9
 
6
 
10
 import com.reactnativenavigation.params.ScreenParams;
7
 import com.reactnativenavigation.params.ScreenParams;
11
 import com.reactnativenavigation.params.TopTabParams;
8
 import com.reactnativenavigation.params.TopTabParams;
19
 
16
 
20
 public class ViewPagerScreen extends Screen {
17
 public class ViewPagerScreen extends Screen {
21
 
18
 
19
+    private static final int OFFSCREEN_PAGE_LIMIT = 99;
22
     private List<ContentView> contentViews;
20
     private List<ContentView> contentViews;
23
     private ViewPager viewPager;
21
     private ViewPager viewPager;
24
 
22
 
36
 
34
 
37
     private void createViewPager() {
35
     private void createViewPager() {
38
         viewPager = new ViewPager(getContext());
36
         viewPager = new ViewPager(getContext());
39
-        viewPager.setOffscreenPageLimit(99);
37
+        viewPager.setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
40
         LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
38
         LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
41
         if (screenParams.styleParams.drawScreenBelowTopBar) {
39
         if (screenParams.styleParams.drawScreenBelowTopBar) {
42
             lp.addRule(BELOW, topBar.getId());
40
             lp.addRule(BELOW, topBar.getId());
47
     private void addPages() {
45
     private void addPages() {
48
         contentViews = new ArrayList<>();
46
         contentViews = new ArrayList<>();
49
         for (TopTabParams topTabParam : screenParams.topTabParams) {
47
         for (TopTabParams topTabParam : screenParams.topTabParams) {
50
-            ContentView contentView = new ContentView(getContext(),
51
-                    topTabParam.screenId,
52
-                    screenParams.passProps,
53
-                    screenParams.navigationParams);
48
+            ContentView contentView = new ContentView(getContext(), screenParams, topTabParam.screenId);
54
             addContent(contentView);
49
             addContent(contentView);
55
             contentViews.add(contentView);
50
             contentViews.add(contentView);
56
         }
51
         }
57
     }
52
     }
58
 
53
 
59
     private void setupViewPager(TabLayout tabLayout) {
54
     private void setupViewPager(TabLayout tabLayout) {
60
-        ContentViewPagerAdapter adapter =
61
-                new ContentViewPagerAdapter(viewPager, contentViews, screenParams.topTabParams);
55
+        ContentViewPagerAdapter adapter = new ContentViewPagerAdapter(contentViews, screenParams.topTabParams);
62
         viewPager.setAdapter(adapter);
56
         viewPager.setAdapter(adapter);
63
         tabLayout.setupWithViewPager(viewPager);
57
         tabLayout.setupWithViewPager(viewPager);
64
     }
58
     }
88
             contentView.preventMountAfterReattachedToWindow();
82
             contentView.preventMountAfterReattachedToWindow();
89
         }
83
         }
90
     }
84
     }
91
-
92
-    public class ContentViewPagerAdapter extends PagerAdapter implements TabLayout.OnTabSelectedListener, ViewPager.OnPageChangeListener {
93
-
94
-        private ViewPager viewPager;
95
-        private List<ContentView> contentViews;
96
-        private List<TopTabParams> topTabParams;
97
-
98
-        public ContentViewPagerAdapter(ViewPager viewPager, List<ContentView> contentViews, List<TopTabParams> topTabParams) {
99
-            this.viewPager = viewPager;
100
-            this.contentViews = contentViews;
101
-            this.topTabParams = topTabParams;
102
-        }
103
-
104
-        @Override
105
-        public Object instantiateItem(ViewGroup container, int position) {
106
-            return contentViews.get(position);
107
-        }
108
-
109
-        @Override
110
-        public void destroyItem(ViewGroup container, int position, Object view) {
111
-
112
-        }
113
-
114
-        @Override
115
-        public int getCount() {
116
-            return contentViews.size();
117
-        }
118
-
119
-        @Override
120
-        public boolean isViewFromObject(View view, Object object) {
121
-            return view == object;
122
-        }
123
-
124
-        @Override
125
-        public CharSequence getPageTitle(int position) {
126
-            return topTabParams.get(position).title;
127
-        }
128
-
129
-        @Override
130
-        public void onTabSelected(TabLayout.Tab tab) {
131
-            int position = tab.getPosition();
132
-            viewPager.setCurrentItem(position);
133
-        }
134
-
135
-        @Override
136
-        public void onTabUnselected(TabLayout.Tab tab) {
137
-
138
-        }
139
-
140
-        @Override
141
-        public void onTabReselected(TabLayout.Tab tab) {
142
-
143
-        }
144
-
145
-        @Override
146
-        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
147
-
148
-        }
149
-
150
-        @Override
151
-        public void onPageSelected(int position) {
152
-
153
-        }
154
-
155
-        @Override
156
-        public void onPageScrollStateChanged(int state) {
157
-
158
-        }
159
-    }
160
 }
85
 }

+ 21
- 13
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java View File

6
 import com.facebook.react.ReactInstanceManager;
6
 import com.facebook.react.ReactInstanceManager;
7
 import com.facebook.react.ReactRootView;
7
 import com.facebook.react.ReactRootView;
8
 import com.reactnativenavigation.NavigationApplication;
8
 import com.reactnativenavigation.NavigationApplication;
9
+import com.reactnativenavigation.params.ScreenParams;
9
 import com.reactnativenavigation.react.ReactViewHacks;
10
 import com.reactnativenavigation.react.ReactViewHacks;
10
 
11
 
11
 public class ContentView extends ReactRootView {
12
 public class ContentView extends ReactRootView {
12
 
13
 
13
     private final String screenId;
14
     private final String screenId;
15
+    private final String navigatorEventId;
14
     private final Bundle passProps;
16
     private final Bundle passProps;
15
     private Bundle navigationParams;
17
     private Bundle navigationParams;
16
 
18
 
17
-    public ContentView(Context context, final String screenId, Bundle passProps, Bundle navigationParams) {
19
+    public ContentView(Context context, ScreenParams screenParams, String screenId) {
18
         super(context);
20
         super(context);
19
         this.screenId = screenId;
21
         this.screenId = screenId;
20
-        this.passProps = passProps;
21
-        this.navigationParams = navigationParams;
22
-        attachToJS();
23
-    }
22
+        navigatorEventId = screenParams.navigatorEventId;
24
 
23
 
25
-    private void attachToJS() {
26
-        ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
27
-        startReactApplication(react, screenId, mergePropsAndNavigationParams());
24
+        passProps = screenParams.passProps;
25
+        navigationParams = screenParams.navigationParams;
26
+        attachToJS();
28
     }
27
     }
29
 
28
 
30
-    private Bundle mergePropsAndNavigationParams() {
31
-        if (passProps != null) {
32
-            navigationParams.putAll(passProps);
33
-        }
34
-        return navigationParams;
29
+    public String getNavigatorEventId() {
30
+        return navigatorEventId;
35
     }
31
     }
36
 
32
 
37
     public void preventUnmountOnDetachedFromWindow() {
33
     public void preventUnmountOnDetachedFromWindow() {
45
     public void preventMountAfterReattachedToWindow() {
41
     public void preventMountAfterReattachedToWindow() {
46
         ReactViewHacks.preventMountAfterReattachedToWindow(this);
42
         ReactViewHacks.preventMountAfterReattachedToWindow(this);
47
     }
43
     }
44
+
45
+    private void attachToJS() {
46
+        ReactInstanceManager react = NavigationApplication.instance.getNavigationReactInstance().getReactInstanceManager();
47
+        startReactApplication(react, screenId, mergePropsAndNavigationParams());
48
+    }
49
+
50
+    private Bundle mergePropsAndNavigationParams() {
51
+        if (passProps != null) {
52
+            navigationParams.putAll(passProps);
53
+        }
54
+        return navigationParams;
55
+    }
48
 }
56
 }