Parcourir la source

Implement statusBarStyle on Android

Guy Carmeli il y a 6 ans
Parent
révision
3e04b8455b

+ 32
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/StatusBarOptions.java Voir le fichier

1
 package com.reactnativenavigation.parse;
1
 package com.reactnativenavigation.parse;
2
 
2
 
3
+import android.support.annotation.Nullable;
4
+
3
 import com.reactnativenavigation.parse.params.Color;
5
 import com.reactnativenavigation.parse.params.Color;
4
 import com.reactnativenavigation.parse.params.NullColor;
6
 import com.reactnativenavigation.parse.params.NullColor;
5
 import com.reactnativenavigation.parse.parsers.ColorParser;
7
 import com.reactnativenavigation.parse.parsers.ColorParser;
7
 import org.json.JSONObject;
9
 import org.json.JSONObject;
8
 
10
 
9
 public class StatusBarOptions {
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
     public static StatusBarOptions parse(JSONObject json) {
38
     public static StatusBarOptions parse(JSONObject json) {
11
         StatusBarOptions result = new StatusBarOptions();
39
         StatusBarOptions result = new StatusBarOptions();
12
         if (json == null) return result;
40
         if (json == null) return result;
13
 
41
 
14
         result.backgroundColor = ColorParser.parse(json, "statusBarBackgroundColor");
42
         result.backgroundColor = ColorParser.parse(json, "statusBarBackgroundColor");
43
+        result.textColorScheme = TextColorScheme.fromString(json.optString("statusBarStyle"));
15
 
44
 
16
         return result;
45
         return result;
17
     }
46
     }
18
 
47
 
19
     public Color backgroundColor = new NullColor();
48
     public Color backgroundColor = new NullColor();
49
+    public TextColorScheme textColorScheme = TextColorScheme.None;
20
 
50
 
21
     public void mergeWith(StatusBarOptions other) {
51
     public void mergeWith(StatusBarOptions other) {
22
         if (other.backgroundColor.hasValue()) backgroundColor = other.backgroundColor;
52
         if (other.backgroundColor.hasValue()) backgroundColor = other.backgroundColor;
53
+        if (other.textColorScheme.hasValue()) textColorScheme = other.textColorScheme;
23
     }
54
     }
24
 
55
 
25
     public void mergeWithDefault(StatusBarOptions defaultOptions) {
56
     public void mergeWithDefault(StatusBarOptions defaultOptions) {
26
         if (!backgroundColor.hasValue()) backgroundColor = defaultOptions.backgroundColor;
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 Voir le fichier

8
 import com.reactnativenavigation.parse.Options;
8
 import com.reactnativenavigation.parse.Options;
9
 import com.reactnativenavigation.parse.OrientationOptions;
9
 import com.reactnativenavigation.parse.OrientationOptions;
10
 import com.reactnativenavigation.parse.StatusBarOptions;
10
 import com.reactnativenavigation.parse.StatusBarOptions;
11
+import com.reactnativenavigation.parse.StatusBarOptions.TextColorScheme;
11
 
12
 
12
 @SuppressWarnings("FieldCanBeLocal")
13
 @SuppressWarnings("FieldCanBeLocal")
13
 public class OptionsPresenter {
14
 public class OptionsPresenter {
42
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
43
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
43
             activity.getWindow().setStatusBarColor(statusBar.backgroundColor.get(Color.BLACK));
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 Voir le fichier

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