extract out StatusActivityItem class

This commit is contained in:
Jed Fox 2022-12-03 13:09:04 -05:00
parent 3212e54bf5
commit ebf3835403
No known key found for this signature in database
GPG Key ID: 0B61D18EA54B47E1
5 changed files with 94 additions and 51 deletions

View File

@ -89,6 +89,8 @@
62FD27D12893707600B205C5 /* BookmarkViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62FD27D02893707600B205C5 /* BookmarkViewController.swift */; };
62FD27D32893707B00B205C5 /* BookmarkViewController+DataSourceProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62FD27D22893707B00B205C5 /* BookmarkViewController+DataSourceProvider.swift */; };
62FD27D52893708A00B205C5 /* BookmarkViewModel+Diffable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62FD27D42893708A00B205C5 /* BookmarkViewModel+Diffable.swift */; };
85904C02293BC0EB0011C817 /* ImageProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85904C01293BC0EB0011C817 /* ImageProvider.swift */; };
85904C04293BC1940011C817 /* URLActivityItemWithMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85904C03293BC1940011C817 /* URLActivityItemWithMetadata.swift */; };
87FFDA5D898A5C42ADCB35E7 /* Pods_Mastodon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A4ABE34829701A4496C5BB64 /* Pods_Mastodon.framework */; };
C24C97032922F30500BAE8CB /* RefreshControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = C24C97022922F30500BAE8CB /* RefreshControl.swift */; };
D87BFC8B291D5C6B00FEE264 /* MastodonLoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D87BFC8A291D5C6B00FEE264 /* MastodonLoginView.swift */; };
@ -602,6 +604,8 @@
7CB58D292DA7ACEF179A9050 /* Pods-Mastodon.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Mastodon.profile.xcconfig"; path = "Target Support Files/Pods-Mastodon/Pods-Mastodon.profile.xcconfig"; sourceTree = "<group>"; };
7CEFFAE9AF9284B13C0A758D /* Pods-MastodonTests.asdk - debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MastodonTests.asdk - debug.xcconfig"; path = "Target Support Files/Pods-MastodonTests/Pods-MastodonTests.asdk - debug.xcconfig"; sourceTree = "<group>"; };
819CEC9DCAD8E8E7BD85A7BB /* Pods-Mastodon.asdk.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Mastodon.asdk.xcconfig"; path = "Target Support Files/Pods-Mastodon/Pods-Mastodon.asdk.xcconfig"; sourceTree = "<group>"; };
85904C01293BC0EB0011C817 /* ImageProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageProvider.swift; sourceTree = "<group>"; };
85904C03293BC1940011C817 /* URLActivityItemWithMetadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLActivityItemWithMetadata.swift; sourceTree = "<group>"; };
8850E70A1D5FF51432E43653 /* Pods-Mastodon-MastodonUITests.asdk - release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Mastodon-MastodonUITests.asdk - release.xcconfig"; path = "Target Support Files/Pods-Mastodon-MastodonUITests/Pods-Mastodon-MastodonUITests.asdk - release.xcconfig"; sourceTree = "<group>"; };
8E79CCBE51FBC3F7FE8CF49F /* Pods-MastodonTests.release snapshot.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MastodonTests.release snapshot.xcconfig"; path = "Target Support Files/Pods-MastodonTests/Pods-MastodonTests.release snapshot.xcconfig"; sourceTree = "<group>"; };
8ED8C4B1F1BA2DCFF2926BB1 /* Pods-Mastodon-NotificationService.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Mastodon-NotificationService.debug.xcconfig"; path = "Target Support Files/Pods-Mastodon-NotificationService/Pods-Mastodon-NotificationService.debug.xcconfig"; sourceTree = "<group>"; };
@ -2520,6 +2524,8 @@
isa = PBXGroup;
children = (
DBF3B7402733EB9400E21627 /* MastodonLocalCode.swift */,
85904C01293BC0EB0011C817 /* ImageProvider.swift */,
85904C03293BC1940011C817 /* URLActivityItemWithMetadata.swift */,
);
path = Helper;
sourceTree = "<group>";
@ -3172,6 +3178,7 @@
62FD27D52893708A00B205C5 /* BookmarkViewModel+Diffable.swift in Sources */,
DB72602725E36A6F00235243 /* MastodonServerRulesViewModel.swift in Sources */,
2D364F7225E66D7500204FDC /* MastodonResendEmailViewController.swift in Sources */,
85904C04293BC1940011C817 /* URLActivityItemWithMetadata.swift in Sources */,
DB68A06325E905E000CFDF14 /* UIApplication.swift in Sources */,
DB02CDAB26256A9500D0A2AF /* ThreadReplyLoaderTableViewCell.swift in Sources */,
DBEFCD80282A2AA900C0ABEA /* ReportServerRulesViewModel.swift in Sources */,
@ -3308,6 +3315,7 @@
DB1D843026566512000346B3 /* KeyboardPreference.swift in Sources */,
DB852D1926FAEB6B00FC9D81 /* SidebarViewController.swift in Sources */,
2D206B9225F60EA700143C56 /* UIControl.swift in Sources */,
85904C02293BC0EB0011C817 /* ImageProvider.swift in Sources */,
DBDFF1932805554900557A48 /* DiscoveryPostsViewModel.swift in Sources */,
DB3E6FE72806A7A200B035AE /* DiscoveryItem.swift in Sources */,
DB8AF55D25C138B7002E6C99 /* UIViewController.swift in Sources */,

