Skip to content

Commit

Permalink
Merge branch 'main' into RDMR-418
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Nov 26, 2024
2 parents d00ad3f + af2317f commit 406e0bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@

public class StatusBar {

public static final String statusBarVisibilityChanged = "statusBarVisibilityChanged";
public static final String statusBarOverlayChanged = "statusBarOverlayChanged";

private int currentStatusBarColor;
private final ChangeListener listener;
private final AppCompatActivity activity;
private final String defaultStyle;

public StatusBar(AppCompatActivity activity, StatusBarConfig config) {
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());
StatusBarInfo info = getInfo();
info.setVisible(true);
listener.onChange(statusBarOverlayChanged, info);
}

public void setStyle(String style) {
Expand Down Expand Up @@ -56,12 +64,18 @@ public void hide() {
View decorView = activity.getWindow().getDecorView();
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(activity.getWindow(), decorView);
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.statusBars());
StatusBarInfo info = getInfo();
info.setVisible(false);
listener.onChange(statusBarVisibilityChanged, info);
}

public void show() {
View decorView = activity.getWindow().getDecorView();
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(activity.getWindow(), decorView);
windowInsetsControllerCompat.show(WindowInsetsCompat.Type.statusBars());
StatusBarInfo info = getInfo();
info.setVisible(true);
listener.onChange(statusBarVisibilityChanged, info);
}

@SuppressWarnings("deprecation")
Expand All @@ -81,6 +95,7 @@ public void setOverlaysWebView(Boolean overlays) {
// recover the previous color of the status bar
activity.getWindow().setStatusBarColor(currentStatusBarColor);
}
listener.onChange(statusBarOverlayChanged, getInfo());
}

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -130,4 +145,8 @@ private int getStatusBarHeight() {
// Fallback if the insets are not available
return 0;
}

public interface ChangeListener {
void onChange(String eventName, StatusBarInfo info);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
@CapacitorPlugin(name = "StatusBar")
public class StatusBarPlugin extends Plugin {

public static final String statusBarVisibilityChanged = "statusBarVisibilityChanged";
public static final String statusBarOverlayChanged = "statusBarOverlayChanged";

private StatusBar implementation;

@Override
public void load() {
StatusBarConfig config = getStatusBarConfig();
implementation = new StatusBar(getActivity(), config);
implementation = new StatusBar(getActivity(), config, (eventName, info) -> notifyListeners(eventName, toJSObject(info), true));
}

private StatusBarConfig getStatusBarConfig() {
Expand Down Expand Up @@ -98,8 +95,6 @@ public void hide(final PluginCall call) {
.executeOnMainThread(
() -> {
implementation.hide();
StatusBarInfo info = implementation.getInfo();
notifyListeners(statusBarVisibilityChanged, toJSObject(info));
call.resolve();
}
);
Expand All @@ -112,8 +107,6 @@ public void show(final PluginCall call) {
.executeOnMainThread(
() -> {
implementation.show();
StatusBarInfo info = implementation.getInfo();
notifyListeners(statusBarVisibilityChanged, toJSObject(info));
call.resolve();
}
);
Expand All @@ -127,13 +120,11 @@ public void getInfo(final PluginCall call) {

@PluginMethod
public void setOverlaysWebView(final PluginCall call) {
final Boolean overlays = call.getBoolean("overlay", true);
final Boolean overlay = call.getBoolean("overlay", true);
getBridge()
.executeOnMainThread(
() -> {
implementation.setOverlaysWebView(overlays);
StatusBarInfo info = implementation.getInfo();
notifyListeners(statusBarOverlayChanged, toJSObject(info));
implementation.setOverlaysWebView(overlay);
call.resolve();
}
);
Expand Down

0 comments on commit 406e0bf

Please sign in to comment.