Browse Source

Wire up Android setTabBadge

Jason Skuby 8 years ago
parent
commit
abcbfdb5d7

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java View File

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

+ 15
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java View File

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
     @Override
208
     @Override
194
     public void switchToTab(ReadableMap params) {
209
     public void switchToTab(ReadableMap params) {
195
         int tabIndex = params.getInt(KEY_TAB_INDEX);
210
         int tabIndex = params.getInt(KEY_TAB_INDEX);

+ 14
- 0
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java View File

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
     @ReactMethod
137
     @ReactMethod
124
     public void switchToTab(final ReadableMap params) {
138
     public void switchToTab(final ReadableMap params) {
125
         final BaseReactActivity context = ContextProvider.getActivityContext();
139
         final BaseReactActivity context = ContextProvider.getActivityContext();

+ 8
- 0
src/platformSpecific.android.js View File

84
   RctActivity.navigatorResetTo(params);
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
 function navigatorSetTitle(navigator, params) {
94
 function navigatorSetTitle(navigator, params) {
88
   RctActivity.setNavigatorTitle(params);
95
   RctActivity.setNavigatorTitle(params);
89
 }
96
 }
157
   dismissModal,
164
   dismissModal,
158
   dismissAllModals,
165
   dismissAllModals,
159
   navigatorSetButtons,
166
   navigatorSetButtons,
167
+  navigatorSetTabBadge,
160
   navigatorSetTitle,
168
   navigatorSetTitle,
161
   navigatorSwitchToTab,
169
   navigatorSwitchToTab,
162
   navigatorToggleTabs
170
   navigatorToggleTabs