-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement overlay and background color in iOS
- Loading branch information
1 parent
f2a5a9f
commit 673b25e
Showing
7 changed files
with
308 additions
and
56 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
status-bar/ios/Sources/StatusBarPlugin/CAPBridgeViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Capacitor | ||
|
||
extension CAPBridgeViewController { | ||
|
||
open override func viewDidAppear(_ animated: Bool) { | ||
super.viewDidAppear(animated) | ||
NotificationCenter.default.post(Notification(name: .capacitorViewDidAppear)) | ||
} | ||
|
||
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { | ||
super.viewWillTransition(to: size, with: coordinator) | ||
NotificationCenter.default.post(Notification(name: .capacitorViewWillTransition)) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
status-bar/ios/Sources/StatusBarPlugin/CAPNotifications.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Capacitor | ||
|
||
extension Notification.Name { | ||
public static let capacitorViewDidAppear = Notification.Name(rawValue: "CapacitorViewDidAppear") | ||
public static let capacitorViewWillTransition = Notification.Name(rawValue: "CapacitorViewWillTransition") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
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 | ||
private var isOverlayingWebview = true | ||
private var backgroundColor = UIColor.black | ||
private var backgroundView: UIView? | ||
private var observers: [NSObjectProtocol] = [] | ||
|
||
init(bridge: CAPBridgeProtocol, config: StatusBarConfig) { | ||
self.bridge = bridge | ||
super.init() | ||
setupObservers(with: config) | ||
} | ||
|
||
deinit { | ||
observers.forEach { NotificationCenter.default.removeObserver($0) } | ||
} | ||
|
||
private func setupObservers(with config: StatusBarConfig) { | ||
observers.append(NotificationCenter.default.addObserver(forName: .capacitorViewDidAppear, object: .none, queue: .none) { [weak self] _ in | ||
self?.handleViewDidAppear(config: config) | ||
}) | ||
observers.append(NotificationCenter.default.addObserver(forName: .capacitorStatusBarTapped, object: .none, queue: .none) { [weak self] _ in | ||
self?.bridge.triggerJSEvent(eventName: "statusTap", target: "window") | ||
}) | ||
observers.append(NotificationCenter.default.addObserver(forName: .capacitorViewWillTransition, object: .none, queue: .none) { [weak self] _ in | ||
self?.handleViewWillTransition() | ||
}) | ||
} | ||
|
||
private func handleViewDidAppear(config: StatusBarConfig) { | ||
setStyle(config.style) | ||
setBackgroundColor(config.backgroundColor) | ||
setOverlaysWebView(config.overlaysWebView) | ||
} | ||
|
||
private func handleViewWillTransition() { | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in | ||
self?.resizeStatusBarBackgroundView() | ||
self?.resizeWebView() | ||
} | ||
} | ||
|
||
func setStyle(_ style: UIStatusBarStyle) { | ||
bridge.statusBarStyle = style | ||
} | ||
|
||
func setBackgroundColor(_ color : UIColor) { | ||
backgroundColor = color | ||
backgroundView?.backgroundColor = color | ||
} | ||
|
||
func setAnimation(_ animation: String) { | ||
if animation == "SLIDE" { | ||
bridge.statusBarAnimation = .slide | ||
} else if animation == "NONE" { | ||
bridge.statusBarAnimation = .none | ||
} else { | ||
bridge.statusBarAnimation = .fade | ||
} | ||
} | ||
|
||
func hide(animation: String) { | ||
setAnimation(animation) | ||
if bridge.statusBarVisible { | ||
bridge.statusBarVisible = false | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in | ||
self?.resizeWebView() | ||
self?.backgroundView?.removeFromSuperview() | ||
self?.backgroundView?.isHidden = true | ||
} | ||
NotificationCenter.default.post(name: .statusBarVisibilityChanged, object: getInfo()) | ||
} | ||
} | ||
|
||
func show(animation: String) { | ||
setAnimation(animation) | ||
if !bridge.statusBarVisible { | ||
bridge.statusBarVisible = true | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in | ||
resizeWebView() | ||
if !isOverlayingWebview { | ||
resizeStatusBarBackgroundView() | ||
bridge.webView?.superview?.addSubview(backgroundView!) | ||
} | ||
backgroundView?.isHidden = false | ||
} | ||
NotificationCenter.default.post(name: .statusBarVisibilityChanged, object: getInfo()) | ||
} | ||
} | ||
|
||
func getInfo() -> StatusBarInfo { | ||
let style: String | ||
switch bridge.statusBarStyle { | ||
case .default: | ||
style = "DEFAULT" | ||
case .lightContent: | ||
style = "DARK" | ||
case .darkContent: | ||
style = "LIGHT" | ||
@unknown default: | ||
style = "DEFAULT" | ||
} | ||
|
||
return StatusBarInfo( | ||
overlays: isOverlayingWebview, | ||
visible: bridge.statusBarVisible, | ||
style: style, | ||
color: UIColor.capacitor.hex(fromColor: backgroundColor), | ||
height: getStatusBarFrame().size.height | ||
) | ||
} | ||
|
||
func setOverlaysWebView(_ overlay: Bool) { | ||
if overlay == isOverlayingWebview { return } | ||
isOverlayingWebview = overlay | ||
if overlay { | ||
backgroundView?.removeFromSuperview() | ||
} else { | ||
initializeBackgroundViewIfNeeded() | ||
bridge.webView?.superview?.addSubview(backgroundView!) | ||
} | ||
resizeWebView() | ||
NotificationCenter.default.post(name: .statusBarOverlayChanged, object: getInfo()) | ||
} | ||
|
||
private func resizeWebView() { | ||
guard | ||
let webView = bridge.webView, | ||
let bounds = bridge.viewController?.view.window?.windowScene?.screen.bounds | ||
else { return } | ||
bridge.viewController?.view.frame = bounds | ||
webView.frame = bounds | ||
let statusBarHeight = getStatusBarFrame().size.height; | ||
var webViewFrame = webView.frame; | ||
|
||
if isOverlayingWebview { | ||
let safeAreaTop = webView.safeAreaInsets.top; | ||
if (statusBarHeight >= safeAreaTop && safeAreaTop > 0) { | ||
webViewFrame.origin.y = safeAreaTop == 40 ? 20 : statusBarHeight - safeAreaTop | ||
} else { | ||
webViewFrame.origin.y = 0 | ||
} | ||
} else { | ||
webViewFrame.origin.y = statusBarHeight; | ||
} | ||
webViewFrame.size.height -= webViewFrame.origin.y | ||
webView.frame = webViewFrame | ||
} | ||
|
||
private func resizeStatusBarBackgroundView() { | ||
backgroundView?.frame = getStatusBarFrame() | ||
} | ||
|
||
private func getStatusBarFrame() -> CGRect { | ||
return UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.windowScene?.statusBarManager?.statusBarFrame ?? .zero | ||
} | ||
|
||
private func initializeBackgroundViewIfNeeded() { | ||
if backgroundView == nil { | ||
backgroundView = UIView(frame: getStatusBarFrame()) | ||
backgroundView!.backgroundColor = backgroundColor | ||
backgroundView!.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin] | ||
backgroundView!.isHidden = !bridge.statusBarVisible | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import UIKit | ||
|
||
public struct StatusBarConfig { | ||
var overlaysWebView = true | ||
var backgroundColor: UIColor = .black | ||
var style: UIStatusBarStyle = .default | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Foundation | ||
|
||
public struct StatusBarInfo { | ||
public var overlays: Bool? | ||
public var visible: Bool? | ||
public var style: String? | ||
public var color: String? | ||
public var height: CGFloat? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.