overhaul using a trip struct that notifies its ui dependencies
This commit is contained in:
@@ -5,35 +5,56 @@ import 'dart:collection';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:anyway/structs/landmark.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:geocoding/geocoding.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class Trip {
|
||||
final String uuid;
|
||||
final String cityName;
|
||||
// TODO: cityName should be inferred from coordinates of the Landmarks
|
||||
final int totalTime;
|
||||
final LinkedList<Landmark> landmarks;
|
||||
class Trip with ChangeNotifier {
|
||||
String uuid;
|
||||
int totalTime;
|
||||
LinkedList<Landmark> landmarks;
|
||||
// could be empty as well
|
||||
|
||||
Future<String> get cityName async {
|
||||
if (GeocodingPlatform.instance == null) {
|
||||
return '${landmarks.first.location[0]}, ${landmarks.first.location[1]}';
|
||||
}
|
||||
List<Placemark> placemarks = await placemarkFromCoordinates(landmarks.first.location[0], landmarks.first.location[1]);
|
||||
return placemarks.first.locality ?? 'Unknown';
|
||||
}
|
||||
|
||||
|
||||
Trip({
|
||||
required this.uuid,
|
||||
required this.cityName,
|
||||
required this.landmarks,
|
||||
this.totalTime = 0
|
||||
});
|
||||
|
||||
this.uuid = 'pending',
|
||||
this.totalTime = 0,
|
||||
LinkedList<Landmark>? landmarks
|
||||
// a trip can be created with no landmarks, but the list should be initialized anyway
|
||||
}) : landmarks = landmarks ?? LinkedList<Landmark>();
|
||||
|
||||
|
||||
factory Trip.fromJson(Map<String, dynamic> json) {
|
||||
Trip trip = Trip(
|
||||
uuid: json['uuid'],
|
||||
cityName: json['city_name'] ?? 'Not communicated',
|
||||
landmarks: LinkedList()
|
||||
totalTime: json['total_time'],
|
||||
);
|
||||
|
||||
return trip;
|
||||
}
|
||||
|
||||
void loadFromJson(Map<String, dynamic> json) {
|
||||
uuid = json['uuid'];
|
||||
totalTime = json['total_time'];
|
||||
notifyListeners();
|
||||
}
|
||||
void addLandmark(Landmark landmark) {
|
||||
landmarks.add(landmark);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateUUID(String newUUID) {
|
||||
uuid = newUUID;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
factory Trip.fromPrefs(SharedPreferences prefs, String uuid) {
|
||||
String? content = prefs.getString('trip_$uuid');
|
||||
@@ -47,7 +68,7 @@ class Trip {
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'uuid': uuid,
|
||||
'city_name': cityName,
|
||||
'total_time': totalTime,
|
||||
'first_landmark_uuid': landmarks.first.uuid
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user