From 338256206f5e11d7e21a714dedb461ae89281526 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 18 Jul 2022 10:24:49 +1000 Subject: Custom share sheet icons and load them with UIImage Found that LPLinkMetadata + NSItemProvider can actually use UIImage. I think this is better now since not using a bundle load and can use assets. --- foray.xcodeproj/project.pbxproj | 4 ---- foray/Coordinators/ForayCoordinator.swift | 6 +++--- foray/Models/PenguinItemViewModel.swift | 19 +++++++++++++++++++ foray/Presenters/PenguinItemPresenter.swift | 2 +- foray/Scenes/ForayTableViewController.swift | 16 ++-------------- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/foray.xcodeproj/project.pbxproj b/foray.xcodeproj/project.pbxproj index 214a004..df040dd 100644 --- a/foray.xcodeproj/project.pbxproj +++ b/foray.xcodeproj/project.pbxproj @@ -23,7 +23,6 @@ C09676BE27EC28B100353D46 /* ForayDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C09676BD27EC28B100353D46 /* ForayDetailView.swift */; }; C0C73E6427EC3A650015497D /* PenguinItemViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C73E6327EC3A650015497D /* PenguinItemViewModel.swift */; }; C0C73E6727EC3BA50015497D /* PenguinItemPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C73E6627EC3BA50015497D /* PenguinItemPresenter.swift */; }; - C0DB8C0E288146FC00B1B594 /* spy.png in Resources */ = {isa = PBXBuildFile; fileRef = C0DB8C0D288146FC00B1B594 /* spy.png */; }; C0FEAF5F27E14C52000A7648 /* ForayDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */; }; /* End PBXBuildFile section */ @@ -45,7 +44,6 @@ C09676BD27EC28B100353D46 /* ForayDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayDetailView.swift; sourceTree = ""; }; C0C73E6327EC3A650015497D /* PenguinItemViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PenguinItemViewModel.swift; sourceTree = ""; }; C0C73E6627EC3BA50015497D /* PenguinItemPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PenguinItemPresenter.swift; sourceTree = ""; }; - C0DB8C0D288146FC00B1B594 /* spy.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = spy.png; path = Assets.xcassets/spy.imageset/spy.png; sourceTree = ""; }; C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayDetailViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -90,7 +88,6 @@ C09676C227EC354700353D46 /* Extensions */, C09676C127EC353D00353D46 /* Scenes */, C0C73E6927ED2F2D0015497D /* Fetchers */, - C0DB8C0D288146FC00B1B594 /* spy.png */, ); path = foray; sourceTree = ""; @@ -214,7 +211,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - C0DB8C0E288146FC00B1B594 /* spy.png in Resources */, C04B45AD27DEF118001451A3 /* Assets.xcassets in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/foray/Coordinators/ForayCoordinator.swift b/foray/Coordinators/ForayCoordinator.swift index 972bfb8..70febce 100644 --- a/foray/Coordinators/ForayCoordinator.swift +++ b/foray/Coordinators/ForayCoordinator.swift @@ -39,7 +39,7 @@ class ForayCoordinator: Coordinator { } func share(item: PenguinItemViewModel) { - let itemSource = ForayLinkPresentationSource(title: "\(item.name) (#\(item.id))", shareURL: URL(string: "https://nick.tay.blue/")!) + let itemSource = ForayLinkPresentationSource(title: "\(item.name) (#\(item.id))", shareURL: URL(string: "https://nick.tay.blue/")!, image: item.icon) let activity = UIActivityViewController(activityItems: [itemSource], applicationActivities: nil) navigationController.present(activity, animated: true) } @@ -61,9 +61,9 @@ class ForayLinkPresentationSource: NSObject, UIActivityItemSource { return metadata.originalURL } - init(title: String, shareURL: URL) { + init(title: String, shareURL: URL, image: UIImage? = nil) { metadata = LPLinkMetadata() - metadata.iconProvider = NSItemProvider(contentsOf: Bundle.main.url(forResource: "spy", withExtension: "png")) + metadata.iconProvider = NSItemProvider(object: image ?? UIImage.fromAsset(.spy)) metadata.originalURL = shareURL metadata.url = shareURL metadata.title = title diff --git a/foray/Models/PenguinItemViewModel.swift b/foray/Models/PenguinItemViewModel.swift index 9b9b058..dcaa16d 100644 --- a/foray/Models/PenguinItemViewModel.swift +++ b/foray/Models/PenguinItemViewModel.swift @@ -6,6 +6,7 @@ // import Foundation +import UIKit struct PenguinItemViewModel { let type: PenguinItemType @@ -13,4 +14,22 @@ struct PenguinItemViewModel { let year: Int let id: String let name: String + + var typeName: String { + switch type { + case .item: + return "Item" + case .quest: + return "Quest" + } + } + + var icon: UIImage { + switch type { + case .item: + return UIImage(named: id) ?? UIImage.fromAsset(.spy) + case .quest: + return UIImage.fromAsset(.spy) + } + } } diff --git a/foray/Presenters/PenguinItemPresenter.swift b/foray/Presenters/PenguinItemPresenter.swift index 45b970c..9d6525d 100644 --- a/foray/Presenters/PenguinItemPresenter.swift +++ b/foray/Presenters/PenguinItemPresenter.swift @@ -21,7 +21,7 @@ class PenguinItemPresenter { return transform(models: apiItems) } - func transform(models: [PenguinItemModel]) -> [PenguinItemViewModel] { + private func transform(models: [PenguinItemModel]) -> [PenguinItemViewModel] { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" diff --git a/foray/Scenes/ForayTableViewController.swift b/foray/Scenes/ForayTableViewController.swift index 3d839a1..64ec086 100644 --- a/foray/Scenes/ForayTableViewController.swift +++ b/foray/Scenes/ForayTableViewController.swift @@ -80,21 +80,9 @@ class ForayTableViewController: UITableViewController, Coordinated { } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let item = self.sections[indexPath.section].items[indexPath.row] - - let type: String - let icon: UIImage - switch item.type { - case .item: - type = "Item" - icon = UIImage(named: item.id) ?? UIImage.fromAsset(.it) - case .quest: - type = "Quest" - icon = UIImage.fromAsset(.spy) - } - guard let cell = tableView.dequeueReusableCell(withIdentifier: "ForayTableViewCell", for: indexPath) as? ForayTableViewCell else { return ForayTableViewCell() } - cell.setData(name: item.name, desc: type + "ID: " + item.id, img: icon) + let item = self.sections[indexPath.section].items[indexPath.row] + cell.setData(name: item.name, desc: item.typeName + "ID: " + item.id, img: item.icon) return cell } -- cgit