Skip to content

Commit

Permalink
feat: use notifyListeners
Browse files Browse the repository at this point in the history
  • Loading branch information
andredestro committed Sep 4, 2024
1 parent e02a3b4 commit 9befd42
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
2 changes: 0 additions & 2 deletions status-bar/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ ext {
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
androidxCoreVersion = project.hasProperty('androidxCoreVersion') ? rootProject.ext.androidxCoreVersion : '1.12.0'
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.6.1'
androidxLocalBroadcastManagerVersion = project.hasProperty('androidxLocalBroadcastManagerVersion') ? rootProject.ext.androidxLocalBroadcastManagerVersion : '1.1.0'
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5'
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1'
}
Expand Down Expand Up @@ -76,7 +75,6 @@ dependencies {

implementation "androidx.core:core:$androidxCoreVersion"
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
implementation "androidx.localbroadcastmanager:localbroadcastmanager:$androidxLocalBroadcastManagerVersion"
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.capacitorjs.plugins.statusbar;

import android.content.Intent;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.View;
Expand All @@ -11,17 +10,13 @@
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

public class StatusBar {

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

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

public StatusBar(AppCompatActivity activity, StatusBarConfig config) {
// save initial color of the status bar
this.activity = activity;
Expand Down Expand Up @@ -59,20 +54,12 @@ public void hide() {
View decorView = activity.getWindow().getDecorView();
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(activity.getWindow(), decorView);
windowInsetsControllerCompat.hide(WindowInsetsCompat.Type.statusBars());
sendBroadcast(statusBarVisibilityChanged);
}

public void show() {
View decorView = activity.getWindow().getDecorView();
WindowInsetsControllerCompat windowInsetsControllerCompat = WindowCompat.getInsetsController(activity.getWindow(), decorView);
windowInsetsControllerCompat.show(WindowInsetsCompat.Type.statusBars());
sendBroadcast(statusBarVisibilityChanged);
}

private void sendBroadcast(String action) {
Intent intent = new Intent(action);
intent.putExtra("info", getInfo());
LocalBroadcastManager.getInstance(activity).sendBroadcast(intent);
}

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

@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
@CapacitorPlugin(name = "StatusBar")
public class StatusBarPlugin extends Plugin {

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

private StatusBar implementation;

@Override
Expand Down Expand Up @@ -95,6 +98,8 @@ public void hide(final PluginCall call) {
.executeOnMainThread(
() -> {
implementation.hide();
StatusBarInfo info = implementation.getInfo();
notifyListeners(statusBarVisibilityChanged, toJSObject(info));
call.resolve();
}
);
Expand All @@ -107,6 +112,8 @@ public void show(final PluginCall call) {
.executeOnMainThread(
() -> {
implementation.show();
StatusBarInfo info = implementation.getInfo();
notifyListeners(statusBarVisibilityChanged, toJSObject(info));
call.resolve();
}
);
Expand All @@ -115,13 +122,7 @@ public void show(final PluginCall call) {
@PluginMethod
public void getInfo(final PluginCall call) {
StatusBarInfo info = implementation.getInfo();

JSObject data = new JSObject();
data.put("visible", info.isVisible());
data.put("style", info.getStyle());
data.put("color", info.getColor());
data.put("overlays", info.isOverlays());
call.resolve(data);
call.resolve(toJSObject(info));
}

@PluginMethod
Expand All @@ -131,8 +132,20 @@ public void setOverlaysWebView(final PluginCall call) {
.executeOnMainThread(
() -> {
implementation.setOverlaysWebView(overlays);
StatusBarInfo info = implementation.getInfo();
notifyListeners(statusBarOverlayChanged, toJSObject(info));
call.resolve();
}
);
}

private JSObject toJSObject(StatusBarInfo info) {
JSObject data = new JSObject();
data.put("visible", info.isVisible());
data.put("style", info.getStyle());
data.put("color", info.getColor());
data.put("overlays", info.isOverlays());
data.put("height", info.getHeight());
return data;
}
}
8 changes: 0 additions & 8 deletions status-bar/ios/Sources/StatusBarPlugin/StatusBar.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import Foundation
import Capacitor

public extension Notification.Name {
static let statusBarVisibilityChanged = Notification.Name("statusBarVisibilityChanged")
static let statusBarOverlayChanged = Notification.Name("statusBarOverlayChanged")
}

@objc public class StatusBar: NSObject {

private var bridge: CAPBridgeProtocol
Expand Down Expand Up @@ -77,7 +72,6 @@ public extension Notification.Name {
self?.backgroundView?.removeFromSuperview()
self?.backgroundView?.isHidden = true
}
NotificationCenter.default.post(name: .statusBarVisibilityChanged, object: getInfo())
}
}

Expand All @@ -93,7 +87,6 @@ public extension Notification.Name {
}
backgroundView?.isHidden = false
}
NotificationCenter.default.post(name: .statusBarVisibilityChanged, object: getInfo())
}
}

Expand Down Expand Up @@ -129,7 +122,6 @@ public extension Notification.Name {
bridge.webView?.superview?.addSubview(backgroundView!)
}
resizeWebView()
NotificationCenter.default.post(name: .statusBarOverlayChanged, object: getInfo())
}

private func resizeWebView() {
Expand Down
42 changes: 35 additions & 7 deletions status-bar/ios/Sources/StatusBarPlugin/StatusBarPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class StatusBarPlugin: CAPPlugin, CAPBridgedPlugin {
CAPPluginMethod(name: "setOverlaysWebView", returnType: CAPPluginReturnPromise),
]
private var statusBar: StatusBar?
private let statusBarVisibilityChanged = "statusBarVisibilityChanged"
private let statusBarOverlayChanged = "statusBarOverlayChanged"

override public func load() {
guard let bridge = bridge else { return }
Expand Down Expand Up @@ -73,6 +75,12 @@ public class StatusBarPlugin: CAPPlugin, CAPBridgedPlugin {
let animation = call.getString("animation", "FADE")
DispatchQueue.main.async { [weak self] in
self?.statusBar?.hide(animation: animation)
guard
let info = self?.statusBar?.getInfo(),
let dict = self?.toDict(info),
let event = self?.statusBarVisibilityChanged
else { return }
self?.notifyListeners(event, data: dict);
}
call.resolve()
}
Expand All @@ -81,27 +89,47 @@ public class StatusBarPlugin: CAPPlugin, CAPBridgedPlugin {
let animation = call.getString("animation", "FADE")
DispatchQueue.main.async { [weak self] in
self?.statusBar?.show(animation: animation)
guard
let info = self?.statusBar?.getInfo(),
let dict = self?.toDict(info),
let event = self?.statusBarVisibilityChanged
else { return }
self?.notifyListeners(event, data: dict);
}
call.resolve()
}

@objc func getInfo(_ call: CAPPluginCall) {
DispatchQueue.main.async { [weak self] in
guard let info = self?.statusBar?.getInfo() else { return }
call.resolve([
"visible": info.visible!,
"style": info.style!,
"color": info.color!,
"overlays": info.overlays!
])
guard
let info = self?.statusBar?.getInfo(),
let dict = self?.toDict(info)
else { return }
call.resolve(dict)
}
}

@objc func setOverlaysWebView(_ call: CAPPluginCall) {
guard let overlay = call.options["overlay"] as? Bool else { return }
DispatchQueue.main.async { [weak self] in
self?.statusBar?.setOverlaysWebView(overlay)
guard
let info = self?.statusBar?.getInfo(),
let dict = self?.toDict(info),
let event = self?.statusBarOverlayChanged
else { return }
self?.notifyListeners(event, data: dict);
}
call.resolve()
}

private func toDict(_ info: StatusBarInfo) -> [String: Any] {
return [
"visible": info.visible!,
"style": info.style!,
"color": info.color!,
"overlays": info.overlays!,
"height": info.height!
]
}
}

0 comments on commit 9befd42

Please sign in to comment.