-
Notifications
You must be signed in to change notification settings - Fork 134
/
ImportDetailsView.swift
139 lines (121 loc) · 5.2 KB
/
ImportDetailsView.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import SwiftUI
struct ImportDetailsView: View {
enum OPMLImportResult {
case none
case success
case failure
}
@EnvironmentObject var theme: Theme
@Environment(\.presentationMode) var presentationMode
@State var opmlURLText = ""
@State var opmlURLImportResult: OPMLImportResult = .none
@State var opmlImportInProgress: Bool = false
@State var opmlButtonTitle: String = L10n.import
let importSource: ImportViewModel.ImportSource
let viewModel: ImportViewModel
var body: some View {
VStack(alignment: .leading, spacing: 16) {
ScrollViewIfNeeded {
VStack(alignment: .leading, spacing: 16) {
Image(importSource.iconName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 64, height: 64)
.cornerRadius(16)
.shadow(color: .black.opacity(0.2), radius: 3, x: 0, y: 1)
Text(L10n.importInstructionsImportFrom(importSource.displayName))
.font(size: 31, style: .largeTitle, weight: .bold, maxSizeCategory: .extraExtraExtraLarge)
.foregroundColor(AppTheme.color(for: .primaryText01, theme: theme))
.fixedSize(horizontal: false, vertical: true)
appInstructions
if importSource.id == .opmlFromURL {
opmlImportView
}
Spacer()
}.padding([.leading, .trailing], Constants.horizontalPadding)
}
// Hide button for other
if !importSource.hideButton {
if importSource.id == .opmlFromURL {
opmlViewButton
} else {
Button(importSource.id == .applePodcasts ? L10n.importInstructionsInstallShortcut : L10n.importInstructionsOpenIn(importSource.displayName)) {
viewModel.openApp(importSource)
}
.buttonStyle(RoundedButtonStyle(theme: theme))
.padding([.leading, .trailing], Constants.horizontalPadding)
}
}
}.padding(.top, 16).padding(.bottom)
.background(AppTheme.color(for: .primaryUi01, theme: theme).ignoresSafeArea())
}
@ViewBuilder
private var appInstructions: some View {
let lines = importSource.steps.split(separator: "\n").map { String($0).trim() }
VStack(alignment: .leading, spacing: 20) {
ForEach(lines, id: \.self) { line in
Text(line)
.font(style: .subheadline, maxSizeCategory: .extraExtraExtraLarge)
.foregroundColor(AppTheme.color(for: .primaryText01, theme: theme))
.fixedSize(horizontal: false, vertical: true)
}
}
}
@ViewBuilder
private var opmlImportView: some View {
VStack {
TextField("https://...", text: $opmlURLText)
.autocapitalization(.none)
.requiredStyle(opmlURLImportResult == .failure)
.keyboardType(.URL)
switch opmlURLImportResult {
case .none: Text("")
case .success:
Text(L10n.opmlImportSucceededTitle)
.foregroundColor((ThemeColor.support02(for: theme.activeTheme).color))
case .failure:
Text(L10n.opmlImportFailedTitle)
.foregroundColor(ThemeColor.support05(for: theme.activeTheme).color)
}
if opmlImportInProgress {
ProgressView(L10n.opmlImporting)
}
}
}
private var opmlViewButton: some View {
Button(action: {
if opmlURLImportResult == .success {
viewModel.navigationController?.dismiss(animated: true)
return
}
opmlURLImportResult = .none
NotificationCenter.default.addObserver(forName: Notification.Name("SJOpmlImportCompleted"), object: nil, queue: nil) { notification in
opmlURLImportResult = .success
opmlImportInProgress = false
}
NotificationCenter.default.addObserver(forName: Notification.Name("SJOpmlImportFailed"), object: nil, queue: nil) { notification in
opmlURLImportResult = .failure
opmlImportInProgress = false
}
guard let url = URL(string: opmlURLText) else {
opmlURLImportResult = .failure
opmlImportInProgress = false
return
}
opmlImportInProgress = true
viewModel.importFromURL(url) { success in
if !success {
opmlURLImportResult = .failure
opmlImportInProgress = false
}
}
}, label: {
Text(opmlURLImportResult == .success ? L10n.done : L10n.import)
})
.buttonStyle(RoundedButtonStyle(theme: theme))
.padding([.leading, .trailing], Constants.horizontalPadding)
}
private enum Constants {
static let horizontalPadding = 24.0
}
}