Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App Clip: Add loading and error states #2745

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions Pocket Casts App Clip/NowPlayingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,41 @@ import StoreKit
import PocketCastsUtils

struct NowPlayingView: View {

enum ScreenState: Int {
case loading
case ready
case failed
}

@State var presentAppStoreOverlay: Bool = false
@State var state: ScreenState = .loading

var body: some View {
VStack {
NowPlayingPlayerItemViewControllerRepresentable()
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
presentAppStoreOverlay = true
Spacer()
HStack { Spacer() }
switch state {
case .loading:
ProgressView()
.progressViewStyle(.circular)
.tint(UIColor.label.color)
case .ready:
NowPlayingPlayerItemViewControllerRepresentable()
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
presentAppStoreOverlay = true
}
}
}
case .failed:
EmptyView()
}
Spacer()
}
.background(state != .ready ? UIColor.systemBackground.color : PlayerColorHelper.playerBackgroundColor01().color)
.appStoreOverlay(isPresented: $presentAppStoreOverlay, configuration: {
SKOverlay.AppClipConfiguration(position: .bottom)
})
.background(Color(uiColor: PlayerColorHelper.playerBackgroundColor01()))
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
handle(userActivity: userActivity)
}
Expand All @@ -32,7 +52,10 @@ struct NowPlayingView: View {
let path = components.path,
path != "/get",
path != "/get/"
else { return }
else {
showErrorMessage(userActivity: userActivity)
return
}

// NOTE: This doesn't handle the redeem URL. See `AppDelegate.handleContinue(_ userActivity: NSUserActivity)` for this logic

Expand All @@ -46,46 +69,63 @@ struct NowPlayingView: View {
PodcastManager.shared.importSharedItemFromUrl(importPath) { shareItem in
guard let shareItem else {
FileLog.shared.addMessage("App Clip: Missing Share Item")
showErrorMessage(userActivity: userActivity)
return
}

guard let episodeUUID = shareItem.episodeHeader?.uuid else {
FileLog.shared.addMessage("App Clip: No episode found in share item")
showErrorMessage(userActivity: userActivity)
return
}

guard let podcastUUID = shareItem.podcastHeader?.uuid else {
FileLog.shared.addMessage("App Clip: No podcast found in share item")
showErrorMessage(userActivity: userActivity)
return
}


loadEpisode(episodeUuid: episodeUUID, podcastUuid: podcastUUID) {
guard let episode = DataManager.sharedManager.findEpisode(uuid: episodeUUID) else {
FileLog.shared.addMessage("App Clip: Could not find Episode")
showErrorMessage(userActivity: userActivity)
return
}

FileLog.shared.addMessage("App Clip: Loaded episode: \(episode.title ?? "unknown")")

state = .ready
PlaybackManager.shared.load(episode: episode, autoPlay: true, overrideUpNext: false)
Analytics.track(.playbackPlay, source: AnalyticsSource.handleUserActivity, properties: ["url": incomingURL.absoluteString, "podcast": podcastUUID, "episode": episode.uuid])
}
}
}

private func showErrorMessage(userActivity: NSUserActivity) {
userActivity.invalidate()
state = .failed
DispatchQueue.main.async {
let rootViewController = (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows.first?.rootViewController
SJUIUtils.showAlert(title: L10n.podcastShareErrorTitle, message: L10n.podcastShareErrorMsg, from: rootViewController)
}
}

private func loadEpisode(episodeUuid: String, podcastUuid: String, timestamp: TimeInterval? = nil, completion: @escaping () -> Void) {
if let podcast = DataManager.sharedManager.findPodcast(uuid: podcastUuid, includeUnsubscribed: true) {
ServerPodcastManager.shared.updatePodcastIfRequired(podcast: podcast) { _ in
completion()
DispatchQueue.main.async {
completion()
}
}

return
}

ServerPodcastManager.shared.addFromUuid(podcastUuid: podcastUuid, subscribe: false, completion: { success in
if success, let _ = DataManager.sharedManager.findPodcast(uuid: podcastUuid, includeUnsubscribed: true) {
completion()
DispatchQueue.main.async {
completion()
}
} else {
DispatchQueue.main.async {
let rootViewController = (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows.first?.rootViewController
Expand Down
4 changes: 2 additions & 2 deletions podcasts/PlayerColorHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ struct PlayerColorHelper {
return ThemeColor.playerBackground02(podcastColor: podcastBackgroundColor, for: theme)
}

static func playerHighlightColor01(for theme: Theme.ThemeType,
static func playerHighlightColor01(for theme: Theme.ThemeType = Theme.sharedTheme.activeTheme,
episode: BaseEpisode? = PlaybackManager.shared.currentEpisode()) -> UIColor {
guard let podcastColor = tint(for: episode, with: theme) else { return UIColor.white }

return ThemeColor.playerHighlight01(podcastColor: podcastColor)
}

static func playerHighlightColor02(for theme: Theme.ThemeType,
static func playerHighlightColor02(for theme: Theme.ThemeType = Theme.sharedTheme.activeTheme,
episode: BaseEpisode? = PlaybackManager.shared.currentEpisode()) -> UIColor {
guard let podcastColor = tint(for: episode, with: theme) else { return UIColor.white }

Expand Down