PhotoViews.swift (8643B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import AVKit 3 import Photos 4 import SubsurfaceLog 5 import SwiftUI 6 7 /// Square thumbnail of a photo or video from the photo library. 8 struct PhotoThumbnail: View { 9 let asset: PHAsset 10 var side: CGFloat = 96 11 @State private var image: UIImage? 12 13 var body: some View { 14 ZStack(alignment: .bottomTrailing) { 15 Group { 16 if let image { 17 Image(uiImage: image).resizable().scaledToFill() 18 } else { 19 Color(.tertiarySystemGroupedBackground) 20 } 21 } 22 .frame(width: side, height: side) 23 .clipped() 24 if asset.mediaType == .video { 25 Label(Units.clock(Int(asset.duration.rounded())), systemImage: "video.fill") 26 .font(.caption2.weight(.semibold)) 27 .labelStyle(.titleAndIcon) 28 .padding(.horizontal, 5) 29 .padding(.vertical, 2) 30 .background(.black.opacity(0.55), in: Capsule()) 31 .foregroundStyle(.white) 32 .padding(4) 33 } 34 } 35 .clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous)) 36 .task(id: asset.localIdentifier) { 37 let options = PHImageRequestOptions() 38 options.deliveryMode = .opportunistic 39 options.isNetworkAccessAllowed = true 40 let scale = UIScreen.main.scale 41 PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width: side * scale, height: side * scale), 42 contentMode: .aspectFill, options: options) { result, _ in 43 if let result { image = result } 44 } 45 } 46 } 47 } 48 49 /// A full-size photo, or a playable video. 50 private struct FullPhoto: View { 51 let asset: PHAsset 52 @State private var image: UIImage? 53 @State private var player: AVPlayer? 54 55 var body: some View { 56 ZStack { 57 if let player { 58 VideoPlayer(player: player) 59 } else if let image { 60 Image(uiImage: image).resizable().scaledToFit() 61 } else { 62 ProgressView().tint(.white) 63 } 64 } 65 .frame(maxWidth: .infinity, maxHeight: .infinity) 66 .task(id: asset.localIdentifier) { 67 let options = PHImageRequestOptions() 68 options.deliveryMode = .highQualityFormat 69 options.isNetworkAccessAllowed = true 70 PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width: 2400, height: 2400), contentMode: .aspectFit, 71 options: options) { result, _ in 72 if let result { image = result } 73 } 74 if asset.mediaType == .video { 75 let video = PHVideoRequestOptions() 76 video.isNetworkAccessAllowed = true 77 PHImageManager.default().requestPlayerItem(forVideo: asset, options: video) { item, _ in 78 guard let item else { return } 79 DispatchQueue.main.async { player = AVPlayer(playerItem: item) } 80 } 81 } 82 } 83 .onDisappear { player?.pause() } 84 } 85 } 86 87 /// Full-screen pager over a dive's photos; the caption gives the time, and the depth when it is known. 88 struct PhotoViewer: View { 89 @Environment(\.dismiss) private var dismiss 90 let photos: [DivePhoto] 91 @State var index: Int 92 let caption: (DivePhoto) -> String 93 94 var body: some View { 95 NavigationStack { 96 TabView(selection: $index) { 97 ForEach(Array(photos.enumerated()), id: \.offset) { position, photo in 98 FullPhoto(asset: photo.asset).tag(position) 99 } 100 } 101 .tabViewStyle(.page(indexDisplayMode: .never)) 102 .background(Color.black) 103 .ignoresSafeArea(edges: .bottom) 104 .safeAreaInset(edge: .bottom) { 105 if photos.indices.contains(index) { 106 Text(caption(photos[index])) 107 .font(.footnote.monospacedDigit()) 108 .foregroundStyle(.white) 109 .padding(.horizontal, 12) 110 .padding(.vertical, 6) 111 .background(.black.opacity(0.5), in: Capsule()) 112 .padding(.bottom, 12) 113 } 114 } 115 .navigationTitle("\(index + 1) of \(photos.count)") 116 .navigationBarTitleDisplayMode(.inline) 117 .toolbarBackground(.black, for: .navigationBar) 118 .toolbarColorScheme(.dark, for: .navigationBar) 119 .toolbar { 120 ToolbarItem(placement: .cancellationAction) { Button("Close") { dismiss() } } 121 } 122 } 123 } 124 } 125 126 /// Content of a "Photos" section: access request, empty state, or a strip of thumbnails. 127 struct PhotoStrip: View { 128 let access: PhotoAccess 129 let photos: [DivePhoto] 130 let loading: Bool 131 let emptyText: String 132 let onGranted: () -> Void 133 let caption: (DivePhoto) -> String 134 @State private var viewing: ViewedPhoto? 135 136 private struct ViewedPhoto: Identifiable { 137 let id: Int 138 } 139 140 var body: some View { 141 switch access.status { 142 case .notDetermined: 143 VStack(alignment: .leading, spacing: 8) { 144 Text("Sub Rascasse can show the photos and videos taken during the dive here. They stay on your iPhone: nothing is uploaded.") 145 .font(.footnote) 146 .foregroundStyle(.secondary) 147 Button { 148 Task { 149 await access.request() 150 if access.granted { onGranted() } 151 } 152 } label: { 153 Label("Allow photo access", systemImage: "photo.on.rectangle") 154 } 155 } 156 case .denied, .restricted: 157 VStack(alignment: .leading, spacing: 8) { 158 Text("Photo access is denied. You can turn it back on in Settings → Privacy → Photos.") 159 .font(.footnote) 160 .foregroundStyle(.secondary) 161 if let url = URL(string: UIApplication.openSettingsURLString) { 162 Link("Open Settings", destination: url) 163 } 164 } 165 default: 166 if loading { 167 ProgressView().frame(maxWidth: .infinity, minHeight: 96) 168 } else if photos.isEmpty { 169 Text(emptyText).font(.footnote).foregroundStyle(.secondary) 170 } else { 171 VStack(alignment: .leading, spacing: 6) { 172 ScrollView(.horizontal, showsIndicators: false) { 173 LazyHStack(spacing: 6) { 174 ForEach(Array(photos.enumerated()), id: \.element.id) { position, photo in 175 PhotoThumbnail(asset: photo.asset) 176 .overlay(alignment: .topLeading) { 177 if photo.offset != nil { 178 Image(systemName: "water.waves") 179 .font(.caption2.weight(.bold)) 180 .padding(4) 181 .background(Color.accentColor, in: Circle()) 182 .foregroundStyle(.white) 183 .padding(4) 184 .accessibilityLabel("Taken during the dive") 185 } 186 } 187 .onTapGesture { viewing = ViewedPhoto(id: position) } 188 .accessibilityAddTraits(.isButton) 189 } 190 } 191 } 192 Text(summary).font(.caption).foregroundStyle(.secondary) 193 } 194 .fullScreenCover(item: $viewing) { viewed in 195 PhotoViewer(photos: photos, index: viewed.id, caption: caption) 196 } 197 } 198 } 199 } 200 201 private var summary: String { 202 let during = photos.filter { $0.offset != nil }.count 203 let total = photos.count == 1 ? String(localized: "1 photo or video") : String(localized: "\(photos.count) photos and videos") 204 return during > 0 ? String(localized: "\(total), \(during) underwater (marked with a wave)") : total 205 } 206 }