Skip to content
Snippets Groups Projects
Commit 32c60e92 authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

add: lastUpdate to persistent storage

parent 461d68e3
No related branches found
No related tags found
2 merge requests!5Clhp map,!4Clhp map
......@@ -10,7 +10,7 @@ const int fetchInterval = 60; // Fetch marker data every n minutes
// Map variables
LatLng mapCenter = LatLng(60.7666, 10.8471);
DateTime lastUpdate = null!; // Last time marker data was fetched from server
DateTime ?lastUpdate; // Last time marker data was fetched from server
// Font variables
const textColor = Colors.white;
......
......@@ -4,6 +4,7 @@ import 'dart:io';
import '../consts.dart';
import 'marker_data.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
// fetchMarkerTemplate requests all marker data from the server
Future<List<Measurement>> fetchMarkerData() async {
......@@ -28,15 +29,17 @@ Future<List<Measurement>> fetchMarkerData() async {
// contains correctly formatted data
if (jsonData != null && jsonData is List) {
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
String filePath = '${appDocumentsDirectory.path}/last_update.txt';
String filePath = '${appDocumentsDirectory.path}/last_data.json';
try { // Write most recent time of update to file
await File(filePath).writeAsString('${DateTime.now()}', mode: FileMode.write);
print('Update time written to file');
await File(filePath).writeAsString(responseBody, mode: FileMode.write);
print('Lake data written to file');
} catch (error) { print('Error in writing to file: $error');}
// Update lastUpdate variable
// Update local and persistent lastUpdate variable
lastUpdate = DateTime.now();
final prefs = await SharedPreferences.getInstance();
await prefs.setString('lastUpdate', '${DateTime.now()}');
return jsonData.map((data) => Measurement.fromJson(data)).toList();
} else {
......
......@@ -8,6 +8,7 @@ import 'package:flutter_map/flutter_map.dart';
import 'cloropleth_map.dart';
import 'dart:typed_data';
import 'package:google_fonts/google_fonts.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// MapContainerWidget is the main widget that contains the map with all
/// its layers, polygons and markers.
......@@ -37,11 +38,28 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
_mapController.move(mapCenter, 9.0);
}
// Initialise lastUpdate variable from persistent storage if server fetch fails
Future<void> checkAndSetLastUpdate() async {
if (lastUpdate == null) {
final prefs = await SharedPreferences.getInstance();
final updateString = prefs.getString('lastUpdate');
if (updateString != null && updateString.isNotEmpty) {
final updateData = DateTime.parse(updateString);
setState(() {
lastUpdate = updateData;
});
}
}
}
@override
Widget build(BuildContext context) {
// Initialise selectedMarker to first element in markerList
selectedMarker ??= widget.markerList[0];
checkAndSetLastUpdate();
const double contPadding = 30; // Container padding space
return LayoutBuilder(
......@@ -116,17 +134,17 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
children: [
const SizedBox(height: 5),
Text( // Display time of most recent server fetch
'Last updated at ${lastUpdate.day == DateTime.now().day
&& lastUpdate.month == DateTime.now().month
&& lastUpdate.year == DateTime.now().year ?
'${lastUpdate.hour}:${lastUpdate.minute}' :
'${lastUpdate.month}:${lastUpdate.day}'}',
'Last updated at ${lastUpdate != null ?
(lastUpdate?.day == DateTime.now().day &&
lastUpdate?.month == DateTime.now().month &&
lastUpdate?.year == DateTime.now().year ?
'${lastUpdate?.hour}:${lastUpdate?.minute}' :
'${lastUpdate?.day}-${lastUpdate?.month}-${lastUpdate?.year}') : ''}',
style: GoogleFonts.dmSans(
fontSize: 12,
fontSize: 13,
color: Colors.black,
),
),
],
),
),
......
......@@ -6,7 +6,9 @@ import FlutterMacOS
import Foundation
import path_provider_foundation
import shared_preferences_foundation
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}
......@@ -73,6 +73,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.0"
file:
dependency: transitive
description:
name: file
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
fl_chart:
dependency: "direct main"
description:
......@@ -107,6 +115,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
google_fonts:
dependency: "direct main"
description:
......@@ -347,6 +360,62 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.0.0"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
sha256: "81429e4481e1ccfb51ede496e916348668fd0921627779233bd24cc3ff6abd02"
url: "https://pub.dev"
source: hosted
version: "2.2.2"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: "8568a389334b6e83415b6aae55378e158fbc2314e074983362d20c562780fb06"
url: "https://pub.dev"
source: hosted
version: "2.2.1"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: "7708d83064f38060c7b39db12aefe449cb8cdc031d6062280087bc4cdb988f5c"
url: "https://pub.dev"
source: hosted
version: "2.3.5"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a"
url: "https://pub.dev"
source: hosted
version: "2.3.0"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
sky_engine:
dependency: transitive
description: flutter
......@@ -456,6 +525,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "13.0.0"
web:
dependency: transitive
description:
name: web
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
win32:
dependency: transitive
description:
......@@ -481,5 +558,5 @@ packages:
source: hosted
version: "1.0.4"
sdks:
dart: ">=3.2.0 <4.0.0"
flutter: ">=3.10.0"
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.19.0"
......@@ -17,6 +17,7 @@ dependencies:
google_fonts: any
syncfusion_flutter_maps: ^20.4.41
path_provider: ^2.0.8
shared_preferences: any
dev_dependencies:
flutter_test:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment