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

Add ArchivePath userActivity to ArchiveNavigationUserActivityModifier #927

Merged
merged 1 commit into from
Oct 1, 2024
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
4 changes: 2 additions & 2 deletions Sources/Site/Music/ArchivePath+PathRestorable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension Array where Element == ArchivePath {
return !self.dropLast().contains { $0 == archivePath }
}

func isPathActive(_ pathRestorable: PathRestorable) -> Bool {
self.last == pathRestorable.archivePath
func isPathActive(_ archivePath: ArchivePath) -> Bool {
self.last == archivePath
}
}
4 changes: 4 additions & 0 deletions Sources/Site/Music/UI/ArchiveNavigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ extension Logger {
// category is active if its path is empty
return path.isEmpty
}

func userActivityActive(for path: ArchivePath) -> Bool {
self.path.isPathActive(path)
}
}

extension ArchiveNavigation: RawRepresentable {
Expand Down
45 changes: 40 additions & 5 deletions Sources/Site/Music/UI/ArchiveNavigationUserActivityModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@ extension ArchiveCategory {
static let activityType = "gdb.SiteApp.view-archiveCategory"
}

extension ArchivePath {
static let activityType = "gdb.SiteApp.view-archivePath"
}

extension Logger {
fileprivate static let navigationUserActivity = Logger(category: "navigationUserActivity")
}

protocol PathRestorableUserActivity: PathRestorable {
var url: URL? { get }
func updateActivity(_ userActivity: NSUserActivity)
}

struct ArchiveNavigationUserActivityModifier: ViewModifier {
let archiveNavigation: ArchiveNavigation
let urlForCategory: (ArchiveCategory) -> URL?
let activityForPath: (ArchivePath) -> PathRestorableUserActivity?

@State private var userActivityCategory: ArchiveNavigation.State.DefaultCategory =
ArchiveNavigation.State.defaultCategory
@State private var userActivityPath: [ArchivePath] = []

func body(content: Content) -> some View {
let isCategoryActive = {
Expand All @@ -31,37 +42,61 @@ struct ArchiveNavigationUserActivityModifier: ViewModifier {
return archiveNavigation.userActivityActive(for: userActivityCategory)
}()

let isPathActive =
!isCategoryActive
&& {
guard let lastPath = userActivityPath.last else { return false }
return archiveNavigation.userActivityActive(for: lastPath)
}()

#if os(iOS) || os(tvOS)
Logger.navigationUserActivity.log(
"\(userActivityCategory?.rawValue ?? "nil", privacy: .public) active: \(isCategoryActive, privacy: .public)"
"\(userActivityCategory?.rawValue ?? "nil", privacy: .public): \(isCategoryActive, privacy: .public) \(userActivityPath, privacy: .public): \(isPathActive)"
)
#elseif os(macOS)
Logger.navigationUserActivity.log(
"\(userActivityCategory.rawValue, privacy: .public) active: \(isCategoryActive, privacy: .public)"
"\(userActivityCategory.rawValue, privacy: .public): \(isCategoryActive, privacy: .public) \(userActivityPath, privacy: .public): \(isPathActive)"
)
#endif

return
content
.onAppear { userActivityCategory = archiveNavigation.category }
.onAppear {
userActivityCategory = archiveNavigation.category
userActivityPath = archiveNavigation.path
}
.onChange(of: archiveNavigation.category) { _, newValue in
userActivityCategory = newValue
}
.onChange(of: archiveNavigation.path) { _, newValue in
userActivityPath = newValue
}
.userActivity(ArchiveCategory.activityType, isActive: isCategoryActive) { userActivity in
#if os(iOS) || os(tvOS)
guard let userActivityCategory else { return }
#endif
Logger.navigationUserActivity.log(
"update category \(userActivityCategory.rawValue, privacy: .public)")
userActivity.update(userActivityCategory, url: urlForCategory(userActivityCategory))
}
.userActivity(ArchivePath.activityType, isActive: isPathActive) { userActivity in
guard let lastPath = userActivityPath.last else { return }
Logger.navigationUserActivity.log(
"update path \(lastPath.formatted(.json), privacy: .public)")
guard let pathUserActivity = activityForPath(lastPath) else { return }
userActivity.update(pathUserActivity)
}
}
}

extension View {
func advertiseUserActivity(
for archiveNavigation: ArchiveNavigation, urlForCategory: @escaping (ArchiveCategory) -> URL?
for archiveNavigation: ArchiveNavigation, urlForCategory: @escaping (ArchiveCategory) -> URL?,
activityForPath: @escaping (ArchivePath) -> PathRestorableUserActivity?
) -> some View {
modifier(
ArchiveNavigationUserActivityModifier(
archiveNavigation: archiveNavigation, urlForCategory: urlForCategory))
archiveNavigation: archiveNavigation, urlForCategory: urlForCategory,
activityForPath: activityForPath))
}
}
3 changes: 2 additions & 1 deletion Sources/Site/Music/UI/ArchiveStateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ struct ArchiveStateView: View {
}
}
.advertiseUserActivity(
for: archiveNavigation, urlForCategory: { model.vault.categoryURLMap[$0] })
for: archiveNavigation, urlForCategory: { model.vault.categoryURLMap[$0] }
) { model.vault.pathUserActivity(for: $0) }
}
}

