-
Notifications
You must be signed in to change notification settings - Fork 6
/
SakatsuListScreen.swift
111 lines (104 loc) · 3.37 KB
/
SakatsuListScreen.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
import SwiftUI
import SakatsuData
public struct SakatsuListScreen: View {
@StateObject private var viewModel: SakatsuListViewModel<SakatsuUserDefaultsClient>
public var body: some View {
NavigationView {
SakatsuListView(
sakatsus: viewModel.uiState.sakatsus,
onCopySakatsuTextButtonClick: { sakatsuIndex in
viewModel.onCopySakatsuTextButtonClick(sakatsuIndex: sakatsuIndex)
}, onEditButtonClick: { sakatsuIndex in
viewModel.onEditButtonClick(sakatsuIndex: sakatsuIndex)
}, onDelete: { offsets in
viewModel.onDelete(at: offsets)
}
)
.navigationTitle("サ活一覧")
.sakatsuListScreenToolbar(
onAddButtonClick: { viewModel.onAddButtonClick() }
)
.sakatsuInputSheet(
shouldShowSheet: viewModel.uiState.shouldShowInputSheet,
selectedSakatsu: viewModel.uiState.selectedSakatsu,
onDismiss: { viewModel.onInputSheetDismiss() },
onSakatsuSave: { viewModel.onSakatsuSave() }
)
.copyingSakatsuTextAlert(
sakatsuText: viewModel.uiState.sakatsuText,
onDismiss: { viewModel.onCopyingSakatsuTextAlertDismiss() }
)
.errorAlert(
error: viewModel.uiState.sakatsuListError,
onDismiss: { viewModel.onErrorAlertDismiss() }
)
}
}
public init() {
self._viewModel = StateObject(wrappedValue: SakatsuListViewModel())
}
}
private extension View {
func sakatsuListScreenToolbar(
onAddButtonClick: @escaping () -> Void
) -> some View {
toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
onAddButtonClick()
} label: {
Image(systemName: "plus")
}
}
ToolbarItem(placement: .navigationBarLeading) {
EditButton()
}
}
}
func sakatsuInputSheet(
shouldShowSheet: Bool,
selectedSakatsu: Sakatsu?,
onDismiss: @escaping () -> Void,
onSakatsuSave: @escaping () -> Void
) -> some View {
sheet(isPresented: .init(get: {
shouldShowSheet
}, set: { _ in
onDismiss()
})) {
NavigationView {
SakatsuInputScreen(
sakatsu: selectedSakatsu,
onSakatsuSave: onSakatsuSave
)
}
}
}
func copyingSakatsuTextAlert(
sakatsuText: String?,
onDismiss: @escaping () -> Void
) -> some View {
alert(
"コピー",
isPresented: .init(get: {
sakatsuText != nil
}, set: { _ in
onDismiss()
}),
presenting: sakatsuText
) { _ in
} message: { sakatsuText in
Text("サ活のテキストをコピーしました。")
.onAppear {
UIPasteboard.general.string = sakatsuText
}
}
}
}
#if DEBUG
struct SakatsuListScreen_Previews: PreviewProvider {
static var previews: some View {
SakatsuListScreen()
}
}
#endif