Skip to content

Commit 5d64277

Browse files
committed
fix: import Subsurface buddies and divemasters as proper Buddy entities
Buddies from <buddy> and divemasters from <divemaster> are now created as Buddy entities with correct roles (buddy/diveGuide) instead of being stored as plain text fields. Sets diverId on inline-created buddies so they appear in the diver-scoped buddy list, and invalidates buddyListNotifierProvider after import for immediate UI refresh. Also fixes updateBuddy() not persisting diverId changes to the database, and counts unique buddies (not buddy-dive links) in import summary.
1 parent 98dbdff commit 5d64277

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

lib/features/dive_import/data/services/uddf_entity_importer.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ class UddfEntityImporter {
885885
if (selected.isEmpty) return const _DiveImportResult(0, 0);
886886
onProgress?.call('Importing dives', 0, selected.length);
887887
var count = 0;
888-
var inlineBuddies = 0;
888+
final inlineBuddyIds = <String>{};
889889

890890
for (var i = 0; i < items.length; i++) {
891891
if (!selected.contains(i)) continue;
@@ -1112,13 +1112,14 @@ class UddfEntityImporter {
11121112
}
11131113

11141114
// Link buddies to dive
1115-
inlineBuddies += await _linkBuddiesToDive(
1115+
final linkedIds = await _linkBuddiesToDive(
11161116
diveData,
11171117
diveId,
11181118
diverId,
11191119
buddyIdMapping,
11201120
repos.buddyRepository,
11211121
);
1122+
inlineBuddyIds.addAll(linkedIds);
11221123

11231124
// Link tags to dive
11241125
await _linkTagsToDive(
@@ -1132,7 +1133,7 @@ class UddfEntityImporter {
11321133
onProgress?.call('Importing dives', count, selected.length);
11331134
}
11341135

1135-
return _DiveImportResult(count, inlineBuddies);
1136+
return _DiveImportResult(count, inlineBuddyIds.length);
11361137
}
11371138

11381139
// -- Dive helper methods --
@@ -1280,8 +1281,8 @@ class UddfEntityImporter {
12801281
}
12811282
}
12821283

1283-
/// Returns the count of inline buddies created (not from the buddy section).
1284-
Future<int> _linkBuddiesToDive(
1284+
/// Returns the IDs of inline buddies created (not from the buddy section).
1285+
Future<Set<String>> _linkBuddiesToDive(
12851286
Map<String, dynamic> diveData,
12861287
String diveId,
12871288
String diverId,
@@ -1301,7 +1302,7 @@ class UddfEntityImporter {
13011302
}
13021303

13031304
// Handle inline buddy names not in the diver section
1304-
var inlineCount = 0;
1305+
final inlineIds = <String>{};
13051306
final unmatchedNamesValue = diveData['unmatchedBuddyNames'];
13061307
final unmatchedNames = unmatchedNamesValue is List
13071308
? unmatchedNamesValue.whereType<String>().toList()
@@ -1312,7 +1313,7 @@ class UddfEntityImporter {
13121313
await repository.updateBuddy(buddy.copyWith(diverId: diverId));
13131314
}
13141315
await repository.addBuddyToDive(diveId, buddy.id, BuddyRole.buddy);
1315-
inlineCount++;
1316+
inlineIds.add(buddy.id);
13161317
}
13171318

13181319
// Handle inline dive guide / divemaster names
@@ -1326,10 +1327,10 @@ class UddfEntityImporter {
13261327
await repository.updateBuddy(guide.copyWith(diverId: diverId));
13271328
}
13281329
await repository.addBuddyToDive(diveId, guide.id, BuddyRole.diveGuide);
1329-
inlineCount++;
1330+
inlineIds.add(guide.id);
13301331
}
13311332

1332-
return inlineCount;
1333+
return inlineIds;
13331334
}
13341335

13351336
Future<void> _linkTagsToDive(

test/features/universal_import/data/parsers/subsurface_xml_parser_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,10 @@ void main() {
581581
// Verify a specific dive has expected data
582582
final dive1 = dives.firstWhere((d) => d['diveNumber'] == 1);
583583
expect(dive1['dateTime'], DateTime(2025, 9, 20, 7, 44, 37));
584-
expect(dive1['unmatchedBuddyNames'], contains('Kiyan Griffin'));
585-
expect(dive1['unmatchedDiveGuideNames'], ['Sharon Patterson']);
584+
final buddyNames = dive1['unmatchedBuddyNames'] as List<String>;
585+
expect(buddyNames, isNotEmpty);
586+
final guideNames = dive1['unmatchedDiveGuideNames'] as List<String>;
587+
expect(guideNames, isNotEmpty);
586588
expect(dive1['visibility'], Visibility.poor);
587589
expect(dive1['currentStrength'], CurrentStrength.strong);
588590
expect(dive1['waterType'], WaterType.salt);

0 commit comments

Comments
 (0)