Browse Source

Added TabSelected event

Guy Carmeli 8 years ago
parent
commit
a2204761d9

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

5
 import android.support.v7.widget.Toolbar;
5
 import android.support.v7.widget.Toolbar;
6
 
6
 
7
 import com.reactnativenavigation.R;
7
 import com.reactnativenavigation.R;
8
-import com.reactnativenavigation.core.Screen;
9
 import com.reactnativenavigation.adapters.ViewPagerAdapter;
8
 import com.reactnativenavigation.adapters.ViewPagerAdapter;
10
 import com.reactnativenavigation.core.RctManager;
9
 import com.reactnativenavigation.core.RctManager;
10
+import com.reactnativenavigation.core.Screen;
11
 
11
 
12
 import java.util.ArrayList;
12
 import java.util.ArrayList;
13
 
13
 
40
 
40
 
41
     private void setupViews() {
41
     private void setupViews() {
42
         ArrayList<Screen> screens = (ArrayList<Screen>) getIntent().getSerializableExtra(EXTRA_SCREENS);
42
         ArrayList<Screen> screens = (ArrayList<Screen>) getIntent().getSerializableExtra(EXTRA_SCREENS);
43
-        ViewPagerAdapter adapter = new ViewPagerAdapter(this, screens);
43
+        ViewPagerAdapter adapter = new ViewPagerAdapter(this, mViewPager, screens);
44
         mViewPager.setAdapter(adapter);
44
         mViewPager.setAdapter(adapter);
45
         mTabLayout.setupWithViewPager(mViewPager);
45
         mTabLayout.setupWithViewPager(mViewPager);
46
+        mTabLayout.setOnTabSelectedListener(adapter);
46
         adapter.notifyDataSetChanged();
47
         adapter.notifyDataSetChanged();
47
     }
48
     }
48
 }
49
 }

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

1
 package com.reactnativenavigation.adapters;
1
 package com.reactnativenavigation.adapters;
2
 
2
 
3
+import android.support.design.widget.TabLayout;
3
 import android.support.v4.view.PagerAdapter;
4
 import android.support.v4.view.PagerAdapter;
5
+import android.support.v4.view.ViewPager;
4
 import android.view.View;
6
 import android.view.View;
5
 import android.view.ViewGroup;
7
 import android.view.ViewGroup;
6
 
8
 
7
 import com.facebook.react.ReactInstanceManager;
9
 import com.facebook.react.ReactInstanceManager;
10
+import com.facebook.react.bridge.Arguments;
11
+import com.facebook.react.bridge.WritableMap;
8
 import com.reactnativenavigation.activities.BaseReactActivity;
12
 import com.reactnativenavigation.activities.BaseReactActivity;
9
 import com.reactnativenavigation.core.RctManager;
13
 import com.reactnativenavigation.core.RctManager;
10
 import com.reactnativenavigation.core.Screen;
14
 import com.reactnativenavigation.core.Screen;
15
 /**
19
 /**
16
  * Created by guyc on 02/04/16.
20
  * Created by guyc on 02/04/16.
17
  */
21
  */
18
-public class ViewPagerAdapter extends PagerAdapter {
22
+public class ViewPagerAdapter extends PagerAdapter implements TabLayout.OnTabSelectedListener {
23
+
24
+    private static final String EVENT_ON_TAB_SELECTED = "OnTabSelected";
19
 
25
 
20
     private BaseReactActivity mContext;
26
     private BaseReactActivity mContext;
27
+    private ViewPager mViewPager;
21
     private final ArrayList<Screen> mScreens;
28
     private final ArrayList<Screen> mScreens;
22
     private final ReactInstanceManager mReactInstanceManager;
29
     private final ReactInstanceManager mReactInstanceManager;
23
 
30
 
24
-    public ViewPagerAdapter(BaseReactActivity context, ArrayList<Screen> screens) {
31
+    public ViewPagerAdapter(BaseReactActivity context, ViewPager viewPager, ArrayList<Screen> screens) {
25
         mContext = context;
32
         mContext = context;
33
+        mViewPager = viewPager;
26
         mScreens = screens;
34
         mScreens = screens;
27
         mReactInstanceManager = RctManager.getInstance().getReactInstanceManager();
35
         mReactInstanceManager = RctManager.getInstance().getReactInstanceManager();
28
     }
36
     }
54
     public CharSequence getPageTitle(int position) {
62
     public CharSequence getPageTitle(int position) {
55
         return mScreens.get(position).title;
63
         return mScreens.get(position).title;
56
     }
64
     }
65
+
66
+    @Override
67
+    public void onTabSelected(TabLayout.Tab tab) {
68
+        int position = tab.getPosition();
69
+        mViewPager.setCurrentItem(position);
70
+
71
+        WritableMap params = Arguments.createMap();
72
+        Screen screen = mScreens.get(position);
73
+        params.putString(Screen.KEY_NAVIGATOR_EVENT_ID, screen.navigatorEventId);
74
+
75
+        RctManager.getInstance().sendEvent(EVENT_ON_TAB_SELECTED, screen.navigatorEventId, params);
76
+    }
77
+
78
+    @Override
79
+    public void onTabUnselected(TabLayout.Tab tab) {
80
+
81
+    }
82
+
83
+    @Override
84
+    public void onTabReselected(TabLayout.Tab tab) {
85
+
86
+    }
57
 }
87
 }

