More rest calls

This commit is contained in:
vjrj 2018-06-10 18:02:50 +02:00
parent b5b8b3da51
commit 5bea196253
5 changed files with 123 additions and 94 deletions

View file

@ -6,11 +6,14 @@ import 'package:comunes_flutter/comunes_flutter.dart';
import 'colors.dart'; import 'colors.dart';
import 'placesAutocompleteUtils.dart'; import 'placesAutocompleteUtils.dart';
import 'basicLocation.dart'; import 'basicLocation.dart';
import 'package:collection/collection.dart' show lowerBound;
import 'locationUtils.dart'; import 'locationUtils.dart';
import 'globals.dart' as globals; import 'globals.dart' as globals;
import 'basicLocationPersist.dart'; import 'basicLocationPersist.dart';
import 'package:http/http.dart' as http; 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 { class ActiveFiresPage extends StatefulWidget {
static const String routeName = '/fires'; static const String routeName = '/fires';
@ -23,6 +26,8 @@ class ActiveFiresPage extends StatefulWidget {
class _ActiveFiresPageState extends State<ActiveFiresPage> { class _ActiveFiresPageState extends State<ActiveFiresPage> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
String lastCheck;
int activeFires = 0;
_ActiveFiresPageState(); _ActiveFiresPageState();
@ -42,20 +47,20 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
}, },
onTap: () { onTap: () {
const String km = '100'; 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'; .lon}/$km';
/* http.post(url, body: {"name": "doodle", "color": "blue"}) http.read(url).then((result) {
.then((response) { int numFires = json.decode(result)['real'];
print("Response status: ${response.statusCode}"); Navigator.push(
print("Response body: ${response.body}"); context,
}); */ new MaterialPageRoute(
// print(url); builder: (context) => new LeafletMap(
http.read(url).then(print); title: desc,
Navigator.push( location: loc,
context, numFires: numFires,
new MaterialPageRoute( kmAround: km)));
builder: (context) => });
new LeafletMap(title: desc, location: loc)));
}); });
} }
@ -83,10 +88,8 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
} }
void handleUndo(BasicLocation item) { void handleUndo(BasicLocation item) {
print('Undo $item');
final int insertionIndex = lowerBound(globals.yourLocations, item);
setState(() { setState(() {
globals.yourLocations.insert(insertionIndex, item); globals.yourLocations.add(item);
persist(); persist();
}); });
} }
@ -137,11 +140,30 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
@override @override
void initState() { void initState() {
super.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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// print('Building Active Fires, saved $length'); print('Building Active Fires');
final title = 'Your locations'; final title = 'Your locations';
return Scaffold( return Scaffold(
key: _scaffoldKey, key: _scaffoldKey,
@ -155,17 +177,17 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
_scaffoldKey.currentState.openDrawer(); _scaffoldKey.currentState.openDrawer();
}, },
), ),
/* actions: <Widget>[
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(<Widget>[
activeFires > 0 && lastCheck != null ?
new Text('${activeFires} active fires worldwide. Updated ${lastCheck}'):
null,
SizedBox(width: 10.0)
])),
body: globals.yourLocations.length > 0 body: globals.yourLocations.length > 0
? _buildSavedLocations() ? _buildSavedLocations()
: new Center( : new Center(

View file

@ -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<Widget> actions;
static final List<FloatingActionButtonLocation> kCenterLocations =
<FloatingActionButtonLocation>[
FloatingActionButtonLocation.centerDocked,
FloatingActionButtonLocation.centerFloat,
];
@override
Widget build(BuildContext context) {
final List<Widget> rowContents = <Widget>[
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),
);
}
}

View file

@ -6,6 +6,7 @@ import 'basicLocation.dart';
String gmapKey; String gmapKey;
String firesApiKey; String firesApiKey;
String firesApiUrl;
String appName = 'All Against The Fire!'; String appName = 'All Against The Fire!';
Future<SharedPreferences> prefs = SharedPreferences.getInstance(); Future<SharedPreferences> prefs = SharedPreferences.getInstance();
final List<BasicLocation> yourLocations = []; final List<BasicLocation> yourLocations = [];

View file

@ -3,17 +3,26 @@ import 'basicLocation.dart';
import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/plugin_api.dart'; import 'package:flutter_map/plugin_api.dart';
import 'package:latlong/latlong.dart'; import 'package:latlong/latlong.dart';
import 'package:comunes_flutter/comunes_flutter.dart';
import 'colors.dart'; import 'colors.dart';
import 'mainDrawer.dart' import 'mainDrawer.dart';
''; import 'customBottomAppBar.dart';
import 'dart:core';
class LeafletMap extends StatefulWidget { class LeafletMap extends StatefulWidget {
final BasicLocation location; final BasicLocation location;
final String title; 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 @override
_LeafletMapState createState() => _LeafletMapState(title: title, location: location); _LeafletMapState createState() => _LeafletMapState(title: title, location: location,
numFires: numFires, kmAround: kmAround
);
} }
class _LeafletMapState extends State<LeafletMap> { class _LeafletMapState extends State<LeafletMap> {
@ -21,7 +30,12 @@ class _LeafletMapState extends State<LeafletMap> {
final BasicLocation location; final BasicLocation location;
final String title; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new Scaffold( return new Scaffold(
@ -37,17 +51,17 @@ class _LeafletMapState extends State<LeafletMap> {
child: const Icon(Icons.notifications_none), child: const Icon(Icons.notifications_none),
backgroundColor: Colors.orange, backgroundColor: Colors.orange,
), ),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
bottomNavigationBar: new MapBottomAppBar( bottomNavigationBar: new CustomBottomAppBar(
menuCallback: () { fabLocation: FloatingActionButtonLocation.centerFloat,
// _scaffoldKey.currentState.openDrawer(); showNotch: false,
}, color: fires100,
fabLocation: FloatingActionButtonLocation.centerDocked, actions: listWithoutNulls(<Widget>[
showNotch: true, numFires > 0 ?
bottomActions: <Widget>[ new Text('${numFires.toString()} fires at $kmAround км around this area'):
new Text('10 fires near you'), new Text('There is no fires at $kmAround км around this area'),
SizedBox(width: 10.0) SizedBox(width: 10.0)
]), ])),
body: new FlutterMap( body: new FlutterMap(
options: new MapOptions( options: new MapOptions(
center: new LatLng(this.location.lat, this.location.lon), center: new LatLng(this.location.lat, this.location.lon),
@ -92,53 +106,3 @@ class _LeafletMapState extends State<LeafletMap> {
)); ));
} }
} }
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<Widget> bottomActions;
static final List<FloatingActionButtonLocation> kCenterLocations =
<FloatingActionButtonLocation>[
FloatingActionButtonLocation.centerDocked,
FloatingActionButtonLocation.centerFloat,
];
@override
Widget build(BuildContext context) {
final List<Widget> rowContents = <Widget>[
/* new IconButton(
icon: const Icon(Icons.menu),
onPressed: () {
menuCallback();
/* showModalBottomSheet<Null>(
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),
);
}
}

View file

@ -12,8 +12,9 @@ Future<Map<String, dynamic>> loadSecrets() async {
void main() { void main() {
loadSecrets().then((secrets) { loadSecrets().then((secrets) {
globals.firesApiKey = secrets['firesApiKey'];
globals.gmapKey = secrets['gmapKey']; globals.gmapKey = secrets['gmapKey'];
globals.firesApiKey = secrets['firesApiKey'];
globals.firesApiUrl = secrets['firesApiUrl'];
globals.prefs.then((prefs) { globals.prefs.then((prefs) {
loadYourLocations(prefs); loadYourLocations(prefs);