Skip to content
This repository was archived by the owner on Jun 4, 2025. It is now read-only.

Commit 143cfb0

Browse files
committed
feat(webview): add loader
feat(FAB): add badge count feat(weloop): add delegate method for badge count update
1 parent c460594 commit 143cfb0

File tree

5 files changed

+43
-19
lines changed

5 files changed

+43
-19
lines changed

Example/WeLoop/AppDelegate.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ extension AppDelegate: WeLoopDelegate {
4646
// The widget could not be launched. Most likely is that the initialization process failed, or the user is missing in autoAuthentication
4747
print(error)
4848
}
49+
50+
func notificationCountUpdated(newCount: Int) {
51+
print(newCount)
52+
}
4953
}

WeLoop/Classes/FloatingButtonController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class FloatingButtonController: UIViewController {
5858
view.setNeedsLayout()
5959
}
6060

61-
func setNotificationBadge(hidden: Bool) {
62-
self.button.setBadge(hidden: hidden)
61+
func setNotificationBadge(count: Int) {
62+
self.button.setBadge(count: count)
6363
}
6464

6565
@objc func keyboardDidShow(notification: NSNotification) {

WeLoop/Classes/WeLoop.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import UIKit
1818
@objc optional func initializationSuccessful()
1919
@objc optional func initializationFailed(with error: Error)
2020
@objc optional func failedToLaunch(with error: Error)
21+
@objc optional func notificationCountUpdated(newCount: Int)
2122
}
2223

2324
public class WeLoop: NSObject {
@@ -219,8 +220,9 @@ public class WeLoop: NSObject {
219220
return URL(string: "\(appURL)?appGuid=\(apiKey)")!
220221
}
221222

222-
func setNotificationBadge(count: Int?) {
223-
fabController?.setNotificationBadge(hidden: count == nil || count! == 0)
223+
func setNotificationBadge(count: Int) {
224+
fabController?.setNotificationBadge(count: count)
225+
delegate?.notificationCountUpdated?(newCount: count)
224226
}
225227
}
226228

WeLoop/Classes/WeLoopButton.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import UIKit
1010

1111
private let size: CGFloat = 60.0
1212
private let badgeSize: CGFloat = 24.0
13+
private let badgePadding: CGFloat = 5.0
1314
private let shadowHighlightedOpacity: Float = 0.2
1415

1516
class WeLoopButton: UIButton {
1617

1718
private let badge = UIView(frame: CGRect(x: 0, y: 0, width: badgeSize, height: badgeSize))
19+
private let badgeLabel = UILabel(frame: CGRect(x: 0, y: 0, width: badgeSize, height: badgeSize))
1820

1921
var color: UIColor? = nil {
2022
didSet {
@@ -58,23 +60,28 @@ class WeLoopButton: UIButton {
5860

5961
func setupBadge() {
6062
insertSubview(badge, at: 1)
61-
badge.translatesAutoresizingMaskIntoConstraints = false
62-
badge.backgroundColor = .white
63-
badge.layer.borderColor = UIColor.red.cgColor
64-
badge.layer.borderWidth = badgeSize / 2 - 2
63+
badge.addSubview(badgeLabel)
64+
65+
badge.backgroundColor = .red
66+
badgeLabel.textColor = .white
67+
badgeLabel.textAlignment = .center
6568
badge.layer.cornerRadius = badgeSize / 2
6669

70+
badge.clipsToBounds = true
6771
badge.isHidden = true
6872

69-
badge.layer.shadowColor = UIColor.red.cgColor
70-
badge.layer.shadowRadius = 3
71-
badge.layer.shadowOpacity = 0.1
72-
badge.layer.shadowOffset = CGSize.zero
73+
badge.translatesAutoresizingMaskIntoConstraints = false
74+
badgeLabel.translatesAutoresizingMaskIntoConstraints = false
7375

74-
badge.widthAnchor.constraint(equalToConstant: badgeSize).isActive = true
76+
badge.widthAnchor.constraint(greaterThanOrEqualToConstant: badgeSize).isActive = true
7577
badge.heightAnchor.constraint(equalToConstant: badgeSize).isActive = true
7678
badge.centerYAnchor.constraint(equalTo: topAnchor, constant: size / (2 * CGFloat.pi)).isActive = true
7779
badge.centerXAnchor.constraint(equalTo: rightAnchor, constant: -size / (2 * CGFloat.pi)).isActive = true
80+
81+
badgeLabel.centerXAnchor.constraint(equalTo: badge.centerXAnchor).isActive = true
82+
badgeLabel.centerYAnchor.constraint(equalTo: badge.centerYAnchor).isActive = true
83+
badgeLabel.leftAnchor.constraint(equalTo: badge.leftAnchor, constant: badgePadding).isActive = true
84+
badgeLabel.rightAnchor.constraint(equalTo: badge.rightAnchor, constant: -badgePadding).isActive = true
7885
}
7986

8087
func configureButton(settings: Settings) {
@@ -98,7 +105,8 @@ class WeLoopButton: UIButton {
98105
}
99106
}
100107

101-
func setBadge(hidden: Bool) {
102-
badge.isHidden = hidden
108+
func setBadge(count: Int) {
109+
badge.isHidden = count < 1
110+
badgeLabel.text = "\(count)"
103111
}
104112
}

WeLoop/Classes/WeLoopViewController.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class WeLoopViewController: UIViewController {
2020

2121
var url: URL?
2222
weak var webView: WKWebView!
23+
weak var indicator: UIActivityIndicatorView!
2324
var window: UIWindow = UIWindow(frame: UIScreen.main.bounds)
2425
private var observation: NSKeyValueObservation?
2526

@@ -64,17 +65,23 @@ class WeLoopViewController: UIViewController {
6465

6566
private func configureWebview() {
6667
let webView = WKWebView(frame: view.bounds, configuration: self.configuration)
68+
let indicator = UIActivityIndicatorView(frame: view.bounds)
6769
self.webView = webView
70+
self.indicator = indicator
6871
view.addSubview(webView)
72+
view.addSubview(indicator)
6973

7074

7175
if #available(iOS 11.0, *) {
7276
let bottomInset = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
7377
self.additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: -bottomInset, right: 0)
7478
}
7579

80+
indicator.style = .gray
81+
indicator.hidesWhenStopped = true
82+
7683
webView.isOpaque = false
77-
webView.backgroundColor = .clear
84+
webView.backgroundColor = .white
7885
webView.scrollView.backgroundColor = .clear
7986

8087
webView.allowsLinkPreview = false
@@ -92,7 +99,7 @@ class WeLoopViewController: UIViewController {
9299

93100
func loadWeLoop() {
94101
guard let url = url else { return }
95-
webView.alpha = 0
102+
indicator.startAnimating()
96103
webView.load(URLRequest(url: url))
97104
}
98105

@@ -107,7 +114,7 @@ class WeLoopViewController: UIViewController {
107114

108115
func sendCurrentUser() {
109116
guard let uuid = WeLoop.shared.apiKey, let token = WeLoop.shared.authenticationToken else { return }
110-
webView?.evaluateJavaScript("GetCurrentUser({ appGuid: \(uuid), token: \(token)})")
117+
webView?.evaluateJavaScript("GetCurrentUser({ appGuid: '\(uuid)', token: '\(token)'})")
111118
}
112119
}
113120

@@ -147,6 +154,9 @@ extension WeLoopViewController: WKNavigationDelegate {
147154
}
148155

149156
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
150-
webView.alpha = 1
157+
indicator.stopAnimating()
158+
//TODO: commented out for now, but at this point the load is finished (files have been loaded) and the spa should render
159+
// a loader. This is not done immediately so there is a little lag, but I can't do anything more on the native side.
160+
//webView.backgroundColor = .clear
151161
}
152162
}

0 commit comments

Comments
 (0)