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 2 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 @@ -226,11 +226,11 @@ This method is only supported on Android.

#### 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.view.View;
import android.view.Window;
Expand All @@ -14,27 +15,39 @@ public class StatusBar {

private int currentStatusBarColor;
private final AppCompatActivity activity;
private final String defaultStyle;
private String currentStyle = "DEFAULT";

public StatusBar(AppCompatActivity activity) {
// save initial color of the status bar
this.activity = activity;
this.currentStatusBarColor = activity.getWindow().getStatusBarColor();
this.defaultStyle = getStyle();
setStyle(this.currentStyle);
}

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.Plugin;
import com.getcapacitor.PluginCall;
Expand All @@ -18,6 +19,12 @@ public void load() {
implementation = new StatusBar(getActivity());
}

@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 @@ -26,7 +26,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