Add support for media uploading progress

- replace the ProgressTracker with a combination of currentValueSubjects and currentValuePublishers
This commit is contained in:
Stefan Ceriu
2023-06-21 12:55:49 +03:00
committed by Stefan Ceriu
parent 9cf2aba16d
commit 8b11a0a5ec
13 changed files with 130 additions and 150 deletions
@@ -1,39 +0,0 @@
//
// Copyright 2023 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 Foundation
protocol ProgressListener {
var progressSubject: CurrentValueSubject<Double, Never> { get }
}
protocol ProgressPublisher: AnyObject {
var publisher: AnyPublisher<Double, Never> { get }
}
final class ProgressTracker: ProgressListener, ProgressPublisher {
let progressSubject: CurrentValueSubject<Double, Never>
var publisher: AnyPublisher<Double, Never> {
progressSubject
.eraseToAnyPublisher()
}
init(initialValue: Double = 0.0) {
progressSubject = .init(initialValue)
}
}
@@ -30,13 +30,13 @@ struct UserIndicator: Equatable, Identifiable {
static func == (lhs: UserIndicator.Progress, rhs: UserIndicator.Progress) -> Bool {
switch (lhs, rhs) {
case (.indeterminate, .indeterminate): return true
case (.published(let lhsPublisher), .published(let rhsPublisher)): return lhsPublisher === rhsPublisher
case (.published(let lhsPublisher), .published(let rhsPublisher)): return lhsPublisher.value == rhsPublisher.value
default: return false
}
}
case indeterminate
case published(ProgressPublisher)
case published(CurrentValuePublisher<Double, Never>)
}
var id: String = UUID().uuidString
@@ -54,14 +54,14 @@ struct UserIndicator: Equatable, Identifiable {
}
}
var progressPublisher: AnyPublisher<Double, Never> {
var progressPublisher: CurrentValuePublisher<Double, Never> {
switch type {
case .toast(let progress), .modal(let progress, _):
switch progress {
case .none, .indeterminate:
return Empty().eraseToAnyPublisher()
case .some(.published(let progress)):
return progress.publisher.eraseToAnyPublisher()
return CurrentValueSubject<Double, Never>(0.0).asCurrentValuePublisher()
case .some(.published(let publisher)):
return publisher
}
}
}
@@ -69,7 +69,7 @@ struct UserIndicatorModalView_Previews: PreviewProvider {
)
.previewDisplayName("Spinner")
UserIndicatorModalView(indicator: UserIndicator(type: .modal(progress: .published(ProgressTracker(initialValue: 0.5)),
UserIndicatorModalView(indicator: UserIndicator(type: .modal(progress: .published(CurrentValueSubject<Double, Never>(0.5).asCurrentValuePublisher()),
interactiveDismissDisabled: false),
title: "Successfully logged in",
iconName: "checkmark")