+ 10
- 7
android/app/src/main/java/com/reactnativenavigation/core/RctManager.java View File

2
 
2
 
3
 import android.app.Application;
3
 import android.app.Application;
4
 import android.content.Context;
4
 import android.content.Context;
5
-import android.util.Log;
6
 
5
 
7
 import com.facebook.react.LifecycleState;
6
 import com.facebook.react.LifecycleState;
8
 import com.facebook.react.ReactInstanceManager;
7
 import com.facebook.react.ReactInstanceManager;
9
 import com.facebook.react.ReactPackage;
8
 import com.facebook.react.ReactPackage;
9
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
10
 
10
 
11
 import java.util.List;
11
 import java.util.List;
12
 
12
 
15
  */
15
  */
16
 public class RctManager {
16
 public class RctManager {
17
     private static final String TAG = "RctManager";
17
     private static final String TAG = "RctManager";
18
+    private static final String KEY_EVENT_ID = "id";
18
     private static RctManager sInstance;
19
     private static RctManager sInstance;
19
 
20
 
20
     private ReactInstanceManager mReactManager;
21
     private ReactInstanceManager mReactManager;
35
         return mReactManager;
36
         return mReactManager;
36
     }
37
     }
37
 
38
 
38
-    public void useLocalDevServer(boolean useLocalDevServer) {
39
-        mUseLocalDevServer = useLocalDevServer;
40
-    }
41
-
42
     public boolean isInitialized() {
39
     public boolean isInitialized() {
43
         return mReactManager != null;
40
         return mReactManager != null;
44
     }
41
     }
45
 
42
 
46
     public void init(Context context, String componentName, List<ReactPackage> packages) {
43
     public void init(Context context, String componentName, List<ReactPackage> packages) {
47
-        Log.i(TAG, "Initializing RctManager");
48
         createReactInstanceManager(context, componentName, packages);
44
         createReactInstanceManager(context, componentName, packages);
49
     }
45
     }
50
 
46
 
81
         mReactManager = builder.build();
77
         mReactManager = builder.build();
82
         return mReactManager;
78
         return mReactManager;
83
     }
79
     }
84
-}
85
 
80
 
81
+    public <T extends ReactContextBaseJavaModule> T getNativeModule(Class<T> nativeModuleClass) {
82
+        if (mReactManager == null || mReactManager.getCurrentReactContext() == null) {
83
+            return null;
84
+        }
85
+
86
+        return mReactManager.getCurrentReactContext().getNativeModule(nativeModuleClass);
87
+    }
88
+}

+ 0
- 42
android/app/src/main/java/com/reactnativenavigation/events/TabSelectedEvent.java View File

1
-package com.reactnativenavigation.events;
2
-
3
-import android.os.SystemClock;
4
-import android.util.Log;
5
-
6
-import com.facebook.react.bridge.Arguments;
7
-import com.facebook.react.bridge.WritableMap;
8
-import com.facebook.react.uimanager.events.Event;
9
-import com.facebook.react.uimanager.events.RCTEventEmitter;
10
-
11
-/**
12
- *
13
- * Created by guyc on 07/03/16.
14
- */
15
-public class TabSelectedEvent extends Event<TabSelectedEvent> {
16
-    public static final String EVENT_NAME = "tabSelected";
17
-    private static final String KEY_POSITION = "position";
18
-
19
-    private int mPosition;
20
-
21
-    public TabSelectedEvent(int viewTag, int position) {
22
-        super(viewTag, SystemClock.uptimeMillis());
23
-        mPosition = position;
24
-    }
25
-
26
-    @Override
27
-    public String getEventName() {
28
-        return EVENT_NAME;
29
-    }
30
-
31
-    @Override
32
-    public void dispatch(RCTEventEmitter rctEventEmitter) {
33
-        Log.d("GUY", "DIspatching event: " + getViewTag());
34
-        rctEventEmitter.receiveEvent(getViewTag(), EVENT_NAME, serializeData());
35
-    }
36
-
37
-    private WritableMap serializeData() {
38
-        WritableMap data = Arguments.createMap();
39
-        data.putInt(KEY_POSITION, mPosition);
40
-        return data;
41
-    }
42
-}