Expand Down
13 changes: 1 addition & 12 deletions Sources/Site/Music/UI/ArtistDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct ArtistDetail: View {
let digest: ArtistDigest
let concertCompare: (Concert, Concert) -> Bool
let isPathNavigable: (PathRestorable) -> Bool
let isPathActive: (PathRestorable) -> Bool

@ViewBuilder private var firstSetElement: some View {
HStack {
Expand Down Expand Up @@ -72,7 +71,6 @@ struct ArtistDetail: View {
.listStyle(.grouped)
#endif
.navigationTitle(digest.artist.name)
.pathRestorableUserActivityModifier(digest, isPathActive: isPathActive)
.archiveShare(digest.artist, url: digest.url)
}
}
Expand All @@ -84,9 +82,6 @@ struct ArtistDetail: View {
concertCompare: vaultPreviewData.comparator.compare(lhs:rhs:),
isPathNavigable: { _ in
true
},
isPathActive: { _ in
true
}
)
.musicDestinations(vaultPreviewData)
Expand All @@ -100,9 +95,6 @@ struct ArtistDetail: View {
concertCompare: vaultPreviewData.comparator.compare(lhs:rhs:),
isPathNavigable: { _ in
true
},
isPathActive: { _ in
true
}
)
.musicDestinations(vaultPreviewData)
Expand All @@ -115,10 +107,7 @@ struct ArtistDetail: View {
ArtistDetail(
digest: vaultPreviewData.artistDigests[1],
concertCompare: vaultPreviewData.comparator.compare(lhs:rhs:),
isPathNavigable: { $0.archivePath != selectedConcert.archivePath },
isPathActive: { _ in
true
}
isPathNavigable: { $0.archivePath != selectedConcert.archivePath }
)
.musicDestinations(vaultPreviewData, path: [selectedConcert.archivePath])
}
Expand Down
18 changes: 6 additions & 12 deletions Sources/Site/Music/UI/MusicDestinationModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,39 @@ import SwiftUI
struct MusicDestinationModifier: ViewModifier {
let vault: Vault
let isPathNavigable: (PathRestorable) -> Bool
let isPathActive: (PathRestorable) -> Bool

func body(content: Content) -> some View {
content
.navigationDestination(for: ArchivePath.self) { archivePath in
switch archivePath {
case .show(let iD):
if let concert = vault.concertMap[iD] {
ShowDetail(
concert: concert, isPathNavigable: isPathNavigable, isPathActive: isPathActive)
ShowDetail(concert: concert, isPathNavigable: isPathNavigable)
}
case .venue(let iD):
if let venueDigest = vault.venueDigestMap[iD] {
VenueDetail(
digest: venueDigest, concertCompare: vault.comparator.compare(lhs:rhs:),
geocode: {
try await vault.atlas.geocode($0.venue)
}, isPathNavigable: isPathNavigable, isPathActive: isPathActive)
geocode: { try await vault.atlas.geocode($0.venue) }, isPathNavigable: isPathNavigable
)
}
case .artist(let iD):
if let artistDigest = vault.artistDigestMap[iD] {
ArtistDetail(
digest: artistDigest, concertCompare: vault.comparator.compare(lhs:rhs:),
isPathNavigable: isPathNavigable, isPathActive: isPathActive)
isPathNavigable: isPathNavigable)
}
case .year(let annum):
YearDetail(
digest: vault.digest(for: annum), concertCompare: vault.comparator.compare(lhs:rhs:),
isPathNavigable: isPathNavigable, isPathActive: isPathActive)
isPathNavigable: isPathNavigable)
}
}
}
}

