소스 검색

Assasinate LocalBroadcastReceiver in favor of a simple EventBus implementation

This is needed since some of the data we need to send in the events
can not be serialised and added to a Bundle.
Guy Carmeli 8 년 전
부모
커밋
d25ab7ce19

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/events/Event.java 파일 보기

@@ -0,0 +1,5 @@
1
+package com.reactnativenavigation.events;
2
+
3
+public interface Event {
4
+    String getType();
5
+}

+ 31
- 0
android/app/src/main/java/com/reactnativenavigation/events/EventBus.java 파일 보기

@@ -0,0 +1,31 @@
1
+package com.reactnativenavigation.events;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
6
+public enum EventBus {
7
+    instance;
8
+
9
+    List<Subscriber> subscribers;
10
+
11
+    EventBus() {
12
+        subscribers = new ArrayList<>();
13
+    }
14
+
15
+    public void register(Subscriber subscriber) {
16
+        if (subscribers.contains(subscriber)) {
17
+            throw new RuntimeException("Subscriber already registered");
18
+        }
19
+        subscribers.add(subscriber);
20
+    }
21
+
22
+    public void unregister(Subscriber subscriber) {
23
+        subscribers.remove(subscriber);
24
+    }
25
+
26
+    public void post(Event event) {
27
+        for (Subscriber subscriber : subscribers) {
28
+            subscriber.onEvent(event);
29
+        }
30
+    }
31
+}

+ 0
- 16
android/app/src/main/java/com/reactnativenavigation/events/LocalBroadcastEvent.java 파일 보기

@@ -1,16 +0,0 @@
1
-package com.reactnativenavigation.events;
2
-
3
-import android.content.Intent;
4
-import android.support.v4.content.LocalBroadcastManager;
5
-
6
-import com.reactnativenavigation.NavigationApplication;
7
-
8
-public abstract class LocalBroadcastEvent {
9
-
10
-    public abstract Intent getIntent();
11
-
12
-    public void send() {
13
-        LocalBroadcastManager.getInstance(NavigationApplication.instance).sendBroadcast(getIntent());
14
-    }
15
-
16
-}

+ 0
- 12
android/app/src/main/java/com/reactnativenavigation/events/ScreenChangeBroadcast.java 파일 보기

@@ -1,12 +0,0 @@
1
-package com.reactnativenavigation.events;
2
-
3
-import android.content.Intent;
4
-
5
-public class ScreenChangeBroadcast extends LocalBroadcastEvent {
6
-    public static final String ACTION = "screenChange";
7
-
8
-    @Override
9
-    public Intent getIntent() {
10
-        return new Intent(ACTION);
11
-    }
12
-}

+ 0
- 40
android/app/src/main/java/com/reactnativenavigation/events/ScreenChangeBroadcastReceiver.java 파일 보기

@@ -1,40 +0,0 @@
1
-package com.reactnativenavigation.events;
2
-
3
-import android.content.BroadcastReceiver;
4
-import android.content.Context;
5
-import android.content.Intent;
6
-import android.content.IntentFilter;
7
-import android.support.v4.content.LocalBroadcastManager;
8
-
9
-import com.reactnativenavigation.NavigationApplication;
10
-
11
-public class ScreenChangeBroadcastReceiver extends BroadcastReceiver {
12
-    private OnScreenChangeListener onTabSelectedListener;
13
-
14
-    public interface OnScreenChangeListener {
15
-        void onScreenChangeListener();
16
-    }
17
-
18
-    public ScreenChangeBroadcastReceiver(OnScreenChangeListener onTabSelectedListener) {
19
-        this.onTabSelectedListener = onTabSelectedListener;
20
-    }
21
-
22
-    @Override
23
-    public void onReceive(Context context, Intent intent) {
24
-        onTabSelectedListener.onScreenChangeListener();
25
-    }
26
-
27
-    public void register() {
28
-        IntentFilter intentFilter = new IntentFilter();
29
-        intentFilter.addAction(ScreenChangeBroadcast.ACTION);
30
-        getBroadcastManager().registerReceiver(this, intentFilter);
31
-    }
32
-
33
-    public void unregister() {
34
-        getBroadcastManager().unregisterReceiver(this);
35
-    }
36
-
37
-    private LocalBroadcastManager getBroadcastManager() {
38
-        return LocalBroadcastManager.getInstance(NavigationApplication.instance);
39
-    }
40
-}

+ 10
- 0
android/app/src/main/java/com/reactnativenavigation/events/ScreenChangedEvent.java 파일 보기

@@ -0,0 +1,10 @@
1
+package com.reactnativenavigation.events;
2
+
3
+public class ScreenChangedEvent implements Event {
4
+    public static final String TYPE = "ScreenChangedEvent";
5
+
6
+    @Override
7
+    public String getType() {
8
+        return TYPE;
9
+    }
10
+}

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/events/Subscriber.java 파일 보기