+ 2
- 3
android/app/src/main/java/com/reactnativenavigation/managers/TabLayoutManager.java View File

18
 import com.facebook.react.uimanager.ViewGroupManager;
18
 import com.facebook.react.uimanager.ViewGroupManager;
19
 import com.facebook.react.uimanager.annotations.ReactProp;
19
 import com.facebook.react.uimanager.annotations.ReactProp;
20
 import com.facebook.react.uimanager.events.EventDispatcher;
20
 import com.facebook.react.uimanager.events.EventDispatcher;
21
-import com.reactnativenavigation.events.TabSelectedEvent;
22
 import com.reactnativenavigation.layouts.ReactTabLayout;
21
 import com.reactnativenavigation.layouts.ReactTabLayout;
23
 import com.reactnativenavigation.layouts.ReactTabLayout.InitialState;
22
 import com.reactnativenavigation.layouts.ReactTabLayout.InitialState;
24
 import com.reactnativenavigation.utils.ResourceUtils;
23
 import com.reactnativenavigation.utils.ResourceUtils;
278
         public void onTabSelected(Tab tab) {
277
         public void onTabSelected(Tab tab) {
279
             super.onTabSelected(tab);
278
             super.onTabSelected(tab);
280
             int position = mTabLayout.indexOf(tab);
279
             int position = mTabLayout.indexOf(tab);
281
-            mTabLayoutManager.mDispatcher.dispatchEvent(new TabSelectedEvent(mTabLayout.getId(position), position));
282
-            mTabLayoutManager.mDispatcher.dispatchEvent(new TabSelectedEvent(mTabLayout.getId(), position));
280
+//            mTabLayoutManager.mDispatcher.dispatchEvent(new TabSelectedEvent(mTabLayout.getId(position), position));
281
+//            mTabLayoutManager.mDispatcher.dispatchEvent(new TabSelectedEvent(mTabLayout.getId(), position));
283
         }
282
         }
284
 
283
 
285
         @Override
284
         @Override

+ 27
- 2
src/Screen.js View File

1
-import { Component, NativeAppEventEmitter } from 'react-native';
1
+import {
2
+  Component,
3
+  NativeAppEventEmitter,
4
+  DeviceEventEmitter,
5
+  Platform
6
+} from 'react-native';
2
 import platformSpecific from './platformSpecific';
7
 import platformSpecific from './platformSpecific';
3
 import Navigation from './Navigation';
8
 import Navigation from './Navigation';
4
 
9
 
11
     this.navigatorEventHandler = null;
16
     this.navigatorEventHandler = null;
12
     this.navigatorEventSubscription = null;
17
     this.navigatorEventSubscription = null;
13
   }
18
   }
19
+
14
   push(params = {}) {
20
   push(params = {}) {
15
     return platformSpecific.navigatorPush(this, params);
21
     return platformSpecific.navigatorPush(this, params);
16
   }
22
   }
23
+
17
   pop(params = {}) {
24
   pop(params = {}) {
18
     return platformSpecific.navigatorPop(this, params);
25
     return platformSpecific.navigatorPop(this, params);
19
   }
26
   }
27
+
20
   popToRoot(params = {}) {
28
   popToRoot(params = {}) {
21
     return platformSpecific.navigatorPopToRoot(this, params);
29
     return platformSpecific.navigatorPopToRoot(this, params);
22
   }
30
   }
31
+
23
   resetTo(params = {}) {
32
   resetTo(params = {}) {
24
     return platformSpecific.navigatorResetTo(this, params);
33
     return platformSpecific.navigatorResetTo(this, params);
25
   }
34
   }
35
+
26
   showModal(params = {}) {
36
   showModal(params = {}) {
27
     return Navigation.showModal(params);
37
     return Navigation.showModal(params);
28
   }
38
   }
