-
-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathComposeContentView.swift
More file actions
287 lines (271 loc) · 12.9 KB
/
ComposeContentView.swift
File metadata and controls
287 lines (271 loc) · 12.9 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// ComposeContentView.swift
//
//
// Created by MainasuK on 22/9/30.
//
import os.log
import SwiftUI
import MastodonAsset
import MastodonCore
import MastodonLocalization
import Stripes
public struct ComposeContentView: View {
static let logger = Logger(subsystem: "ComposeContentView", category: "View")
var logger: Logger { ComposeContentView.logger }
static let contentViewCoordinateSpace = "ComposeContentView.Content"
static var margin: CGFloat = 16
@ObservedObject var viewModel: ComposeContentViewModel
public var body: some View {
VStack(spacing: .zero) {
Group {
// content warning
if viewModel.isContentWarningActive {
MetaTextViewRepresentable(
string: $viewModel.contentWarning,
width: viewModel.viewLayoutFrame.layoutFrame.width - ComposeContentView.margin * 2,
configurationHandler: { metaText in
viewModel.contentWarningMetaText = metaText
metaText.textView.attributedPlaceholder = {
var attributes = metaText.textAttributes
attributes[.foregroundColor] = UIColor.secondaryLabel
return NSAttributedString(
string: L10n.Scene.Compose.contentInputPlaceholder,
attributes: attributes
)
}()
metaText.textView.returnKeyType = .next
metaText.textView.tag = ComposeContentViewModel.MetaTextViewKind.contentWarning.rawValue
metaText.textView.delegate = viewModel
metaText.delegate = viewModel
}
)
.fixedSize(horizontal: false, vertical: true)
.padding(.horizontal, ComposeContentView.margin)
.background(
Color(UIColor.systemBackground)
.overlay(
HStack {
Stripes(config: StripesConfig(
background: Color.yellow,
foreground: Color.black,
degrees: 45,
barWidth: 2.5,
barSpacing: 3.5
))
.frame(width: ComposeContentView.margin * 0.5)
.frame(maxHeight: .infinity)
.id(UUID())
Spacer()
Stripes(config: StripesConfig(
background: Color.yellow,
foreground: Color.black,
degrees: 45,
barWidth: 2.5,
barSpacing: 3.5
))
.frame(width: ComposeContentView.margin * 0.5)
.frame(maxHeight: .infinity)
.scaleEffect(x: -1, y: 1, anchor: .center)
.id(UUID())
}
)
)
} // end if viewModel.isContentWarningActive
// author
authorView
.padding(.top, 14)
.padding(.horizontal, ComposeContentView.margin)
.fixedSize(horizontal: false, vertical: true)
// content editor
MetaTextViewRepresentable(
string: $viewModel.content,
width: viewModel.viewLayoutFrame.layoutFrame.width - ComposeContentView.margin * 2,
configurationHandler: { metaText in
viewModel.contentMetaText = metaText
metaText.textView.attributedPlaceholder = {
var attributes = metaText.textAttributes
attributes[.foregroundColor] = UIColor.secondaryLabel
return NSAttributedString(
string: L10n.Scene.Compose.contentInputPlaceholder,
attributes: attributes
)
}()
metaText.textView.keyboardType = .twitter
metaText.textView.tag = ComposeContentViewModel.MetaTextViewKind.content.rawValue
metaText.textView.delegate = viewModel
metaText.delegate = viewModel
metaText.textView.becomeFirstResponder()
}
)
.frame(minHeight: 100)
.fixedSize(horizontal: false, vertical: true)
.padding(.horizontal, ComposeContentView.margin)
.background(
GeometryReader { proxy in
Color.clear.preference(key: ViewFramePreferenceKey.self, value: proxy.frame(in: .named(ComposeContentView.contentViewCoordinateSpace)))
}
.onPreferenceChange(ViewFramePreferenceKey.self) { frame in
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): content textView frame: \(frame.debugDescription)")
let rect = frame.standardized
viewModel.contentTextViewFrame = CGRect(
origin: frame.origin,
size: CGSize(width: floor(rect.width), height: floor(rect.height))
)
}
)
// poll
pollView
.padding(.horizontal, ComposeContentView.margin)
// media
mediaView
.padding(.horizontal, ComposeContentView.margin)
}
.background(
GeometryReader { proxy in
Color.clear.preference(key: ViewFramePreferenceKey.self, value: proxy.frame(in: .local))
}
.onPreferenceChange(ViewFramePreferenceKey.self) { frame in
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): content frame: \(frame.debugDescription)")
let rect = frame.standardized
viewModel.contentCellFrame = CGRect(
origin: frame.origin,
size: CGSize(width: floor(rect.width), height: floor(rect.height))
)
}
)
Spacer()
} // end VStack
.coordinateSpace(name: ComposeContentView.contentViewCoordinateSpace)
} // end body
}
extension ComposeContentView {
var authorView: some View {
HStack(spacing: 8) {
AnimatedImage(imageURL: viewModel.avatarURL)
.frame(width: 46, height: 46)
.background(Color(UIColor.systemFill))
.cornerRadius(12)
VStack(alignment: .leading, spacing: 4) {
Spacer()
MetaLabelRepresentable(
textStyle: .statusName,
metaContent: viewModel.name
)
Text(viewModel.username)
.font(.system(size: 15, weight: .regular))
.foregroundColor(.secondary)
Spacer()
}
Spacer()
}
.accessibilityElement(children: .ignore)
.accessibilityLabel(L10n.Scene.Compose.Accessibility.postingAs([viewModel.name.string, viewModel.username].joined(separator: ", ")))
}
}
extension ComposeContentView {
// MARK: - poll
var pollView: some View {
VStack {
if viewModel.isPollActive {
// poll option TextField
ReorderableForEach(
items: $viewModel.pollOptions
) { $pollOption in
if let _index = viewModel.pollOptions.firstIndex(of: pollOption) {
PollOptionRow(
viewModel: pollOption,
index: _index,
moveUp: _index == 0 ? nil : {
viewModel.pollOptions.swapAt(_index, _index - 1)
},
moveDown: _index == viewModel.pollOptions.count - 1 ? nil : {
viewModel.pollOptions.swapAt(_index, _index + 1)
},
removeOption: viewModel.pollOptions.count <= 2 ? nil : {
viewModel.pollOptions.remove(at: _index)
},
deleteBackwardResponseTextFieldRelayDelegate: viewModel
) { textField in
viewModel.customEmojiPickerInputViewModel.configure(textInput: textField)
}
}
}
if viewModel.maxPollOptionLimit != viewModel.pollOptions.count {
Button(action: viewModel.createNewPollOptionIfCould) {
PollAddOptionRow()
.accessibilityLabel(L10n.Scene.Compose.Poll.addOption)
}
}
Menu {
Picker(selection: $viewModel.pollExpireConfigurationOption) {
ForEach(PollComposeItem.ExpireConfiguration.Option.allCases, id: \.self) { option in
Text(option.title)
}
} label: {
Text(L10n.Scene.Compose.Poll.durationTime(viewModel.pollExpireConfigurationOption.title))
}
} label: {
HStack {
Text(L10n.Scene.Compose.Poll.durationTime(viewModel.pollExpireConfigurationOption.title))
.foregroundColor(Color(UIColor.label.withAlphaComponent(0.8))) // Gray/800
.font(Font(UIFontMetrics(forTextStyle: .subheadline).scaledFont(for: .systemFont(ofSize: 13, weight: .semibold))))
Spacer()
}
.padding(.vertical, 8)
}
}
} // end VStack
.accessibilityElement(children: .contain)
.accessibilityLabel(L10n.Scene.Compose.Poll.title)
}
// MARK: - media
var mediaView: some View {
VStack(spacing: 16) {
ForEach(viewModel.attachmentViewModels, id: \.self) { attachmentViewModel in
AttachmentView(viewModel: attachmentViewModel)
.clipShape(RoundedRectangle(cornerRadius: 4))
.badgeView(
Button {
viewModel.attachmentViewModels.removeAll(where: { $0 === attachmentViewModel })
} label: {
Image(systemName: "minus.circle.fill")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(.red)
.background(Color.white)
.clipShape(Circle())
}
)
} // end ForEach
} // end VStack
}
}
//private struct ScrollOffsetPreferenceKey: PreferenceKey {
// static var defaultValue: CGPoint = .zero
//
// static func reduce(value: inout CGPoint, nextValue: () -> CGPoint) { }
//}
private struct ViewFramePreferenceKey: PreferenceKey {
static var defaultValue: CGRect = .zero
static func reduce(value: inout CGRect, nextValue: () -> CGRect) { }
}
// MARK: - TypeIdentifiedItemProvider
extension PollComposeItem.Option: TypeIdentifiedItemProvider {
public static var typeIdentifier: String {
return Bundle(for: PollComposeItem.Option.self).bundleIdentifier! + String(describing: type(of: PollComposeItem.Option.self))
}
}
// MARK: - NSItemProviderWriting
extension PollComposeItem.Option: NSItemProviderWriting {
public func loadData(
withTypeIdentifier typeIdentifier: String,
forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void
) -> Progress? {
completionHandler(nil, nil)
return nil
}
public static var writableTypeIdentifiersForItemProvider: [String] {
return [Self.typeIdentifier]
}
}