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 ...@@ -10,7 +10,7 @@ const int fetchInterval = 60; // Fetch marker data every n minutes
// Map variables // Map variables
LatLng mapCenter = LatLng(60.7666, 10.8471); 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 // Font variables
const textColor = Colors.white; const textColor = Colors.white;
......
...@@ -4,6 +4,7 @@ import 'dart:io'; ...@@ -4,6 +4,7 @@ import 'dart:io';
import '../consts.dart'; import '../consts.dart';
import 'marker_data.dart'; import 'marker_data.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
// fetchMarkerTemplate requests all marker data from the server // fetchMarkerTemplate requests all marker data from the server
Future<List<Measurement>> fetchMarkerData() async { Future<List<Measurement>> fetchMarkerData() async {
...@@ -28,15 +29,17 @@ Future<List<Measurement>> fetchMarkerData() async { ...@@ -28,15 +29,17 @@ Future<List<Measurement>> fetchMarkerData() async {
// contains correctly formatted data // contains correctly formatted data
if (jsonData != null && jsonData is List) { if (jsonData != null && jsonData is List) {
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); 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 try { // Write most recent time of update to file
await File(filePath).writeAsString('${DateTime.now()}', mode: FileMode.write); await File(filePath).writeAsString(responseBody, mode: FileMode.write);
print('Update time written to file'); print('Lake data written to file');
} catch (error) { print('Error in writing to file: $error');} } catch (error) { print('Error in writing to file: $error');}
// Update lastUpdate variable // Update local and persistent lastUpdate variable
lastUpdate = DateTime.now(); lastUpdate = DateTime.now();
final prefs = await SharedPreferences.getInstance();
await prefs.setString('lastUpdate', '${DateTime.now()}');
return jsonData.map((data) => Measurement.fromJson(data)).toList(); return jsonData.map((data) => Measurement.fromJson(data)).toList();
} else { } else {
......
...@@ -8,6 +8,7 @@ import 'package:flutter_map/flutter_map.dart'; ...@@ -8,6 +8,7 @@ import 'package:flutter_map/flutter_map.dart';
import 'cloropleth_map.dart'; import 'cloropleth_map.dart';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:google_fonts/google_fonts.dart'; 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 /// MapContainerWidget is the main widget that contains the map with all
/// its layers, polygons and markers. /// its layers, polygons and markers.
...@@ -37,11 +38,28 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -37,11 +38,28 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
_mapController.move(mapCenter, 9.0); _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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// Initialise selectedMarker to first element in markerList // Initialise selectedMarker to first element in markerList
selectedMarker ??= widget.markerList[0]; selectedMarker ??= widget.markerList[0];
checkAndSetLastUpdate();
const double contPadding = 30; // Container padding space const double contPadding = 30; // Container padding space
return LayoutBuilder( return LayoutBuilder(
...@@ -116,17 +134,17 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -116,17 +134,17 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
children: [ children: [
const SizedBox(height: 5), const SizedBox(height: 5),
Text( // Display time of most recent server fetch Text( // Display time of most recent server fetch
'Last updated at ${lastUpdate.day == DateTime.now().day 'Last updated at ${lastUpdate != null ?
&& lastUpdate.month == DateTime.now().month (lastUpdate?.day == DateTime.now().day &&
&& lastUpdate.year == DateTime.now().year ? lastUpdate?.month == DateTime.now().month &&
'${lastUpdate.hour}:${lastUpdate.minute}' : lastUpdate?.year == DateTime.now().year ?
'${lastUpdate.month}:${lastUpdate.day}'}', '${lastUpdate?.hour}:${lastUpdate?.minute}' :
'${lastUpdate?.day}-${lastUpdate?.month}-${lastUpdate?.year}') : ''}',
style: GoogleFonts.dmSans( style: GoogleFonts.dmSans(
fontSize: 12, fontSize: 13,
color: Colors.black, color: Colors.black,
), ),
), ),
], ],
), ),
), ),
......
...@@ -6,7 +6,9 @@ import FlutterMacOS ...@@ -6,7 +6,9 @@ import FlutterMacOS
import Foundation import Foundation
import path_provider_foundation import path_provider_foundation
import shared_preferences_foundation
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
} }
...@@ -73,6 +73,14 @@ packages: ...@@ -73,6 +73,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.0" version: "2.1.0"
file:
dependency: transitive
description:
name: file
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
fl_chart: fl_chart:
dependency: "direct main" dependency: "direct main"
description: description:
...@@ -107,6 +115,11 @@ packages: ...@@ -107,6 +115,11 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
google_fonts: google_fonts:
dependency: "direct main" dependency: "direct main"
description: description:
...@@ -347,6 +360,62 @@ packages: ...@@ -347,6 +360,62 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "5.0.0" 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: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
...@@ -456,6 +525,14 @@ packages: ...@@ -456,6 +525,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "13.0.0" version: "13.0.0"
web:
dependency: transitive
description:
name: web
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
win32: win32:
dependency: transitive dependency: transitive
description: description:
...@@ -481,5 +558,5 @@ packages: ...@@ -481,5 +558,5 @@ packages:
source: hosted source: hosted
version: "1.0.4" version: "1.0.4"
sdks: sdks:
dart: ">=3.2.0 <4.0.0" dart: ">=3.3.0 <4.0.0"
flutter: ">=3.10.0" flutter: ">=3.19.0"
...@@ -17,6 +17,7 @@ dependencies: ...@@ -17,6 +17,7 @@ dependencies:
google_fonts: any google_fonts: any
syncfusion_flutter_maps: ^20.4.41 syncfusion_flutter_maps: ^20.4.41
path_provider: ^2.0.8 path_provider: ^2.0.8
shared_preferences: any
dev_dependencies: dev_dependencies:
flutter_test: 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