Compare commits
2 Commits
5df67920ea
...
9323921754
| Author | SHA1 | Date | |
|---|---|---|---|
| 9323921754 | |||
| 9e37ebbc22 |
@@ -4,6 +4,7 @@ import 'package:provider/provider.dart';
|
|||||||
import 'pages/flood_station_page.dart';
|
import 'pages/flood_station_page.dart';
|
||||||
import 'pages/landing_page.dart';
|
import 'pages/landing_page.dart';
|
||||||
import 'services/flood_station_provider.dart';
|
import 'services/flood_station_provider.dart';
|
||||||
|
import 'package:timezone/data/latest.dart' as tz;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(
|
runApp(
|
||||||
@@ -19,6 +20,7 @@ class MyApp extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
tz.initializeTimeZones();
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: 'Floodwatch',
|
title: 'Floodwatch',
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import 'package:timezone/timezone.dart' as tz;
|
||||||
|
|
||||||
import '../model/flood_station.dart';
|
import '../model/flood_station.dart';
|
||||||
import '../model/reading.dart';
|
import '../model/reading.dart';
|
||||||
@@ -8,7 +9,7 @@ class Api {
|
|||||||
static const String _rootUrl =
|
static const String _rootUrl =
|
||||||
'https://environment.data.gov.uk/flood-monitoring';
|
'https://environment.data.gov.uk/flood-monitoring';
|
||||||
|
|
||||||
static Future<List<FloodStation>> fetchStations() async {
|
static Future<List<FloodStation>> fetchAllStations() async {
|
||||||
List<FloodStation> stations = [];
|
List<FloodStation> stations = [];
|
||||||
final response = await http.get(Uri.parse('$_rootUrl/id/stations'));
|
final response = await http.get(Uri.parse('$_rootUrl/id/stations'));
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
@@ -20,12 +21,29 @@ class Api {
|
|||||||
return stations;
|
return stations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// [limit] limits the number of entries that are requested from the API and [offset] returns the
|
||||||
|
/// list starting from the specified number
|
||||||
|
|
||||||
|
static Future<List<FloodStation>> fetchStationsByRange(
|
||||||
|
int limit, int offset) async {
|
||||||
|
List<FloodStation> stations = [];
|
||||||
|
final response = await http
|
||||||
|
.get(Uri.parse('$_rootUrl/id/stations?_limit=$limit&_offset=$offset'));
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
final Map<String, dynamic> jsonStr = jsonDecode(response.body);
|
||||||
|
for (final str in jsonStr['items']) {
|
||||||
|
stations.add(FloodStation.fromMap(str));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stations;
|
||||||
|
}
|
||||||
|
|
||||||
static Future<List<Reading>> fetchReadingsFromStation(
|
static Future<List<Reading>> fetchReadingsFromStation(
|
||||||
String stationId) async {
|
String stationId) async {
|
||||||
List<Reading> readings = [];
|
List<Reading> readings = [];
|
||||||
final dateTime = DateTime.now().subtract(Duration(days: 1));
|
final dateTime = _getCurrentUKTime().subtract(Duration(days: 1)).toUtc();
|
||||||
final url =
|
final url =
|
||||||
'$_rootUrl/id/stations/$stationId/readings?since=${dateTime.toIso8601String()}Z&_sorted';
|
'$_rootUrl/id/stations/$stationId/readings?since=${dateTime.toIso8601String()}&_sorted';
|
||||||
final response = await http.get(Uri.parse(url));
|
final response = await http.get(Uri.parse(url));
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final Map<String, dynamic> jsonStr = jsonDecode(response.body);
|
final Map<String, dynamic> jsonStr = jsonDecode(response.body);
|
||||||
@@ -35,4 +53,9 @@ class Api {
|
|||||||
}
|
}
|
||||||
return readings.reversed.toList();
|
return readings.reversed.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static DateTime _getCurrentUKTime() {
|
||||||
|
final london = tz.getLocation('Europe/London');
|
||||||
|
return tz.TZDateTime.now(london);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,31 @@ import '../model/flood_station.dart';
|
|||||||
import 'api.dart';
|
import 'api.dart';
|
||||||
|
|
||||||
class FloodStationProvider extends ChangeNotifier {
|
class FloodStationProvider extends ChangeNotifier {
|
||||||
List<FloodStation> _allStations = [];
|
|
||||||
FloodStation? selectedStation;
|
FloodStation? selectedStation;
|
||||||
|
|
||||||
|
List<FloodStation> _allStations = [];
|
||||||
|
|
||||||
List<FloodStation> get allStations => _allStations;
|
List<FloodStation> get allStations => _allStations;
|
||||||
|
|
||||||
|
Future loadAllStationsInBatches({silent = false}) {
|
||||||
|
int offset = 0;
|
||||||
|
return Future.doWhile(() async {
|
||||||
|
final stations = await Api.fetchStationsByRange(500, offset);
|
||||||
|
if (stations.isNotEmpty) {
|
||||||
|
_allStations.addAll(stations);
|
||||||
|
if (!silent) {
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
offset += 500;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Future loadAllStations({silent = false}) {
|
Future loadAllStations({silent = false}) {
|
||||||
return Api.fetchStations().then(
|
return Api.fetchAllStations().then(
|
||||||
(value) {
|
(value) {
|
||||||
_allStations = value;
|
_allStations = value;
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ dependencies:
|
|||||||
http: ^1.3.0
|
http: ^1.3.0
|
||||||
intl: ^0.20.2
|
intl: ^0.20.2
|
||||||
provider: ^6.1.2
|
provider: ^6.1.2
|
||||||
|
timezone: ^0.10.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user