diff --git a/lib/activeFires.dart b/lib/activeFires.dart index 3089553..6fd597f 100644 --- a/lib/activeFires.dart +++ b/lib/activeFires.dart @@ -6,11 +6,14 @@ import 'package:comunes_flutter/comunes_flutter.dart'; import 'colors.dart'; import 'placesAutocompleteUtils.dart'; import 'basicLocation.dart'; -import 'package:collection/collection.dart' show lowerBound; import 'locationUtils.dart'; import 'globals.dart' as globals; import 'basicLocationPersist.dart'; import 'package:http/http.dart' as http; +import 'dart:convert' show json; +import 'customBottomAppBar.dart'; +import 'package:simple_moment/simple_moment.dart'; +import 'dart:convert'; class ActiveFiresPage extends StatefulWidget { static const String routeName = '/fires'; @@ -23,6 +26,8 @@ class ActiveFiresPage extends StatefulWidget { class _ActiveFiresPageState extends State { final GlobalKey _scaffoldKey = new GlobalKey(); + String lastCheck; + int activeFires = 0; _ActiveFiresPageState(); @@ -42,20 +47,20 @@ class _ActiveFiresPageState extends State { }, onTap: () { const String km = '100'; - var url = 'https://fires.comunes.org/api/v1/fires-in/${globals.firesApiKey}/${loc.lat}/${loc + var url = '${globals.firesApiUrl}fires-in/${globals + .firesApiKey}/${loc.lat}/${loc .lon}/$km'; - /* http.post(url, body: {"name": "doodle", "color": "blue"}) - .then((response) { - print("Response status: ${response.statusCode}"); - print("Response body: ${response.body}"); - }); */ - // print(url); - http.read(url).then(print); - Navigator.push( - context, - new MaterialPageRoute( - builder: (context) => - new LeafletMap(title: desc, location: loc))); + http.read(url).then((result) { + int numFires = json.decode(result)['real']; + Navigator.push( + context, + new MaterialPageRoute( + builder: (context) => new LeafletMap( + title: desc, + location: loc, + numFires: numFires, + kmAround: km))); + }); }); } @@ -83,10 +88,8 @@ class _ActiveFiresPageState extends State { } void handleUndo(BasicLocation item) { - print('Undo $item'); - final int insertionIndex = lowerBound(globals.yourLocations, item); setState(() { - globals.yourLocations.insert(insertionIndex, item); + globals.yourLocations.add(item); persist(); }); } @@ -137,11 +140,30 @@ class _ActiveFiresPageState extends State { @override void initState() { super.initState(); + http.read('${globals.firesApiUrl}status/last-fire-check').then((result) { + try { + var now = Moment.now(); + var last = DateTime.parse(json.decode(result)['value']); + setState(() { + lastCheck = now.from(last); + }); + } catch (e) { + print('Cannot get the last fire check'); + } + }); + http.read('${globals.firesApiUrl}status/active-fires-count').then((result) { + try { + int count = json.decode(result)['total']; + setState(() {activeFires = count;}); + } catch (e) { + print('Cannot get the last fire stats'); + } + }); } @override Widget build(BuildContext context) { - // print('Building Active Fires, saved $length'); + print('Building Active Fires'); final title = 'Your locations'; return Scaffold( key: _scaffoldKey, @@ -155,17 +177,17 @@ class _ActiveFiresPageState extends State { _scaffoldKey.currentState.openDrawer(); }, ), - /* actions: [ - new IconButton( - icon: Icon(Icons.location_searching), onPressed: () {}), - IconButton( - icon: Icon(Icons.more_vert), - onPressed: () { - print('More button'); - }, - ), - ], */ ), + bottomNavigationBar: new CustomBottomAppBar( + fabLocation: FloatingActionButtonLocation.centerDocked, + showNotch: true, + color: fires100, + actions: listWithoutNulls([ + activeFires > 0 && lastCheck != null ? + new Text('${activeFires} active fires worldwide. Updated ${lastCheck}'): + null, + SizedBox(width: 10.0) + ])), body: globals.yourLocations.length > 0 ? _buildSavedLocations() : new Center( diff --git a/lib/customBottomAppBar.dart b/lib/customBottomAppBar.dart new file mode 100644 index 0000000..5dfd661 --- /dev/null +++ b/lib/customBottomAppBar.dart @@ -0,0 +1,41 @@ +import 'package:flutter/material.dart'; + +class CustomBottomAppBar extends StatelessWidget { + const CustomBottomAppBar( + {this.fabLocation, + this.showNotch, + this.color = Colors.black45, + this.actions}); + + final Color color; + final FloatingActionButtonLocation fabLocation; + final bool showNotch; + final List actions; + + static final List kCenterLocations = + [ + FloatingActionButtonLocation.centerDocked, + FloatingActionButtonLocation.centerFloat, + ]; + + @override + Widget build(BuildContext context) { + final List rowContents = [ + new SizedBox(height:56.0) + ]; + + if (kCenterLocations.contains(fabLocation)) { + rowContents.add( + const Expanded(child: const SizedBox()), + ); + } + + rowContents.addAll(this.actions); + + return new BottomAppBar( + color: color, + hasNotch: showNotch, + child: new Row(children: rowContents), + ); + } +} \ No newline at end of file diff --git a/lib/globals.dart b/lib/globals.dart index 564a0e2..f234e36 100644 --- a/lib/globals.dart +++ b/lib/globals.dart @@ -6,6 +6,7 @@ import 'basicLocation.dart'; String gmapKey; String firesApiKey; +String firesApiUrl; String appName = 'All Against The Fire!'; Future prefs = SharedPreferences.getInstance(); final List yourLocations = []; diff --git a/lib/leafletMap.dart b/lib/leafletMap.dart index 84541be..a31c66f 100644 --- a/lib/leafletMap.dart +++ b/lib/leafletMap.dart @@ -3,17 +3,26 @@ import 'basicLocation.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_map/plugin_api.dart'; import 'package:latlong/latlong.dart'; +import 'package:comunes_flutter/comunes_flutter.dart'; import 'colors.dart'; -import 'mainDrawer.dart' - ''; +import 'mainDrawer.dart'; +import 'customBottomAppBar.dart'; +import 'dart:core'; + class LeafletMap extends StatefulWidget { final BasicLocation location; final String title; + final int numFires; + final String kmAround; - LeafletMap({@required this.title, @required this.location}); + LeafletMap({@required this.title, @required this.location, + @required this.numFires, @required this.kmAround + }); @override - _LeafletMapState createState() => _LeafletMapState(title: title, location: location); + _LeafletMapState createState() => _LeafletMapState(title: title, location: location, + numFires: numFires, kmAround: kmAround + ); } class _LeafletMapState extends State { @@ -21,7 +30,12 @@ class _LeafletMapState extends State { final BasicLocation location; final String title; - _LeafletMapState({@required this.title, @required this.location}); + final int numFires; + final String kmAround; + + _LeafletMapState({@required this.title, @required this.location, + @required this.numFires, @required this.kmAround + }); @override Widget build(BuildContext context) { return new Scaffold( @@ -37,17 +51,17 @@ class _LeafletMapState extends State { child: const Icon(Icons.notifications_none), backgroundColor: Colors.orange, ), - floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, - bottomNavigationBar: new MapBottomAppBar( - menuCallback: () { - // _scaffoldKey.currentState.openDrawer(); - }, - fabLocation: FloatingActionButtonLocation.centerDocked, - showNotch: true, - bottomActions: [ - new Text('10 fires near you'), + floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, + bottomNavigationBar: new CustomBottomAppBar( + fabLocation: FloatingActionButtonLocation.centerFloat, + showNotch: false, + color: fires100, + actions: listWithoutNulls([ + numFires > 0 ? + new Text('${numFires.toString()} fires at $kmAround км around this area'): + new Text('There is no fires at $kmAround км around this area'), SizedBox(width: 10.0) - ]), + ])), body: new FlutterMap( options: new MapOptions( center: new LatLng(this.location.lat, this.location.lon), @@ -92,53 +106,3 @@ class _LeafletMapState extends State { )); } } - -class MapBottomAppBar extends StatelessWidget { - const MapBottomAppBar( - {this.menuCallback, - this.fabLocation, - this.showNotch, - this.bottomActions}); - - final Color color = fires600; - final FloatingActionButtonLocation fabLocation; - final bool showNotch; - final VoidCallback menuCallback; - final List bottomActions; - - static final List kCenterLocations = - [ - FloatingActionButtonLocation.centerDocked, - FloatingActionButtonLocation.centerFloat, - ]; - - @override - Widget build(BuildContext context) { - final List rowContents = [ - /* new IconButton( - icon: const Icon(Icons.menu), - onPressed: () { - menuCallback(); - /* showModalBottomSheet( - context: context, - builder: (BuildContext context) => const _DemoDrawer(), - ); */ - }, - ), */ - ]; - - if (kCenterLocations.contains(fabLocation)) { - rowContents.add( - const Expanded(child: const SizedBox()), - ); - } - - rowContents.addAll(this.bottomActions); - - return new BottomAppBar( - color: color, - hasNotch: showNotch, - child: new Row(children: rowContents), - ); - } -} diff --git a/lib/main.dart b/lib/main.dart index 9dd4715..618db07 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -12,8 +12,9 @@ Future> loadSecrets() async { void main() { loadSecrets().then((secrets) { - globals.firesApiKey = secrets['firesApiKey']; globals.gmapKey = secrets['gmapKey']; + globals.firesApiKey = secrets['firesApiKey']; + globals.firesApiUrl = secrets['firesApiUrl']; globals.prefs.then((prefs) { loadYourLocations(prefs);