Quellcode durchsuchen

Fix buttons not getting set in Modals

* Due to race condition when showing modal, buttons were added to
previous Toolbar.

* Minor refactor, moved `updateStyles` from BaseReactActivity to
StyleHelper class since it's now also used by RnnModal
Guy Carmeli vor 8 Jahren
Ursprung
Commit
daee13f55a

+ 17
- 22
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java Datei anzeigen

@@ -25,9 +25,11 @@ import com.facebook.react.common.ReactConstants;
25 25
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
26 26
 import com.facebook.react.shell.MainReactPackage;
27 27
 import com.reactnativenavigation.BuildConfig;
28
+import com.reactnativenavigation.controllers.ModalController;
28 29
 import com.reactnativenavigation.core.RctManager;
29 30
 import com.reactnativenavigation.core.objects.Button;
30 31
 import com.reactnativenavigation.core.objects.Screen;
32
+import com.reactnativenavigation.modal.RnnModal;
31 33
 import com.reactnativenavigation.packages.RnnPackage;
32 34
 import com.reactnativenavigation.utils.ContextProvider;
33 35
 import com.reactnativenavigation.utils.StyleHelper;
@@ -172,14 +174,6 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
172 174
         setContentView(mReactRootView);
173 175
     }
174 176
 
175
-    public void setNavigationStyle(Screen screen) {
176
-        if (mToolbar != null) {
177
-            mToolbar.setStyle(screen);
178
-        }
179
-
180
-        StyleHelper.setWindowStyle(getWindow(), this, screen);
181
-    }
182
-
183 177
     @Override
184 178
     protected void onResume() {
185 179
         super.onResume();
@@ -190,16 +184,6 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
190 184
         }
191 185
     }
192 186
 
193
-    public void updateStyles() {
194
-        try {
195
-            mToolbar.update(getCurrentScreen());
196
-            setNavigationStyle(getCurrentScreen());
197
-            mToolbar.setupToolbarButtonsAsync(getCurrentScreen());
198
-        } catch (Exception e) {
199
-            Log.w("RNNavigation", "Tried to update styles with no screen!");
200
-        }
201
-    }
202
-
203 187
     @Override
204 188
     protected void onPause() {
205 189
         super.onPause();
@@ -228,7 +212,7 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
228 212
 
229 213
     @CallSuper
230 214
     public void push(Screen screen) {
231
-        setNavigationStyle(screen);
215
+        StyleHelper.updateStyles(mToolbar, screen);
232 216
         if (mToolbar != null) {
233 217
             mToolbar.update(screen);
234 218
 
@@ -251,15 +235,26 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
251 235
 
252 236
     protected abstract String getCurrentNavigatorId();
253 237
 
254
-    protected abstract Screen getCurrentScreen();
238
+    @CallSuper
239
+    protected Screen getCurrentScreen() {
240
+        ModalController modalController = ModalController.getInstance();
241
+        if (modalController.isModalDisplayed()) {
242
+            RnnModal modal = modalController.get();
243
+            assert modal != null;
244
+            return modal.getCurrentScreen();
245
+        }
246
+
247
+        return null;
248
+    }
255 249
 
256 250
     public abstract int getScreenStackSize();
257 251
 
258 252
     @Override
259 253
     public boolean onCreateOptionsMenu(Menu menu) {
260 254
         mMenu = menu;
261
-        if (mToolbar != null) {
262
-            mToolbar.setupToolbarButtonsAsync(getCurrentScreen());
255
+        Screen currentScreen = getCurrentScreen();
256
+        if (mToolbar != null && currentScreen != null) {
257
+            mToolbar.setupToolbarButtonsAsync(currentScreen);
263 258
         }
264 259
         return super.onCreateOptionsMenu(menu);
265 260
     }

+ 17
- 9
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java Datei anzeigen

@@ -12,6 +12,7 @@ import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
12 12
 import com.reactnativenavigation.R;
13 13
 import com.reactnativenavigation.core.RctManager;
14 14
 import com.reactnativenavigation.core.objects.Screen;
15
+import com.reactnativenavigation.utils.StyleHelper;
15 16
 import com.reactnativenavigation.views.RnnToolBar;
16 17
 import com.reactnativenavigation.views.ScreenStack;
17 18
 
@@ -59,21 +60,21 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
59 60
     }
60 61
 
61 62
     private void setupPages(ArrayList<Screen> screens) {
62
-        new SetupTabsTask(this, screens).execute();
63
+        new SetupTabsTask(this, mToolbar, screens).execute();
63 64
     }
64 65
 
65 66
     private void setupToolbar(ArrayList<Screen> screens) {
66 67
         mToolbar.setScreens(screens);
67 68
         Screen initialScreen = screens.get(0);
68 69
         mToolbar.update(initialScreen);
69
-        setNavigationStyle(initialScreen);
70
+        StyleHelper.updateStyles(mToolbar, initialScreen);
70 71
     }
71 72
 
72 73
     @Override
73 74
     protected void onResume() {
74 75
         super.onResume();
75 76
         if(mScreenStacks != null) {
76
-            updateStyles();
77
+            StyleHelper.updateStyles(mToolbar, getCurrentScreen());
77 78
         }
78 79
     }
79 80
 
@@ -109,7 +110,7 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
109 110
                 stack.push(screen);
110 111
             }
111 112
         }
112
-        updateStyles();
113
+        StyleHelper.updateStyles(mToolbar, getCurrentScreen());
113 114
     }
