-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentViewPersistence.swift
More file actions
159 lines (142 loc) · 5.24 KB
/
ContentViewPersistence.swift
File metadata and controls
159 lines (142 loc) · 5.24 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// ContentViewPersistence.swift
// SwiftUIDemo
//
// Created by 墨枫 on 2024/1/9.
//
import SwiftUI
struct ContentViewPersistence: View {
@StateObject var viewModel3 = ViewModel3()
var body: some View {
NavigationView {
ScrollView {
/*
NoteCardItemView(id: UUID(), content: "纵然敌众我寡,吾亦一往无前。", updateTime: "2023-03-10")
NoteCardItemView(id: UUID(), content: "躲起来的星星,其实也在努力发光。", updateTime: "2023-03-10")*/
ForEach(viewModel3.noteModels) { item in
NoteCardItemView(id: item.id, content: item.content, updateTime: item.updateTime)
.contextMenu {
Button("删除") {
viewModel3.deleteItem(itemId: item.id)
}
}
.onTapGesture {
viewModel3.isEdit = true
viewModel3.noteId = item.id
viewModel3.content = item.content
viewModel3.showNewNoteView.toggle()
}
}
}
.navigationBarTitle("我的笔记", displayMode: .inline)
.navigationBarItems(leading: darkModeBtn, trailing: addBtn)
}
.preferredColorScheme(viewModel3.darkMode ? .dark : .light)
.sheet(isPresented: $viewModel3.showNewNoteView) {
NewNoteView(id: viewModel3.noteId, content: viewModel3.content, showNewNoteView: $viewModel3.showNewNoteView,
viewModel3: viewModel3)
}
}
private var addBtn: some View {
Image(systemName: "plus.circle.fill")
.font(.system(size: 20))
.foregroundColor(.blue)
.onTapGesture {
viewModel3.isEdit = false
viewModel3.noteId = UUID()
viewModel3.content = ""
viewModel3.showNewNoteView.toggle()
}
}
private var darkModeBtn: some View {
Image(systemName: viewModel3.darkMode ? "sun.max.circle.fill" : "moon.circle.fill")
.font(.system(size: 20))
.foregroundColor(viewModel3.darkMode ? .white : .gray)
.onTapGesture {
viewModel3.darkMode.toggle()
}
}
}
struct NoteCardItemView: View {
var id: UUID
var content: String
var updateTime: String
var body: some View {
HStack {
VStack(alignment: .leading) {
Text(content)
.font(.system(size: 17))
.lineLimit(3)
Spacer()
HStack {
Spacer()
Text(updateTime)
.font(.system(size: 14))
.foregroundColor(.gray)
}
}
.padding()
Spacer()
}
.frame(maxWidth: .infinity, minHeight: 100, maxHeight: 140)
.background(Color(.systemGray6))
.cornerRadius(8)
.padding(.horizontal)
}
}
struct NewNoteView: View {
@State var id: UUID
@State var content: String = ""
@Binding var showNewNoteView: Bool
var viewModel3 = ViewModel3()
var body: some View {
NavigationView {
VStack {
ZStack(alignment: .topLeading) {
TextEditor(text: $content)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color(.systemGray5), lineWidth: 1)
)
if content.isEmpty {
Text("请输入内容")
.foregroundColor(Color(UIColor.placeholderText))
.padding(10)
}
}
.frame(height: 320)
.padding()
Spacer()
}
.navigationBarTitle(viewModel3.isEdit ? "编辑笔记" : "新增笔记", displayMode: .inline)
.navigationBarItems(leading: closeBtn, trailing: saveBtn)
.preferredColorScheme(viewModel3.darkMode ? .dark: .light)
}
}
private var closeBtn: some View {
Image(systemName: "xmark.circle.fill")
.font(.system(size: 20))
.foregroundColor(.gray)
.onTapGesture {
self.showNewNoteView.toggle()
}
}
private var saveBtn: some View {
Text("保存")
.font(.system(size: 20))
.foregroundColor(.blue)
.onTapGesture {
if viewModel3.isEdit {
let updateItem = NoteModel2(id: id, content: content,
updateTime: viewModel3.getTime())
viewModel3.editItem(item: updateItem)
} else {
viewModel3.addItem(content: content, updateTime: viewModel3.getTime())
}
self.showNewNoteView.toggle()
}
}
}
#Preview {
ContentViewPersistence()
}