39
+
29
   dismissModal(params = {}) {
40
   dismissModal(params = {}) {
30
     return Navigation.dismissModal(params);
41
     return Navigation.dismissModal(params);
31
   }
42
   }
43
+
32
   showLightBox(params = {}) {
44
   showLightBox(params = {}) {
33
     return Navigation.showLightBox(params);
45
     return Navigation.showLightBox(params);
34
   }
46
   }
47
+
35
   dismissLightBox(params = {}) {
48
   dismissLightBox(params = {}) {
36
     return Navigation.dismissLightBox(params);
49
     return Navigation.dismissLightBox(params);
37
   }
50
   }
51
+
38
   setButtons(params = {}) {
52
   setButtons(params = {}) {
39
     return platformSpecific.navigatorSetButtons(this, this.navigatorEventID, params);
53
     return platformSpecific.navigatorSetButtons(this, this.navigatorEventID, params);
40
   }
54
   }
55
+
41
   setTitle(params = {}) {
56
   setTitle(params = {}) {
42
     return platformSpecific.navigatorSetTitle(this, params);
57
     return platformSpecific.navigatorSetTitle(this, params);
43
   }
58
   }
59
+
44
   toggleDrawer(params = {}) {
60
   toggleDrawer(params = {}) {
45
     return platformSpecific.navigatorToggleDrawer(this, params);
61
     return platformSpecific.navigatorToggleDrawer(this, params);
46
   }
62
   }
63
+
47
   toggleTabs(params = {}) {
64
   toggleTabs(params = {}) {
48
     return platformSpecific.navigatorToggleTabs(this, params);
65
     return platformSpecific.navigatorToggleTabs(this, params);
49
   }
66
   }
67
+
50
   setTabBadge(params = {}) {
68
   setTabBadge(params = {}) {
51
     return platformSpecific.navigatorSetTabBadge(this, params);
69
     return platformSpecific.navigatorSetTabBadge(this, params);
52
   }
70
   }
71
+
53
   switchToTab(params = {}) {
72
   switchToTab(params = {}) {
54
     return platformSpecific.navigatorSwitchToTab(this, params);
73
     return platformSpecific.navigatorSwitchToTab(this, params);
55
   }
74
   }
75
+
56
   setOnNavigatorEvent(callback) {
76
   setOnNavigatorEvent(callback) {
57
     this.navigatorEventHandler = callback;
77
     this.navigatorEventHandler = callback;
58
     if (!this.navigatorEventSubscription) {
78
     if (!this.navigatorEventSubscription) {
59
-      this.navigatorEventSubscription = NativeAppEventEmitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
79
+      let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
80
+      this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
60
       _allNavigatorEventHandlers[this.navigatorEventID] = (event) => this.onNavigatorEvent(event);
81
       _allNavigatorEventHandlers[this.navigatorEventID] = (event) => this.onNavigatorEvent(event);
61
     }
82
     }
62
   }
83
   }
70
       _allNavigatorEventHandlers[i](event);
91
       _allNavigatorEventHandlers[i](event);
71
     }
92
     }
72
   }
93
   }
94
+
73
   onNavigatorEvent(event) {
95
   onNavigatorEvent(event) {
74
     if (this.navigatorEventHandler) {
96
     if (this.navigatorEventHandler) {
75
       this.navigatorEventHandler(event);
97
       this.navigatorEventHandler(event);
76
     }
98
     }
77
   }
99
   }
100
+
78
   cleanup() {
101
   cleanup() {
79
     if (this.navigatorEventSubscription) {
102
     if (this.navigatorEventSubscription) {
80
       this.navigatorEventSubscription.remove();
103
       this.navigatorEventSubscription.remove();
86
 export default class Screen extends Component {
109
 export default class Screen extends Component {
87
   static navigatorStyle = {};
110
   static navigatorStyle = {};
88
   static navigatorButtons = {};
111
   static navigatorButtons = {};
112
+  
89
   constructor(props) {
113
   constructor(props) {
90
     super(props);
114
     super(props);
91
     if (props.navigatorID) {
115
     if (props.navigatorID) {
92
       this.navigator = new Navigator(props.navigatorID, props.navigatorEventID);
116
       this.navigator = new Navigator(props.navigatorID, props.navigatorEventID);
93
     }
117
     }
94
   }
118
   }
119
+
95
   componentWillUnmount() {
120
   componentWillUnmount() {
96
     if (this.navigator) {
121
     if (this.navigator) {
97
       this.navigator.cleanup();
122
       this.navigator.cleanup();