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 ArchiveNavigationUserActivityModifier #924

Merged
merged 2 commits into from
Sep 30, 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
2 changes: 0 additions & 2 deletions Sources/Site/Music/UI/ArchiveCategorySplit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ struct ArchiveCategorySplit: View {
} label: {
category.label
}
.archiveCategoryUserActivity(
category, url: { vault.categoryURLMap[$0] }, isActive: isCategoryActive)
}
}

Expand Down
44 changes: 0 additions & 44 deletions Sources/Site/Music/UI/ArchiveCategoryUserActivityModifier.swift

This file was deleted.

66 changes: 66 additions & 0 deletions Sources/Site/Music/UI/ArchiveNavigationUserActivityModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// ArchiveNavigationUserActivityModifier.swift
// site
//
// Created by Greg Bolsinga on 9/29/24.
//

import SwiftUI
import os

extension ArchiveCategory {
static let activityType = "gdb.SiteApp.view-archiveCategory"
}

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

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

@State private var userActivityCategory: ArchiveNavigation.State.DefaultCategory =
ArchiveNavigation.State.defaultCategory

func body(content: Content) -> some View {
let isCategoryActive = {
#if os(iOS) || os(tvOS)
guard let userActivityCategory else { return false }
#endif
return archiveNavigation.userActivityActive(for: userActivityCategory)
}()

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

return
content
.onAppear { userActivityCategory = archiveNavigation.category }
.onChange(of: archiveNavigation.category) { _, newValue in
userActivityCategory = newValue
}
.userActivity(ArchiveCategory.activityType, isActive: isCategoryActive) { userActivity in
#if os(iOS) || os(tvOS)
guard let userActivityCategory else { return }
#endif
userActivity.update(userActivityCategory, url: urlForCategory(userActivityCategory))
}
}
}

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

Expand Down