functional datastructure. Needs to be able to write to storage as well
This commit is contained in:
@@ -1,25 +1,49 @@
|
||||
// Represents a collection of landmarks that represent a journey
|
||||
// Different instances of a Trip can be saved and loaded by the user
|
||||
|
||||
import 'dart:collection';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fast_network_navigation/structs/landmark.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class Trip {
|
||||
final String uuid;
|
||||
final String cityName;
|
||||
final List<Landmark> landmarks;
|
||||
// TODO: cityName should be inferred from coordinates of the Landmarks
|
||||
final LinkedList<Landmark> landmarks;
|
||||
// could be empty as well
|
||||
|
||||
|
||||
Trip({required this.uuid, required this.cityName, required this.landmarks});
|
||||
Trip({
|
||||
required this.uuid,
|
||||
required this.cityName,
|
||||
required this.landmarks,
|
||||
});
|
||||
|
||||
factory Trip.fromJson(Map<String, dynamic> json) {
|
||||
List<Landmark> landmarks = [];
|
||||
for (var landmark in json['landmarks']) {
|
||||
landmarks.add(Landmark.fromJson(landmark));
|
||||
}
|
||||
return Trip(
|
||||
uuid: json['uuid'],
|
||||
cityName: json['cityName'],
|
||||
landmarks: landmarks,
|
||||
landmarks: LinkedList()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
factory Trip.fromPrefs(SharedPreferences prefs, String uuid) {
|
||||
String? content = prefs.getString('trip_$uuid');
|
||||
Map<String, dynamic> json = jsonDecode(content!);
|
||||
Trip trip = Trip.fromJson(json);
|
||||
String? firstUUID = json['entry_uuid'];
|
||||
appendLandmarks(trip.landmarks, prefs, firstUUID);
|
||||
return trip;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper
|
||||
|
||||
appendLandmarks(LinkedList<Landmark> landmarks, SharedPreferences prefs, String? firstUUID) {
|
||||
while (firstUUID != null) {
|
||||
var (head, nextUUID) = getLandmarkFromPrefs(prefs, firstUUID);
|
||||
landmarks.add(head);
|
||||
firstUUID = nextUUID;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user