114 115
 
115 116
     @Override
@@ -118,7 +119,7 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
118 119
         for (ScreenStack stack: mScreenStacks) {
119 120
             if (stack.peek().navigatorId.equals(navigatorId)) {
120 121
                 Screen popped = stack.pop();
121
-                updateStyles();
122
+                StyleHelper.updateStyles(mToolbar, getCurrentScreen());
122 123
                 return popped;
123 124
             }
124 125
         }
@@ -127,7 +128,12 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
127 128
 
128 129
     @Override
129 130
     protected Screen getCurrentScreen() {
130
-        return mScreenStacks.get(mCurrentStackPosition).peek();
131
+        Screen currentScreen = super.getCurrentScreen();
132
+        if (currentScreen != null) {
133
+            return currentScreen;
134
+        }
135
+
136
+        return mScreenStacks != null ? mScreenStacks.get(mCurrentStackPosition).peek() : null;
131 137
     }
132 138
 
133 139
     @Override
@@ -148,15 +154,17 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
148 154
         mContentFrame.removeAllViews();
149 155
         mContentFrame.addView(mScreenStacks.get(position), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
150 156
         mCurrentStackPosition = position;
151
-        updateStyles();
157
+        StyleHelper.updateStyles(mToolbar, getCurrentScreen());
152 158
     }
153 159
 
