aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Tay <nick@windblume.net>2022-07-18 00:07:21 +1000
committerNicholas Tay <nick@windblume.net>2022-07-18 00:07:21 +1000
commit8b255702c674bf1bfeca96697711f6e4f088dfdb (patch)
tree98a3ee132dd02973ca1e5c02136b3fe539b22275
parentbd7761216a065b0dd859cb19d709996739a240cd (diff)
downloadforayios-8b255702c674bf1bfeca96697711f6e4f088dfdb.tar.gz
forayios-8b255702c674bf1bfeca96697711f6e4f088dfdb.tar.bz2
forayios-8b255702c674bf1bfeca96697711f6e4f088dfdb.zip
Try out 'share' aka UIActivityViewController
-rw-r--r--foray.xcodeproj/project.pbxproj4
-rw-r--r--foray/Coordinators/ForayCoordinator.swift32
-rw-r--r--foray/Scenes/ForayDetailView.swift29
-rw-r--r--foray/Scenes/ForayDetailViewController.swift12
4 files changed, 75 insertions, 2 deletions
diff --git a/foray.xcodeproj/project.pbxproj b/foray.xcodeproj/project.pbxproj
index df040dd..214a004 100644
--- a/foray.xcodeproj/project.pbxproj
+++ b/foray.xcodeproj/project.pbxproj
@@ -23,6 +23,7 @@
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 */
@@ -44,6 +45,7 @@
C09676BD27EC28B100353D46 /* ForayDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayDetailView.swift; sourceTree = "<group>"; };
C0C73E6327EC3A650015497D /* PenguinItemViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PenguinItemViewModel.swift; sourceTree = "<group>"; };
C0C73E6627EC3BA50015497D /* PenguinItemPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PenguinItemPresenter.swift; sourceTree = "<group>"; };
+ C0DB8C0D288146FC00B1B594 /* spy.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = spy.png; path = Assets.xcassets/spy.imageset/spy.png; sourceTree = "<group>"; };
C0FEAF5E27E14C52000A7648 /* ForayDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForayDetailViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -88,6 +90,7 @@
C09676C227EC354700353D46 /* Extensions */,
C09676C127EC353D00353D46 /* Scenes */,
C0C73E6927ED2F2D0015497D /* Fetchers */,
+ C0DB8C0D288146FC00B1B594 /* spy.png */,
);
path = foray;
sourceTree = "<group>";
@@ -211,6 +214,7 @@
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 874f486..972bfb8 100644
--- a/foray/Coordinators/ForayCoordinator.swift
+++ b/foray/Coordinators/ForayCoordinator.swift
@@ -7,6 +7,7 @@
import Foundation
import UIKit
+import LinkPresentation
class ForayCoordinator: Coordinator {
var childCoordinators = [Coordinator]()
@@ -36,4 +37,35 @@ class ForayCoordinator: Coordinator {
func hideLoading() {
loadingOverlay.hide()
}
+
+ func share(item: PenguinItemViewModel) {
+ let itemSource = ForayLinkPresentationSource(title: "\(item.name) (#\(item.id))", shareURL: URL(string: "https://nick.tay.blue/")!)
+ let activity = UIActivityViewController(activityItems: [itemSource], applicationActivities: nil)
+ navigationController.present(activity, animated: true)
+ }
+}
+
+class ForayLinkPresentationSource: NSObject, UIActivityItemSource {
+ private let metadata: LPLinkMetadata
+
+ func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
+ return metadata
+ }
+
+ func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
+ guard let url = metadata.originalURL else { return "" }
+ return url.absoluteURL
+ }
+
+ func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
+ return metadata.originalURL
+ }
+
+ init(title: String, shareURL: URL) {
+ metadata = LPLinkMetadata()
+ metadata.iconProvider = NSItemProvider(contentsOf: Bundle.main.url(forResource: "spy", withExtension: "png"))
+ metadata.originalURL = shareURL
+ metadata.url = shareURL
+ metadata.title = title
+ }
}
diff --git a/foray/Scenes/ForayDetailView.swift b/foray/Scenes/ForayDetailView.swift
index 5997016..cd70ffc 100644
--- a/foray/Scenes/ForayDetailView.swift
+++ b/foray/Scenes/ForayDetailView.swift
@@ -38,6 +38,21 @@ class ForayDetailView: UIView {
l.numberOfLines = 10
return l
}()
+
+ private lazy var shareButton: UIButton = {
+ let b = UIButton()
+ b.addTarget(self, action: #selector(onShareButton), for: .touchUpInside)
+ b.setTitle("Share", for: .normal)
+ b.setTitleColor(UIColor.systemBlue, for: .normal)
+ b.backgroundColor = UIColor.clear
+ return b
+ }()
+
+ enum ActionType {
+ case onShare
+ }
+
+ var onAction: ((ActionType) -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
@@ -56,6 +71,7 @@ class ForayDetailView: UIView {
container.addSubview(nameLabel)
container.addSubview(itemImageView)
+ container.addSubview(shareButton)
container.addSubview(descLabel)
setupConstraints()
@@ -80,8 +96,14 @@ class ForayDetailView: UIView {
make.leading.trailing.equalToSuperview()
make.height.equalTo(150)
}
+ shareButton.snp.makeConstraints { (make) in
+ make.top.equalTo(itemImageView.snp.bottom).offset(16)
+ make.width.equalTo(50)
+ make.height.equalTo(25)
+ make.centerX.equalToSuperview()
+ }
descLabel.snp.makeConstraints { (make) in
- make.top.equalTo(itemImageView.snp.bottom).offset(32)
+ make.top.equalTo(shareButton.snp.bottom).offset(32)
make.leading.trailing.equalToSuperview()
make.bottom.equalTo(container.snp.bottom).inset(16)
}
@@ -93,4 +115,9 @@ class ForayDetailView: UIView {
itemImageView.image = image
}
+ @objc
+ private func onShareButton() {
+ onAction?(.onShare)
+ }
+
}
diff --git a/foray/Scenes/ForayDetailViewController.swift b/foray/Scenes/ForayDetailViewController.swift
index 52aa6b8..24de38f 100644
--- a/foray/Scenes/ForayDetailViewController.swift
+++ b/foray/Scenes/ForayDetailViewController.swift
@@ -13,10 +13,19 @@ class ForayDetailViewController: UIViewController, HasCustomView, Coordinated {
typealias CoordinatorType = ForayCoordinator
var coordinator: ForayCoordinator?
-
+ private var item: PenguinItemViewModel?
+
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Details"
+
+ customView.onAction = { [weak self] (action) in
+ guard let self = self, let item = self.item else { return }
+ switch action {
+ case .onShare:
+ self.coordinator?.share(item: item)
+ }
+ }
}
override func loadView() {
@@ -24,6 +33,7 @@ class ForayDetailViewController: UIViewController, HasCustomView, Coordinated {
}
public func setDetails(item: PenguinItemViewModel) {
+ self.item = item
let image: UIImage
var description: String = "Type: "
switch item.type {