commit 9057e7ca52156945bd8ab6f4f86f4277c4b1f974
parent fd8cb13b71f22b10cf9e316cf5379f47b116c18b
Author: Jean Schurger <jean@schurger.org>
Date: Fri, 13 Nov 2026 23:49:47 -0500
Site map and list: pink camera on sites that have dive photos
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K7deYG3yp7VDUZJzSBkvTc
Diffstat:
3 files changed, 84 insertions(+), 5 deletions(-)
diff --git a/App/Model/AppModel.swift b/App/Model/AppModel.swift
@@ -1,6 +1,7 @@
import Foundation
import GitLite
import Observation
+import Photos
import SubsurfaceLog
struct DiveSection: Identifiable {
@@ -285,6 +286,50 @@ final class AppModel {
}
}
+ // MARK: 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é).
+ var sitesWithPhotos: Set<UInt32> = []
+ @ObservationIgnored private var scanningSitePhotos = 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 {
+ let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
+ guard status == .authorized || status == .limited, !scanningSitePhotos else { return }
+ scanningSitePhotos = true
+ defer { scanningSitePhotos = 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> {
+ await Task.detached(priority: .utility) {
+ var found = Set<UInt32>()
+ for (uuid, dives) in bySite {
+ let site = log.sites[uuid]
+ 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)
+ }
+ }
+ return found
+ }.value
+ }
+ sitesWithPhotos = 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 }
+ guard !missing.isEmpty else { return }
+ for site in missing.prefix(40) {
+ _ = await SiteTimeZone.zone(for: site)
+ try? await Task.sleep(for: .seconds(1.5))
+ }
+ sitesWithPhotos = await scan()
+ }
+
// MARK: Création de compte
private var cloudUserAgent: String { "SubRascasse:\(Brand.shortVersion):iOS" }
diff --git a/App/Model/DivePhotos.swift b/App/Model/DivePhotos.swift
@@ -38,6 +38,12 @@ enum SiteTimeZone {
return TimeZone(identifier: name)
}
+ /// Fuseau estimé sans réseau : celui de la longitude (heure d'hiver), exact à une heure près.
+ static func estimated(for site: DiveSite?) -> TimeZone {
+ guard let longitude = site?.longitude else { return .current }
+ return TimeZone(secondsFromGMT: Int((longitude / 15).rounded()) * 3600) ?? .current
+ }
+
/// Sans position, ou si la recherche échoue : le fuseau de l'iPhone.
static func zone(for site: DiveSite?) async -> TimeZone {
guard let site, let latitude = site.latitude, let longitude = site.longitude else { return .current }
@@ -73,6 +79,17 @@ 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
+ /// 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 {
+ 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
+ }
+
/// Photos et vidéos de la fenêtre, dans l'ordre de prise de vue.
static func photos(for dive: Dive, elapsed: Int, zone: TimeZone, limit: Int = 300) -> [DivePhoto] {
let window = window(for: dive, elapsed: elapsed, zone: zone)
diff --git a/App/Views/SitesView.swift b/App/Views/SitesView.swift
@@ -4,7 +4,7 @@ import SwiftUI
struct SitesView: View {
@Environment(AppModel.self) private var model
- @State private var mode = 0
+ @State private var mode = LaunchOptions.flag("-sitesList") ? 1 : 0
@State private var selection: UInt32?
@State private var search = ""
@@ -14,6 +14,12 @@ struct SitesView: View {
var id: UInt32 { site.uuid }
}
+ private struct Pin: Identifiable {
+ let entry: Entry
+ let hasPhotos: Bool
+ var id: String { "\(entry.site.uuid)-\(hasPhotos)" }
+ }
+
private var entries: [Entry] {
var counts = [UInt32: Int]()
for dive in model.logbook.dives {
@@ -47,6 +53,7 @@ struct SitesView: View {
.frame(width: 180)
}
}
+ .task(id: model.logbook.dives.count) { await model.refreshSitesWithPhotos() }
.navigationDestination(for: UInt32.self) { SiteDetailView(uuid: $0) }
.navigationDestination(for: String.self) { DiveDetailView(diveID: $0) }
}
@@ -54,10 +61,14 @@ struct SitesView: View {
private func map(_ located: [Entry]) -> some View {
Map(selection: $selection) {
- ForEach(located) { entry in
- Marker(entry.site.name, systemImage: "water.waves",
+ // L'identité de l'épingle change avec la présence de photos : sans cela, la carte garde l'ancienne.
+ ForEach(located.map { Pin(entry: $0, hasPhotos: model.sitesWithPhotos.contains($0.site.uuid)) }) { pin in
+ let entry = pin.entry
+ // Un appareil photo rose quand la photothèque a des photos d'une plongée faite ici.
+ let hasPhotos = pin.hasPhotos
+ Marker(entry.site.name, systemImage: hasPhotos ? "camera.fill" : "water.waves",
coordinate: CLLocationCoordinate2D(latitude: entry.site.latitude ?? 0, longitude: entry.site.longitude ?? 0))
- .tint(Color.accentColor)
+ .tint(hasPhotos ? Color.pink : Color.accentColor)
.tag(entry.site.uuid)
}
}
@@ -103,6 +114,12 @@ struct SitesView: View {
}
}
Spacer()
+ if model.sitesWithPhotos.contains(entry.site.uuid) {
+ Image(systemName: "camera.fill")
+ .font(.caption)
+ .foregroundStyle(.pink)
+ .accessibilityLabel("Avec photos")
+ }
Text("\(entry.count)")
.font(.callout.monospacedDigit())
.foregroundStyle(.secondary)
@@ -168,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) } },
+ onGranted: { Task { await loadPhotos(dives); await model.refreshSitesWithPhotos() } },
caption: { photo in
photo.asset.creationDate?.formatted(Date.FormatStyle(date: .abbreviated, time: .shortened, timeZone: photoZone)) ?? ""
})