547 refactored userNotification into userIndicator

This commit is contained in:
Mauro Romito
2023-02-14 11:09:55 +01:00
committed by Mauro
parent b2d99e27a5
commit d7f80807df
21 changed files with 149 additions and 148 deletions
@@ -16,10 +16,10 @@
import Foundation
class MockUserNotificationController: UserNotificationControllerProtocol {
func submitNotification(_ notification: UserNotification) { }
class MockUserIndicatorController: UserIndicatorControllerProtocol {
func submitIndicator(_ indicator: UserIndicator) { }
func retractNotificationWithId(_ id: String) { }
func retractIndicatorWithId(_ id: String) { }
func retractAllNotifications() { }
func retractAllIndicators() { }
}
@@ -14,15 +14,15 @@
// limitations under the License.
//
import SwiftUI
import Foundation
enum UserNotificationType {
enum UserIndicatorType {
case toast
case modal
}
struct UserNotification: Equatable, Identifiable {
static func == (lhs: UserNotification, rhs: UserNotification) -> Bool {
struct UserIndicator: Equatable, Identifiable {
static func == (lhs: UserIndicator, rhs: UserIndicator) -> Bool {
lhs.id == rhs.id &&
lhs.type == rhs.type &&
lhs.title == rhs.title &&
@@ -31,7 +31,7 @@ struct UserNotification: Equatable, Identifiable {
}
var id: String = UUID().uuidString
var type = UserNotificationType.toast
var type = UserIndicatorType.toast
var title: String
var iconName: String?
var persistent = false
@@ -16,7 +16,7 @@
import SwiftUI
class UserNotificationController: ObservableObject, UserNotificationControllerProtocol, CustomStringConvertible {
class UserIndicatorController: ObservableObject, UserIndicatorControllerProtocol, CustomStringConvertible {
private let rootCoordinator: CoordinatorProtocol
private var dismisalTimer: Timer?
@@ -25,15 +25,15 @@ class UserNotificationController: ObservableObject, UserNotificationControllerPr
var nonPersistentDisplayDuration = 2.5
var minimumDisplayDuration = 0.5
@Published private(set) var activeNotification: UserNotification?
private(set) var notificationQueue = [UserNotification]() {
@Published private(set) var activeIndicator: UserIndicator?
private(set) var indicatorQueue = [UserIndicator]() {
didSet {
activeNotification = notificationQueue.last
activeIndicator = indicatorQueue.last
if let activeNotification, !activeNotification.persistent {
if let activeIndicator, !activeIndicator.persistent {
dismisalTimer?.invalidate()
dismisalTimer = Timer.scheduledTimer(withTimeInterval: nonPersistentDisplayDuration, repeats: false) { [weak self] _ in
self?.retractNotificationWithId(activeNotification.id)
self?.retractIndicatorWithId(activeIndicator.id)
}
}
}
@@ -45,35 +45,35 @@ class UserNotificationController: ObservableObject, UserNotificationControllerPr
func toPresentable() -> AnyView {
AnyView(
UserNotificationPresenter(userNotificationController: self, rootView: rootCoordinator.toPresentable())
UserIndicatorPresenter(userIndicatorController: self, rootView: rootCoordinator.toPresentable())
)
}
func submitNotification(_ notification: UserNotification) {
if let index = notificationQueue.firstIndex(where: { $0.id == notification.id }) {
notificationQueue[index] = notification
func submitIndicator(_ indicator: UserIndicator) {
if let index = indicatorQueue.firstIndex(where: { $0.id == indicator.id }) {
indicatorQueue[index] = indicator
} else {
retractNotificationWithId(notification.id)
notificationQueue.append(notification)
retractIndicatorWithId(indicator.id)
indicatorQueue.append(indicator)
}
displayTimes[notification.id] = .now
displayTimes[indicator.id] = .now
}
func retractAllNotifications() {
for notification in notificationQueue {
retractNotificationWithId(notification.id)
func retractAllIndicators() {
for indicator in indicatorQueue {
retractIndicatorWithId(indicator.id)
}
}
func retractNotificationWithId(_ id: String) {
func retractIndicatorWithId(_ id: String) {
guard let displayTime = displayTimes[id], abs(displayTime.timeIntervalSinceNow) <= minimumDisplayDuration else {
notificationQueue.removeAll { $0.id == id }
indicatorQueue.removeAll { $0.id == id }
return
}
Timer.scheduledTimer(withTimeInterval: minimumDisplayDuration, repeats: false) { [weak self] _ in
self?.notificationQueue.removeAll { $0.id == id }
self?.indicatorQueue.removeAll { $0.id == id }
self?.displayTimes[id] = nil
}
}
@@ -81,6 +81,6 @@ class UserNotificationController: ObservableObject, UserNotificationControllerPr
// MARK: - CustomStringConvertible
var description: String {
"UserNotificationController(\(String(describing: rootCoordinator)))"
"UserIndicatorController(\(String(describing: rootCoordinator)))"
}
}
@@ -16,8 +16,8 @@
import Foundation
protocol UserNotificationControllerProtocol: CoordinatorProtocol {
func submitNotification(_ notification: UserNotification)
func retractNotificationWithId(_ id: String)
func retractAllNotifications()
protocol UserIndicatorControllerProtocol: CoordinatorProtocol {
func submitIndicator(_ indicator: UserIndicator)
func retractIndicatorWithId(_ id: String)
func retractAllIndicators()
}
@@ -17,8 +17,8 @@
import Combine
import SwiftUI
struct UserNotificationModalView: View {
let notification: UserNotification
struct UserIndicatorModalView: View {
let indicator: UserIndicator
@State private var progressFraction: Double?
var body: some View {
@@ -31,10 +31,10 @@ struct UserNotificationModalView: View {
}
HStack {
if let iconName = notification.iconName {
if let iconName = indicator.iconName {
Image(systemName: iconName)
}
Text(notification.title)
Text(indicator.title)
.font(.element.body)
.foregroundColor(.element.primaryContent)
}
@@ -45,30 +45,30 @@ struct UserNotificationModalView: View {
.background(Color.element.quinaryContent)
.clipShape(RoundedCornerShape(radius: 12.0, corners: .allCorners))
.shadow(color: .black.opacity(0.1), radius: 10.0, y: 4.0)
.onReceive(notification.progressPublisher?.publisher ?? Empty().eraseToAnyPublisher()) { progress in
.onReceive(indicator.progressPublisher?.publisher ?? Empty().eraseToAnyPublisher()) { progress in
progressFraction = progress
}
.transition(.opacity)
}
.id(notification.id)
.id(indicator.id)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.black.opacity(0.1))
.ignoresSafeArea()
}
}
struct UserNotificationModalView_Previews: PreviewProvider {
struct UserIndicatorModalView_Previews: PreviewProvider {
static var previews: some View {
Group {
UserNotificationModalView(notification: UserNotification(type: .modal,
title: "Successfully logged in",
iconName: "checkmark")
UserIndicatorModalView(indicator: UserIndicator(type: .modal,
title: "Successfully logged in",
iconName: "checkmark")
)
.previewDisplayName("Spinner")
UserNotificationModalView(notification: UserNotification(type: .modal,
title: "Successfully logged in",
iconName: "checkmark",
progressPublisher: ProgressTracker(initialValue: 0.5))
UserIndicatorModalView(indicator: UserIndicator(type: .modal,
title: "Successfully logged in",
iconName: "checkmark",
progressPublisher: ProgressTracker(initialValue: 0.5))
)
.previewDisplayName("Progress Bar")
}
@@ -16,27 +16,27 @@
import SwiftUI
struct UserNotificationPresenter: View {
@ObservedObject var userNotificationController: UserNotificationController
struct UserIndicatorPresenter: View {
@ObservedObject var userIndicatorController: UserIndicatorController
let rootView: AnyView
var body: some View {
ZStack(alignment: .top) {
rootView
notificationViewFor(notification: userNotificationController.activeNotification)
indicatorViewFor(indicator: userIndicatorController.activeIndicator)
}
.animation(.elementDefault, value: userNotificationController.activeNotification)
.animation(.elementDefault, value: userIndicatorController.activeIndicator)
}
@ViewBuilder
private func notificationViewFor(notification: UserNotification?) -> some View {
private func indicatorViewFor(indicator: UserIndicator?) -> some View {
ZStack { // Need a container to properly animate transitions
if let notification {
switch notification.type {
if let indicator {
switch indicator.type {
case .toast:
UserNotificationToastView(notification: notification)
UserIndicatorToastView(indicator: indicator)
case .modal:
UserNotificationModalView(notification: notification)
UserIndicatorModalView(indicator: indicator)
}
}
}
@@ -16,19 +16,19 @@
import SwiftUI
struct UserNotificationToastView: View {
let notification: UserNotification
struct UserIndicatorToastView: View {
let indicator: UserIndicator
var body: some View {
HStack {
if let iconName = notification.iconName {
if let iconName = indicator.iconName {
Image(systemName: iconName)
}
Text(notification.title)
Text(indicator.title)
.font(.element.footnote)
.foregroundColor(.element.primaryContent)
}
.id(notification.id)
.id(indicator.id)
.padding(.horizontal, 12.0)
.padding(.vertical, 10.0)
.frame(minWidth: 150.0)
@@ -46,13 +46,13 @@ struct UserNotificationToastView: View {
}
}
struct UserNotificationToastView_Previews: PreviewProvider {
struct UserIndicatorToastView_Previews: PreviewProvider {
static var previews: some View {
VStack {
UserNotificationToastView(notification: UserNotification(title: "Successfully logged in",
iconName: "checkmark"))
UserIndicatorToastView(indicator: UserIndicator(title: "Successfully logged in",
iconName: "checkmark"))
UserNotificationToastView(notification: UserNotification(title: "Toast without icon"))
UserIndicatorToastView(indicator: UserIndicator(title: "Toast without icon"))
}
}
}