Vernissage/VernissageShare/ShareViewController.swift

84 lines
2.9 KiB
Swift
Raw Normal View History

2023-04-07 13:20:37 +02:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the Apache License 2.0.
//
import SwiftUI
import UIKit
import Social
2023-04-07 18:58:15 +02:00
import ClientKit
import EnvironmentKit
2023-10-20 07:45:18 +02:00
import SwiftData
2023-04-07 13:20:37 +02:00
class ShareViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
2023-04-07 18:58:15 +02:00
guard let item = extensionContext?.inputItems.first as? NSExtensionItem else {
return
}
2023-04-07 13:20:37 +02:00
2023-04-07 18:58:15 +02:00
guard let attachments = item.attachments else {
return
}
2023-04-07 13:20:37 +02:00
2023-04-07 18:58:15 +02:00
// Create shared objects.
let applicationState = ApplicationState.shared
let client = Client.shared
2023-04-07 13:20:37 +02:00
2023-04-07 18:58:15 +02:00
// Get curret signed in user.
2023-10-20 07:45:18 +02:00
let modelContext = SwiftDataHandler.shared.sharedModelContainer.mainContext
guard let currentAccount = AccountDataHandler.shared.getCurrentAccountData(modelContext: modelContext) else {
2023-04-07 18:58:15 +02:00
return
2023-04-07 13:20:37 +02:00
}
2023-04-07 18:58:15 +02:00
// Create Pixelfed client.
let accountModel = currentAccount.toAccountModel()
client.setAccount(account: accountModel)
2023-04-08 08:32:53 +02:00
// Set application state (with default instance settings).
applicationState.changeApplicationState(accountModel: accountModel,
instance: nil,
2023-10-22 10:09:02 +02:00
lastSeenStatusId: accountModel.lastSeenStatusId,
lastSeenNotificationId: accountModel.lastSeenNotificationId)
2023-04-08 08:32:53 +02:00
2023-04-07 18:58:15 +02:00
// Update application settings from database.
2023-10-20 07:45:18 +02:00
ApplicationSettingsHandler.shared.update(applicationState: applicationState, modelContext: modelContext)
2023-04-07 18:58:15 +02:00
// Create view.
let view = ComposeView(attachments: attachments)
2023-10-19 13:24:02 +02:00
.environment(applicationState)
.environment(client)
2023-04-07 18:58:15 +02:00
.tint(applicationState.tintColor.color())
// Add view to current UIViewController.
let childView = UIHostingController(rootView: view)
addChild(childView)
childView.view.frame = self.view.bounds
self.view.addSubview(childView.view)
childView.didMove(toParent: self)
childView.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
childView.view.topAnchor.constraint(equalTo: self.view.topAnchor),
childView.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
childView.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
childView.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
2023-04-07 13:20:37 +02:00
NotificationCenter.default.addObserver(forName: NotificationsName.shareSheetClose, object: nil, queue: nil) { _ in
self.close()
}
}
func close() {
extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
}