subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

commit 679e1ac64f834d840e1fb8dc5dcdce0e4437f8ca
parent 6c59fe3c42fcf074db622456acbc8afcd52d9f1c
Author: Jean Schurger <jean@schurger.org>
Date:   Fri, 13 Nov 2026 23:57:43 -0500

Dive list: pink camera and video icons when the photo library has photos or videos of the dive

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K7deYG3yp7VDUZJzSBkvTc

Diffstat:
MApp/Model/AppModel.swift | 48++++++++++++++++++++++++++----------------------
MApp/Model/DivePhotos.swift | 24+++++++++++++++++-------
MApp/Views/DiveDetailView.swift | 2+-
MApp/Views/DiveListView.swift | 18+++++++++++++++---
MApp/Views/SitesView.swift | 4++--
5 files changed, 61 insertions(+), 35 deletions(-)

diff --git a/App/Model/AppModel.swift b/App/Model/AppModel.swift @@ -286,48 +286,52 @@ final class AppModel { } } - // MARK: Lieux qui ont des photos + // MARK: Plongées et lieux qui ont des photos - /// Lieux dont au moins une plongée a des photos dans la photothèque (vide tant que l'accès n'est pas accordé). + /// Ce que la photothèque contient pour chaque plongée (vide tant que l'accès n'est pas accordé). + var photoPresence: [String: DivePhotoFinder.Presence] = [:] + /// Lieux dont au moins une plongée a des photos ou des vidéos. var sitesWithPhotos: Set<UInt32> = [] - @ObservationIgnored private var scanningSitePhotos = false + @ObservationIgnored private var scanningPhotos = false - /// Cherche, lieu par lieu, s'il existe des photos prises pendant une plongée. Ne demande jamais l'autorisation : - /// sans elle, rien ne se passe. Le fuseau de chaque lieu est demandé une fois à Apple, sans se presser. - func refreshSitesWithPhotos() async { + /// Cherche, plongée par plongée, s'il existe des photos ou des vidéos prises à ce moment-là. Ne demande jamais + /// l'autorisation : sans elle, rien ne se passe. Le fuseau de chaque lieu est demandé une fois à Apple, sans se presser. + func refreshPhotoPresence() async { let status = PHPhotoLibrary.authorizationStatus(for: .readWrite) - guard status == .authorized || status == .limited, !scanningSitePhotos else { return } - scanningSitePhotos = true - defer { scanningSitePhotos = false } + guard status == .authorized || status == .limited, !scanningPhotos else { return } + scanningPhotos = true + defer { scanningPhotos = false } let log = logbook - var bySite = [UInt32: [Dive]]() - for dive in log.dives { if let uuid = dive.siteUUID { bySite[uuid, default: []].append(dive) } } // Premier passage, tout de suite, avec les fuseaux connus ou estimés. - func scan() async -> Set<UInt32> { + func scan() async -> [String: DivePhotoFinder.Presence] { await Task.detached(priority: .utility) { - var found = Set<UInt32>() - for (uuid, dives) in bySite { - let site = log.sites[uuid] + var found = [String: DivePhotoFinder.Presence]() + for dive in log.dives { + let site = log.site(of: dive) let known = SiteTimeZone.cached(for: site) let zone = known ?? SiteTimeZone.estimated(for: site) - if dives.prefix(60).contains(where: { DivePhotoFinder.hasPhotos(for: $0, zone: zone, slack: known == nil ? 3600 : 0) }) { - found.insert(uuid) - } + let presence = DivePhotoFinder.presence(for: dive, zone: zone, slack: known == nil && site?.hasLocation == true ? 3600 : 0) + if presence.any { found[dive.id] = presence } } return found }.value } - sitesWithPhotos = await scan() + func publish(_ found: [String: DivePhotoFinder.Presence]) { + photoPresence = found + sitesWithPhotos = Set(log.dives.filter { found[$0.id] != nil }.compactMap(\.siteUUID)) + } + publish(await scan()) // Puis on retrouve les vrais fuseaux qui manquent, un lieu à la fois, et on affine. - let missing = bySite.keys.compactMap { log.sites[$0] }.filter { $0.hasLocation && SiteTimeZone.cached(for: $0) == nil } + let used = Set(log.dives.compactMap(\.siteUUID)) + let missing = used.compactMap { log.sites[$0] }.filter { $0.hasLocation && SiteTimeZone.cached(for: $0) == nil } guard !missing.isEmpty else { return } - for site in missing.prefix(40) { + for site in missing.prefix(80) { _ = await SiteTimeZone.zone(for: site) try? await Task.sleep(for: .seconds(1.5)) } - sitesWithPhotos = await scan() + publish(await scan()) } // MARK: Création de compte diff --git a/App/Model/DivePhotos.swift b/App/Model/DivePhotos.swift @@ -79,15 +79,25 @@ enum DivePhotoFinder { return DateInterval(start: start.addingTimeInterval(-20 * 60), duration: TimeInterval(elapsed + 50 * 60)) } - /// Vrai s'il existe au moins une photo dans la fenêtre de la plongée. `slack` élargit la fenêtre quand le fuseau + /// Ce que la photothèque contient pour une plongée. + struct Presence: Equatable { + var photos = false + var videos = false + var any: Bool { photos || videos } + } + + /// Y a-t-il des photos, des vidéos, dans la fenêtre de la plongée ? `slack` élargit la fenêtre quand le fuseau /// du lieu n'est qu'estimé (à une heure près, à cause de l'heure d'été). - static func hasPhotos(for dive: Dive, zone: TimeZone, slack: TimeInterval = 0) -> Bool { + static func presence(for dive: Dive, zone: TimeZone, slack: TimeInterval = 0) -> Presence { let window = window(for: dive, elapsed: dive.durationSeconds, zone: zone) - let options = PHFetchOptions() - options.predicate = NSPredicate(format: "creationDate >= %@ AND creationDate <= %@", - window.start.addingTimeInterval(-slack) as NSDate, window.end.addingTimeInterval(slack) as NSDate) - options.fetchLimit = 1 - return PHAsset.fetchAssets(with: options).count > 0 + func exists(_ type: PHAssetMediaType) -> Bool { + let options = PHFetchOptions() + options.predicate = NSPredicate(format: "creationDate >= %@ AND creationDate <= %@", + window.start.addingTimeInterval(-slack) as NSDate, window.end.addingTimeInterval(slack) as NSDate) + options.fetchLimit = 1 + return PHAsset.fetchAssets(with: type, options: options).count > 0 + } + return Presence(photos: exists(.image), videos: exists(.video)) } /// Photos et vidéos de la fenêtre, dans l'ordre de prise de vue. diff --git a/App/Views/DiveDetailView.swift b/App/Views/DiveDetailView.swift @@ -84,7 +84,7 @@ struct DiveDetailView: View { emptyText: DivePhotoFinder.timeIsGuessed(dive) ? "Aucune photo ce jour-là dans la photothèque de cet iPhone." : "Aucune photo autour de cette plongée dans la photothèque de cet iPhone.", - onGranted: { Task { await loadPhotos(dive) } }, + onGranted: { Task { await loadPhotos(dive); await model.refreshPhotoPresence() } }, caption: { photoCaption($0) }) .id("photos") } diff --git a/App/Views/DiveListView.swift b/App/Views/DiveListView.swift @@ -59,6 +59,7 @@ struct DiveListView: View { } } .sheet(isPresented: $adding) { NewDiveView() } + .task(id: model.logbook.dives.count) { await model.refreshPhotoPresence() } .modifier(FileImport(isPresented: $importing)) .onChange(of: model.logbook.dives.count) { // Option de capture : ouvrir une plongée qui vient d'être importée. @@ -134,9 +135,20 @@ struct DiveRow: View { .frame(minWidth: 34, minHeight: 34) .background(Color.accentColor.gradient, in: RoundedRectangle(cornerRadius: 9, style: .continuous)) VStack(alignment: .leading, spacing: 3) { - Text(model.siteName(of: dive)) - .font(.body.weight(.medium)) - .lineLimit(1) + HStack(spacing: 6) { + Text(model.siteName(of: dive)) + .font(.body.weight(.medium)) + .lineLimit(1) + if let presence = model.photoPresence[dive.id] { + HStack(spacing: 3) { + if presence.photos { Image(systemName: "camera.fill") } + if presence.videos { Image(systemName: "video.fill") } + } + .font(.caption2) + .foregroundStyle(.pink) + .accessibilityLabel(presence.photos && presence.videos ? "Avec photos et vidéos" : (presence.videos ? "Avec vidéos" : "Avec photos")) + } + } HStack(spacing: 6) { Text("\(DiveDate.short(dive)) · \(DiveDate.time(dive))") if dive.rating > 0 { diff --git a/App/Views/SitesView.swift b/App/Views/SitesView.swift @@ -53,7 +53,7 @@ struct SitesView: View { .frame(width: 180) } } - .task(id: model.logbook.dives.count) { await model.refreshSitesWithPhotos() } + .task(id: model.logbook.dives.count) { await model.refreshPhotoPresence() } .navigationDestination(for: UInt32.self) { SiteDetailView(uuid: $0) } .navigationDestination(for: String.self) { DiveDetailView(diveID: $0) } } @@ -185,7 +185,7 @@ struct SiteDetailView: View { Section("Photos") { PhotoStrip(access: photoAccess, photos: photos, loading: photosLoading, emptyText: "Aucune photo de vos plongées ici dans la photothèque de cet iPhone.", - onGranted: { Task { await loadPhotos(dives); await model.refreshSitesWithPhotos() } }, + onGranted: { Task { await loadPhotos(dives); await model.refreshPhotoPresence() } }, caption: { photo in photo.asset.creationDate?.formatted(Date.FormatStyle(date: .abbreviated, time: .shortened, timeZone: photoZone)) ?? "" })