Skip to content

Commit cf6fa59

Browse files
committed
feat(widget): add deep link for album snap view.
1 parent caabdb8 commit cf6fa59

11 files changed

Lines changed: 163 additions & 71 deletions

File tree

‎Snapper.xcodeproj/project.xcproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@
9797
}, {
9898
"target": "SnapperWidget",
9999
"inclusions": [
100+
"App/AppDeepLink.swift",
100101
"App/Constants.swift",
101102
"Models/Album.swift",
102103
"Models/Genre.swift",
103104
"Models/Track.swift",
104105
"Resources/Assets.xcassets",
106+
"Resources/Localization/Search.xcstrings",
105107
"Services/Api/Dto/AlbumDto.swift",
106108
"Services/Api/Dto/AlbumWikiResponseDto.swift",
107109
"Services/Api/Dto/TrackDto.swift",

‎Snapper/App/AppDeepLink.swift‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
3+
nonisolated enum AppDeepLink: String {
4+
case camera = "musicsnap://search/camera"
5+
6+
var url: URL? { URL(string: rawValue) }
7+
8+
init?(url: URL) {
9+
self.init(rawValue: url.absoluteString)
10+
}
11+
}

‎Snapper/App/AppNavigation.swift‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import SwiftUI
44
@Observable
55
final class AppNavigation {
66
var screen: AppScreen = .search
7+
8+
var deepLink: AppDeepLink?
9+
10+
func open(_ deepLink: AppDeepLink) {
11+
switch deepLink {
12+
case .camera:
13+
self.screen = .search
14+
self.deepLink = deepLink
15+
}
16+
}
717
}
818

919
extension EnvironmentValues {

‎Snapper/App/AppScreen.swift‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import SwiftUI
2+
3+
enum AppScreen: Codable, Hashable, Identifiable, CaseIterable {
4+
case search
5+
case history
6+
case spatial
7+
8+
var id: AppScreen { self }
9+
}
10+
11+
extension AppScreen {
12+
@ViewBuilder
13+
var label: some View {
14+
switch self {
15+
case .search:
16+
Label(.Search.title, systemImage: "house")
17+
18+
case .history:
19+
Label(.History.title, systemImage: "clock")
20+
21+
case .spatial:
22+
Label(.Spatial.title, systemImage: "map")
23+
}
24+
}
25+
26+
@ViewBuilder
27+
var destination: some View {
28+
switch self {
29+
case .search:
30+
AlbumSearchView()
31+
32+
case .history:
33+
AlbumHistoryView()
34+
35+
case .spatial:
36+
AlbumMapView()
37+
}
38+
}
39+
}

‎Snapper/Features/AlbumSearch/AlbumSearchView.swift‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct AlbumSearchView: View {
99
private var openFirstResult = true
1010

1111
@Environment(\.locale) private var locale
12+
@Environment(\.navigation) private var navigation
1213
@Environment(\.colorScheme) private var systemScheme
1314

1415
@Environment(AlbumSearchViewModel.self) private var vm
@@ -98,9 +99,7 @@ struct AlbumSearchView: View {
9899
.sheet(isPresented: $isSettingsPresented) {
99100
SettingsSheet()
100101
.preferredColorScheme(preference.colorScheme ?? systemScheme)
101-
.presentationDetents([
102-
.fraction(3 / 4)
103-
])
102+
.presentationDetents([.fraction(3 / 4)])
104103
}
105104
.sheet(item: $selectedAlbum) { album in
106105
AlbumCreation(album: album)
@@ -109,6 +108,19 @@ struct AlbumSearchView: View {
109108
.withFullBackground()
110109
.dismissKeyboardOnTap()
111110
}
111+
.onChange(of: canPresentCameraFromDeepLink, initial: true) { _, canPresentCamera in
112+
guard canPresentCamera, let navigation else { return }
113+
navigation.deepLink = nil
114+
isCameraPresented = true
115+
}
116+
}
117+
118+
private var canPresentCameraFromDeepLink: Bool {
119+
true
120+
&& navigation?.screen == .search
121+
&& navigation?.deepLink == .camera
122+
&& isSettingsPresented == false
123+
&& selectedAlbum == nil
112124
}
113125

114126
private func onBarcode(_ barcode: String) {

‎Snapper/Features/AlbumSearch/CameraAlbumScanScreen.swift‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ struct CameraAlbumScanScreen: View {
4040
)
4141
}
4242
}
43-
.navigationToolbar(title: "Snap a photo") {
43+
.navigationTitle(.Search.snapTitle)
44+
.navigationBarTitleDisplayMode(.inline)
45+
.withAlertDestination($alertItem)
46+
.withCloseButton {
4447
requestClose()
4548
}
46-
.withAlertDestination($alertItem)
4749
}
4850
.overlay {
4951
AlbumScanStatusView(model: model)

‎Snapper/Features/AlbumSearch/CameraScanScreen.swift‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ struct CameraScanScreen: View {
3939
)
4040
}
4141
}
42-
.navigationToolbar(title: "Snap a photo") {
42+
.navigationTitle(.Search.snapTitle)
43+
.navigationBarTitleDisplayMode(.inline)
44+
.withAlertDestination($alertItem)
45+
.withCloseButton {
4346
requestClose()
4447
}
45-
.withAlertDestination($alertItem)
4648
}
4749
.task {
4850
await scan()

‎Snapper/Resources/Localization/Search.xcstrings‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323
}
2424
}
2525
},
26+
"snap.title" : {
27+
"extractionState" : "manual",
28+
"localizations" : {
29+
"en" : {
30+
"stringUnit" : {
31+
"state" : "translated",
32+
"value" : "Snap a photo"
33+
}
34+
}
35+
}
36+
},
2637
"state.cancelled" : {
2738
"extractionState" : "manual",
2839
"localizations" : {

‎Snapper/SnapperApp.swift‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ private struct ContentView: View {
3434
@Environment(\.apiClient) private var client
3535

3636
@State private var isLaunching = true
37+
@State private var navigation = AppNavigation()
3738

3839
var body: some View {
3940
TransitionView(
@@ -42,7 +43,7 @@ private struct ContentView: View {
4243
) {
4344
LaunchView()
4445
} second: {
45-
SnapperAppShell()
46+
SnapperAppShell(navigation: navigation)
4647
}
4748
.preferredColorScheme(preference.colorScheme)
4849
.task {
@@ -54,5 +55,10 @@ private struct ContentView: View {
5455
.task {
5556
try? await client.health() // Warm up the backend.
5657
}
58+
.onOpenURL { url in
59+
if let deepLink = AppDeepLink(url: url) {
60+
navigation.open(deepLink)
61+
}
62+
}
5763
}
5864
}

‎Snapper/SnapperAppShell.swift‎

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ struct SnapperAppShell: View {
77

88
@Environment(AlbumLocationCaptureCoordinator.self) private var locationCoordinator
99

10-
@State private var navigation = AppNavigation()
10+
@Bindable var navigation: AppNavigation
1111

1212
var body: some View {
13-
@Bindable var navigation = navigation
14-
1513
AppTabView(selection: $navigation.screen)
1614
.environment(\.navigation, navigation)
1715
.onChange(of: phase, initial: true) { _, next in
@@ -30,7 +28,7 @@ struct SnapperAppShell: View {
3028
}
3129
}
3230

33-
struct AppTabView: View {
31+
private struct AppTabView: View {
3432
@Binding var selection: AppScreen
3533

3634
var body: some View {
@@ -45,41 +43,3 @@ struct AppTabView: View {
4543
}
4644
}
4745
}
48-
49-
enum AppScreen: Codable, Hashable, Identifiable, CaseIterable {
50-
case search
51-
case history
52-
case spatial
53-
54-
var id: AppScreen { self }
55-
}
56-
57-
extension AppScreen {
58-
@ViewBuilder
59-
var label: some View {
60-
switch self {
61-
case .search:
62-
Label(.Search.title, systemImage: "house")
63-
64-
case .history:
65-
Label(.History.title, systemImage: "clock")
66-
67-
case .spatial:
68-
Label(.Spatial.title, systemImage: "map")
69-
}
70-
}
71-
72-
@ViewBuilder
73-
var destination: some View {
74-
switch self {
75-
case .search:
76-
AlbumSearchView()
77-
78-
case .history:
79-
AlbumHistoryView()
80-
81-
case .spatial:
82-
AlbumMapView()
83-
}
84-
}
85-
}

0 commit comments

Comments
 (0)