-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
PostView.swift
203 lines (193 loc) · 7.57 KB
/
PostView.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// PostView.swift
// Blogger
//
// Created by 莊智凱 on 2022/1/8.
//
import SwiftUI
import CoreData
struct PostView: View {
@AppStorage("preferColorScheme") var preferColorScheme: String = "System"
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \SavedPost.timestamp, ascending: true)], animation: .default) private var SavedPosts: FetchedResults<SavedPost>
@Environment(\.colorScheme) private var colorScheme
@State private var showShareSheet = false
@State private var savedInCoreData = false
@State private var showMore = false
@State private var authorName = ""
@State private var postDate = Date()
@State private var showDisplaySetting = false
@State private var ScreenBright = 0.0
@Binding var readPost: Bool
@Binding var postUrl: String
@Binding var entry: Entries
var body: some View {
VStack {
HStack {
ForEach(entry.author, id: \.avatar.src) { author in
HStack {
AsyncImage(url: URL(string: author.avatar.src.hasPrefix("https:") ? author.avatar.src : "https:" + author.avatar.src)) { image in
image
.resizable()
.scaledToFit()
} placeholder: {
Color.gray.overlay { ProgressView() }
}
.frame(width: 25, height: 25)
.clipShape(Circle())
}
}
( Text(authorName) + Text(" in ") + Text(postDate, format: .dateTime.year().month().day()) )
.font(.subheadline).foregroundColor(colorScheme == .light ? .black : .white)
.onAppear {
entry.author.forEach { author in
authorName = author.name.name
}
postDate = entry.published.published
}
Spacer()
Button {
readPost = false
} label: {
Image(systemName: "multiply.circle.fill")
.resizable()
.scaledToFit()
.frame(width: 25, height: 25)
.foregroundColor(.gray)
}
}
.padding(.horizontal)
BrowserView(url: $postUrl)
Spacer()
HStack {
Button {
if postUrl.hasSuffix("#comments") {
postUrl = String(postUrl.dropLast(9))
postUrl += "#comments"
} else {
postUrl += "#comments"
}
} label: {
Image(systemName: "message")
.resizable()
.scaledToFit()
.frame(width: 25, height: 25)
}
.frame(maxWidth: .infinity)
Button {
showShareSheet = true
} label: {
Image(systemName: "square.and.arrow.up")
.resizable()
.scaledToFit()
.frame(width: 25, height: 25)
}
.frame(maxWidth: .infinity)
BookmarkView(entry: $entry)
.frame(width: 25, height: 25)
.frame(maxWidth: .infinity)
.foregroundColor(.accentColor)
.onAppear {
savedInCoreData = false
SavedPosts.forEach { SavedPost in
if entry.id.id == SavedPost.id! {
savedInCoreData = true
}
}
entry.isSave = savedInCoreData
}
Button {
showMore = true
} label: {
Image(systemName: "ellipsis")
.resizable()
.scaledToFit()
.frame(width: 25, height: 25)
}
.frame(maxWidth: .infinity)
}
.padding(.vertical, 5)
}
.sheet(isPresented: $showShareSheet) {
ShareSheet(activityItems: [URL(string: postUrl) ?? postUrl])
}
.actionSheet(isPresented: $showMore) {
ActionSheet(
title: Text("Author: \(authorName)"),
message: ( Text("Post on: ") + Text(postDate, format: .dateTime.year().month().day()) ),
buttons: [
.default(
Text("Display Setting"),
action: { showDisplaySetting = true }
),
.default(
Text("Comment"),
action: {
if postUrl.hasSuffix("#comments") {
postUrl = String(postUrl.dropLast(9))
postUrl += "#comments"
} else {
postUrl += "#comments"
}
}
),
.default(
Text("Share"),
action: { showShareSheet = true }
),
.cancel()
]
)
}
.bottomSheet(isPresented: $showDisplaySetting) {
VStack {
Text("Display Settings")
.bold()
.font(.title3)
.frame(maxWidth: .infinity)
.overlay(
Button {
showDisplaySetting = false
} label: {
Text(Image(systemName: "xmark"))
.bold()
.font(.title3)
}
, alignment: .leading
)
Divider()
HStack {
Text(Image(systemName: "sun.max"))
Slider(value: $ScreenBright, in: 0...1)
.onAppear {
ScreenBright = UIScreen.main.brightness
}
.onChange(of: UIScreen.main.brightness) { _ in
ScreenBright = UIScreen.main.brightness
}
Text(Image(systemName: "sun.max"))
}
Divider()
HStack {
Text("Appearance")
Spacer()
Picker(selection: $preferColorScheme) {
Text("System").tag("System")
Text("Light").tag("Light")
Text("Dark").tag("Dark")
} label: {
Text("Appearance")
}
.pickerStyle(.segmented)
.frame(width: 180)
}
Spacer()
}
.padding()
}
.onChange(of: ScreenBright) { _ in
UIScreen.main.brightness = ScreenBright
}
.preferredColorScheme(preferColorScheme == "System" ? colorScheme : preferColorScheme == "Dark" ? .dark : .light)
}
}