extension View {
func musicDestinations(_ vault: Vault, path: [ArchivePath] = []) -> some View {
modifier(
MusicDestinationModifier(
vault: vault, isPathNavigable: path.isPathNavigable(_:), isPathActive: path.isPathActive(_:)
))
modifier(MusicDestinationModifier(vault: vault, isPathNavigable: path.isPathNavigable(_:)))
}
}
47 changes: 0 additions & 47 deletions Sources/Site/Music/UI/PathRestorableUserActivityModifier.swift

This file was deleted.

11 changes: 0 additions & 11 deletions Sources/Site/Music/UI/ShowDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import SwiftUI
struct ShowDetail: View {
let concert: Concert
let isPathNavigable: (PathRestorable) -> Bool
let isPathActive: (PathRestorable) -> Bool

private var venueName: String {
guard let venue = concert.venue else {
Expand Down Expand Up @@ -61,7 +60,6 @@ struct ShowDetail: View {
.listStyle(.grouped)
#endif
.navigationTitle(venueName)
.pathRestorableUserActivityModifier(concert, isPathActive: isPathActive)
.archiveShare(concert, url: concert.url)
}
}
Expand All @@ -72,9 +70,6 @@ struct ShowDetail: View {
concert: vaultPreviewData.concerts[0],
isPathNavigable: { _ in
true
},
isPathActive: { _ in
true
}
)
.musicDestinations(vaultPreviewData)
Expand All @@ -87,9 +82,6 @@ struct ShowDetail: View {
concert: vaultPreviewData.concerts[1],
isPathNavigable: { _ in
true
},
isPathActive: { _ in
true
}
)
.musicDestinations(vaultPreviewData)
Expand All @@ -102,9 +94,6 @@ struct ShowDetail: View {
concert: vaultPreviewData.concerts[2],
isPathNavigable: { _ in
true
},
isPathActive: { _ in
true
}
)
.musicDestinations(vaultPreviewData)
Expand Down
5 changes: 0 additions & 5 deletions Sources/Site/Music/UI/VenueDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ struct VenueDetail: View {
let concertCompare: (Concert, Concert) -> Bool
let geocode: geocoder?
let isPathNavigable: (PathRestorable) -> Bool
let isPathActive: (PathRestorable) -> Bool

@State private var item: MKMapItem? = nil

Expand Down Expand Up @@ -94,7 +93,6 @@ struct VenueDetail: View {
.listStyle(.grouped)
#endif
.navigationTitle(digest.venue.name)
.pathRestorableUserActivityModifier(digest, isPathActive: isPathActive)
.archiveShare(digest.venue, url: digest.url)
}
}
Expand All @@ -107,9 +105,6 @@ struct VenueDetail: View {
geocode: nil,
isPathNavigable: { _ in
true
},
isPathActive: { _ in
true
}
)
.musicDestinations(vaultPreviewData)
Expand Down
5 changes: 0 additions & 5 deletions Sources/Site/Music/UI/YearDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct YearDetail: View {
let digest: AnnumDigest
let concertCompare: (Concert, Concert) -> Bool
let isPathNavigable: (PathRestorable) -> Bool
let isPathActive: (PathRestorable) -> Bool

private var concerts: [Concert] {
digest.concerts
Expand Down Expand Up @@ -48,7 +47,6 @@ struct YearDetail: View {
.listStyle(.grouped)
#endif
.navigationTitle(Text(digest.annum.formatted()))
.pathRestorableUserActivityModifier(digest, isPathActive: isPathActive)
.archiveShare(digest.annum, url: digest.url)
}
}
Expand All @@ -60,9 +58,6 @@ struct YearDetail: View {
concertCompare: vaultPreviewData.comparator.compare(lhs:rhs:),
isPathNavigable: { _ in
true
},
isPathActive: { _ in
true
}
)
.musicDestinations(vaultPreviewData)
Expand Down
23 changes: 23 additions & 0 deletions Sources/Site/Music/Vault+ArchivePath.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Vault+ArchivePath.swift
// site
//
// Created by Greg Bolsinga on 9/30/24.
//

import Foundation

extension Vault {
func pathUserActivity(for path: ArchivePath) -> PathRestorableUserActivity? {
switch path {
case .show(let iD):
return concertMap[iD]
case .venue(let iD):
return venueDigestMap[iD]
case .artist(let iD):
return artistDigestMap[iD]
case .year(let annum):
return digest(for: annum)
}
}
}
Loading