Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(statusbar)!: make DEFAULT change style based on the Android theme #2166

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions status-bar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ of the space underneath it.

#### Style

| Members | Value | Description | Since |
| ------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
| **`Dark`** | <code>'DARK'</code> | Light text for dark backgrounds. | 1.0.0 |
| **`Light`** | <code>'LIGHT'</code> | Dark text for light backgrounds. | 1.0.0 |
| **`Default`** | <code>'DEFAULT'</code> | The style is based on the device appearance. If the device is using Dark mode, the statusbar text will be light. If the device is using Light mode, the statusbar text will be dark. On Android the default will be the one the app was launched with. | 1.0.0 |
| Members | Value | Description | Since |
| ------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
| **`Dark`** | <code>'DARK'</code> | Light text for dark backgrounds. | 1.0.0 |
| **`Light`** | <code>'LIGHT'</code> | Dark text for light backgrounds. | 1.0.0 |
| **`Default`** | <code>'DEFAULT'</code> | The style is based on the device appearance. If the device is using Dark mode, the statusbar text will be light. If the device is using Light mode, the statusbar text will be dark. | 1.0.0 |


#### Animation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.capacitorjs.plugins.statusbar;

import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.util.DisplayMetrics;
Expand All @@ -21,15 +22,13 @@ public class StatusBar {
private int currentStatusBarColor;
private final ChangeListener listener;
private final AppCompatActivity activity;
private final String defaultStyle;
private String currentStyle = "DEFAULT";

public StatusBar(AppCompatActivity activity, StatusBarConfig config, ChangeListener listener) {
// save initial color of the status bar
this.activity = activity;
this.currentStatusBarColor = activity.getWindow().getStatusBarColor();
this.listener = listener;
this.defaultStyle = getStyle();

setBackgroundColor(config.getBackgroundColor());
setStyle(config.getStyle());
setOverlaysWebView(config.isOverlaysWebView());
Expand All @@ -41,15 +40,27 @@ public StatusBar(AppCompatActivity activity, StatusBarConfig config, ChangeListe
public void setStyle(String style) {
Window window = activity.getWindow();
View decorView = window.getDecorView();

this.currentStyle = style;
if (style.equals("DEFAULT")) {
style = this.defaultStyle;
style = getStyleForTheme();
}

WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(window, decorView);
windowInsetsControllerCompat.setAppearanceLightStatusBars(!style.equals("DARK"));
}

private String getStyleForTheme() {
int currentNightMode = activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if (currentNightMode != Configuration.UI_MODE_NIGHT_YES) {
return "LIGHT";
}
return "DARK";
}

public void updateStyle() {
setStyle(this.currentStyle);
}

@SuppressWarnings("deprecation")
public void setBackgroundColor(int color) {
Window window = activity.getWindow();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.capacitorjs.plugins.statusbar;

import android.content.res.Configuration;
import com.getcapacitor.JSObject;
import com.getcapacitor.Logger;
import com.getcapacitor.Plugin;
Expand Down Expand Up @@ -49,6 +50,12 @@ private String styleFromConfig(String style) {
}
}

@Override
protected void handleOnConfigurationChanged(Configuration newConfig) {
super.handleOnConfigurationChanged(newConfig);
implementation.updateStyle();
}

@PluginMethod
public void setStyle(final PluginCall call) {
final String style = call.getString("style");
Expand Down
1 change: 0 additions & 1 deletion status-bar/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export enum Style {
* The style is based on the device appearance.
* If the device is using Dark mode, the statusbar text will be light.
* If the device is using Light mode, the statusbar text will be dark.
* On Android the default will be the one the app was launched with.
*
* @since 1.0.0
*/
Expand Down
Loading