From b0132640ab6d0c8a4119c3d6ecc7555df1ed9700 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sun, 31 Jan 2021 05:57:30 -0800 Subject: [PATCH] Add "Open in Safari" activity --- Activities/OpenInSafariActivity.swift | 39 ++++++++++++++++++++++ Localizations/Localizable.strings | 1 + Metatext.xcodeproj/project.pbxproj | 12 +++++++ View Controllers/TableViewController.swift | 4 ++- 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 Activities/OpenInSafariActivity.swift diff --git a/Activities/OpenInSafariActivity.swift b/Activities/OpenInSafariActivity.swift new file mode 100644 index 0000000..29daad3 --- /dev/null +++ b/Activities/OpenInSafariActivity.swift @@ -0,0 +1,39 @@ +// Copyright © 2021 Metabolist. All rights reserved. + +import UIKit + +final class OpenInSafariActivity: UIActivity { + private var url: URL? + + override var activityType: UIActivity.ActivityType? { + .init(String(describing: Self.self)) + } + + override var activityTitle: String? { + NSLocalizedString("Open in Safari", comment: "") + } + + override var activityImage: UIImage? { + UIImage(systemName: "safari", withConfiguration: UIImage.SymbolConfiguration(scale: .large)) + } + + override func canPerform(withActivityItems activityItems: [Any]) -> Bool { + activityItems.allSatisfy { + guard let url = $0 as? URL else { return false } + + return UIApplication.shared.canOpenURL(url) + } + } + + override func prepare(withActivityItems activityItems: [Any]) { + url = activityItems.first { $0 is URL } as? URL + } + + override func perform() { + guard let url = url else { return } + + UIApplication.shared.open(url) { + self.activityDidFinish($0) + } + } +} diff --git a/Localizations/Localizable.strings b/Localizations/Localizable.strings index 70f6f5a..60460f1 100644 --- a/Localizations/Localizable.strings +++ b/Localizations/Localizable.strings @@ -28,6 +28,7 @@ "account.unblock.confirm-%@" = "Unblock %@?"; "account.unfollow" = "Unfollow"; "account.unmute" = "Unmute"; +"activity.open-in-safari" = "Open in Safari"; "add" = "Add"; "apns-default-message" = "New notification"; "add-identity.instance-url" = "Instance URL"; diff --git a/Metatext.xcodeproj/project.pbxproj b/Metatext.xcodeproj/project.pbxproj index c18db54..e6b143e 100644 --- a/Metatext.xcodeproj/project.pbxproj +++ b/Metatext.xcodeproj/project.pbxproj @@ -57,6 +57,7 @@ D036EBC2259FE2AD00EC1CFC /* UIVIewController+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0E7AD3825870B13005F5E2D /* UIVIewController+Extensions.swift */; }; D03D87F425C23C44004DCBB2 /* SecondaryNavigationTitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D03D87F325C23C44004DCBB2 /* SecondaryNavigationTitleView.swift */; }; D0477F1525C68BAC005C5368 /* PrefetchRequestModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0477F1425C68BAC005C5368 /* PrefetchRequestModifier.swift */; }; + D0477F2C25C6EBAD005C5368 /* OpenInSafariActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0477F2B25C6EBAD005C5368 /* OpenInSafariActivity.swift */; }; D04F9E8E259E9C950081B0C9 /* ViewModels in Frameworks */ = {isa = PBXBuildFile; productRef = D04F9E8D259E9C950081B0C9 /* ViewModels */; }; D05936CF25A8D79800754FDF /* EditAttachmentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D05936CE25A8D79800754FDF /* EditAttachmentViewController.swift */; }; D05936D025A8D79800754FDF /* EditAttachmentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D05936CE25A8D79800754FDF /* EditAttachmentViewController.swift */; }; @@ -253,6 +254,7 @@ D036AA16254CA823009094DF /* StatusBodyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBodyView.swift; sourceTree = ""; }; D03D87F325C23C44004DCBB2 /* SecondaryNavigationTitleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecondaryNavigationTitleView.swift; sourceTree = ""; }; D0477F1425C68BAC005C5368 /* PrefetchRequestModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefetchRequestModifier.swift; sourceTree = ""; }; + D0477F2B25C6EBAD005C5368 /* OpenInSafariActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenInSafariActivity.swift; sourceTree = ""; }; D047FA8C24C3E21200AF17C5 /* Metatext.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Metatext.app; sourceTree = BUILT_PRODUCTS_DIR; }; D05936CE25A8D79800754FDF /* EditAttachmentViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditAttachmentViewController.swift; sourceTree = ""; }; D05936DD25A937EC00754FDF /* EditThumbnailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditThumbnailView.swift; sourceTree = ""; }; @@ -549,9 +551,18 @@ path = "View Repesentables"; sourceTree = ""; }; + D0477F2A25C6EB90005C5368 /* Activities */ = { + isa = PBXGroup; + children = ( + D0477F2B25C6EBAD005C5368 /* OpenInSafariActivity.swift */, + ); + path = Activities; + sourceTree = ""; + }; D047FA7F24C3E21000AF17C5 = { isa = PBXGroup; children = ( + D0477F2A25C6EB90005C5368 /* Activities */, D0C7D45224F76169001EBDBB /* Assets.xcassets */, D0AD03552505814D0085A466 /* Base16 */, D0FE1C9625368A15003EF1EB /* Caches */, @@ -969,6 +980,7 @@ D0C7D49A24F7616A001EBDBB /* TableView.swift in Sources */, D08B8D622540DE3B00B1EBEF /* ZoomTransitionController.swift in Sources */, D0F0B12E251A97E400942152 /* TableViewController.swift in Sources */, + D0477F2C25C6EBAD005C5368 /* OpenInSafariActivity.swift in Sources */, D0DD50CB256B1F24004A04F7 /* ReportView.swift in Sources */, D0477F1525C68BAC005C5368 /* PrefetchRequestModifier.swift in Sources */, D097F41B25BE3E1A00859F2C /* SearchScope+Extensions.swift in Sources */, diff --git a/View Controllers/TableViewController.swift b/View Controllers/TableViewController.swift index f532f2d..dcb94ae 100644 --- a/View Controllers/TableViewController.swift +++ b/View Controllers/TableViewController.swift @@ -513,7 +513,9 @@ private extension TableViewController { } func share(url: URL) { - let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil) + let activityViewController = UIActivityViewController( + activityItems: [url], + applicationActivities: [OpenInSafariActivity()]) if UIDevice.current.userInterfaceIdiom == .pad { guard let sourceView = tableView.viewWithTag(url.hashValue) else { return }