Skip to content

Commit

Permalink
feat(statusbar)!: make DEFAULT change style based on the Android theme
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Aug 8, 2024
1 parent f2a5a9f commit cd52ba0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
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,40 @@ 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,7 @@
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 +20,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

0 comments on commit cd52ba0

Please sign in to comment.