import 'dart:async'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import '../consts.dart'; import 'loading_page.dart'; import '../widgets/main_layout.dart'; import '../data_classes.dart'; import '../server_requests/fetch_markers.dart'; import '../server_requests/fetch_relation.dart'; class DefaultPage extends StatefulWidget { const DefaultPage({Key? key}) : super(key: key); @override _DefaultPageState createState() => _DefaultPageState(); } class _DefaultPageState extends State<DefaultPage> { late Timer _timer; bool showBar = false; bool serverConnection = true; bool dialogShown = false; late Future<List<Measurement>> markerListFuture; late Future<Uint8List> relationFuture; @override void initState() { super.initState(); // Try to fetch measurement data from server markerListFuture = fetchMeasurements().then((fetchResult) { List<Measurement> measurements = fetchResult.measurements; serverConnection = fetchResult.connected; // Return the measurements return measurements; }).catchError((error) { serverConnection = false; throw Exception("Failed to fetch measurements: $error"); }); // Attempt to fetch relation from server if app establishes connection if (serverConnection){ relationFuture = fetchRelation(); } else { // Read last saved data relationFuture = loadSavedRelation(); } // Schedule fetchMarkerData to run periodically based on fetchInterval const Duration interval = Duration(minutes: fetchInterval); // NB fetchInterval value to be determined _timer = Timer.periodic(interval, (timer) { fetchMeasurements(); }); } @override void dispose() { _timer.cancel(); super.dispose(); } /// Display message to user void showConnectionMessage() { showDialog( context: context, builder: (context) => AlertDialog( actions: [ TextButton( onPressed: () { Navigator.of(context).pop(); }, child: const Text("Ok"), ) ], title: const Center( child: Text("No server connection") ), contentPadding: const EdgeInsets.all(10.0), content: const Text( "The app may display outdated information. Use with caution!", textAlign: TextAlign.center, // Align text center ), ), ); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( backgroundColor: Colors.black87, leading: IconButton( icon: const Icon( Icons.menu, color: Colors.white54 ), onPressed: () { // Not implemented }, ), title: Text( 'Mjøsa', style: regTextStyleBig, ), actions: [ IconButton( icon: const Icon( Icons.search, color: Colors.white54 ), onPressed: () { setState(() { showBar = !showBar; }); }, ), ], ), body: FutureBuilder( future: Future.wait([markerListFuture, relationFuture]), builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) { List<Measurement> markerList = snapshot.data![0] as List<Measurement>; Uint8List relation = snapshot.data![1] as Uint8List; return Container( // Return container with list view and background color color: const Color(0xff151515), child: ListView( children: [ MapContainerWidget( markerList: markerList, relation: relation, serverConnection: serverConnection, ), ], ), ); } ), ), ); } }