From c88b699424b647fefdbbfa7036744e643deafbfe Mon Sep 17 00:00:00 2001 From: Sara <sarasdj@stud.ntnu.no> Date: Thu, 11 Apr 2024 10:50:45 +0200 Subject: [PATCH] update: comments and variable names, app --- app/lib/consts.dart | 2 +- app/lib/pages/default_page.dart | 2 +- app/lib/server_requests/fetch_markers.dart | 8 +++-- app/lib/server_requests/fetch_relation.dart | 8 +++-- app/lib/server_requests/init_state.dart | 6 ++-- app/lib/widgets/choropleth_map.dart | 6 ++-- app/lib/widgets/main_layout.dart | 35 ++++++++++----------- 7 files changed, 35 insertions(+), 32 deletions(-) diff --git a/app/lib/consts.dart b/app/lib/consts.dart index f31ec731..6ec58394 100644 --- a/app/lib/consts.dart +++ b/app/lib/consts.dart @@ -13,7 +13,7 @@ const String mapEndpoint = "update_map"; String selectedLake = 'Mjøsa'; // Init to Mjøsa, NB should be initialised to last selected lake Uint8List selectedRelation = Uint8List(0); List<Measurement> selectedMarkerList = []; -Measurement? selectedTile; +Measurement? selectedSubDiv; LatLng mapCenter = LatLng(60.8000, 10.8471); DateTime ?lastUpdate; // Last time data was fetched from server diff --git a/app/lib/pages/default_page.dart b/app/lib/pages/default_page.dart index fe40b009..3e14bd8d 100644 --- a/app/lib/pages/default_page.dart +++ b/app/lib/pages/default_page.dart @@ -86,7 +86,7 @@ class _DefaultPageState extends State<DefaultPage> { child: ListView( children: [ MapContainerWidget( - markerList: selectedMarkerList, + measurements: selectedMarkerList, relation: selectedRelation, serverConnection: serverConnection, ), diff --git a/app/lib/server_requests/fetch_markers.dart b/app/lib/server_requests/fetch_markers.dart index 818bec97..a69c2cf8 100644 --- a/app/lib/server_requests/fetch_markers.dart +++ b/app/lib/server_requests/fetch_markers.dart @@ -55,14 +55,16 @@ Future<FetchResult> fetchMeasurements() async { } } } - return loadSavedData(); + return loadMeasurements(); } catch (e) { - return loadSavedData(); + print("Error in fetching measurements from server: $e"); + return loadMeasurements(); } } -Future<FetchResult> loadSavedData() async { +Future<FetchResult> loadMeasurements() async { try { + print("Loading measurements from file"); // Get latest saved data from file if the server does not respond Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); String filePath = '${appDocumentsDirectory.path}/last_data.json'; diff --git a/app/lib/server_requests/fetch_relation.dart b/app/lib/server_requests/fetch_relation.dart index 876fa900..e4553c25 100644 --- a/app/lib/server_requests/fetch_relation.dart +++ b/app/lib/server_requests/fetch_relation.dart @@ -37,15 +37,17 @@ Future<Uint8List> fetchRelation() async { return Uint8List.fromList(utf8.encode(responseBody)); } } - return loadSavedRelation(); + return loadRelation(); } catch (e) { - return loadSavedRelation(); + print("Error in fetching relation from server: $e"); + return loadRelation(); } } /// Load last saved relation data form last_relation.json -Future<Uint8List> loadSavedRelation() async { +Future<Uint8List> loadRelation() async { try { + print("Loading relation from file"); // Get latest saved relation from file if the server does not respond Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); String filePath = '${appDocumentsDirectory.path}/last_relation.json'; diff --git a/app/lib/server_requests/init_state.dart b/app/lib/server_requests/init_state.dart index 77eccc9d..6cdf33eb 100644 --- a/app/lib/server_requests/init_state.dart +++ b/app/lib/server_requests/init_state.dart @@ -21,9 +21,9 @@ Future<void> initialiseState(bool fetchSearchOptions) async { try { if (!internetConnection) { // Read data from files if no internet connection - selectedRelation = await loadSavedRelation(); + selectedRelation = await loadRelation(); - FetchResult fetchResult = await loadSavedData(); + FetchResult fetchResult = await loadMeasurements(); List<Measurement> measurements = fetchResult.measurements; selectedMarkerList = measurements; @@ -43,7 +43,7 @@ Future<void> initialiseState(bool fetchSearchOptions) async { if (serverConnection) { relationFuture = fetchRelation(); } else { // Read last saved data - relationFuture = loadSavedRelation(); + relationFuture = loadRelation(); } if (fetchSearchOptions) { diff --git a/app/lib/widgets/choropleth_map.dart b/app/lib/widgets/choropleth_map.dart index 770ce4ef..173f860e 100644 --- a/app/lib/widgets/choropleth_map.dart +++ b/app/lib/widgets/choropleth_map.dart @@ -27,14 +27,14 @@ class ChoroplethMap extends StatefulWidget { final Uint8List relation; final List<Measurement> measurements; - final void Function(int _selectedIndex) onSelectionChanged; // Callback function + final void Function(int _selectedIndex) onSelectionChanged; @override _ChoroplethMapState createState() => _ChoroplethMapState(); } class _ChoroplethMapState extends State<ChoroplethMap> { - int _selectedIndex = -1; + int _selectedIndex = -1; // Subdivision/map tile index Color _selectedColor = Colors.grey; // Initialise to gray late MapShapeSource _dataSource; late final MapZoomPanBehavior _zoomPanBehavior = MapZoomPanBehavior(); @@ -103,7 +103,7 @@ class _ChoroplethMapState extends State<ChoroplethMap> { strokeWidth: 1, // Shape selection selectedIndex: _selectedIndex, - onSelectionChanged: (int index) { // Shape selection behavior + onSelectionChanged: (int index) { setState(() { _selectedIndex = index; _selectedColor = _subdivisions[index].color; diff --git a/app/lib/widgets/main_layout.dart b/app/lib/widgets/main_layout.dart index 25e6f0bf..40f4a772 100644 --- a/app/lib/widgets/main_layout.dart +++ b/app/lib/widgets/main_layout.dart @@ -15,12 +15,12 @@ import '../utils/format_month.dart'; /// MapContainerWidget is the main widget that contains the map with all /// its layers, polygons and markers. class MapContainerWidget extends StatefulWidget { - final List<Measurement> markerList; + final List<Measurement> measurements; final Uint8List relation; final bool serverConnection; const MapContainerWidget({Key? key, - required this.markerList, + required this.measurements, required this.relation, required this.serverConnection, }) : super(key: key); @@ -33,11 +33,11 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { bool isMinimized = true; // Quick view box state tacker - bool satLayer = false; // Satellite layer visibility tracker - bool OSMlayer = false; + bool satLayer = false; // Satellite layer visibility state + bool osmLayer = false; // OSM layer visibility state - bool isSatTapped = false; // Button tap state tracker, satellite - bool isMapTapped = false; // Button tap state tracker, OSmap + bool isSatTapped = false; // Satellite button tap state tracker + bool isMapTapped = false; // OSM button tap state tracker // Initialise lastUpdate variable from persistent storage if server fetch fails Future<void> checkAndSetLastUpdate() async { @@ -58,11 +58,10 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { void handleSelection(int index) { String indexString = index.toString(); setState(() { - // NB should be optimised - for (Measurement measurement in widget.markerList) { + for (Measurement measurement in widget.measurements) { for (SubDiv subdivision in measurement.subDivs) { if (subdivision.sub_div_id == indexString) { - selectedTile= widget.markerList[index]; + selectedSubDiv= widget.measurements[index]; break; } } @@ -73,7 +72,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { @override Widget build(BuildContext context) { // Initialise selectedMarker to first element in markerList - selectedTile ??= widget.markerList[0]; + selectedSubDiv ??= widget.measurements[0]; checkAndSetLastUpdate(); @@ -105,7 +104,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { padding: const EdgeInsets.all(15.0), // Padding around map child: ChoroplethMap( relation: widget.relation, - measurements: widget.markerList, + measurements: widget.measurements, onSelectionChanged: handleSelection,), ), ), @@ -113,8 +112,8 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { width: screenWidth * boxWidth, height: screenWidth * boxHeight, child: Visibility( - visible: OSMlayer, - child: OSM(markerList: widget.markerList), + visible: osmLayer, + child: OSM(markerList: widget.measurements), ), ), Positioned( // Satellite button @@ -145,12 +144,12 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { child: GestureDetector( onTap: () { setState(() { - OSMlayer = !OSMlayer; // Toggle satellite layer state on press + osmLayer = !osmLayer; // Toggle satellite layer state on press }); }, child: Container( padding: const EdgeInsets.all(8), - decoration: OSMlayer ? const BoxDecoration( // Add decoration only when pressed + decoration: osmLayer ? const BoxDecoration( // Add decoration only when pressed shape: BoxShape.circle, color: Colors.grey, ) : null, @@ -238,16 +237,16 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { style: subHeadingStyle, ), Text( - 'Date: ${(selectedTile?.timeMeasured.day ?? '-')}/${(selectedTile?.timeMeasured.month ?? '-')}/${(selectedTile?.timeMeasured.year ?? '-')}', + 'Date: ${(selectedSubDiv?.timeMeasured.day ?? '-')}/${(selectedSubDiv?.timeMeasured.month ?? '-')}/${(selectedSubDiv?.timeMeasured.year ?? '-')}', style: regTextStyle, ), Text( - 'Time: ${selectedTile?.timeMeasured.hour}:00', + 'Time: ${selectedSubDiv?.timeMeasured.hour}:00', style: regTextStyle, ), const SizedBox(height: contPadding), Text( - 'Measuring point: (${selectedTile?.measurementID}, ${selectedTile?.measurementID})', + 'Measuring point: (${selectedSubDiv?.measurementID}, ${selectedSubDiv?.measurementID})', style: regTextStyle, ), ], -- GitLab