2022-07-06 14:49:05 +01:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2022-06-13 17:28:36 +01:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2022-06-13 17:28:36 +01:00
|
|
|
//
|
|
|
|
|
2023-05-30 12:32:07 +02:00
|
|
|
import Combine
|
2023-02-14 11:09:55 +01:00
|
|
|
import Foundation
|
2022-06-13 17:28:36 +01:00
|
|
|
|
2023-05-31 12:56:58 +02:00
|
|
|
enum UserIndicatorType: Equatable {
|
2023-06-16 17:41:25 +01:00
|
|
|
case toast(progress: UserIndicator.Progress?)
|
2023-06-28 14:59:57 +03:00
|
|
|
case modal(progress: UserIndicator.Progress?, interactiveDismissDisabled: Bool, allowsInteraction: Bool)
|
2023-05-31 12:56:58 +02:00
|
|
|
|
2023-06-16 17:41:25 +01:00
|
|
|
static var toast: Self { .toast(progress: .none) }
|
2023-06-28 14:59:57 +03:00
|
|
|
static var modal: Self { .modal(progress: .indeterminate, interactiveDismissDisabled: false, allowsInteraction: false) }
|
2022-11-16 15:37:34 +02:00
|
|
|
}
|
2022-06-13 17:28:36 +01:00
|
|
|
|
2023-02-14 11:09:55 +01:00
|
|
|
struct UserIndicator: Equatable, Identifiable {
|
2023-06-16 17:41:25 +01:00
|
|
|
enum Progress: Equatable {
|
|
|
|
static func == (lhs: UserIndicator.Progress, rhs: UserIndicator.Progress) -> Bool {
|
|
|
|
switch (lhs, rhs) {
|
|
|
|
case (.indeterminate, .indeterminate): return true
|
2023-06-21 12:55:49 +03:00
|
|
|
case (.published(let lhsPublisher), .published(let rhsPublisher)): return lhsPublisher.value == rhsPublisher.value
|
2023-06-16 17:41:25 +01:00
|
|
|
default: return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case indeterminate
|
2023-06-21 12:55:49 +03:00
|
|
|
case published(CurrentValuePublisher<Double, Never>)
|
2023-05-30 12:32:07 +02:00
|
|
|
}
|
|
|
|
|
2022-11-16 15:37:34 +02:00
|
|
|
var id: String = UUID().uuidString
|
2023-06-16 17:41:25 +01:00
|
|
|
var type: UserIndicatorType = .toast
|
2022-11-16 15:37:34 +02:00
|
|
|
var title: String
|
|
|
|
var iconName: String?
|
|
|
|
var persistent = false
|
2023-06-16 17:41:25 +01:00
|
|
|
|
|
|
|
// MARK: - Associated values from the type
|
|
|
|
|
|
|
|
var progress: Progress? {
|
|
|
|
switch type {
|
|
|
|
case .toast(let progress): return progress
|
2023-06-28 14:59:57 +03:00
|
|
|
case .modal(let progress, _, _): return progress
|
2023-06-16 17:41:25 +01:00
|
|
|
}
|
|
|
|
}
|
2023-05-30 12:32:07 +02:00
|
|
|
|
2023-06-21 12:55:49 +03:00
|
|
|
var progressPublisher: CurrentValuePublisher<Double, Never> {
|
2023-06-16 17:41:25 +01:00
|
|
|
switch type {
|
2023-06-28 14:59:57 +03:00
|
|
|
case .toast(let progress), .modal(let progress, _, _):
|
2023-06-16 17:41:25 +01:00
|
|
|
switch progress {
|
|
|
|
case .none, .indeterminate:
|
2023-06-21 12:55:49 +03:00
|
|
|
return CurrentValueSubject<Double, Never>(0.0).asCurrentValuePublisher()
|
|
|
|
case .some(.published(let publisher)):
|
|
|
|
return publisher
|
2023-06-16 17:41:25 +01:00
|
|
|
}
|
2023-05-30 12:32:07 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 12:56:58 +02:00
|
|
|
|
|
|
|
var interactiveDismissDisabled: Bool {
|
|
|
|
switch type {
|
|
|
|
case .toast:
|
|
|
|
return false
|
2023-06-28 14:59:57 +03:00
|
|
|
case .modal(_, let interactiveDismissDisabled, _):
|
2023-05-31 12:56:58 +02:00
|
|
|
return interactiveDismissDisabled
|
|
|
|
}
|
|
|
|
}
|
2023-06-28 14:59:57 +03:00
|
|
|
|
|
|
|
var allowsInteraction: Bool {
|
|
|
|
switch type {
|
|
|
|
case .toast:
|
|
|
|
return true
|
|
|
|
case .modal(_, _, let allowsInteraction):
|
|
|
|
return allowsInteraction
|
|
|
|
}
|
|
|
|
}
|
2022-06-13 17:28:36 +01:00
|
|
|
}
|