-
-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathShareViewController.swift
More file actions
346 lines (285 loc) · 14.3 KB
/
ShareViewController.swift
File metadata and controls
346 lines (285 loc) · 14.3 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// ShareViewController.swift
// ShareActionExtension
//
// Created by MainasuK on 2022/11/13.
//
import os.log
import UIKit
import Combine
import CoreDataStack
import MastodonCore
import MastodonUI
import MastodonAsset
import MastodonLocalization
import UniformTypeIdentifiers
final class ShareViewController: UIViewController {
let logger = Logger(subsystem: "ShareViewController", category: "ViewController")
var disposeBag = Set<AnyCancellable>()
let context = AppContext.shared
private(set) lazy var viewModel = ShareViewModel(context: context)
let publishButton: UIButton = {
let button = RoundedEdgesButton(type: .custom)
button.cornerRadius = 10
button.contentEdgeInsets = UIEdgeInsets(top: 6, left: 16, bottom: 5, right: 16) // set 28pt height
button.titleLabel?.font = .systemFont(ofSize: 14, weight: .bold)
button.setTitle(L10n.Scene.Compose.composeAction, for: .normal)
return button
}()
private func configurePublishButtonApperance() {
publishButton.adjustsImageWhenHighlighted = false
publishButton.setBackgroundImage(.placeholder(color: Asset.Colors.Label.primary.color), for: .normal)
publishButton.setBackgroundImage(.placeholder(color: Asset.Colors.Label.primary.color.withAlphaComponent(0.5)), for: .highlighted)
publishButton.setBackgroundImage(.placeholder(color: Asset.Colors.Button.disabled.color), for: .disabled)
publishButton.setTitleColor(Asset.Colors.Label.primaryReverse.color, for: .normal)
}
private(set) lazy var cancelBarButtonItem = UIBarButtonItem(title: L10n.Common.Controls.Actions.cancel, style: .plain, target: self, action: #selector(ShareViewController.cancelBarButtonItemPressed(_:)))
private(set) lazy var publishBarButtonItem: UIBarButtonItem = {
let barButtonItem = UIBarButtonItem(customView: publishButton)
publishButton.addTarget(self, action: #selector(ShareViewController.publishBarButtonItemPressed(_:)), for: .touchUpInside)
return barButtonItem
}()
let activityIndicatorBarButtonItem: UIBarButtonItem = {
let indicatorView = UIActivityIndicatorView(style: .medium)
let barButtonItem = UIBarButtonItem(customView: indicatorView)
indicatorView.startAnimating()
return barButtonItem
}()
private var composeContentViewModel: ComposeContentViewModel?
private var composeContentViewController: ComposeContentViewController?
let notSignInLabel: UILabel = {
let label = UILabel()
label.font = .preferredFont(forTextStyle: .subheadline)
label.textColor = .secondaryLabel
label.text = "No Available Account" // TODO: i18n
return label
}()
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension ShareViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupTheme(theme: ThemeService.shared.currentTheme.value)
ThemeService.shared.apply(theme: ThemeService.shared.currentTheme.value)
ThemeService.shared.currentTheme
.receive(on: DispatchQueue.main)
.sink { [weak self] theme in
guard let self = self else { return }
self.setupTheme(theme: theme)
}
.store(in: &disposeBag)
view.backgroundColor = .systemBackground
title = L10n.Scene.Compose.Title.newPost
navigationItem.leftBarButtonItem = cancelBarButtonItem
navigationItem.rightBarButtonItem = publishBarButtonItem
do {
guard let authContext = try setupAuthContext() else {
setupHintLabel()
return
}
viewModel.authContext = authContext
let composeContentViewModel = ComposeContentViewModel(
context: context,
authContext: authContext,
composeContext: .composeStatus,
destination: .topLevel,
initialContent: ""
)
let composeContentViewController = ComposeContentViewController()
composeContentViewController.viewModel = composeContentViewModel
addChild(composeContentViewController)
composeContentViewController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(composeContentViewController.view)
composeContentViewController.view.pinToParent()
composeContentViewController.didMove(toParent: self)
self.composeContentViewModel = composeContentViewModel
self.composeContentViewController = composeContentViewController
Task { @MainActor in
let inputItems = self.extensionContext?.inputItems.compactMap { $0 as? NSExtensionItem } ?? []
await load(inputItems: inputItems)
} // end Task
} catch {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): error: \(error.localizedDescription)")
}
viewModel.$isPublishing
.receive(on: DispatchQueue.main)
.sink { [weak self] isBusy in
guard let self = self else { return }
self.navigationItem.rightBarButtonItem = isBusy ? self.activityIndicatorBarButtonItem : self.publishBarButtonItem
}
.store(in: &disposeBag)
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
configurePublishButtonApperance()
}
}
extension ShareViewController {
@objc private func cancelBarButtonItemPressed(_ sender: UIBarButtonItem) {
logger.debug("\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
extensionContext?.cancelRequest(withError: NSError(domain: "org.joinmastodon.app.ShareActionExtension", code: -1))
}
@objc private func publishBarButtonItemPressed(_ sender: UIBarButtonItem) {
logger.debug("\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
Task { @MainActor in
viewModel.isPublishing = true
do {
guard let statusPublisher = try composeContentViewModel?.statusPublisher(),
let authContext = viewModel.authContext
else {
throw AppError.badRequest
}
_ = try await statusPublisher.publish(api: context.apiService, authContext: authContext)
self.publishButton.setTitle(L10n.Common.Controls.Actions.done, for: .normal)
try await Task.sleep(nanoseconds: 1 * .second)
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
} catch {
let alertController = UIAlertController.standardAlert(of: error)
present(alertController, animated: true)
return
}
viewModel.isPublishing = false
}
}
}
extension ShareViewController {
private func setupAuthContext() throws -> AuthContext? {
let request = MastodonAuthentication.activeSortedFetchRequest // use active order
let _authentication = try context.managedObjectContext.fetch(request).first
let _authContext = _authentication.flatMap { AuthContext(authentication: $0) }
return _authContext
}
private func setupHintLabel() {
notSignInLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(notSignInLabel)
NSLayoutConstraint.activate([
notSignInLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
notSignInLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
private func setupTheme(theme: Theme) {
view.backgroundColor = theme.systemElevatedBackgroundColor
let barAppearance = UINavigationBarAppearance()
barAppearance.configureWithDefaultBackground()
barAppearance.backgroundColor = theme.navigationBarBackgroundColor
navigationItem.standardAppearance = barAppearance
navigationItem.compactAppearance = barAppearance
navigationItem.scrollEdgeAppearance = barAppearance
}
private func showDismissConfirmAlertController() {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) // can not use alert in extension
let discardAction = UIAlertAction(title: L10n.Common.Controls.Actions.discard, style: .destructive) { _ in
self.extensionContext?.cancelRequest(withError: ShareError.userCancelShare)
}
alertController.addAction(discardAction)
let okAction = UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .cancel, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
// MARK: - UIAdaptivePresentationControllerDelegate
extension ShareViewController: UIAdaptivePresentationControllerDelegate {
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
return composeContentViewModel?.shouldDismiss ?? true
}
func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
showDismissConfirmAlertController()
}
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension ShareViewController {
private func load(inputItems: [NSExtensionItem]) async {
guard let composeContentViewModel = self.composeContentViewModel,
let authContext = viewModel.authContext
else {
assertionFailure()
return
}
var itemProviders: [NSItemProvider] = []
for item in inputItems {
itemProviders.append(contentsOf: item.attachments ?? [])
}
let _textProvider = itemProviders.first { provider in
return provider.hasRepresentationConforming(toTypeIdentifier: UTType.plainText.identifier, fileOptions: [])
}
let _urlProvider = itemProviders.first { provider in
return provider.hasRepresentationConforming(toTypeIdentifier: UTType.url.identifier, fileOptions: [])
}
let _movieProvider = itemProviders.first { provider in
return provider.hasRepresentationConforming(toTypeIdentifier: UTType.movie.identifier, fileOptions: [])
}
let imageProviders = itemProviders.filter { provider in
return provider.hasRepresentationConforming(toTypeIdentifier: UTType.image.identifier, fileOptions: [])
}
async let text = ShareViewController.loadText(textProvider: _textProvider)
async let url = ShareViewController.loadURL(textProvider: _urlProvider)
let content = await [text, url]
.compactMap { $0 }
.joined(separator: " ")
// passby the viewModel `content` value
if !content.isEmpty {
composeContentViewModel.content = content + " "
composeContentViewModel.contentMetaText?.textView.insertText(content + " ")
}
if let movieProvider = _movieProvider {
let mediaAttachmentSettings = authContext.mastodonAuthenticationBox.authenticationRecord.object(in: context.managedObjectContext)?.instance?.configurationV2?.mediaAttachments
let attachmentViewModel = AttachmentViewModel(
api: context.apiService,
authContext: authContext,
input: .itemProvider(movieProvider),
sizeLimit: .init(image: nil, video: nil),
delegate: composeContentViewModel,
mediaAttachmentSettings: mediaAttachmentSettings
)
composeContentViewModel.attachmentViewModels.append(attachmentViewModel)
} else if !imageProviders.isEmpty {
let mediaAttachmentSettings = authContext.mastodonAuthenticationBox.authenticationRecord.object(in: context.managedObjectContext)?.instance?.configurationV2?.mediaAttachments
let attachmentViewModels = imageProviders.map { provider in
AttachmentViewModel(
api: context.apiService,
authContext: authContext,
input: .itemProvider(provider),
sizeLimit: .init(image: nil, video: nil),
delegate: composeContentViewModel,
mediaAttachmentSettings: mediaAttachmentSettings
)
}
composeContentViewModel.attachmentViewModels.append(contentsOf: attachmentViewModels)
}
}
private static func loadText(textProvider: NSItemProvider?) async -> String? {
guard let textProvider = textProvider else { return nil }
do {
let item = try await textProvider.loadItem(forTypeIdentifier: UTType.plainText.identifier)
guard let text = item as? String else { return nil }
return text
} catch {
return nil
}
}
private static func loadURL(textProvider: NSItemProvider?) async -> String? {
guard let textProvider = textProvider else { return nil }
do {
let item = try await textProvider.loadItem(forTypeIdentifier: UTType.url.identifier)
guard let url = item as? URL else { return nil }
return url.absoluteString
} catch {
return nil
}
}
}
extension ShareViewController {
enum ShareError: Error {
case `internal`(error: Error)
case userCancelShare
case missingAuthentication
}
}
extension AppContext {
static let shared = AppContext()
}