From ebdbce007e884087bb9edb65bb80ae1febe764be Mon Sep 17 00:00:00 2001 From: Fethi El Hassasna Date: Thu, 24 Nov 2022 23:58:10 -0500 Subject: [PATCH] Refactor handoff capability --- SwiftRadio.xcodeproj/project.pbxproj | 12 ++++-- .../AnimationFrames.swift | 0 SwiftRadio/Helpers/Handoffable.swift | 43 +++++++++++++++++++ .../LogoShareView.swift | 0 .../{UI Helpers => Helpers}/LogoShareView.xib | 0 .../ShareActivity.swift | 0 .../{UI Helpers => Helpers}/Storyboard.swift | 0 .../UIImage+Cache.swift | 0 .../UIImage+DropShadow.swift | 0 .../UIImageView+Cache.swift | 0 .../UIViewController+Email.swift | 0 .../StationsViewController.swift | 34 +-------------- 12 files changed, 53 insertions(+), 36 deletions(-) rename SwiftRadio/{UI Helpers => Helpers}/AnimationFrames.swift (100%) create mode 100644 SwiftRadio/Helpers/Handoffable.swift rename SwiftRadio/{UI Helpers => Helpers}/LogoShareView.swift (100%) rename SwiftRadio/{UI Helpers => Helpers}/LogoShareView.xib (100%) rename SwiftRadio/{UI Helpers => Helpers}/ShareActivity.swift (100%) rename SwiftRadio/{UI Helpers => Helpers}/Storyboard.swift (100%) rename SwiftRadio/{UI Helpers => Helpers}/UIImage+Cache.swift (100%) rename SwiftRadio/{UI Helpers => Helpers}/UIImage+DropShadow.swift (100%) rename SwiftRadio/{UI Helpers => Helpers}/UIImageView+Cache.swift (100%) rename SwiftRadio/{UI Helpers => Helpers}/UIViewController+Email.swift (100%) diff --git a/SwiftRadio.xcodeproj/project.pbxproj b/SwiftRadio.xcodeproj/project.pbxproj index 9aa7eaab..6ee396b5 100644 --- a/SwiftRadio.xcodeproj/project.pbxproj +++ b/SwiftRadio.xcodeproj/project.pbxproj @@ -66,6 +66,8 @@ CE6ECD04292F358C008B3C16 /* UIViewController+Email.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE6ECD02292F358C008B3C16 /* UIViewController+Email.swift */; }; CE963ECC29135A6F004F299E /* StationsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE963ECB29135A6F004F299E /* StationsManager.swift */; }; CEA82F4A2921F260009E9FA0 /* Info-CarPlay.plist in Resources */ = {isa = PBXBuildFile; fileRef = CEA82F492921F260009E9FA0 /* Info-CarPlay.plist */; }; + CED6353B293081ED002B216F /* Handoffable.swift in Sources */ = {isa = PBXBuildFile; fileRef = CED6353A293081ED002B216F /* Handoffable.swift */; }; + CED6353C293081ED002B216F /* Handoffable.swift in Sources */ = {isa = PBXBuildFile; fileRef = CED6353A293081ED002B216F /* Handoffable.swift */; }; CEDABBEB291217AF00C0367F /* UIImageView+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEDABBEA291217AF00C0367F /* UIImageView+Cache.swift */; }; CEDABBED29121BBA00C0367F /* UIImage+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEDABBEC29121BBA00C0367F /* UIImage+Cache.swift */; }; CF72ACE721F7155200461EED /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CF72ACE621F714D000461EED /* Main.storyboard */; }; @@ -115,6 +117,7 @@ CE6ECD02292F358C008B3C16 /* UIViewController+Email.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Email.swift"; sourceTree = ""; }; CE963ECB29135A6F004F299E /* StationsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StationsManager.swift; sourceTree = ""; }; CEA82F492921F260009E9FA0 /* Info-CarPlay.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-CarPlay.plist"; sourceTree = ""; }; + CED6353A293081ED002B216F /* Handoffable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Handoffable.swift; sourceTree = ""; }; CEDABBEA291217AF00C0367F /* UIImageView+Cache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+Cache.swift"; sourceTree = ""; }; CEDABBEC29121BBA00C0367F /* UIImage+Cache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Cache.swift"; sourceTree = ""; }; CF72ACE621F714D000461EED /* Main.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = ""; }; @@ -182,7 +185,7 @@ children = ( CE0A4994291F3A220071C0CC /* SwiftRadio.entitlements */, CE0A4993291F39660071C0CC /* CarPlay */, - CE8062F7291EF5D9008BD097 /* UI Helpers */, + CE8062F7291EF5D9008BD097 /* Helpers */, CE6ECCF8292F139D008B3C16 /* Coordinators */, CE8062F6291EF5B3008BD097 /* ViewControllers */, CE8062F5291EF598008BD097 /* Model */, @@ -263,7 +266,7 @@ path = ViewControllers; sourceTree = ""; }; - CE8062F7291EF5D9008BD097 /* UI Helpers */ = { + CE8062F7291EF5D9008BD097 /* Helpers */ = { isa = PBXGroup; children = ( CE6ECCFF292F215F008B3C16 /* Storyboard.swift */, @@ -273,10 +276,11 @@ 94D260951B45E3FA00DE671C /* AnimationFrames.swift */, 94E9761B1B1A8F3200F52B1E /* UIImage+DropShadow.swift */, 53113F38230C720900462C0E /* ShareActivity.swift */, + CED6353A293081ED002B216F /* Handoffable.swift */, 6258DCD722D93A3500166C65 /* LogoShareView.swift */, 6258DCD922D93A5400166C65 /* LogoShareView.xib */, ); - path = "UI Helpers"; + path = Helpers; sourceTree = ""; }; /* End PBXGroup section */ @@ -456,6 +460,7 @@ CE6ECD00292F215F008B3C16 /* Storyboard.swift in Sources */, 94E9761C1B1A8F3200F52B1E /* UIImage+DropShadow.swift in Sources */, 94452E551AD7086800BFE7A5 /* AboutViewController.swift in Sources */, + CED6353B293081ED002B216F /* Handoffable.swift in Sources */, 94452E4F1AD6F24700BFE7A5 /* PopUpMenuViewController.swift in Sources */, CE6ECCFD292F1445008B3C16 /* MainCoordinator.swift in Sources */, 9409E11C1ABF6FEA00312E2B /* AppDelegate.swift in Sources */, @@ -485,6 +490,7 @@ CE6ECD01292F215F008B3C16 /* Storyboard.swift in Sources */, CE6A3E39291F376D0058C82A /* UIImage+DropShadow.swift in Sources */, CE6A3E3A291F376D0058C82A /* AboutViewController.swift in Sources */, + CED6353C293081ED002B216F /* Handoffable.swift in Sources */, CE6A3E3B291F376D0058C82A /* PopUpMenuViewController.swift in Sources */, CE6ECCFE292F1448008B3C16 /* MainCoordinator.swift in Sources */, CE6A3E3C291F376D0058C82A /* AppDelegate.swift in Sources */, diff --git a/SwiftRadio/UI Helpers/AnimationFrames.swift b/SwiftRadio/Helpers/AnimationFrames.swift similarity index 100% rename from SwiftRadio/UI Helpers/AnimationFrames.swift rename to SwiftRadio/Helpers/AnimationFrames.swift diff --git a/SwiftRadio/Helpers/Handoffable.swift b/SwiftRadio/Helpers/Handoffable.swift new file mode 100644 index 00000000..b1f14251 --- /dev/null +++ b/SwiftRadio/Helpers/Handoffable.swift @@ -0,0 +1,43 @@ +// +// Handoffable.swift +// SwiftRadio +// +// Created by Fethi El Hassasna on 2022-11-24. +// Copyright © 2022 matthewfecher.com. All rights reserved. +// + +import UIKit +import FRadioPlayer + +protocol Handoffable: UIResponder {} + +extension Handoffable { + + func setupHandoffUserActivity() { + userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb) + userActivity?.becomeCurrent() + } + + func updateHandoffUserActivity(_ activity: NSUserActivity?, station: RadioStation?) { + guard let activity = activity else { return } + + defer { updateUserActivityState(activity) } + + guard let metadata = FRadioPlayer.shared.currentMetadata, let artistName = metadata.artistName, let trackName = metadata.trackName else { + activity.webpageURL = nil + return + } + + activity.webpageURL = getHandoffURL(artistName: artistName, trackName: trackName) + } + + private func getHandoffURL(artistName: String, trackName: String) -> URL? { + var components = URLComponents() + components.scheme = "https" + components.host = "google.com" + components.path = "/search" + components.queryItems = [URLQueryItem]() + components.queryItems?.append(URLQueryItem(name: "q", value: "\(artistName) \(trackName)")) + return components.url + } +} diff --git a/SwiftRadio/UI Helpers/LogoShareView.swift b/SwiftRadio/Helpers/LogoShareView.swift similarity index 100% rename from SwiftRadio/UI Helpers/LogoShareView.swift rename to SwiftRadio/Helpers/LogoShareView.swift diff --git a/SwiftRadio/UI Helpers/LogoShareView.xib b/SwiftRadio/Helpers/LogoShareView.xib similarity index 100% rename from SwiftRadio/UI Helpers/LogoShareView.xib rename to SwiftRadio/Helpers/LogoShareView.xib diff --git a/SwiftRadio/UI Helpers/ShareActivity.swift b/SwiftRadio/Helpers/ShareActivity.swift similarity index 100% rename from SwiftRadio/UI Helpers/ShareActivity.swift rename to SwiftRadio/Helpers/ShareActivity.swift diff --git a/SwiftRadio/UI Helpers/Storyboard.swift b/SwiftRadio/Helpers/Storyboard.swift similarity index 100% rename from SwiftRadio/UI Helpers/Storyboard.swift rename to SwiftRadio/Helpers/Storyboard.swift diff --git a/SwiftRadio/UI Helpers/UIImage+Cache.swift b/SwiftRadio/Helpers/UIImage+Cache.swift similarity index 100% rename from SwiftRadio/UI Helpers/UIImage+Cache.swift rename to SwiftRadio/Helpers/UIImage+Cache.swift diff --git a/SwiftRadio/UI Helpers/UIImage+DropShadow.swift b/SwiftRadio/Helpers/UIImage+DropShadow.swift similarity index 100% rename from SwiftRadio/UI Helpers/UIImage+DropShadow.swift rename to SwiftRadio/Helpers/UIImage+DropShadow.swift diff --git a/SwiftRadio/UI Helpers/UIImageView+Cache.swift b/SwiftRadio/Helpers/UIImageView+Cache.swift similarity index 100% rename from SwiftRadio/UI Helpers/UIImageView+Cache.swift rename to SwiftRadio/Helpers/UIImageView+Cache.swift diff --git a/SwiftRadio/UI Helpers/UIViewController+Email.swift b/SwiftRadio/Helpers/UIViewController+Email.swift similarity index 100% rename from SwiftRadio/UI Helpers/UIViewController+Email.swift rename to SwiftRadio/Helpers/UIViewController+Email.swift diff --git a/SwiftRadio/ViewControllers/StationsViewController.swift b/SwiftRadio/ViewControllers/StationsViewController.swift index 47b4946d..15348877 100755 --- a/SwiftRadio/ViewControllers/StationsViewController.swift +++ b/SwiftRadio/ViewControllers/StationsViewController.swift @@ -16,7 +16,7 @@ protocol StationsViewControllerDelegate: AnyObject { func presentPopUpMenuController(_ stationsViewController: StationsViewController) } -class StationsViewController: UIViewController { +class StationsViewController: UIViewController, Handoffable { // MARK: - Delegate weak var delegate: StationsViewControllerDelegate? @@ -281,38 +281,6 @@ extension StationsViewController: UISearchResultsUpdating { } } -// MARK: - Handoff Functionality - GH - -extension StationsViewController { - - func setupHandoffUserActivity() { - userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb) - userActivity?.becomeCurrent() - } - - func updateHandoffUserActivity(_ activity: NSUserActivity?, station: RadioStation?) { - guard let activity = activity else { return } - activity.webpageURL = player.currentMetadata == nil ? nil : getHandoffURL() - updateUserActivityState(activity) - } - - override func updateUserActivityState(_ activity: NSUserActivity) { - super.updateUserActivityState(activity) - } - - private func getHandoffURL() -> URL? { - guard let station = manager.currentStation else { return nil } - - var components = URLComponents() - components.scheme = "https" - components.host = "google.com" - components.path = "/search" - components.queryItems = [URLQueryItem]() - components.queryItems?.append(URLQueryItem(name: "q", value: "\(station.artistName) \(station.trackName)")) - return components.url - } -} - // MARK: - FRadioPlayerObserver extension StationsViewController: FRadioPlayerObserver {