Browse Source

Implement statusBarStyle on Android

Guy Carmeli 6 years ago
parent
commit
3e04b8455b

+ 32
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/StatusBarOptions.java View File

@@ -1,5 +1,7 @@
1 1
 package com.reactnativenavigation.parse;
2 2
 
3
+import android.support.annotation.Nullable;
4
+
3 5
 import com.reactnativenavigation.parse.params.Color;
4 6
 import com.reactnativenavigation.parse.params.NullColor;
5 7
 import com.reactnativenavigation.parse.parsers.ColorParser;
@@ -7,22 +9,52 @@ import com.reactnativenavigation.parse.parsers.ColorParser;
7 9
 import org.json.JSONObject;
8 10
 
9 11
 public class StatusBarOptions {
12
+    public enum TextColorScheme {
13
+        Light("light"), Dark("dark"), None("none");
14
+
15
+        private String scheme;
16
+
17
+        TextColorScheme(String scheme) {
18
+            this.scheme = scheme;
19
+        }
20
+
21
+        public static TextColorScheme fromString(@Nullable String scheme) {
22
+            if (scheme == null) return None;
23
+            switch (scheme) {
24
+                case "light":
25
+                    return Light;
26
+                case "dark":
27
+                    return Dark;
28
+                default:
29
+                    return None;
30
+            }
31
+        }
32
+
33
+        public boolean hasValue() {
34
+            return !scheme.equals(None.scheme);
35
+        }
36
+    }
37
+
10 38
     public static StatusBarOptions parse(JSONObject json) {
11 39
         StatusBarOptions result = new StatusBarOptions();
12 40
         if (json == null) return result;
13 41
 
14 42
         result.backgroundColor = ColorParser.parse(json, "statusBarBackgroundColor");
43
+        result.textColorScheme = TextColorScheme.fromString(json.optString("statusBarStyle"));
15 44
 
16 45
         return result;
17 46
     }
18 47
 
19 48
     public Color backgroundColor = new NullColor();
49
+    public TextColorScheme textColorScheme = TextColorScheme.None;
20 50
 
21 51
     public void mergeWith(StatusBarOptions other) {
22 52
         if (other.backgroundColor.hasValue()) backgroundColor = other.backgroundColor;
53
+        if (other.textColorScheme.hasValue()) textColorScheme = other.textColorScheme;
23 54
     }
24 55
 
25 56
     public void mergeWithDefault(StatusBarOptions defaultOptions) {
26 57
         if (!backgroundColor.hasValue()) backgroundColor = defaultOptions.backgroundColor;
58
+        if (!textColorScheme.hasValue()) textColorScheme = defaultOptions.textColorScheme;
27 59
     }
28 60
 }

+ 21
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java View File

@@ -8,6 +8,7 @@ import android.view.View;
8 8
 import com.reactnativenavigation.parse.Options;
9 9
 import com.reactnativenavigation.parse.OrientationOptions;
10 10
 import com.reactnativenavigation.parse.StatusBarOptions;
11
+import com.reactnativenavigation.parse.StatusBarOptions.TextColorScheme;
11 12
 
12 13
 @SuppressWarnings("FieldCanBeLocal")
13 14
 public class OptionsPresenter {
@@ -42,5 +43,25 @@ public class OptionsPresenter {
42 43
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
43 44
             activity.getWindow().setStatusBarColor(statusBar.backgroundColor.get(Color.BLACK));
44 45
         }
46
+        setTextColorScheme(statusBar.textColorScheme);
47
+    }
48
+
49
+    private void setTextColorScheme(TextColorScheme scheme) {
50
+        final View view = activity.getWindow().getDecorView();
51
+        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
52
+        if (scheme == TextColorScheme.Dark) {
53
+            int flags = view.getSystemUiVisibility();
54
+            flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
55
+            view.setSystemUiVisibility(flags);
56
+        } else {
57
+            clearDarkTextColorScheme(view);
58
+        }
59
+    }
60
+
61
+    private static void clearDarkTextColorScheme(View view) {
62
+        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
63
+        int flags = view.getSystemUiVisibility();
64
+        flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
65
+        view.setSystemUiVisibility(flags);
45 66
     }
46 67
 }

+ 2
- 1
playground/src/screens/OptionsScreen.js View File

@@ -16,7 +16,8 @@ const FAB = 'fab';
16 16
 class OptionsScreen extends Component {
17 17
   static get options() {
18 18
     return {
19
-      statusBarBackgroundColor: 'red',
19
+      statusBarBackgroundColor: '#EDEDED',
20
+      statusBarStyle: 'dark',
20 21
       topBar: {
21 22
         title: {
22 23
           text: 'Static Title',