More redux work in fires page

This commit is contained in:
vjrj 2018-06-28 10:42:59 +02:00
parent 5d947f9577
commit 7dddf03e32
13 changed files with 210 additions and 54 deletions

View file

@ -79,6 +79,6 @@ class AppState extends Object with _$AppStateSerializerMixin {
typedef void AddYourLocationFunction(YourLocation loc);
typedef void DeleteYourLocationFunction(ObjectId id);
typedef void UpdateYourLocationFunction(ObjectId id, YourLocation loc);
typedef void ToggleSubscriptionFunction(ObjectId id);
typedef void ToggleSubscriptionFunction(YourLocation loc);
typedef void SubscribeFunction(ObjectId id);
typedef void UnSubscribeFunction(ObjectId id);

View file

@ -1,12 +1,11 @@
import 'dart:async';
import 'dart:convert';
import 'package:bson_objectid/bson_objectid.dart';
import 'package:fires_flutter/models/yourLocation.dart';
import 'package:http/http.dart' as ht;
import 'package:jaguar_resty/jaguar_resty.dart' as resty;
import 'package:fires_flutter/models/yourLocation.dart';
import 'appState.dart';
class FiresApi {
@ -14,7 +13,8 @@ class 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);
@ -28,25 +28,87 @@ class FiresApi {
final String url = '${state.firesApiUrl}mobile/users';
/* print(url);
print(params); */
String resp = await resty.post(url).json(params).go().then((response) {
return await resty.post(url).json(params).go().then((response) {
if (response.statusCode == 200) {
// print(response.body);
return json.decode(response.body)['data']['userId'];
}
});
return resp;
}
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';
List<YourLocation> resp = await resty.get(url).go().then((response) {
final String url = '${state
.firesApiUrl}mobile/subscriptions/all/$apiKey/$mobileToken';
return await resty.get(url).go().then((response) {
if (response.statusCode == 200) {
// print(response.body);
print(json.decode(response.body)['data']['subscriptions']); return [];
final dataSubscriptions =
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'];
var id = el['_id']['_str'];
subscribed.add(new YourLocation(
id: ObjectId.fromHexString(el['_id']['_str']),
lat: lat,
lon: lon,
subscribed: true,
distance: el['distance']));
}
return subscribed;
}
});
}
Future<String> subscribe(AppState state, YourLocation loc) async {
assert(state.firesApiUrl != null);
assert(state.firesApiKey != null);
assert(state.user.token != null);
assert(loc != null);
assert(loc.lat != null);
assert(loc.lon != null);
assert(loc.id != null);
assert(loc.distance != null);
final params = {
"token": state.firesApiKey,
"mobileToken": state.user.token,
"id": loc.id.toHexString(),
"lat": loc.lat,
"lon": loc.lon,
"distance": loc.distance
};
final String url = '${state
.firesApiUrl}mobile/subscriptions';
return await resty.post(url).json(params).go().then((response) {
if (response.statusCode == 200) {
// print(response.body);
return json.decode(response.body)['data']['subsId'];
} else {
// take care of? "Unexpected error in REST call: Error: The user is already subscribed to this area [on-already-subscribed]"
print(response.body);
}
});
}
Future<bool> unsubscribe(AppState state, String subsId) async {
assert(state.firesApiUrl != null);
assert(state.firesApiKey != null);
assert(state.user.token != null);
final apiKey = state.firesApiKey;
final mobileToken = state.user.token;
assert(subsId != null);
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;
}
});
return resp;
}
}

View file

@ -20,6 +20,7 @@ class YourLocation extends Object with _$YourLocationSerializerMixin {
final double lon;
String description;
bool subscribed;
int distance;
factory YourLocation.fromJson(Map<String, dynamic> json) =>
_$YourLocationFromJson(json);
@ -31,14 +32,13 @@ class YourLocation extends Object with _$YourLocationSerializerMixin {
@required this.lat,
@required this.lon,
this.description,
this.distance: 10,
this.subscribed: false}) {
if (this.description == null)
this.description = 'Position: ${this.lat}, ${this.lon}';
if (this.id == null) this.id = new ObjectId();
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
@ -48,7 +48,8 @@ class YourLocation extends Object with _$YourLocationSerializerMixin {
lat == other.lat &&
lon == other.lon &&
description == other.description &&
subscribed == other.subscribed;
subscribed == other.subscribed &&
distance == other.distance;
@override
int get hashCode =>
@ -56,11 +57,11 @@ class YourLocation extends Object with _$YourLocationSerializerMixin {
lat.hashCode ^
lon.hashCode ^
description.hashCode ^
subscribed.hashCode;
subscribed.hashCode ^
distance.hashCode;
@override
String toString() {
return 'YourLocation {id: $id, lat: $lat, lon: $lon, description: $description, subscribed: $subscribed}';
return 'YourLocation {id: $id, lat: $lat, lon: $lon, description: $description, subscribed: $subscribed, distance: $distance}';
}
}

View file

@ -14,24 +14,33 @@ YourLocation _$YourLocationFromJson(Map<String, dynamic> json) =>
lat: (json['lat'] as num).toDouble(),
lon: (json['lon'] as num).toDouble(),
description: json['description'] as String,
distance: json['distance'] as int,
subscribed: json['subscribed'] as bool);
abstract class _$YourLocationSerializerMixin {
ObjectId get id;
double get lat;
double get lon;
String get description;
bool get subscribed;
int get distance;
Map<String, dynamic> toJson() => new _$YourLocationJsonMapWrapper(this);
}
class _$YourLocationJsonMapWrapper extends $JsonMapWrapper {
final _$YourLocationSerializerMixin _v;
_$YourLocationJsonMapWrapper(this._v);
@override
Iterable<String> get keys =>
const ['id', 'lat', 'lon', 'description', 'subscribed'];
const ['id', 'lat', 'lon', 'description', 'subscribed', 'distance'];
@override
dynamic operator [](Object key) {
@ -47,6 +56,8 @@ class _$YourLocationJsonMapWrapper extends $JsonMapWrapper {
return _v.description;
case 'subscribed':
return _v.subscribed;
case 'distance':
return _v.distance;
}
}
return null;