154 160
     private static class SetupTabsTask extends AsyncTask<Void, Void, Map<Screen, Drawable>> {
155 161
         private BottomTabActivity mActivity;
162
+        private RnnToolBar mToolBar;
156 163
         private ArrayList<Screen> mScreens;
157 164
 
158
-        public SetupTabsTask(BottomTabActivity context, ArrayList<Screen> screens) {
165
+        public SetupTabsTask(BottomTabActivity context, RnnToolBar toolBar, ArrayList<Screen> screens) {
159 166
             mActivity = context;
167
+            mToolBar = toolBar;
160 168
             mScreens = screens;
161 169
         }
162 170
 
@@ -174,7 +182,7 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
174 182
         @Override
175 183
         protected void onPostExecute(Map<Screen, Drawable> icons) {
176 184
             mActivity.setTabsWithIcons(mScreens, icons);
177
-            mActivity.updateStyles();
185
+            StyleHelper.updateStyles(mToolBar, mActivity.getCurrentScreen());
178 186
         }
179 187
     }
180 188
 

+ 10
- 3
android/app/src/main/java/com/reactnativenavigation/activities/SingleScreenActivity.java Datei anzeigen

@@ -5,6 +5,7 @@ import android.widget.FrameLayout;
5 5
 import com.reactnativenavigation.R;
6 6
 import com.reactnativenavigation.core.RctManager;
7 7
 import com.reactnativenavigation.core.objects.Screen;
8
+import com.reactnativenavigation.utils.StyleHelper;
8 9
 import com.reactnativenavigation.views.RnnToolBar;
9 10
 import com.reactnativenavigation.views.ScreenStack;
10 11
 
@@ -31,27 +32,28 @@ public class SingleScreenActivity extends BaseReactActivity {
31 32
 
32 33
         mScreenStack = new ScreenStack(this);
33 34
         FrameLayout contentFrame = (FrameLayout) findViewById(R.id.contentFrame);
35
+        assert contentFrame != null;
34 36
         contentFrame.addView(mScreenStack);
35 37
         mScreenStack.push(screen);
36 38
     }
37 39
 
38 40
     protected void setupToolbar(Screen screen) {
39 41
         mToolbar.update(screen);
40
-        setNavigationStyle(screen);
42
+        StyleHelper.updateStyles(mToolbar, screen);
41 43
     }
42 44
     
43 45
     @Override
44 46
     public void push(Screen screen) {
45 47
         super.push(screen);
46 48
         mScreenStack.push(screen);
47
-        updateStyles();
49
+        StyleHelper.updateStyles(mToolbar, screen);
48 50
     }
49 51
 
50 52
     @Override
51 53
     public Screen pop(String navigatorId) {
52 54
         super.pop(navigatorId);
53 55
         Screen screen = mScreenStack.pop();
54
-        updateStyles();
56
+        StyleHelper.updateStyles(mToolbar, screen);
55 57
         return screen;
56 58
     }
57 59
 
@@ -62,6 +64,11 @@ public class SingleScreenActivity extends BaseReactActivity {
62 64
 
63 65
     @Override
64 66
     protected Screen getCurrentScreen() {
67
+        Screen currentScreen = super.getCurrentScreen();
68
+        if (currentScreen != null) {
69
+            return currentScreen;
70
+        }
71
+
65 72
         return mScreenStack.peek();
66 73
     }
67 74
 

+ 0
- 2
android/app/src/main/java/com/reactnativenavigation/activities/TabActivity.java Datei anzeigen

@@ -46,9 +46,7 @@ public class TabActivity extends BaseReactActivity {
46 46
         setNavigationStyle(initialScreen);
47 47
     }
48 48
 
49
-    @Override
50 49
     public void setNavigationStyle(Screen screen) {
51
-        super.setNavigationStyle(screen);
52 50
         mTabLayout.setStyle(screen);
53 51
     }
54 52
 

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/adapters/ViewPagerAdapter.java Datei anzeigen

@@ -11,7 +11,7 @@ import com.facebook.react.bridge.WritableMap;
11 11
 import com.reactnativenavigation.activities.BaseReactActivity;
12 12
 import com.reactnativenavigation.core.RctManager;
13 13
 import com.reactnativenavigation.core.objects.Screen;
14
-import com.reactnativenavigation.utils.ContextProvider;
14
+import com.reactnativenavigation.utils.StyleHelper;
15 15
 import com.reactnativenavigation.views.RnnToolBar;
16 16
 import com.reactnativenavigation.views.ScreenStack;
17 17
 
@@ -111,7 +111,7 @@ public class ViewPagerAdapter extends PagerAdapter implements TabLayout.OnTabSel
111 111
         mToolbar.setTitle(newScreen.title == null ? "" : newScreen.title);
112 112
 
113 113
         // Set navigation color
114
-        ContextProvider.getActivityContext().setNavigationStyle(newScreen);
114
+        StyleHelper.updateStyles(mToolbar, newScreen);
115 115
 
116 116
         // Send tab selected event
117 117
         WritableMap params = Arguments.createMap();

+ 8
- 2
android/app/src/main/java/com/reactnativenavigation/modal/RnnModal.java Datei anzeigen

@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
4 4
 import android.app.Dialog;
5 5
 import android.content.Context;
6 6
 import android.content.DialogInterface;
7
+import android.support.annotation.Nullable;
7 8
 import android.view.LayoutInflater;
8 9
 import android.view.View;
9 10
 import android.view.Window;
@@ -15,7 +16,6 @@ import com.reactnativenavigation.R;
15 16
 import com.reactnativenavigation.activities.BaseReactActivity;
16 17
 import com.reactnativenavigation.controllers.ModalController;
17 18
 import com.reactnativenavigation.core.objects.Screen;
18
-import com.reactnativenavigation.utils.ContextProvider;
19 19
 import com.reactnativenavigation.utils.SdkSupports;
20 20
 import com.reactnativenavigation.utils.StyleHelper;
21 21
 import com.reactnativenavigation.views.RctView;
@@ -73,9 +73,15 @@ public class RnnModal extends Dialog implements DialogInterface.OnDismissListene
73 73
         if (mScreenStack.isEmpty()) {
74 74
             dismiss();
75 75
         }
76
+        mToolBar.update(popped);
76 77
         return popped;
77 78
     }
78 79
 
80
+    @Nullable
81
+    public Screen getCurrentScreen() {
82
+        return mScreenStack.isEmpty() ? null : mScreenStack.peek();
83
+    }
84
+
79 85
     @Override
80 86
     public void onBackPressed() {
81 87
         pop();
@@ -84,6 +90,6 @@ public class RnnModal extends Dialog implements DialogInterface.OnDismissListene
84 90
     @Override
85 91
     public void onDismiss(DialogInterface dialog) {
86 92
         ModalController.getInstance().remove();
87
-        ContextProvider.getActivityContext().updateStyles();
93
+        StyleHelper.updateStyles(mToolBar, getCurrentScreen());
88 94
     }
89 95
 }

+ 21
- 0
android/app/src/main/java/com/reactnativenavigation/utils/StyleHelper.java Datei anzeigen

@@ -2,14 +2,35 @@ package com.reactnativenavigation.utils;
2 2
 
3 3
 import android.content.Context;
4 4
 import android.support.v4.content.ContextCompat;
5
+import android.util.Log;
5 6
 import android.view.Window;
6 7
 
8
+import com.reactnativenavigation.activities.BaseReactActivity;
7 9
 import com.reactnativenavigation.core.objects.Screen;
10
+import com.reactnativenavigation.views.RnnToolBar;
8 11
 
9 12
 /**
10 13
  * Created by guyc on 07/05/16.
11 14
  */
12 15
 public class StyleHelper {
16
+
17
+    public static void updateStyles(RnnToolBar toolBar, Screen screen) {
18
+        try {
19
+            toolBar.updateAndSetButtons(screen);
20
+            setWindowStyle(screen);
21
+            toolBar.setupToolbarButtonsAsync(screen);
22
+        } catch (Exception e) {
23
+            Log.w("RNNavigation", "Tried to update styles with no screen!");
24
+        }
25
+    }
26
+
27
+    private static void setWindowStyle(Screen screen) {
28
+        BaseReactActivity context = ContextProvider.getActivityContext();
29
+        if (context != null) {
30
+            StyleHelper.setWindowStyle(context.getWindow(), context, screen);
31
+        }
32
+    }
33
+
13 34
     public static void setWindowStyle(Window window, Context context, Screen screen) {
14 35
         if (SdkSupports.lollipop()) {
15 36
             final int black = ContextCompat.getColor(context, android.R.color.black);

+ 8
- 3
android/app/src/main/java/com/reactnativenavigation/views/RnnToolBar.java Datei anzeigen

@@ -157,6 +157,10 @@ public class RnnToolBar extends Toolbar {
157 157
         ((AppCompatActivity) getContext()).setSupportActionBar(this);
158 158
         setTitle(screen.title);
159 159
         setStyle(screen);
160
+    }
161
+
162
+    public void updateAndSetButtons(Screen screen) {
163
+        update(screen);
160 164
         setupToolbarButtonsAsync(screen);
161 165
     }
162 166
 
@@ -210,8 +214,7 @@ public class RnnToolBar extends Toolbar {
210 214
             // Remove prev screen buttons
211 215
             if(mOldButtons == null) {
212 216
                 menu.clear();
213
-            }
214
-            else {
217
+            } else {
215 218
                 for (Button btn : mOldButtons) {
216 219
                     menu.removeItem(btn.getItemId());
217 220
                 }
@@ -222,12 +225,14 @@ public class RnnToolBar extends Toolbar {
222 225
             for (i = 0; i < mNewButtons.size(); i++) {
223 226
                 Button button = mNewButtons.get(i);
224 227
                 MenuItem item = menu.add(Menu.NONE, button.getItemId(), i, button.title);
228
+                item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
229
+
225 230
                 if (icons.containsKey(button.id)) {
226 231
                     Drawable icon = icons.get(button.id);
227 232
                     if (mTintColor != null) {
228 233
                         ImageUtils.tint(icon, mTintColor);
229 234
                     }
230
-                    item.setIcon(icon).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
235
+                    item.setIcon(icon);
231 236
                 }
232 237
             }
233 238