Last changes not published

This commit is contained in:
vjrj 2026-03-05 01:18:27 +01:00
parent ac65ccf990
commit 21ec08303a
43 changed files with 607 additions and 613 deletions

View file

@ -1,26 +1,26 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter/material.dart';
import 'falsePositiveTypes.dart';
import 'package:bson_objectid/bson_objectid.dart';
import 'package:fires_flutter/models/yourLocation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:http/http.dart' as ht;
import 'package:jaguar_resty/jaguar_resty.dart' as resty;
import 'package:latlong/latlong.dart';
import '../globals.dart' as globals;
import '../objectIdUtils.dart';
import '../redux/actions.dart';
import 'appState.dart';
import 'falsePositiveTypes.dart';
class FiresApi {
FiresApi() {
resty.globalClient = new ht.IOClient();
}
Future<String> createUser(AppState state, String mobileToken,
String lang) async {
Future<String> createUser(
AppState state, String mobileToken, String lang) async {
assert(state.firesApiUrl != null);
assert(state.firesApiKey != null);
assert(mobileToken != null);
@ -38,6 +38,8 @@ class FiresApi {
if (response.statusCode == 200) {
// print(response.body);
return json.decode(response.body)['data']['userId'];
} else {
return throw "Unexpected error on create user";
}
});
}
@ -45,28 +47,29 @@ class FiresApi {
Future<List<YourLocation>> fetchYourLocations(AppState state) async {
final apiKey = state.firesApiKey;
final mobileToken = state.user.token;
final String url = '${state
.firesApiUrl}mobile/subscriptions/all/$apiKey/$mobileToken';
final String url =
'${state.firesApiUrl}mobile/subscriptions/all/$apiKey/$mobileToken';
// if (globals.isDevelopment) print('$url');
return await resty.get(url).go().then((response) {
if (response.statusCode == 200) {
// if (globals.isDevelopment) print(response.body);
final dataSubscriptions =
json.decode(response.body)['data']['subscriptions'];
json.decode(response.body)['data']['subscriptions'];
List<YourLocation> subscribed = [];
for (int i = 0; i < dataSubscriptions.length; i++) {
var el = dataSubscriptions[i];
var lat = el['location']['lat'];
var lon = el['location']['lon'];
subscribed.add(new YourLocation(
id: ObjectId.fromHexString(el['_id']['_str']),
lat: lat,
lon: lon,
subscribed: true,
distance: el['distance']));
id: objectIdFromJson(el['_id']['_str']),
lat: lat,
lon: lon,
subscribed: true,
distance: el['distance']));
}
return subscribed;
} else {
return throw "Unexpected error fetching your locations";
}
});
}
@ -83,13 +86,12 @@ class FiresApi {
final params = {
"token": state.firesApiKey,
"mobileToken": state.user.token,
"id": loc.id.toHexString(),
"id": loc.id.hexString,
"lat": loc.lat,
"lon": loc.lon,
"distance": loc.distance
};
final String url = '${state
.firesApiUrl}mobile/subscriptions';
final String url = '${state.firesApiUrl}mobile/subscriptions';
return await resty.post(url).json(params).go().then((response) {
if (response.statusCode == 200) {
// print(response.body);
@ -97,6 +99,7 @@ class FiresApi {
} else {
// take care of? "Unexpected error in REST call: Error: The user is already subscribed to this area [on-already-subscribed]"
print(response.body);
return throw "Unexpected error on subscribe";
}
});
}
@ -108,22 +111,24 @@ class FiresApi {
final apiKey = state.firesApiKey;
final mobileToken = state.user.token;
assert(subsId != null);
final String url = '${state
.firesApiUrl}mobile/subscriptions/$apiKey/$mobileToken/$subsId';
final String url =
'${state.firesApiUrl}mobile/subscriptions/$apiKey/$mobileToken/$subsId';
return await resty.delete(url).go().then((response) {
if (response.statusCode == 200) {
// print(response.body);
return true;
} else {
return throw "Unexpected error on unsubscribe";
}
});
}
Future<UpdateFireMapStatsAction> getFiresInLocation(
{AppState state, double lat, double lon, int distance}) async {
{AppState state, double lat, double lon, int distance}) async {
assert(state.firesApiUrl != null);
assert(state.firesApiKey != null);
var url = '${state.firesApiUrl}fires-in-full/${state
.firesApiKey}/${lat}/${lon}/${distance}';
var url =
'${state.firesApiUrl}fires-in-full/${state.firesApiKey}/$lat/$lon/$distance';
if (globals.isDevelopment) print(url);
return await resty.get(url).go().then((response) {
if (response.statusCode == 200) {
@ -138,13 +143,13 @@ class FiresApi {
var industriesCount = industries.length;
var falsePosCount = falsePos.length;
print(
'(Pos: $lat, $lon) real: $numFires, fire: $firesCount falsePos: $falsePosCount industries: $industriesCount');
'(Pos: $lat, $lon) real: $numFires, fire: $firesCount falsePos: $falsePosCount industries: $industriesCount');
}
return new UpdateFireMapStatsAction(
numFires: numFires,
fires: fires,
falsePos: falsePos,
industries: industries);
numFires: numFires,
fires: fires,
falsePos: falsePos,
industries: industries);
} else
throw Exception('Wrong response trying to get fire data');
});
@ -153,8 +158,8 @@ class FiresApi {
Future<List<Polyline>> getMonitoredAreas({AppState state}) async {
assert(state.firesApiUrl != null);
assert(state.firesApiKey != null);
var url = '${state.firesApiUrl}status/subs-public-union/${state
.firesApiKey}';
var url =
'${state.firesApiUrl}status/subs-public-union/${state.firesApiKey}';
var color = const Color(0xFF145A32);
return await resty.get(url).go().then((response) {
if (response.statusCode == 200) {
@ -162,8 +167,8 @@ class FiresApi {
// print(resultDecoded['data']['union']);
List<Polyline> union = [];
final multipolygon =
json.decode(resultDecoded['data']['union']['value'])['geometry']
['coordinates'];
json.decode(resultDecoded['data']['union']['value'])['geometry']
['coordinates'];
for (List<dynamic> polygon in multipolygon) {
for (List<dynamic> hole in polygon) {
List<LatLng> points = [];
@ -171,7 +176,7 @@ class FiresApi {
points.add(new LatLng(point[1].toDouble(), point[0].toDouble()));
}
union.add(
new Polyline(points: points, color: color, strokeWidth: 3.0));
new Polyline(points: points, color: color, strokeWidth: 3.0));
}
}
return union;
@ -181,7 +186,7 @@ class FiresApi {
}
Future<bool> markFalsePositive(AppState state, String mobileToken,
String sealed, FalsePositiveType type) async {
String sealed, FalsePositiveType type) async {
assert(state.firesApiUrl != null);
assert(state.firesApiKey != null);
assert(mobileToken != null);
@ -198,8 +203,8 @@ class FiresApi {
return await resty.post(url).json(params).go().then((response) {
if (response.statusCode == 200) {
// print(response.body);
if (globals.isDevelopment) print(
json.decode(response.body)['data']['upsert']);
if (globals.isDevelopment)
print(json.decode(response.body)['data']['upsert']);
return true;
} else {
debugPrint(json.decode(response.body));