From af2317f75f72d80308fac83ae65b7196cca98371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Destro?= <254790+andredestro@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:40:29 +0000 Subject: [PATCH] fix(statusbar): fix info when notify listeners (#2247) --- .../plugins/statusbar/StatusBar.java | 21 ++++++++++++++++++- .../plugins/statusbar/StatusBarPlugin.java | 15 +++---------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java b/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java index a6570d4c1..4b8155d6a 100644 --- a/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java +++ b/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBar.java @@ -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) { @@ -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") @@ -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") @@ -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); + } } diff --git a/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBarPlugin.java b/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBarPlugin.java index 0f5c5fbda..6c32f8d9d 100644 --- a/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBarPlugin.java +++ b/status-bar/android/src/main/java/com/capacitorjs/plugins/statusbar/StatusBarPlugin.java @@ -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() { @@ -98,8 +95,6 @@ public void hide(final PluginCall call) { .executeOnMainThread( () -> { implementation.hide(); - StatusBarInfo info = implementation.getInfo(); - notifyListeners(statusBarVisibilityChanged, toJSObject(info)); call.resolve(); } ); @@ -112,8 +107,6 @@ public void show(final PluginCall call) { .executeOnMainThread( () -> { implementation.show(); - StatusBarInfo info = implementation.getInfo(); - notifyListeners(statusBarVisibilityChanged, toJSObject(info)); call.resolve(); } ); @@ -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(); } );