Add new friends system, and sharing legs support
All checks were successful
Release / meta (push) Successful in 9s
Release / linux-build (push) Successful in 6m37s
Release / web-build (push) Successful in 5m29s
Release / android-build (push) Successful in 15m58s
Release / release-master (push) Successful in 20s
Release / release-dev (push) Successful in 26s

This commit is contained in:
2026-01-03 01:07:08 +00:00
parent 42af39b442
commit 89b760508d
19 changed files with 2832 additions and 712 deletions

View File

@@ -12,3 +12,5 @@ part 'data_service_trips.dart';
part 'data_service_notifications.dart';
part 'data_service_badges.dart';
part 'data_service_stats.dart';
part 'data_service_friendships.dart';
part 'data_service_leg_share.dart';

View File

@@ -64,6 +64,18 @@ class DataService extends ChangeNotifier {
bool isLocoTimelineLoading(int locoId) =>
_isLocoTimelineLoading[locoId] ?? false;
// Friendships
List<Friendship> _friendships = [];
List<Friendship> _pendingIncoming = [];
List<Friendship> _pendingOutgoing = [];
List<Friendship> get friendships => _friendships;
List<Friendship> get pendingIncoming => _pendingIncoming;
List<Friendship> get pendingOutgoing => _pendingOutgoing;
bool _isFriendshipsLoading = false;
bool get isFriendshipsLoading => _isFriendshipsLoading;
bool _isPendingFriendshipsLoading = false;
bool get isPendingFriendshipsLoading => _isPendingFriendshipsLoading;
// Trips
List<TripSummary> _trips = [];
List<TripSummary> get trips => _trips;

View File

@@ -0,0 +1,282 @@
part of 'data_service.dart';
extension DataServiceFriendships on DataService {
Future<void> fetchFriendships() async {
_isFriendshipsLoading = true;
try {
final json = await api.get('/friendships');
List<dynamic>? list;
if (json is List) {
list = json;
} else if (json is Map) {
for (final key in ['friendships', 'data', 'items', 'results']) {
final value = json[key];
if (value is List) {
list = value;
break;
}
}
}
final parsed = list
?.whereType<Map>()
.map((e) => Friendship.fromJson(
e.map((k, v) => MapEntry(k.toString(), v)),
))
.where((f) => f.isAccepted)
.toList();
_friendships = parsed ?? [];
} catch (e) {
debugPrint('Failed to fetch friendships: $e');
_friendships = [];
} finally {
_isFriendshipsLoading = false;
_notifyAsync();
}
}
Future<void> fetchPendingFriendships() async {
_isPendingFriendshipsLoading = true;
try {
final incoming = await _fetchPending('incoming');
final outgoing = await _fetchPending('outgoing');
_pendingIncoming = incoming;
_pendingOutgoing = outgoing;
} catch (e) {
debugPrint('Failed to fetch pending friendships: $e');
_pendingIncoming = [];
_pendingOutgoing = [];
} finally {
_isPendingFriendshipsLoading = false;
_notifyAsync();
}
}
Future<List<Friendship>> _fetchPending(String direction) async {
final json = await api.get('/friendships/pending?direction=$direction');
List<dynamic>? list;
if (json is List) {
list = json;
} else if (json is Map) {
for (final key in ['friendships', 'data', 'items', 'results']) {
final value = json[key];
if (value is List) {
list = value;
break;
}
}
}
return list
?.whereType<Map>()
.map((e) => Friendship.fromJson(
e.map((k, v) => MapEntry(k.toString(), v)),
))
.where((f) => f.isPending)
.toList() ??
const [];
}
Future<Friendship?> fetchFriendshipById(String friendshipId) async {
try {
final json = await api.get('/friendships/$friendshipId');
if (json is Map) {
return Friendship.fromJson(
json.map((k, v) => MapEntry(k.toString(), v)),
);
}
} catch (e) {
debugPrint('Failed to fetch friendship $friendshipId: $e');
}
return null;
}
Future<List<UserSummary>> searchUsers(
String query, {
bool friendsOnly = false,
}) async {
final encoded = Uri.encodeComponent(query);
final json = await api.get(
'/users/search?search=$encoded${friendsOnly ? '&friends_only=true' : ''}',
);
List<dynamic>? list;
if (json is List) {
list = json;
} else if (json is Map) {
for (final key in ['users', 'data', 'results', 'items']) {
final value = json[key];
if (value is List) {
list = value;
break;
}
}
}
if (list == null) return const [];
return list
.whereType<Map>()
.map((e) => UserSummary.fromJson(
e.map((k, v) => MapEntry(k.toString(), v)),
))
.toList();
}
Future<Friendship> fetchFriendshipStatus(String targetUserId) async {
try {
final json = await api.get('/friendships/status/$targetUserId');
if (json is Map) {
return Friendship.fromStatusJson(
json.map((k, v) => MapEntry(k.toString(), v)),
targetUserId: targetUserId,
);
}
} catch (e) {
debugPrint('Failed to fetch friendship status for $targetUserId: $e');
}
return Friendship(
id: null,
status: 'none',
requesterId: '',
addresseeId: targetUserId,
);
}
Future<Friendship> requestFriendship(
String targetUserId, {
UserSummary? targetUser,
}) async {
final json = await api.post('/friendships/request', {
"target_user_id": targetUserId,
});
final friendship = _parseAndUpsertFriendship(
json,
fallbackStatus: 'pending',
overrideAddressee: targetUser,
);
_pendingOutgoing = [friendship, ..._pendingOutgoing];
_notifyAsync();
return friendship;
}
Future<Friendship> acceptFriendship(String friendshipId) async {
final json = await api.post('/friendships/$friendshipId/accept', {});
final friendship = _parseAndUpsertFriendship(json, fallbackStatus: 'accepted');
_pendingIncoming = _pendingIncoming.where((f) => f.id != friendshipId).toList();
_notifyAsync();
return friendship;
}
Future<Friendship> rejectFriendship(String friendshipId) async {
final json = await api.post('/friendships/$friendshipId/reject', {});
final friendship = _parseAndUpsertFriendship(json, fallbackStatus: 'declined');
_pendingIncoming = _pendingIncoming.where((f) => f.id != friendshipId).toList();
_notifyAsync();
return friendship;
}
Future<Friendship> cancelFriendship(String friendshipId) async {
final json = await api.post('/friendships/$friendshipId/cancel', {});
final friendship =
_parseAndRemoveFriendship(json, friendshipId, status: 'none');
_pendingOutgoing =
_pendingOutgoing.where((f) => f.id != friendshipId).toList();
_notifyAsync();
return friendship;
}
Future<void> deleteFriendship(String friendshipId) async {
try {
await api.delete('/friendships/$friendshipId');
} catch (e) {
debugPrint('Failed to delete friendship $friendshipId: $e');
rethrow;
}
_friendships = _friendships.where((f) => f.id != friendshipId).toList();
_pendingIncoming =
_pendingIncoming.where((f) => f.id != friendshipId).toList();
_pendingOutgoing =
_pendingOutgoing.where((f) => f.id != friendshipId).toList();
_notifyAsync();
}
Future<Friendship> blockUser(String targetUserId) async {
final json = await api.post('/friendships/block', {
"target_user_id": targetUserId,
});
final friendship = _parseAndUpsertFriendship(json, fallbackStatus: 'blocked');
_pendingIncoming = _pendingIncoming
.where((f) => f.addresseeId != targetUserId && f.requesterId != targetUserId)
.toList();
_pendingOutgoing = _pendingOutgoing
.where((f) => f.addresseeId != targetUserId && f.requesterId != targetUserId)
.toList();
_notifyAsync();
return friendship;
}
Friendship _parseAndUpsertFriendship(
dynamic json, {
String fallbackStatus = 'none',
UserSummary? overrideAddressee,
}) {
Friendship friendship;
if (json is Map) {
friendship = Friendship.fromJson(
json.map((k, v) => MapEntry(k.toString(), v)),
);
} else {
friendship = Friendship(
id: null,
status: fallbackStatus,
requesterId: '',
addresseeId: '',
);
}
if (overrideAddressee != null &&
(friendship.addressee == null ||
friendship.addressee!.userId.isEmpty ||
friendship.addressee!.displayName.isEmpty)) {
friendship = friendship.copyWith(
addressee: overrideAddressee,
addresseeId: overrideAddressee.userId,
);
}
_upsertFriendship(friendship);
return friendship;
}
Friendship _parseAndRemoveFriendship(
dynamic json,
String friendshipId, {
required String status,
}) {
Friendship friendship;
if (json is Map) {
friendship = Friendship.fromJson(
json.map((k, v) => MapEntry(k.toString(), v)),
);
} else {
friendship = Friendship(
id: friendshipId,
status: status,
requesterId: '',
addresseeId: '',
);
}
_friendships = _friendships.where((f) => f.id != friendshipId).toList();
_notifyAsync();
return friendship;
}
void _upsertFriendship(Friendship friendship) {
if (friendship.id == null || friendship.id!.isEmpty) {
_notifyAsync();
return;
}
final idx = _friendships.indexWhere((f) => f.id == friendship.id);
if (idx >= 0) {
_friendships[idx] = friendship;
} else {
_friendships = [friendship, ..._friendships];
}
_friendships = _friendships.where((f) => f.isAccepted).toList();
_notifyAsync();
}
}

View File

@@ -0,0 +1,97 @@
part of 'data_service.dart';
extension DataServiceLegShare on DataService {
Future<LegShareData?> fetchLegShare(String legShareId) async {
try {
final json = await api.get('/legs/share/$legShareId');
if (json is Map) {
return LegShareData.fromJson(
json.map((k, v) => MapEntry(k.toString(), v)),
);
}
} catch (e) {
debugPrint('Failed to fetch leg share $legShareId: $e');
rethrow;
}
return null;
}
Future<void> rejectLegShare(String legShareId) async {
try {
await api.post('/legs/share/$legShareId/reject', {});
} catch (e) {
debugPrint('Failed to reject leg share $legShareId: $e');
rethrow;
}
}
Future<int?> acceptLegShare(LegShareData share) async {
final entry = share.entry;
final hasRoute = entry.route.isNotEmpty;
final locosPayload = <Map<String, dynamic>>[];
for (var i = 0; i < entry.locos.length; i++) {
final loco = entry.locos[i];
final pos = loco.allocPos != 0 ? loco.allocPos : i;
locosPayload.add({
"loco_id": loco.id,
"alloc_pos": pos,
"alloc_powering": loco.powering ? 1 : 0,
});
}
final payload = <String, dynamic>{
"leg_share_id": share.id,
"leg_trip": null,
"leg_begin_time": entry.beginTime.toIso8601String(),
if (entry.endTime != null) "leg_end_time": entry.endTime!.toIso8601String(),
if (entry.originTime != null)
"leg_origin_time": entry.originTime!.toIso8601String(),
if (entry.destinationTime != null)
"leg_destination_time": entry.destinationTime!.toIso8601String(),
"leg_notes": entry.notes,
"leg_headcode": entry.headcode,
"leg_network": entry.network,
"leg_origin": entry.origin,
"leg_destination": entry.destination,
"leg_begin_delay": entry.beginDelayMinutes ?? 0,
if (entry.endDelayMinutes != null) "leg_end_delay": entry.endDelayMinutes,
"locos": locosPayload,
"share_user_ids": <String>[],
};
dynamic response;
if (hasRoute) {
response = await api.post(
'/add',
{
...payload,
"leg_route": entry.route,
"leg_mileage": entry.mileage,
},
);
} else {
response = await api.post(
'/add/manual',
{
...payload,
"leg_start": entry.start,
"leg_end": entry.end,
"leg_distance": entry.mileage,
"isKilometers": false,
},
);
}
final legId = _parseNullableInt(
response is Map ? response['leg_id'] : null,
);
if (legId != null) return legId;
return null;
}
int? _parseNullableInt(dynamic value) {
if (value is int) return value;
if (value is num) return value.toInt();
if (value is String) return int.tryParse(value);
return null;
}
}