Procházet zdrojové kódy

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 před 8 roky
rodič
revize
daee13f55a

+ 17
- 22
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java Zobrazit soubor

25
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
25
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
26
 import com.facebook.react.shell.MainReactPackage;
26
 import com.facebook.react.shell.MainReactPackage;
27
 import com.reactnativenavigation.BuildConfig;
27
 import com.reactnativenavigation.BuildConfig;
28
+import com.reactnativenavigation.controllers.ModalController;
28
 import com.reactnativenavigation.core.RctManager;
29
 import com.reactnativenavigation.core.RctManager;
29
 import com.reactnativenavigation.core.objects.Button;
30
 import com.reactnativenavigation.core.objects.Button;
30
 import com.reactnativenavigation.core.objects.Screen;
31
 import com.reactnativenavigation.core.objects.Screen;
32
+import com.reactnativenavigation.modal.RnnModal;
31
 import com.reactnativenavigation.packages.RnnPackage;
33
 import com.reactnativenavigation.packages.RnnPackage;
32
 import com.reactnativenavigation.utils.ContextProvider;
34
 import com.reactnativenavigation.utils.ContextProvider;
33
 import com.reactnativenavigation.utils.StyleHelper;
35
 import com.reactnativenavigation.utils.StyleHelper;
172
         setContentView(mReactRootView);
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
     @Override
177
     @Override
184
     protected void onResume() {
178
     protected void onResume() {
185
         super.onResume();
179
         super.onResume();
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
     @Override
187
     @Override
204
     protected void onPause() {
188
     protected void onPause() {
205
         super.onPause();
189
         super.onPause();
228
 
212
 
229
     @CallSuper
213
     @CallSuper
230
     public void push(Screen screen) {
214
     public void push(Screen screen) {
231
-        setNavigationStyle(screen);
215
+        StyleHelper.updateStyles(mToolbar, screen);
232
         if (mToolbar != null) {
216
         if (mToolbar != null) {
233
             mToolbar.update(screen);
217
             mToolbar.update(screen);
234
 
218
 
251
 
235
 
252
     protected abstract String getCurrentNavigatorId();
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
     public abstract int getScreenStackSize();
250
     public abstract int getScreenStackSize();
257
 
251
 
258
     @Override
252
     @Override
259
     public boolean onCreateOptionsMenu(Menu menu) {
253
     public boolean onCreateOptionsMenu(Menu menu) {
260
         mMenu = menu;
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
         return super.onCreateOptionsMenu(menu);
259
         return super.onCreateOptionsMenu(menu);
265
     }
260
     }

+ 17
- 9
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java Zobrazit soubor

12
 import com.reactnativenavigation.R;
12
 import com.reactnativenavigation.R;
13
 import com.reactnativenavigation.core.RctManager;
13
 import com.reactnativenavigation.core.RctManager;
14
 import com.reactnativenavigation.core.objects.Screen;
14
 import com.reactnativenavigation.core.objects.Screen;
15
+import com.reactnativenavigation.utils.StyleHelper;
15
 import com.reactnativenavigation.views.RnnToolBar;
16
 import com.reactnativenavigation.views.RnnToolBar;
16
 import com.reactnativenavigation.views.ScreenStack;
17
 import com.reactnativenavigation.views.ScreenStack;
17
 
18
 
59
     }
60
     }
60
 
61
 
61
     private void setupPages(ArrayList<Screen> screens) {
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
     private void setupToolbar(ArrayList<Screen> screens) {
66
     private void setupToolbar(ArrayList<Screen> screens) {
66
         mToolbar.setScreens(screens);
67
         mToolbar.setScreens(screens);
67
         Screen initialScreen = screens.get(0);
68
         Screen initialScreen = screens.get(0);
68
         mToolbar.update(initialScreen);
69
         mToolbar.update(initialScreen);
69
-        setNavigationStyle(initialScreen);
70
+        StyleHelper.updateStyles(mToolbar, initialScreen);
70
     }
71
     }
71
 
72
 
72
     @Override
73
     @Override
73
     protected void onResume() {
74
     protected void onResume() {
74
         super.onResume();
75
         super.onResume();
75
         if(mScreenStacks != null) {
76
         if(mScreenStacks != null) {
76
-            updateStyles();
77
+            StyleHelper.updateStyles(mToolbar, getCurrentScreen());
77
         }
78
         }
78
     }
79
     }
79
 
80
 
109
                 stack.push(screen);
110
                 stack.push(screen);
110
             }
111
             }
111
         }
112
         }
112
-        updateStyles();
113
+        StyleHelper.updateStyles(mToolbar, getCurrentScreen());
113
     }
114
     }
114
 
115
 
115
     @Override
116
     @Override
118
         for (ScreenStack stack: mScreenStacks) {
119
         for (ScreenStack stack: mScreenStacks) {
119
             if (stack.peek().navigatorId.equals(navigatorId)) {
120
             if (stack.peek().navigatorId.equals(navigatorId)) {
120
                 Screen popped = stack.pop();
121
                 Screen popped = stack.pop();
121
-                updateStyles();
122
+                StyleHelper.updateStyles(mToolbar, getCurrentScreen());
122
                 return popped;
123
                 return popped;
123
             }
124
             }
124
         }
125
         }
127
 
128
 
128
     @Override
129
     @Override
129
     protected Screen getCurrentScreen() {
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
     @Override
139
     @Override
148
         mContentFrame.removeAllViews();
154
         mContentFrame.removeAllViews();
149
         mContentFrame.addView(mScreenStacks.get(position), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
155
         mContentFrame.addView(mScreenStacks.get(position), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
150
         mCurrentStackPosition = position;
156
         mCurrentStackPosition = position;
151
-        updateStyles();
157
+        StyleHelper.updateStyles(mToolbar, getCurrentScreen());
152
     }
158
     }
153
 
159
 
154
     private static class SetupTabsTask extends AsyncTask<Void, Void, Map<Screen, Drawable>> {
160
     private static class SetupTabsTask extends AsyncTask<Void, Void, Map<Screen, Drawable>> {
155
         private BottomTabActivity mActivity;
161
         private BottomTabActivity mActivity;
162
+        private RnnToolBar mToolBar;
156
         private ArrayList<Screen> mScreens;
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
             mActivity = context;
166
             mActivity = context;
167
+            mToolBar = toolBar;
160
             mScreens = screens;
168
             mScreens = screens;
161
         }
169
         }
162
 
170
 
174
         @Override
182
         @Override
175
         protected void onPostExecute(Map<Screen, Drawable> icons) {
183
         protected void onPostExecute(Map<Screen, Drawable> icons) {
176
             mActivity.setTabsWithIcons(mScreens, icons);
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 Zobrazit soubor

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

+ 0
- 2
android/app/src/main/java/com/reactnativenavigation/activities/TabActivity.java Zobrazit soubor

46
         setNavigationStyle(initialScreen);
46
         setNavigationStyle(initialScreen);
47
     }
47
     }
48
 
48
 
49
-    @Override
50
     public void setNavigationStyle(Screen screen) {
49
     public void setNavigationStyle(Screen screen) {
51
-        super.setNavigationStyle(screen);
52
         mTabLayout.setStyle(screen);
50
         mTabLayout.setStyle(screen);
53
     }
51
     }
54
 
52
 

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/adapters/ViewPagerAdapter.java Zobrazit soubor

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

+ 8
- 2
android/app/src/main/java/com/reactnativenavigation/modal/RnnModal.java Zobrazit soubor

4
 import android.app.Dialog;
4
 import android.app.Dialog;
5
 import android.content.Context;
5
 import android.content.Context;
6
 import android.content.DialogInterface;
6
 import android.content.DialogInterface;
7
+import android.support.annotation.Nullable;
7
 import android.view.LayoutInflater;
8
 import android.view.LayoutInflater;
8
 import android.view.View;
9
 import android.view.View;
9
 import android.view.Window;
10
 import android.view.Window;
15
 import com.reactnativenavigation.activities.BaseReactActivity;
16
 import com.reactnativenavigation.activities.BaseReactActivity;
16
 import com.reactnativenavigation.controllers.ModalController;
17
 import com.reactnativenavigation.controllers.ModalController;
17
 import com.reactnativenavigation.core.objects.Screen;
18
 import com.reactnativenavigation.core.objects.Screen;
18
-import com.reactnativenavigation.utils.ContextProvider;
19
 import com.reactnativenavigation.utils.SdkSupports;
19
 import com.reactnativenavigation.utils.SdkSupports;
20
 import com.reactnativenavigation.utils.StyleHelper;
20
 import com.reactnativenavigation.utils.StyleHelper;
21
 import com.reactnativenavigation.views.RctView;
21
 import com.reactnativenavigation.views.RctView;
73
         if (mScreenStack.isEmpty()) {
73
         if (mScreenStack.isEmpty()) {
74
             dismiss();
74
             dismiss();
75
         }
75
         }
76
+        mToolBar.update(popped);
76
         return popped;
77
         return popped;
77
     }
78
     }
