|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:submersion/l10n/arb/app_localizations.dart'; |
| 4 | + |
| 5 | +/// Identifies each configurable section on the Dive Details page. |
| 6 | +/// |
| 7 | +/// Declaration order defines the default display order. The two fixed sections |
| 8 | +/// (Header and Dive Profile Chart) are not included — they always render first. |
| 9 | +enum DiveDetailSectionId { |
| 10 | + decoO2, |
| 11 | + sacSegments, |
| 12 | + details, |
| 13 | + environment, |
| 14 | + altitude, |
| 15 | + tide, |
| 16 | + weights, |
| 17 | + tanks, |
| 18 | + buddies, |
| 19 | + signatures, |
| 20 | + equipment, |
| 21 | + sightings, |
| 22 | + media, |
| 23 | + tags, |
| 24 | + notes, |
| 25 | + customFields, |
| 26 | + dataSources; |
| 27 | + |
| 28 | + /// Human-readable name shown in the settings UI (English fallback). |
| 29 | + String get displayName { |
| 30 | + return switch (this) { |
| 31 | + decoO2 => 'Deco Status / Tissue Loading', |
| 32 | + sacSegments => 'SAC Rate by Segment', |
| 33 | + details => 'Details', |
| 34 | + environment => 'Environment', |
| 35 | + altitude => 'Altitude', |
| 36 | + tide => 'Tide', |
| 37 | + weights => 'Weights', |
| 38 | + tanks => 'Tanks', |
| 39 | + buddies => 'Buddies', |
| 40 | + signatures => 'Signatures', |
| 41 | + equipment => 'Equipment', |
| 42 | + sightings => 'Marine Life Sightings', |
| 43 | + media => 'Media', |
| 44 | + tags => 'Tags', |
| 45 | + notes => 'Notes', |
| 46 | + customFields => 'Custom Fields', |
| 47 | + dataSources => 'Data Sources', |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + /// Short description shown below the name in the settings UI |
| 52 | + /// (English fallback). |
| 53 | + String get description { |
| 54 | + return switch (this) { |
| 55 | + decoO2 => 'NDL, ceiling, tissue heat map, O2 toxicity', |
| 56 | + sacSegments => 'Phase/time segmentation, cylinder breakdown', |
| 57 | + details => 'Type, location, trip, dive center, interval', |
| 58 | + environment => 'Air/water temp, visibility, current', |
| 59 | + altitude => 'Altitude value, category, deco requirement', |
| 60 | + tide => 'Tide cycle graph and timing', |
| 61 | + weights => 'Weight breakdown, total weight', |
| 62 | + tanks => 'Tank list, gas mixes, pressures, per-tank SAC', |
| 63 | + buddies => 'Buddy list with roles', |
| 64 | + signatures => 'Buddy/instructor signature display and capture', |
| 65 | + equipment => 'Equipment used in dive', |
| 66 | + sightings => 'Species spotted, sighting details', |
| 67 | + media => 'Photos/videos gallery', |
| 68 | + tags => 'Dive tags', |
| 69 | + notes => 'Dive notes/description', |
| 70 | + customFields => 'User-defined custom fields', |
| 71 | + dataSources => 'Connected dive computers, source management', |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + /// Localized display name resolved via [AppLocalizations]. |
| 76 | + String localizedDisplayName(AppLocalizations l10n) { |
| 77 | + return switch (this) { |
| 78 | + decoO2 => l10n.diveDetailSection_decoO2_name, |
| 79 | + sacSegments => l10n.diveDetailSection_sacSegments_name, |
| 80 | + details => l10n.diveDetailSection_details_name, |
| 81 | + environment => l10n.diveDetailSection_environment_name, |
| 82 | + altitude => l10n.diveDetailSection_altitude_name, |
| 83 | + tide => l10n.diveDetailSection_tide_name, |
| 84 | + weights => l10n.diveDetailSection_weights_name, |
| 85 | + tanks => l10n.diveDetailSection_tanks_name, |
| 86 | + buddies => l10n.diveDetailSection_buddies_name, |
| 87 | + signatures => l10n.diveDetailSection_signatures_name, |
| 88 | + equipment => l10n.diveDetailSection_equipment_name, |
| 89 | + sightings => l10n.diveDetailSection_sightings_name, |
| 90 | + media => l10n.diveDetailSection_media_name, |
| 91 | + tags => l10n.diveDetailSection_tags_name, |
| 92 | + notes => l10n.diveDetailSection_notes_name, |
| 93 | + customFields => l10n.diveDetailSection_customFields_name, |
| 94 | + dataSources => l10n.diveDetailSection_dataSources_name, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + /// Localized description resolved via [AppLocalizations]. |
| 99 | + String localizedDescription(AppLocalizations l10n) { |
| 100 | + return switch (this) { |
| 101 | + decoO2 => l10n.diveDetailSection_decoO2_description, |
| 102 | + sacSegments => l10n.diveDetailSection_sacSegments_description, |
| 103 | + details => l10n.diveDetailSection_details_description, |
| 104 | + environment => l10n.diveDetailSection_environment_description, |
| 105 | + altitude => l10n.diveDetailSection_altitude_description, |
| 106 | + tide => l10n.diveDetailSection_tide_description, |
| 107 | + weights => l10n.diveDetailSection_weights_description, |
| 108 | + tanks => l10n.diveDetailSection_tanks_description, |
| 109 | + buddies => l10n.diveDetailSection_buddies_description, |
| 110 | + signatures => l10n.diveDetailSection_signatures_description, |
| 111 | + equipment => l10n.diveDetailSection_equipment_description, |
| 112 | + sightings => l10n.diveDetailSection_sightings_description, |
| 113 | + media => l10n.diveDetailSection_media_description, |
| 114 | + tags => l10n.diveDetailSection_tags_description, |
| 115 | + notes => l10n.diveDetailSection_notes_description, |
| 116 | + customFields => l10n.diveDetailSection_customFields_description, |
| 117 | + dataSources => l10n.diveDetailSection_dataSources_description, |
| 118 | + }; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/// Visibility and ordering configuration for a single dive detail section. |
| 123 | +class DiveDetailSectionConfig { |
| 124 | + final DiveDetailSectionId id; |
| 125 | + final bool visible; |
| 126 | + |
| 127 | + const DiveDetailSectionConfig({required this.id, required this.visible}); |
| 128 | + |
| 129 | + DiveDetailSectionConfig copyWith({bool? visible}) { |
| 130 | + return DiveDetailSectionConfig(id: id, visible: visible ?? this.visible); |
| 131 | + } |
| 132 | + |
| 133 | + Map<String, dynamic> toJson() => {'id': id.name, 'visible': visible}; |
| 134 | + |
| 135 | + factory DiveDetailSectionConfig.fromJson(Map<String, dynamic> json) { |
| 136 | + final idStr = json['id'] as String; |
| 137 | + final id = DiveDetailSectionId.values.firstWhere((e) => e.name == idStr); |
| 138 | + return DiveDetailSectionConfig( |
| 139 | + id: id, |
| 140 | + visible: json['visible'] as bool? ?? true, |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + static DiveDetailSectionConfig? tryFromJson(Map<String, dynamic> json) { |
| 145 | + try { |
| 146 | + return DiveDetailSectionConfig.fromJson(json); |
| 147 | + } catch (_) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + static const List<DiveDetailSectionConfig> defaultSections = [ |
| 153 | + DiveDetailSectionConfig(id: DiveDetailSectionId.decoO2, visible: true), |
| 154 | + DiveDetailSectionConfig(id: DiveDetailSectionId.sacSegments, visible: true), |
| 155 | + DiveDetailSectionConfig(id: DiveDetailSectionId.details, visible: true), |
| 156 | + DiveDetailSectionConfig(id: DiveDetailSectionId.environment, visible: true), |
| 157 | + DiveDetailSectionConfig(id: DiveDetailSectionId.altitude, visible: true), |
| 158 | + DiveDetailSectionConfig(id: DiveDetailSectionId.tide, visible: true), |
| 159 | + DiveDetailSectionConfig(id: DiveDetailSectionId.weights, visible: true), |
| 160 | + DiveDetailSectionConfig(id: DiveDetailSectionId.tanks, visible: true), |
| 161 | + DiveDetailSectionConfig(id: DiveDetailSectionId.buddies, visible: true), |
| 162 | + DiveDetailSectionConfig(id: DiveDetailSectionId.signatures, visible: true), |
| 163 | + DiveDetailSectionConfig(id: DiveDetailSectionId.equipment, visible: true), |
| 164 | + DiveDetailSectionConfig(id: DiveDetailSectionId.sightings, visible: true), |
| 165 | + DiveDetailSectionConfig(id: DiveDetailSectionId.media, visible: true), |
| 166 | + DiveDetailSectionConfig(id: DiveDetailSectionId.tags, visible: true), |
| 167 | + DiveDetailSectionConfig(id: DiveDetailSectionId.notes, visible: true), |
| 168 | + DiveDetailSectionConfig( |
| 169 | + id: DiveDetailSectionId.customFields, |
| 170 | + visible: true, |
| 171 | + ), |
| 172 | + DiveDetailSectionConfig(id: DiveDetailSectionId.dataSources, visible: true), |
| 173 | + ]; |
| 174 | + |
| 175 | + static String sectionsToJson(List<DiveDetailSectionConfig> sections) { |
| 176 | + return jsonEncode(sections.map((s) => s.toJson()).toList()); |
| 177 | + } |
| 178 | + |
| 179 | + static List<DiveDetailSectionConfig> sectionsFromJson(String? json) { |
| 180 | + if (json == null || json.isEmpty) return List.of(defaultSections); |
| 181 | + try { |
| 182 | + final decoded = jsonDecode(json) as List; |
| 183 | + final sections = decoded |
| 184 | + .map((e) => e is Map<String, dynamic> ? tryFromJson(e) : null) |
| 185 | + .whereType<DiveDetailSectionConfig>() |
| 186 | + .toList(); |
| 187 | + if (sections.isEmpty) return List.of(defaultSections); |
| 188 | + return ensureAllSections(sections); |
| 189 | + } catch (_) { |
| 190 | + return List.of(defaultSections); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + static List<DiveDetailSectionConfig> ensureAllSections( |
| 195 | + List<DiveDetailSectionConfig> sections, |
| 196 | + ) { |
| 197 | + final presentIds = sections.map((s) => s.id).toSet(); |
| 198 | + final missing = DiveDetailSectionId.values |
| 199 | + .where((id) => !presentIds.contains(id)) |
| 200 | + .map((id) => DiveDetailSectionConfig(id: id, visible: true)); |
| 201 | + if (missing.isEmpty) return sections; |
| 202 | + return [...sections, ...missing]; |
| 203 | + } |
| 204 | +} |
0 commit comments