mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:55:59 +00:00
547 refactored userNotification into userIndicator
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class MockUserIndicatorController: UserIndicatorControllerProtocol {
|
||||
func submitIndicator(_ indicator: UserIndicator) { }
|
||||
|
||||
func retractIndicatorWithId(_ id: String) { }
|
||||
|
||||
func retractAllIndicators() { }
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum UserIndicatorType {
|
||||
case toast
|
||||
case modal
|
||||
}
|
||||
|
||||
struct UserIndicator: Equatable, Identifiable {
|
||||
static func == (lhs: UserIndicator, rhs: UserIndicator) -> Bool {
|
||||
lhs.id == rhs.id &&
|
||||
lhs.type == rhs.type &&
|
||||
lhs.title == rhs.title &&
|
||||
lhs.iconName == rhs.iconName &&
|
||||
lhs.persistent == rhs.persistent
|
||||
}
|
||||
|
||||
var id: String = UUID().uuidString
|
||||
var type = UserIndicatorType.toast
|
||||
var title: String
|
||||
var iconName: String?
|
||||
var persistent = false
|
||||
var progressPublisher: ProgressPublisher?
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
class UserIndicatorController: ObservableObject, UserIndicatorControllerProtocol, CustomStringConvertible {
|
||||
private let rootCoordinator: CoordinatorProtocol
|
||||
|
||||
private var dismisalTimer: Timer?
|
||||
private var displayTimes = [String: Date]()
|
||||
|
||||
var nonPersistentDisplayDuration = 2.5
|
||||
var minimumDisplayDuration = 0.5
|
||||
|
||||
@Published private(set) var activeIndicator: UserIndicator?
|
||||
private(set) var indicatorQueue = [UserIndicator]() {
|
||||
didSet {
|
||||
activeIndicator = indicatorQueue.last
|
||||
|
||||
if let activeIndicator, !activeIndicator.persistent {
|
||||
dismisalTimer?.invalidate()
|
||||
dismisalTimer = Timer.scheduledTimer(withTimeInterval: nonPersistentDisplayDuration, repeats: false) { [weak self] _ in
|
||||
self?.retractIndicatorWithId(activeIndicator.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init(rootCoordinator: CoordinatorProtocol) {
|
||||
self.rootCoordinator = rootCoordinator
|
||||
}
|
||||
|
||||
func toPresentable() -> AnyView {
|
||||
AnyView(
|
||||
UserIndicatorPresenter(userIndicatorController: self, rootView: rootCoordinator.toPresentable())
|
||||
)
|
||||
}
|
||||
|
||||
func submitIndicator(_ indicator: UserIndicator) {
|
||||
if let index = indicatorQueue.firstIndex(where: { $0.id == indicator.id }) {
|
||||
indicatorQueue[index] = indicator
|
||||
} else {
|
||||
retractIndicatorWithId(indicator.id)
|
||||
indicatorQueue.append(indicator)
|
||||
}
|
||||
|
||||
displayTimes[indicator.id] = .now
|
||||
}
|
||||
|
||||
func retractAllIndicators() {
|
||||
for indicator in indicatorQueue {
|
||||
retractIndicatorWithId(indicator.id)
|
||||
}
|
||||
}
|
||||
|
||||
func retractIndicatorWithId(_ id: String) {
|
||||
guard let displayTime = displayTimes[id], abs(displayTime.timeIntervalSinceNow) <= minimumDisplayDuration else {
|
||||
indicatorQueue.removeAll { $0.id == id }
|
||||
return
|
||||
}
|
||||
|
||||
Timer.scheduledTimer(withTimeInterval: minimumDisplayDuration, repeats: false) { [weak self] _ in
|
||||
self?.indicatorQueue.removeAll { $0.id == id }
|
||||
self?.displayTimes[id] = nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - CustomStringConvertible
|
||||
|
||||
var description: String {
|
||||
"UserIndicatorController(\(String(describing: rootCoordinator)))"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol UserIndicatorControllerProtocol: CoordinatorProtocol {
|
||||
func submitIndicator(_ indicator: UserIndicator)
|
||||
func retractIndicatorWithId(_ id: String)
|
||||
func retractAllIndicators()
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
struct UserIndicatorModalView: View {
|
||||
let indicator: UserIndicator
|
||||
@State private var progressFraction: Double?
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
VStack(spacing: 12.0) {
|
||||
if let progressFraction = progressFraction {
|
||||
ProgressView(value: progressFraction)
|
||||
} else {
|
||||
ProgressView()
|
||||
}
|
||||
|
||||
HStack {
|
||||
if let iconName = indicator.iconName {
|
||||
Image(systemName: iconName)
|
||||
}
|
||||
Text(indicator.title)
|
||||
.font(.element.body)
|
||||
.foregroundColor(.element.primaryContent)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.frame(minWidth: 150.0)
|
||||
.fixedSize(horizontal: true, vertical: false)
|
||||
.background(Color.element.quinaryContent)
|
||||
.clipShape(RoundedCornerShape(radius: 12.0, corners: .allCorners))
|
||||
.shadow(color: .black.opacity(0.1), radius: 10.0, y: 4.0)
|
||||
.onReceive(indicator.progressPublisher?.publisher ?? Empty().eraseToAnyPublisher()) { progress in
|
||||
progressFraction = progress
|
||||
}
|
||||
.transition(.opacity)
|
||||
}
|
||||
.id(indicator.id)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.background(.black.opacity(0.1))
|
||||
.ignoresSafeArea()
|
||||
}
|
||||
}
|
||||
|
||||
struct UserIndicatorModalView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Group {
|
||||
UserIndicatorModalView(indicator: UserIndicator(type: .modal,
|
||||
title: "Successfully logged in",
|
||||
iconName: "checkmark")
|
||||
)
|
||||
.previewDisplayName("Spinner")
|
||||
UserIndicatorModalView(indicator: UserIndicator(type: .modal,
|
||||
title: "Successfully logged in",
|
||||
iconName: "checkmark",
|
||||
progressPublisher: ProgressTracker(initialValue: 0.5))
|
||||
)
|
||||
.previewDisplayName("Progress Bar")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct UserIndicatorPresenter: View {
|
||||
@ObservedObject var userIndicatorController: UserIndicatorController
|
||||
let rootView: AnyView
|
||||
|
||||
var body: some View {
|
||||
ZStack(alignment: .top) {
|
||||
rootView
|
||||
indicatorViewFor(indicator: userIndicatorController.activeIndicator)
|
||||
}
|
||||
.animation(.elementDefault, value: userIndicatorController.activeIndicator)
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func indicatorViewFor(indicator: UserIndicator?) -> some View {
|
||||
ZStack { // Need a container to properly animate transitions
|
||||
if let indicator {
|
||||
switch indicator.type {
|
||||
case .toast:
|
||||
UserIndicatorToastView(indicator: indicator)
|
||||
case .modal:
|
||||
UserIndicatorModalView(indicator: indicator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct UserIndicatorToastView: View {
|
||||
let indicator: UserIndicator
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
if let iconName = indicator.iconName {
|
||||
Image(systemName: iconName)
|
||||
}
|
||||
Text(indicator.title)
|
||||
.font(.element.footnote)
|
||||
.foregroundColor(.element.primaryContent)
|
||||
}
|
||||
.id(indicator.id)
|
||||
.padding(.horizontal, 12.0)
|
||||
.padding(.vertical, 10.0)
|
||||
.frame(minWidth: 150.0)
|
||||
.background(Color.element.quaternaryContent)
|
||||
.clipShape(RoundedCornerShape(radius: 24.0, corners: .allCorners))
|
||||
.shadow(color: .black.opacity(0.1), radius: 10.0, y: 4.0)
|
||||
.transition(toastTransition)
|
||||
}
|
||||
|
||||
private var toastTransition: AnyTransition {
|
||||
AnyTransition
|
||||
.asymmetric(insertion: .move(edge: .top),
|
||||
removal: .move(edge: .bottom))
|
||||
.combined(with: .opacity)
|
||||
}
|
||||
}
|
||||
|
||||
struct UserIndicatorToastView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
VStack {
|
||||
UserIndicatorToastView(indicator: UserIndicator(title: "Successfully logged in",
|
||||
iconName: "checkmark"))
|
||||
|
||||
UserIndicatorToastView(indicator: UserIndicator(title: "Toast without icon"))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user