78
 
79
 
80
+    @Nullable
81
+    public Screen getCurrentScreen() {
82
+        return mScreenStack.isEmpty() ? null : mScreenStack.peek();
83
+    }
84
+
79
     @Override
85
     @Override
80
     public void onBackPressed() {
86
     public void onBackPressed() {
81
         pop();
87
         pop();
84
     @Override
90
     @Override
85
     public void onDismiss(DialogInterface dialog) {
91
     public void onDismiss(DialogInterface dialog) {
86
         ModalController.getInstance().remove();
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 Zobrazit soubor

2
 
2
 
3
 import android.content.Context;
3
 import android.content.Context;
4
 import android.support.v4.content.ContextCompat;
4
 import android.support.v4.content.ContextCompat;
5
+import android.util.Log;
5
 import android.view.Window;
6
 import android.view.Window;
6
 
7
 
8
+import com.reactnativenavigation.activities.BaseReactActivity;
7
 import com.reactnativenavigation.core.objects.Screen;
9
 import com.reactnativenavigation.core.objects.Screen;
10
+import com.reactnativenavigation.views.RnnToolBar;
8
 
11
 
9
 /**
12
 /**
10
  * Created by guyc on 07/05/16.
13
  * Created by guyc on 07/05/16.
11
  */
14
  */
12
 public class StyleHelper {
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
     public static void setWindowStyle(Window window, Context context, Screen screen) {
34
     public static void setWindowStyle(Window window, Context context, Screen screen) {
14
         if (SdkSupports.lollipop()) {
35
         if (SdkSupports.lollipop()) {
15
             final int black = ContextCompat.getColor(context, android.R.color.black);
36
             final int black = ContextCompat.getColor(context, android.R.color.black);

+ 8
- 3
android/app/src/main/java/com/reactnativenavigation/views/RnnToolBar.java Zobrazit soubor

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