-
Notifications
You must be signed in to change notification settings - Fork 134
/
NameFolderView.swift
79 lines (64 loc) · 2.27 KB
/
NameFolderView.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
import PocketCastsDataModel
import SwiftUI
struct NameFolderView: View {
enum Field: Hashable {
case name
}
@EnvironmentObject var theme: Theme
@ObservedObject var model: FolderModel
@State var focusOnTextField = false
var dismissAction: (String?) -> Void
var numberOfSelectedPodcasts = 0
var body: some View {
VStack(alignment: .leading) {
Text(L10n.name.localizedUppercase)
.textStyle(SecondaryText())
.font(.subheadline)
.onChange(of: model.name, perform: model.validateFolderName)
TextField(L10n.folderName, text: $model.name)
.focusMe(state: $focusOnTextField)
.themedTextField()
Spacer()
NavigationLink(destination: ColorPreviewFolderView(model: model, dismissAction: dismissAction)) {
Text(L10n.continue)
.textStyle(RoundedButton())
}
}
.padding()
.navigationTitle(L10n.folderNameTitle)
.onAppear {
// this appears to be a known issue with SwiftUI, in that it just passes this onto UIKit which can't set focus while a view is appearing, so here we artificially delay it
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
focusOnTextField = true
}
Analytics.track(.folderCreateNameShown, properties: ["number_of_podcasts": numberOfSelectedPodcasts])
}
.applyDefaultThemeOptions()
}
}
struct NameFolderView_Previews: PreviewProvider {
static var previews: some View {
NameFolderView(model: FolderModel(), dismissAction: { _ in })
.environmentObject(Theme(previewTheme: .light))
}
}
// MARK: - FocusState wrapper
struct FocusModifier: ViewModifier {
@FocusState var focused: Bool
@Binding var state: Bool
init(_ state: Binding<Bool>) {
_state = state
}
func body(content: Content) -> some View {
content.focused($focused, equals: true)
.onChange(of: state, perform: changeFocus)
}
private func changeFocus(_ value: Bool) {
focused = value
}
}
extension View {
func focusMe(state: Binding<Bool>) -> some View {
modifier(FocusModifier(state))
}
}