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

update: comments and variable names, app

parent 2b068946
No related branches found
No related tags found
1 merge request!12Clhp map
...@@ -13,7 +13,7 @@ const String mapEndpoint = "update_map"; ...@@ -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 String selectedLake = 'Mjøsa'; // Init to Mjøsa, NB should be initialised to last selected lake
Uint8List selectedRelation = Uint8List(0); Uint8List selectedRelation = Uint8List(0);
List<Measurement> selectedMarkerList = []; List<Measurement> selectedMarkerList = [];
Measurement? selectedTile; Measurement? selectedSubDiv;
LatLng mapCenter = LatLng(60.8000, 10.8471); LatLng mapCenter = LatLng(60.8000, 10.8471);
DateTime ?lastUpdate; // Last time data was fetched from server DateTime ?lastUpdate; // Last time data was fetched from server
......
...@@ -86,7 +86,7 @@ class _DefaultPageState extends State<DefaultPage> { ...@@ -86,7 +86,7 @@ class _DefaultPageState extends State<DefaultPage> {
child: ListView( child: ListView(
children: [ children: [
MapContainerWidget( MapContainerWidget(
markerList: selectedMarkerList, measurements: selectedMarkerList,
relation: selectedRelation, relation: selectedRelation,
serverConnection: serverConnection, serverConnection: serverConnection,
), ),
......
...@@ -55,14 +55,16 @@ Future<FetchResult> fetchMeasurements() async { ...@@ -55,14 +55,16 @@ Future<FetchResult> fetchMeasurements() async {
} }
} }
} }
return loadSavedData(); return loadMeasurements();
} catch (e) { } catch (e) {
return loadSavedData(); print("Error in fetching measurements from server: $e");
return loadMeasurements();
} }
} }
Future<FetchResult> loadSavedData() async { Future<FetchResult> loadMeasurements() async {
try { try {
print("Loading measurements from file");
// Get latest saved data from file if the server does not respond // Get latest saved data from file if the server does not respond
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
String filePath = '${appDocumentsDirectory.path}/last_data.json'; String filePath = '${appDocumentsDirectory.path}/last_data.json';
......
...@@ -37,15 +37,17 @@ Future<Uint8List> fetchRelation() async { ...@@ -37,15 +37,17 @@ Future<Uint8List> fetchRelation() async {
return Uint8List.fromList(utf8.encode(responseBody)); return Uint8List.fromList(utf8.encode(responseBody));
} }
} }
return loadSavedRelation(); return loadRelation();
} catch (e) { } catch (e) {
return loadSavedRelation(); print("Error in fetching relation from server: $e");
return loadRelation();
} }
} }
/// Load last saved relation data form last_relation.json /// Load last saved relation data form last_relation.json
Future<Uint8List> loadSavedRelation() async { Future<Uint8List> loadRelation() async {
try { try {
print("Loading relation from file");
// Get latest saved relation from file if the server does not respond // Get latest saved relation from file if the server does not respond
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
String filePath = '${appDocumentsDirectory.path}/last_relation.json'; String filePath = '${appDocumentsDirectory.path}/last_relation.json';
......
...@@ -21,9 +21,9 @@ Future<void> initialiseState(bool fetchSearchOptions) async { ...@@ -21,9 +21,9 @@ Future<void> initialiseState(bool fetchSearchOptions) async {
try { try {
if (!internetConnection) { // Read data from files if no internet connection 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; List<Measurement> measurements = fetchResult.measurements;
selectedMarkerList = measurements; selectedMarkerList = measurements;
...@@ -43,7 +43,7 @@ Future<void> initialiseState(bool fetchSearchOptions) async { ...@@ -43,7 +43,7 @@ Future<void> initialiseState(bool fetchSearchOptions) async {
if (serverConnection) { if (serverConnection) {
relationFuture = fetchRelation(); relationFuture = fetchRelation();
} else { // Read last saved data } else { // Read last saved data
relationFuture = loadSavedRelation(); relationFuture = loadRelation();
} }
if (fetchSearchOptions) { if (fetchSearchOptions) {
......
...@@ -27,14 +27,14 @@ class ChoroplethMap extends StatefulWidget { ...@@ -27,14 +27,14 @@ class ChoroplethMap extends StatefulWidget {
final Uint8List relation; final Uint8List relation;
final List<Measurement> measurements; final List<Measurement> measurements;
final void Function(int _selectedIndex) onSelectionChanged; // Callback function final void Function(int _selectedIndex) onSelectionChanged;
@override @override
_ChoroplethMapState createState() => _ChoroplethMapState(); _ChoroplethMapState createState() => _ChoroplethMapState();
} }
class _ChoroplethMapState extends State<ChoroplethMap> { class _ChoroplethMapState extends State<ChoroplethMap> {
int _selectedIndex = -1; int _selectedIndex = -1; // Subdivision/map tile index
Color _selectedColor = Colors.grey; // Initialise to gray Color _selectedColor = Colors.grey; // Initialise to gray
late MapShapeSource _dataSource; late MapShapeSource _dataSource;
late final MapZoomPanBehavior _zoomPanBehavior = MapZoomPanBehavior(); late final MapZoomPanBehavior _zoomPanBehavior = MapZoomPanBehavior();
...@@ -103,7 +103,7 @@ class _ChoroplethMapState extends State<ChoroplethMap> { ...@@ -103,7 +103,7 @@ class _ChoroplethMapState extends State<ChoroplethMap> {
strokeWidth: 1, strokeWidth: 1,
// Shape selection // Shape selection
selectedIndex: _selectedIndex, selectedIndex: _selectedIndex,
onSelectionChanged: (int index) { // Shape selection behavior onSelectionChanged: (int index) {
setState(() { setState(() {
_selectedIndex = index; _selectedIndex = index;
_selectedColor = _subdivisions[index].color; _selectedColor = _subdivisions[index].color;
......
...@@ -15,12 +15,12 @@ import '../utils/format_month.dart'; ...@@ -15,12 +15,12 @@ import '../utils/format_month.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.
class MapContainerWidget extends StatefulWidget { class MapContainerWidget extends StatefulWidget {
final List<Measurement> markerList; final List<Measurement> measurements;
final Uint8List relation; final Uint8List relation;
final bool serverConnection; final bool serverConnection;
const MapContainerWidget({Key? key, const MapContainerWidget({Key? key,
required this.markerList, required this.measurements,
required this.relation, required this.relation,
required this.serverConnection, required this.serverConnection,
}) : super(key: key); }) : super(key: key);
...@@ -33,11 +33,11 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -33,11 +33,11 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
bool isMinimized = true; // Quick view box state tacker bool isMinimized = true; // Quick view box state tacker
bool satLayer = false; // Satellite layer visibility tracker bool satLayer = false; // Satellite layer visibility state
bool OSMlayer = false; bool osmLayer = false; // OSM layer visibility state
bool isSatTapped = false; // Button tap state tracker, satellite bool isSatTapped = false; // Satellite button tap state tracker
bool isMapTapped = false; // Button tap state tracker, OSmap bool isMapTapped = false; // OSM button tap state tracker
// Initialise lastUpdate variable from persistent storage if server fetch fails // Initialise lastUpdate variable from persistent storage if server fetch fails
Future<void> checkAndSetLastUpdate() async { Future<void> checkAndSetLastUpdate() async {
...@@ -58,11 +58,10 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -58,11 +58,10 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
void handleSelection(int index) { void handleSelection(int index) {
String indexString = index.toString(); String indexString = index.toString();
setState(() { setState(() {
// NB should be optimised for (Measurement measurement in widget.measurements) {
for (Measurement measurement in widget.markerList) {
for (SubDiv subdivision in measurement.subDivs) { for (SubDiv subdivision in measurement.subDivs) {
if (subdivision.sub_div_id == indexString) { if (subdivision.sub_div_id == indexString) {
selectedTile= widget.markerList[index]; selectedSubDiv= widget.measurements[index];
break; break;
} }
} }
...@@ -73,7 +72,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -73,7 +72,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// Initialise selectedMarker to first element in markerList // Initialise selectedMarker to first element in markerList
selectedTile ??= widget.markerList[0]; selectedSubDiv ??= widget.measurements[0];
checkAndSetLastUpdate(); checkAndSetLastUpdate();
...@@ -105,7 +104,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -105,7 +104,7 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
padding: const EdgeInsets.all(15.0), // Padding around map padding: const EdgeInsets.all(15.0), // Padding around map
child: ChoroplethMap( child: ChoroplethMap(
relation: widget.relation, relation: widget.relation,
measurements: widget.markerList, measurements: widget.measurements,
onSelectionChanged: handleSelection,), onSelectionChanged: handleSelection,),
), ),
), ),
...@@ -113,8 +112,8 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -113,8 +112,8 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
width: screenWidth * boxWidth, width: screenWidth * boxWidth,
height: screenWidth * boxHeight, height: screenWidth * boxHeight,
child: Visibility( child: Visibility(
visible: OSMlayer, visible: osmLayer,
child: OSM(markerList: widget.markerList), child: OSM(markerList: widget.measurements),
), ),
), ),
Positioned( // Satellite button Positioned( // Satellite button
...@@ -145,12 +144,12 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -145,12 +144,12 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
child: GestureDetector( child: GestureDetector(
onTap: () { onTap: () {
setState(() { setState(() {
OSMlayer = !OSMlayer; // Toggle satellite layer state on press osmLayer = !osmLayer; // Toggle satellite layer state on press
}); });
}, },
child: Container( child: Container(
padding: const EdgeInsets.all(8), 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, shape: BoxShape.circle,
color: Colors.grey, color: Colors.grey,
) : null, ) : null,
...@@ -238,16 +237,16 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -238,16 +237,16 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
style: subHeadingStyle, style: subHeadingStyle,
), ),
Text( Text(
'Date: ${(selectedTile?.timeMeasured.day ?? '-')}/${(selectedTile?.timeMeasured.month ?? '-')}/${(selectedTile?.timeMeasured.year ?? '-')}', 'Date: ${(selectedSubDiv?.timeMeasured.day ?? '-')}/${(selectedSubDiv?.timeMeasured.month ?? '-')}/${(selectedSubDiv?.timeMeasured.year ?? '-')}',
style: regTextStyle, style: regTextStyle,
), ),
Text( Text(
'Time: ${selectedTile?.timeMeasured.hour}:00', 'Time: ${selectedSubDiv?.timeMeasured.hour}:00',
style: regTextStyle, style: regTextStyle,
), ),
const SizedBox(height: contPadding), const SizedBox(height: contPadding),
Text( Text(
'Measuring point: (${selectedTile?.measurementID}, ${selectedTile?.measurementID})', 'Measuring point: (${selectedSubDiv?.measurementID}, ${selectedSubDiv?.measurementID})',
style: regTextStyle, style: regTextStyle,
), ),
], ],
......
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