瀏覽代碼

Wire up Android setTabBadge

Jason Skuby 8 年之前
父節點
當前提交
abcbfdb5d7

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java 查看文件

@@ -46,6 +46,7 @@ import javax.annotation.Nullable;
46 46
 public abstract class BaseReactActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {
47 47
 
48 48
     protected static final String KEY_ANIMATED = "animated";
49
+    protected static final String KEY_BADGE = "badge";
49 50
     protected static final String KEY_HIDDEN = "hidden";
50 51
     protected static final String KEY_TAB_INDEX = "tabIndex";
51 52
     protected static final String KEY_TITLE = "title";
@@ -370,6 +371,10 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
370 371
         mToolbar.setTitle(title.getString(KEY_TITLE));
371 372
     }
372 373
 
374
+    public void setTabBadge(ReadableMap params) {
375
+        return;
376
+    }
377
+
373 378
     public void switchToTab(ReadableMap params) {
374 379
         return;
375 380
     }

+ 15
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java 查看文件

@@ -190,6 +190,21 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
190 190
         }
191 191
     }
192 192
 
193
+    @Override
194
+    public void setTabBadge(ReadableMap params) {
195
+        // Badge comes across as int, but if it's 0 clear the notification
196
+        int badgeCount = params.getInt(KEY_BADGE);
197
+        String badge = (badgeCount > 0) ? Integer.toString(badgeCount) : "";
198
+
199
+        // Tab index is optional, so default to current tab
200
+        int tabIndex = mBottomNavigation.getCurrentItem();
201
+        if (params.hasKey(KEY_TAB_INDEX)) {
202
+            tabIndex = params.getInt(KEY_TAB_INDEX);
203
+        }
204
+
205
+        mBottomNavigation.setNotification(badge, tabIndex);
206
+    }
207
+
193 208
     @Override
194 209
     public void switchToTab(ReadableMap params) {
195 210
         int tabIndex = params.getInt(KEY_TAB_INDEX);

+ 14
- 0
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java 查看文件

@@ -120,6 +120,20 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
120 120
         });
121 121
     }
122 122
 
123
+    @ReactMethod
124
+    public void setTabBadge(final ReadableMap params) {
125
+        final BaseReactActivity context = ContextProvider.getActivityContext();
126
+        if (context == null || context.isFinishing()) {
127
+            return;
128
+        }
129
+        context.runOnUiThread(new Runnable() {
130
+            @Override
131
+            public void run() {
132
+                context.setTabBadge(params);
133
+            }
134
+        });
135
+    }
136
+
123 137
     @ReactMethod
124 138
     public void switchToTab(final ReadableMap params) {
125 139
         final BaseReactActivity context = ContextProvider.getActivityContext();

+ 8
- 0
src/platformSpecific.android.js 查看文件

@@ -84,6 +84,13 @@ function navigatorResetTo(navigator, params) {
84 84
   RctActivity.navigatorResetTo(params);
85 85
 }
86 86
 
87
+function navigatorSetTabBadge(navigator, params) {
88
+  RctActivity.setTabBadge({
89
+    tabIndex: params.tabIndex,
90
+    badge: params.badge
91
+  });
92
+}
93
+
87 94
 function navigatorSetTitle(navigator, params) {
88 95
   RctActivity.setNavigatorTitle(params);
89 96
 }
@@ -157,6 +164,7 @@ export default {
157 164
   dismissModal,
158 165
   dismissAllModals,
159 166
   navigatorSetButtons,
167
+  navigatorSetTabBadge,
160 168
   navigatorSetTitle,
161 169
   navigatorSwitchToTab,
162 170
   navigatorToggleTabs