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 '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<ActiveFiresPage> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
String lastCheck;
int activeFires = 0;
_ActiveFiresPageState();
@ -42,20 +47,20 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
},
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<ActiveFiresPage> {
}
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<ActiveFiresPage> {
@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<ActiveFiresPage> {
_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
? _buildSavedLocations()
: 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 firesApiKey;
String firesApiUrl;
String appName = 'All Against The Fire!';
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
final List<BasicLocation> yourLocations = [];

View file

@ -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<LeafletMap> {
@ -21,7 +30,12 @@ class _LeafletMapState extends State<LeafletMap> {
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<LeafletMap> {
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: <Widget>[
new Text('10 fires near you'),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
bottomNavigationBar: new CustomBottomAppBar(
fabLocation: FloatingActionButtonLocation.centerFloat,
showNotch: false,
color: fires100,
actions: listWithoutNulls(<Widget>[
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<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() {
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);