View File

@ -0,0 +1,39 @@
//
// ImageProvider.swift
// Mastodon
//
// Created by Jed Fox on 2022-12-03.
//
import Foundation
import AlamofireImage
import UniformTypeIdentifiers
import UIKit
class ImageProvider: NSObject, NSItemProviderWriting {
let url: URL
let filter: ImageFilter?
init(url: URL, filter: ImageFilter? = nil) {
self.url = url
self.filter = filter
}
var itemProvider: NSItemProvider {
NSItemProvider(object: self)
}
static var writableTypeIdentifiersForItemProvider: [String] {
[UTType.png.identifier]
}
func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping @Sendable (Data?, Error?) -> Void) -> Progress? {
let receipt = UIImageView.af.sharedImageDownloader.download(URLRequest(url: url), filter: filter, completion: { response in
switch response.result {
case .failure(let error): completionHandler(nil, error)
case .success(let image): completionHandler(image.pngData(), nil)
}
})
return receipt?.request.downloadProgress
}
}

View File

@ -0,0 +1,33 @@
//
// URLActivityItemWithMetadata.swift
// Mastodon
//
// Created by Jed Fox on 2022-12-03.
//
import UIKit
import LinkPresentation
class URLActivityItemWithMetadata: NSObject, UIActivityItemSource {
init(url: URL, configureMetadata: (LPLinkMetadata) -> Void) {
self.url = url
self.metadata = LPLinkMetadata()
metadata.url = url
configureMetadata(metadata)
}
let url: URL
let metadata: LPLinkMetadata
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
url
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
url
}
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
metadata
}
}

View File

@ -59,8 +59,18 @@ extension DataSourceFacade {
status: ManagedObjectRecord<Status>
) async throws -> UIActivityViewController {
var activityItems: [Any] = try await dependency.context.managedObjectContext.perform {
guard let status = status.object(in: dependency.context.managedObjectContext) else { return [] }
return [StatusActivityItem(status: status)].compactMap { $0 } as [Any]
guard let status = status.object(in: dependency.context.managedObjectContext),
let url = URL(string: status.url ?? status.uri)
else { return [] }
return [
URLActivityItemWithMetadata(url: url) { metadata in
metadata.title = "\(status.author.displayName) (@\(status.author.acctWithDomain))"
metadata.iconProvider = ImageProvider(
url: status.author.avatarImageURLWithFallback(domain: status.author.domain),
filter: ScaledToSizeFilter(size: CGSize.authorAvatarButtonSize)
).itemProvider
}
] as [Any]
}
var applicationActivities: [UIActivity] = [
SafariActivity(sceneCoordinator: dependency.coordinator), // open URL
@ -77,54 +87,6 @@ extension DataSourceFacade {
)
return activityViewController
}
private class StatusActivityItem: NSObject, UIActivityItemSource {
init?(status: Status) {
guard let url = URL(string: status.url ?? status.uri) else { return nil }
self.url = url
self.metadata = LPLinkMetadata()
metadata.url = url
metadata.title = "\(status.author.displayName) (@\(status.author.acctWithDomain))"
metadata.iconProvider = NSItemProvider(object: IconProvider(url: status.author.avatarImageURLWithFallback(domain: status.author.domain)))
}
let url: URL
let metadata: LPLinkMetadata
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
url
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
url
}
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
metadata
}
private class IconProvider: NSObject, NSItemProviderWriting {
let url: URL
init(url: URL) {
self.url = url
}
static var writableTypeIdentifiersForItemProvider: [String] {
[UTType.png.identifier]
}
func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping @Sendable (Data?, Error?) -> Void) -> Progress? {
let filter = ScaledToSizeFilter(size: CGSize.authorAvatarButtonSize)
let receipt = UIImageView.af.sharedImageDownloader.download(URLRequest(url: url), filter: filter, completion: { response in
switch response.result {
case .failure(let error): completionHandler(nil, error)
case .success(let image): completionHandler(image.pngData(), nil)
}
})
return receipt?.request.downloadProgress
}
}
}
}
// ActionToolBar

View File

@ -95,7 +95,8 @@ extension ShareViewController {
let composeContentViewModel = ComposeContentViewModel(
context: context,
authContext: authContext,
kind: .post
destination: .topLevel,
initialContent: ""
)
let composeContentViewController = ComposeContentViewController()
composeContentViewController.viewModel = composeContentViewModel