-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
AddTagButtomSheetView.swift
103 lines (93 loc) · 3.44 KB
/
AddTagButtomSheetView.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
//
// AddTagButtomSheetView.swift
// Blogger
//
// Created by 莊智凱 on 2022/1/19.
//
import SwiftUI
struct AddTagButtomSheetView: View {
@Environment(\.colorScheme) private var colorScheme
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Tag.timestamp, ascending: true)], animation: .default) private var tags: FetchedResults<Tag>
@FocusState private var isFocused: Bool
@State private var inputTag = ""
@State private var inCoreData = false
@Binding var showAddTagButtomSheet: Bool
@Binding var showNonInputTagAlert: Bool
@Binding var showAlreadyHaveTagAlert: Bool
var body: some View {
VStack {
HStack {
Text("Customize your interests").bold().frame(maxWidth: .infinity)
.overlay {
HStack {
Button {
showAddTagButtomSheet = false
} label: {
Text(Image(systemName: "xmark")).bold()
}
Spacer()
}
}
}.font(.title3)
Spacer()
Text("Input a word you want to add as a tag on tap of Home").multilineTextAlignment(.center)
TextField("Input a tag...", text: $inputTag)
.padding(7)
.background(colorScheme == .light ? Color(red: 0.949, green: 0.949, blue: 0.949) : Color(red: 41/255, green: 41/255, blue: 41/255))
.cornerRadius(8)
.padding()
.focused($isFocused)
.onSubmit(of: .search) {
if inputTag != "" {
inCoreData = false
tags.forEach { tag in
if tag.tag! == inputTag {
inCoreData = true
}
}
if inCoreData == true {
showAlreadyHaveTagAlert = true
} else {
addTag(tag: inputTag)
}
}
}
Button {
if inputTag != "" {
inCoreData = false
tags.forEach { tag in
if tag.tag! == inputTag {
inCoreData = true
}
}
if inCoreData == true {
showAlreadyHaveTagAlert = true
} else { addTag(tag: inputTag) }
} else { showNonInputTagAlert = true }
showAddTagButtomSheet = false
} label: {
Text("Done")
.padding(.vertical)
.padding(.horizontal, 100)
.foregroundColor(.white)
.background(Color.accentColor)
.clipShape(Capsule())
}
.padding()
Spacer()
}
.padding()
}
private func addTag(tag: String) {
withAnimation {
let newTag = Tag(context: viewContext)
newTag.timestamp = Date()
newTag.tag = tag
do {
try viewContext.save()
} catch {
}
}
}
}