Impressia/VernissageShare/ShareViewController.swift

81 lines
2.7 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-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.
guard let currentAccount = AccountDataHandler.shared.getCurrentAccountData() else {
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,
lastSeenStatusId: accountModel.lastSeenStatusId)
2023-04-07 18:58:15 +02:00
// Update application settings from database.
ApplicationSettingsHandler.shared.update(applicationState: applicationState)
// Create view.
let view = ComposeView(attachments: attachments)
.environmentObject(applicationState)
.environmentObject(client)
.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)
}
}