@@ -0,0 +1,5 @@
1
+package com.reactnativenavigation.events;
2
+
3
+public interface Subscriber {
4
+    void onEvent(Event event);
5
+}

+ 3
- 2
android/app/src/main/java/com/reactnativenavigation/screens/ContentViewPagerAdapter.java 파일 보기

@@ -7,7 +7,8 @@ import android.view.ViewGroup;
7 7
 import com.facebook.react.bridge.Arguments;
8 8
 import com.facebook.react.bridge.WritableMap;
9 9
 import com.reactnativenavigation.NavigationApplication;
10
-import com.reactnativenavigation.events.ScreenChangeBroadcast;
10
+import com.reactnativenavigation.events.EventBus;
11
+import com.reactnativenavigation.events.ScreenChangedEvent;
11 12
 import com.reactnativenavigation.params.TopTabParams;
12 13
 import com.reactnativenavigation.views.ContentView;
13 14
 
@@ -40,7 +41,7 @@ public class ContentViewPagerAdapter extends PagerAdapter {
40 41
     }
41 42
 
42 43
     private void sendScreenChangeBroadcast() {
43
-        new ScreenChangeBroadcast().send();
44
+        EventBus.instance.post(new ScreenChangedEvent());
44 45
     }
45 46
 
46 47
     private void sendTabSelectedEventToJs() {

+ 14
- 15
android/app/src/main/java/com/reactnativenavigation/views/SnackbarAndFabContainer.java 파일 보기

@@ -4,23 +4,20 @@ import android.content.Context;
4 4
 import android.support.annotation.NonNull;
5 5
 import android.support.design.widget.CoordinatorLayout;
6 6
 
7
-import com.reactnativenavigation.events.ScreenChangeBroadcastReceiver;
7
+import com.reactnativenavigation.events.Event;
8
+import com.reactnativenavigation.events.EventBus;
9
+import com.reactnativenavigation.events.ScreenChangedEvent;
10
+import com.reactnativenavigation.events.Subscriber;
8 11
 import com.reactnativenavigation.params.FabParams;
9 12
 import com.reactnativenavigation.params.SnackbarParams;
10 13
 
11
-public class SnackbarAndFabContainer extends CoordinatorLayout implements Snakbar.OnDismissListener, ScreenChangeBroadcastReceiver.OnScreenChangeListener {
14
+public class SnackbarAndFabContainer extends CoordinatorLayout implements Snakbar.OnDismissListener, Subscriber{
12 15
     private Snakbar snakbar;
13
-    private ScreenChangeBroadcastReceiver screenChangeBroadcastReceiver;
14 16
     private FloatingActionButtonCoordinator actionButtonCoordinator;
15 17
 
16 18
     public SnackbarAndFabContainer(Context context) {
17 19
         super(context);
18
-        registerTabSelectedReceiver();
19
-    }
20
-
21
-    private void registerTabSelectedReceiver() {
22
-        screenChangeBroadcastReceiver = new ScreenChangeBroadcastReceiver(this);
23
-        screenChangeBroadcastReceiver.register();
20
+        EventBus.instance.register(this);
24 21
     }
25 22
 
26 23
     public void showSnackbar(final String navigatorEventId, final SnackbarParams params) {
@@ -43,15 +40,17 @@ public class SnackbarAndFabContainer extends CoordinatorLayout implements Snakba
43 40
     }
44 41
 
45 42
     public void destroy() {
46
-        screenChangeBroadcastReceiver.unregister();
47
-    }
48
-
49
-    @Override
50
-    public void onScreenChangeListener() {
51
-        onScreenChange();
43
+        EventBus.instance.unregister(this);
52 44
     }
53 45
 
54 46
     public void showFab(@NonNull FabParams fabParams) {
55 47
         actionButtonCoordinator = new FloatingActionButtonCoordinator(this, fabParams);
56 48
     }
49
+
50
+    @Override
51
+    public void onEvent(Event event) {
52
+        if (event.getType() == ScreenChangedEvent.TYPE) {
53
+            onScreenChange();
54
+        }
55
+    }
57 56
 }

+ 3
- 1
src/deprecated/platformSpecificDeprecated.android.js 파일 보기

@@ -375,7 +375,9 @@ function getFab(screen) {
375 375
     const fab = screen.fab;
376 376
     debugger;
377 377
     fab.collapsedIcon = resolveAssetSource(fab.collapsedIcon).uri;
378
-    fab.expendedIcon = resolveAssetSource(fab.expendedIcon).uri;
378
+    if (fab.expendedIcon) {
379
+      fab.expendedIcon = resolveAssetSource(fab.expendedIcon).uri;
380
+    }
379 381
 
380 382
     if (fab.actions) {
381 383
       _.forEach(fab.actions, (action) => {