Selaa lähdekoodia

Added TabSelected event

Guy Carmeli 8 vuotta sitten
vanhempi
commit
a2204761d9

+ 3
- 2
android/app/src/main/java/com/reactnativenavigation/activities/TabActivity.java Näytä tiedosto

@@ -5,9 +5,9 @@ import android.support.v4.view.ViewPager;
5 5
 import android.support.v7.widget.Toolbar;
6 6
 
7 7
 import com.reactnativenavigation.R;
8
-import com.reactnativenavigation.core.Screen;
9 8
 import com.reactnativenavigation.adapters.ViewPagerAdapter;
10 9
 import com.reactnativenavigation.core.RctManager;
10
+import com.reactnativenavigation.core.Screen;
11 11
 
12 12
 import java.util.ArrayList;
13 13
 
@@ -40,9 +40,10 @@ public class TabActivity extends BaseReactActivity {
40 40
 
41 41
     private void setupViews() {
42 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 44
         mViewPager.setAdapter(adapter);
45 45
         mTabLayout.setupWithViewPager(mViewPager);
46
+        mTabLayout.setOnTabSelectedListener(adapter);
46 47
         adapter.notifyDataSetChanged();
47 48
     }
48 49
 }

+ 32
- 2
android/app/src/main/java/com/reactnativenavigation/adapters/ViewPagerAdapter.java Näytä tiedosto

@@ -1,10 +1,14 @@
1 1
 package com.reactnativenavigation.adapters;
2 2
 
3
+import android.support.design.widget.TabLayout;
3 4
 import android.support.v4.view.PagerAdapter;
5
+import android.support.v4.view.ViewPager;
4 6
 import android.view.View;
5 7
 import android.view.ViewGroup;
6 8
 
7 9
 import com.facebook.react.ReactInstanceManager;
10
+import com.facebook.react.bridge.Arguments;
11
+import com.facebook.react.bridge.WritableMap;
8 12
 import com.reactnativenavigation.activities.BaseReactActivity;
9 13
 import com.reactnativenavigation.core.RctManager;
10 14
 import com.reactnativenavigation.core.Screen;
@@ -15,14 +19,18 @@ import java.util.ArrayList;
15 19
 /**
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 26
     private BaseReactActivity mContext;
27
+    private ViewPager mViewPager;
21 28
     private final ArrayList<Screen> mScreens;
22 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 32
         mContext = context;
33
+        mViewPager = viewPager;
26 34
         mScreens = screens;
27 35
         mReactInstanceManager = RctManager.getInstance().getReactInstanceManager();
28 36
     }
@@ -54,4 +62,26 @@ public class ViewPagerAdapter extends PagerAdapter {
54 62
     public CharSequence getPageTitle(int position) {
55 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 Näytä tiedosto

@@ -2,11 +2,11 @@ package com.reactnativenavigation.core;
2 2
 
3 3
 import android.app.Application;
4 4
 import android.content.Context;
5
-import android.util.Log;
6 5
 
7 6
 import com.facebook.react.LifecycleState;
8 7
 import com.facebook.react.ReactInstanceManager;
9 8
 import com.facebook.react.ReactPackage;
9
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
10 10
 
11 11
 import java.util.List;
12 12
 
@@ -15,6 +15,7 @@ import java.util.List;
15 15
  */
16 16
 public class RctManager {
17 17
     private static final String TAG = "RctManager";
18
+    private static final String KEY_EVENT_ID = "id";
18 19
     private static RctManager sInstance;
19 20
 
20 21
     private ReactInstanceManager mReactManager;
@@ -35,16 +36,11 @@ public class RctManager {
35 36
         return mReactManager;
36 37
     }
37 38
 
38
-    public void useLocalDevServer(boolean useLocalDevServer) {
39
-        mUseLocalDevServer = useLocalDevServer;
40
-    }
41
-
42 39
     public boolean isInitialized() {
43 40
         return mReactManager != null;
44 41
     }
45 42
 
46 43
     public void init(Context context, String componentName, List<ReactPackage> packages) {
47
-        Log.i(TAG, "Initializing RctManager");
48 44
         createReactInstanceManager(context, componentName, packages);
49 45
     }
50 46
 
@@ -81,5 +77,12 @@ public class RctManager {
81 77
         mReactManager = builder.build();
82 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 Näytä tiedosto

@@ -1,42 +0,0 @@
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 Näytä tiedosto

@@ -18,7 +18,6 @@ import com.facebook.react.uimanager.UIManagerModule;
18 18
 import com.facebook.react.uimanager.ViewGroupManager;
19 19
 import com.facebook.react.uimanager.annotations.ReactProp;
20 20
 import com.facebook.react.uimanager.events.EventDispatcher;
21
-import com.reactnativenavigation.events.TabSelectedEvent;
22 21
 import com.reactnativenavigation.layouts.ReactTabLayout;
23 22
 import com.reactnativenavigation.layouts.ReactTabLayout.InitialState;
24 23
 import com.reactnativenavigation.utils.ResourceUtils;
@@ -278,8 +277,8 @@ public class TabLayoutManager extends ViewGroupManager<ReactTabLayout> {
278 277
         public void onTabSelected(Tab tab) {
279 278
             super.onTabSelected(tab);
280 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 284
         @Override

+ 27
- 2
src/Screen.js Näytä tiedosto

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