This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathIndeterminateExample.swift
78 lines (68 loc) · 2.38 KB
/
IndeterminateExample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// IndeterminateExample.swift
// UICircularProgressRingExample
//
// Created by Luis on 5/30/20.
// Copyright © 2020 Luis. All rights reserved.
//
import Combine
import UICircularProgressRing
import SwiftUI
struct IndeterminateExample: View {
@State private var progress: RingProgress = .percent(0)
private let onDidTapSubject = PassthroughSubject<Void, Never>()
private var onDidTapPublisher: AnyPublisher<Void, Never> {
onDidTapSubject.eraseToAnyPublisher()
}
private let onDidTapIndeterminateSubject = PassthroughSubject<Void, Never>()
private var onDidTapIndeterminatePublisher: AnyPublisher<Void, Never> {
onDidTapIndeterminateSubject.eraseToAnyPublisher()
}
private var progressPublisher: AnyPublisher<RingProgress, Never> {
onDidTapPublisher
.map {
self.progress == .percent(1) || self.progress.isIndeterminate ?
RingProgress.percent(0) :
RingProgress.percent(1)
}
.merge(with: onDidTapIndeterminateSubject.map { RingProgress.indeterminate })
.prepend(progress)
.eraseToAnyPublisher()
}
var body: some View {
VStack {
ProgressRing(progress: $progress)
.animation(.easeInOut(duration: 5))
.padding(32)
HStack {
Button(action: { self.onDidTapSubject.send(()) }) {
buttonLabel
}
.padding(16)
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(8)
.offset(y: -32)
Button(action: { self.onDidTapIndeterminateSubject.send(()) }) {
Text("Make Indeterminate")
}
.padding(16)
.foregroundColor(.white)
.background(Color.red)
.cornerRadius(8)
.offset(y: -32)
}
}
.navigationBarTitle("Indeterminate")
.onReceive(progressPublisher) { progress in
self.progress = progress
}
}
private var buttonLabel: some View {
if progress == .percent(1) || progress.isIndeterminate {
return Text("Restart Progress")
} else {
return Text("Start Progress")
}
}
}