More redux work
This commit is contained in:
parent
657fecdf17
commit
eedd39f6fc
11 changed files with 346 additions and 181 deletions
|
|
@ -5,17 +5,17 @@ import 'package:fires_flutter/models/yourLocation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_fab_dialer/flutter_fab_dialer.dart';
|
import 'package:flutter_fab_dialer/flutter_fab_dialer.dart';
|
||||||
import 'package:flutter_redux/flutter_redux.dart';
|
import 'package:flutter_redux/flutter_redux.dart';
|
||||||
|
import 'package:redux/src/store.dart';
|
||||||
|
|
||||||
import 'colors.dart';
|
import 'colors.dart';
|
||||||
import 'generated/i18n.dart';
|
import 'generated/i18n.dart';
|
||||||
import 'genericMap.dart';
|
|
||||||
import 'globalFiresBottomStats.dart';
|
import 'globalFiresBottomStats.dart';
|
||||||
import 'locationUtils.dart';
|
import 'locationUtils.dart';
|
||||||
import 'mainDrawer.dart';
|
import 'mainDrawer.dart';
|
||||||
import 'models/appState.dart';
|
import 'models/appState.dart';
|
||||||
import 'package:redux/src/store.dart';
|
|
||||||
import 'placesAutocompleteUtils.dart';
|
import 'placesAutocompleteUtils.dart';
|
||||||
import 'redux/actions.dart';
|
import 'redux/actions.dart';
|
||||||
|
import 'yourLocationMap.dart';
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
class _ViewModel {
|
class _ViewModel {
|
||||||
|
|
@ -124,31 +124,13 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleUndo(YourLocation item) {
|
|
||||||
setState(() {
|
|
||||||
/* FIXME
|
|
||||||
globals.yourLocations.add(item);
|
|
||||||
persistYourLocations(); */
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _buildItem(YourLocation item, onDelete, onToggle, onTap) {
|
Widget _buildItem(YourLocation item, onDelete, onToggle, onTap) {
|
||||||
final ThemeData theme = Theme.of(context);
|
final ThemeData theme = Theme.of(context);
|
||||||
return new Dismissible(
|
return new Dismissible(
|
||||||
key: new ObjectKey(item),
|
key: new ObjectKey(item),
|
||||||
direction: DismissDirection.horizontal,
|
direction: DismissDirection.horizontal,
|
||||||
onDismissed: (DismissDirection direction) {
|
onDismissed: (DismissDirection direction) {
|
||||||
setState(() {
|
onDelete(item);
|
||||||
onDelete(item.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
||||||
content: new Text(S.of(context).youDeletedThisPlace),
|
|
||||||
action: new SnackBarAction(
|
|
||||||
label: S.of(context).UNDO,
|
|
||||||
onPressed: () {
|
|
||||||
handleUndo(item);
|
|
||||||
})));
|
|
||||||
},
|
},
|
||||||
background: new Container(
|
background: new Container(
|
||||||
color: theme.primaryColor,
|
color: theme.primaryColor,
|
||||||
|
|
@ -175,11 +157,26 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
|
||||||
converter: (store) {
|
converter: (store) {
|
||||||
return new _ViewModel(
|
return new _ViewModel(
|
||||||
onAdd: (loc) {
|
onAdd: (loc) {
|
||||||
store.dispatch(new AddYourLocationAction(loc));
|
if (store.state.yourLocations
|
||||||
gotoLocationMap(store, loc, context);
|
.any((l) => loc.lat == l.lat && loc.lon == l.lon)) {
|
||||||
|
// Already added
|
||||||
|
showSnackMsg(S.of(context).addedThisLocation);
|
||||||
|
} else {
|
||||||
|
store.dispatch(new AddYourLocationAction(loc));
|
||||||
|
new Timer(new Duration(milliseconds: 1000), () {
|
||||||
|
gotoLocationMap(store, loc, context);
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onDelete: (id) {
|
onDelete: (loc) {
|
||||||
store.dispatch(new DeleteYourLocationAction(id));
|
store.dispatch(new DeleteYourLocationAction(loc));
|
||||||
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
||||||
|
content: new Text(S.of(context).youDeletedThisPlace),
|
||||||
|
action: new SnackBarAction(
|
||||||
|
label: S.of(context).UNDO,
|
||||||
|
onPressed: () {
|
||||||
|
store.dispatch(new AddYourLocationAction(loc));
|
||||||
|
})));
|
||||||
},
|
},
|
||||||
onToggleSubs: (loc) {
|
onToggleSubs: (loc) {
|
||||||
store.dispatch(new ToggleSubscriptionAction(loc));
|
store.dispatch(new ToggleSubscriptionAction(loc));
|
||||||
|
|
@ -237,10 +234,11 @@ class _ActiveFiresPageState extends State<ActiveFiresPage> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void gotoLocationMap(Store<AppState> store, YourLocation loc, BuildContext context) {
|
void gotoLocationMap(
|
||||||
|
Store<AppState> store, YourLocation loc, BuildContext context) {
|
||||||
store.dispatch(new ShowYourLocationMapAction(loc));
|
store.dispatch(new ShowYourLocationMapAction(loc));
|
||||||
Navigator.push(
|
Navigator.push(context,
|
||||||
context, new MaterialPageRoute(builder: (context) => new GenericMap()));
|
new MaterialPageRoute(builder: (context) => new YourLocationMap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void onAddYourLocation(AddYourLocationFunction onAdd) {
|
void onAddYourLocation(AddYourLocationFunction onAdd) {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import 'package:redux/redux.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
globals.isDevelopment = true;
|
globals.isDevelopment = true;
|
||||||
var logRedux = false;
|
var logRedux = true;
|
||||||
|
|
||||||
List<Middleware> devMiddlewares = logRedux ? [
|
List<Middleware> devMiddlewares = logRedux ? [
|
||||||
LoggingMiddleware.printer(formatter: LoggingMiddleware.multiLineFormatter)
|
LoggingMiddleware.printer(formatter: LoggingMiddleware.multiLineFormatter)
|
||||||
|
|
|
||||||
|
|
@ -72,16 +72,17 @@ class AppState extends Object with _$AppStateSerializerMixin {
|
||||||
return 'AppState{\nuser: ${user}\nisLoading: $isLoading\nisLoaded: $isLoaded\napiKey: ${ellipse(
|
return 'AppState{\nuser: ${user}\nisLoading: $isLoading\nisLoaded: $isLoaded\napiKey: ${ellipse(
|
||||||
firesApiKey, 8)}\napiUrl: ${ellipse(
|
firesApiKey, 8)}\napiUrl: ${ellipse(
|
||||||
firesApiUrl, 8)}\nyourLocations count: ${yourLocations
|
firesApiUrl, 8)}\nyourLocations count: ${yourLocations
|
||||||
.length}\nyourLocations: ${yourLocations}';
|
.length}\nyourLocations: ${yourLocations}\nfireMapState: $fireMapState}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef void AddYourLocationFunction(YourLocation loc);
|
typedef void AddYourLocationFunction(YourLocation loc);
|
||||||
typedef void DeleteYourLocationFunction(ObjectId id);
|
typedef void DeleteYourLocationFunction(YourLocation loc);
|
||||||
typedef void ToggleSubscriptionFunction(YourLocation loc);
|
typedef void ToggleSubscriptionFunction(YourLocation loc);
|
||||||
|
|
||||||
typedef void OnLocationTapFunction(YourLocation loc);
|
typedef void OnLocationTapFunction(YourLocation loc);
|
||||||
typedef void OnSubscribeFunction(YourLocation loc);
|
typedef void OnSubscribeFunction(YourLocation loc);
|
||||||
|
typedef void OnSubscribeDistanceChangeFunction(YourLocation loc);
|
||||||
typedef void OnUnSubscribeFunction(YourLocation loc);
|
typedef void OnUnSubscribeFunction(YourLocation loc);
|
||||||
typedef void OnSubscribeConfirmedFunction(YourLocation loc);
|
typedef void OnSubscribeConfirmedFunction(YourLocation loc);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,75 @@
|
||||||
import 'package:meta/meta.dart';
|
|
||||||
import 'package:fires_flutter/models/yourLocation.dart';
|
import 'package:fires_flutter/models/yourLocation.dart';
|
||||||
import 'package:redux/redux.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
enum FireMapStatus { view, subscriptionConfirm, unsubscribe }
|
enum FireMapStatus { view, subscriptionConfirm, unsubscribe }
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
class FireMapState {
|
class FireMapState {
|
||||||
final FireMapStatus status;
|
final FireMapStatus status;
|
||||||
|
final int numFires;
|
||||||
|
final List<dynamic> fires;
|
||||||
|
final List<dynamic> falsePos;
|
||||||
|
final List<dynamic> industries;
|
||||||
final YourLocation yourLocation;
|
final YourLocation yourLocation;
|
||||||
|
|
||||||
const FireMapState.initial(): this.status = FireMapStatus.view, this.yourLocation = null;
|
const FireMapState.initial()
|
||||||
|
: this.status = FireMapStatus.view,
|
||||||
|
this.yourLocation = null,
|
||||||
|
this.numFires = 0,
|
||||||
|
this.fires = const [],
|
||||||
|
this.falsePos = const [],
|
||||||
|
this.industries = const [];
|
||||||
|
|
||||||
FireMapState({this.status: FireMapStatus.view, this.yourLocation});
|
FireMapState(
|
||||||
|
{this.status: FireMapStatus.view,
|
||||||
|
this.yourLocation,
|
||||||
|
this.numFires,
|
||||||
|
this.fires,
|
||||||
|
this.falsePos,
|
||||||
|
this.industries});
|
||||||
|
|
||||||
FireMapState copyWith({FireMapStatus status, YourLocation yourLocation}) {
|
FireMapState copyWith({
|
||||||
|
FireMapStatus status,
|
||||||
|
YourLocation yourLocation,
|
||||||
|
int numFires,
|
||||||
|
List<dynamic> fires,
|
||||||
|
List<dynamic> falsePos,
|
||||||
|
List<dynamic> industries,
|
||||||
|
}) {
|
||||||
return new FireMapState(
|
return new FireMapState(
|
||||||
yourLocation: yourLocation ?? this.yourLocation,
|
yourLocation: yourLocation ?? this.yourLocation,
|
||||||
|
numFires: numFires ?? this.numFires,
|
||||||
|
fires: fires ?? this.fires,
|
||||||
|
falsePos: falsePos ?? this.falsePos,
|
||||||
|
industries: industries ?? this.industries,
|
||||||
status: status ?? this.status);
|
status: status ?? this.status);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool operator ==(Object other) =>
|
bool operator ==(Object other) =>
|
||||||
identical(this, other) ||
|
identical(this, other) ||
|
||||||
other is FireMapState &&
|
other is FireMapState &&
|
||||||
runtimeType == other.runtimeType &&
|
runtimeType == other.runtimeType &&
|
||||||
status == other.status &&
|
status == other.status &&
|
||||||
yourLocation == other.yourLocation;
|
numFires == other.numFires &&
|
||||||
|
fires == other.fires &&
|
||||||
|
falsePos == other.falsePos &&
|
||||||
|
industries == other.industries &&
|
||||||
|
yourLocation == other.yourLocation;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
int get hashCode =>
|
int get hashCode =>
|
||||||
status.hashCode ^
|
status.hashCode ^
|
||||||
yourLocation.hashCode;
|
numFires.hashCode ^
|
||||||
|
fires.hashCode ^
|
||||||
|
falsePos.hashCode ^
|
||||||
|
industries.hashCode ^
|
||||||
|
yourLocation.hashCode;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return 'FireMapState{status: $status, numFires: $numFires, fires: ${fires
|
||||||
|
.length}, falsePos: ${falsePos.length}, industries: ${industries
|
||||||
|
.length}, yourLocation: $yourLocation}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ import 'package:fires_flutter/models/yourLocation.dart';
|
||||||
import 'package:http/http.dart' as ht;
|
import 'package:http/http.dart' as ht;
|
||||||
import 'package:jaguar_resty/jaguar_resty.dart' as resty;
|
import 'package:jaguar_resty/jaguar_resty.dart' as resty;
|
||||||
|
|
||||||
|
import '../globals.dart' as globals;
|
||||||
|
import '../redux/actions.dart';
|
||||||
import 'appState.dart';
|
import 'appState.dart';
|
||||||
|
|
||||||
class FiresApi {
|
class FiresApi {
|
||||||
|
|
@ -110,4 +112,35 @@ class FiresApi {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<UpdateYourLocationMapStatsAction> getYourLocationFireStats(
|
||||||
|
AppState state, YourLocation location) async {
|
||||||
|
assert(state.firesApiUrl != null);
|
||||||
|
assert(state.firesApiKey != null);
|
||||||
|
var url = '${state.firesApiUrl}fires-in-full/${state
|
||||||
|
.firesApiKey}/${location.lat}/${location.lon}/${location.distance}';
|
||||||
|
return await resty.get(url).go().then((response) {
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
var resultDecoded = json.decode(response.body);
|
||||||
|
// print(resultDecoded);
|
||||||
|
int numFires = resultDecoded['real'];
|
||||||
|
List fires = resultDecoded['fires'];
|
||||||
|
List falsePos = resultDecoded['falsePos'];
|
||||||
|
List industries = resultDecoded['industries'];
|
||||||
|
|
||||||
|
if (globals.isDevelopment) {
|
||||||
|
var firesCount = fires.length;
|
||||||
|
var industriesCount = industries.length;
|
||||||
|
var falsePosCount = falsePos.length;
|
||||||
|
print(
|
||||||
|
'real: $numFires, fire: $firesCount falsePos: $falsePosCount industries: $industriesCount');
|
||||||
|
}
|
||||||
|
return new UpdateYourLocationMapStatsAction(
|
||||||
|
numFires: numFires,
|
||||||
|
fires: fires,
|
||||||
|
falsePos: falsePos,
|
||||||
|
industries: industries);
|
||||||
|
} else throw Exception('Wrong response trying to get stats');
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import 'package:bson_objectid/bson_objectid.dart';
|
import 'package:bson_objectid/bson_objectid.dart';
|
||||||
import 'package:fires_flutter/models/yourLocation.dart';
|
import 'package:fires_flutter/models/yourLocation.dart';
|
||||||
|
import 'package:just_debounce_it/just_debounce_it.dart';
|
||||||
import 'package:redux/redux.dart';
|
import 'package:redux/redux.dart';
|
||||||
|
|
||||||
import '../globals.dart' as globals;
|
import '../globals.dart' as globals;
|
||||||
|
|
@ -47,8 +48,43 @@ void fetchYourLocationsMiddleware(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action is DeleteYourLocationAction) {
|
if (action is DeleteYourLocationAction) {
|
||||||
unsubsViaApi(store, action.id,
|
store.dispatch(new DeletedYourLocationAction(action.loc.id));
|
||||||
() => store.dispatch(new DeletedYourLocationAction(action.id)));
|
if (action.loc.subscribed) {
|
||||||
|
unsubsViaApi(store, action.loc.id, () {
|
||||||
|
persistYourLocations(store.state.yourLocations);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
persistYourLocations(store.state.yourLocations);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action is ShowYourLocationMapAction) {
|
||||||
|
api
|
||||||
|
.getYourLocationFireStats(store.state, action.loc)
|
||||||
|
.then((result) => store.dispatch(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action is UpdateLocalYourLocationAction) {
|
||||||
|
if (action.loc.subscribed)
|
||||||
|
Debounce.seconds(
|
||||||
|
2,
|
||||||
|
() => api
|
||||||
|
.getYourLocationFireStats(store.state, action.loc)
|
||||||
|
.then((result) => store.dispatch(result)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action is SubscribeConfirmAction) {
|
||||||
|
subscribeViaApi(store, action.loc, (sub) {
|
||||||
|
store.dispatch(new UpdateLocalYourLocationAction(action.loc));
|
||||||
|
persistYourLocations(store.state.yourLocations);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action is UnSubscribeAction) {
|
||||||
|
unsubsViaApi(store, action.loc.id, () {
|
||||||
|
store.dispatch(new UpdateLocalYourLocationAction(action.loc));
|
||||||
|
persistYourLocations(store.state.yourLocations);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action is ToggleSubscriptionAction) {
|
if (action is ToggleSubscriptionAction) {
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,44 @@ import 'actions.dart';
|
||||||
final fireMapReducer = combineReducers<FireMapState>([
|
final fireMapReducer = combineReducers<FireMapState>([
|
||||||
new TypedReducer<FireMapState, ShowYourLocationMapAction>(
|
new TypedReducer<FireMapState, ShowYourLocationMapAction>(
|
||||||
_showYourLocationMap),
|
_showYourLocationMap),
|
||||||
|
new TypedReducer<FireMapState, UpdateYourLocationMapStatsAction>(
|
||||||
|
_updateYourLocationMapStats),
|
||||||
|
new TypedReducer<FireMapState, SubscribeAction>(_subscribeYourLocationMap),
|
||||||
|
new TypedReducer<FireMapState, SubscribeConfirmAction>(
|
||||||
|
_subscribeConfirmYourLocationMap),
|
||||||
|
new TypedReducer<FireMapState, UnSubscribeAction>(
|
||||||
|
_unsubscribeYourLocationMap),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
FireMapState _updateYourLocationMapStats(
|
||||||
|
FireMapState state, UpdateYourLocationMapStatsAction action) {
|
||||||
|
return state.copyWith(
|
||||||
|
numFires: action.numFires,
|
||||||
|
fires: action.fires,
|
||||||
|
falsePos: action.falsePos,
|
||||||
|
industries: action.industries);
|
||||||
|
}
|
||||||
|
|
||||||
FireMapState _showYourLocationMap(
|
FireMapState _showYourLocationMap(
|
||||||
FireMapState state, ShowYourLocationMapAction action) {
|
FireMapState state, ShowYourLocationMapAction action) {
|
||||||
if (action.loc.subscribed) {
|
return state.copyWith(
|
||||||
return state.copyWith(
|
status: action.loc.subscribed
|
||||||
status: action.loc.subscribed
|
? FireMapStatus.unsubscribe
|
||||||
? FireMapStatus.unsubscribe
|
: FireMapStatus.view,
|
||||||
: FireMapStatus.view,
|
yourLocation: action.loc);
|
||||||
yourLocation: action.loc);
|
}
|
||||||
}
|
|
||||||
|
FireMapState _subscribeYourLocationMap(
|
||||||
|
FireMapState state, SubscribeAction action) {
|
||||||
|
return state.copyWith(status: FireMapStatus.subscriptionConfirm);
|
||||||
|
}
|
||||||
|
|
||||||
|
FireMapState _subscribeConfirmYourLocationMap(
|
||||||
|
FireMapState state, SubscribeConfirmAction action) {
|
||||||
|
return state.copyWith(status: FireMapStatus.unsubscribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
FireMapState _unsubscribeYourLocationMap(
|
||||||
|
FireMapState state, UnSubscribeAction action) {
|
||||||
|
return state.copyWith(status: FireMapStatus.view);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import 'package:bson_objectid/bson_objectid.dart';
|
import 'package:bson_objectid/bson_objectid.dart';
|
||||||
import 'package:fires_flutter/models/yourLocation.dart';
|
import 'package:fires_flutter/models/yourLocation.dart';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
abstract class YourLocationActions {}
|
abstract class YourLocationActions {}
|
||||||
|
|
||||||
|
|
@ -22,10 +22,23 @@ class ShowYourLocationMapAction extends YourLocationActions {
|
||||||
ShowYourLocationMapAction(this.loc);
|
ShowYourLocationMapAction(this.loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
class DeleteYourLocationAction extends YourLocationActions {
|
class UpdateYourLocationMapStatsAction extends YourLocationActions {
|
||||||
ObjectId id;
|
int numFires;
|
||||||
|
List<dynamic> fires = [];
|
||||||
|
List<dynamic> falsePos = [];
|
||||||
|
List<dynamic> industries = [];
|
||||||
|
|
||||||
DeleteYourLocationAction(this.id);
|
UpdateYourLocationMapStatsAction(
|
||||||
|
{@required this.numFires,
|
||||||
|
@required this.fires,
|
||||||
|
@required this.falsePos,
|
||||||
|
@required this.industries});
|
||||||
|
}
|
||||||
|
|
||||||
|
class DeleteYourLocationAction extends YourLocationActions {
|
||||||
|
YourLocation loc;
|
||||||
|
|
||||||
|
DeleteYourLocationAction(this.loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
class DeletedYourLocationAction extends YourLocationActions {
|
class DeletedYourLocationAction extends YourLocationActions {
|
||||||
|
|
@ -34,12 +47,10 @@ class DeletedYourLocationAction extends YourLocationActions {
|
||||||
DeletedYourLocationAction(this.id);
|
DeletedYourLocationAction(this.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class UpdateLocalYourLocationAction extends YourLocationActions {
|
||||||
class UpdateYourLocationAction extends YourLocationActions {
|
|
||||||
ObjectId id;
|
|
||||||
YourLocation loc;
|
YourLocation loc;
|
||||||
|
|
||||||
UpdateYourLocationAction(this.id, this.loc);
|
UpdateLocalYourLocationAction(this.loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
class ToggleSubscriptionAction extends YourLocationActions {
|
class ToggleSubscriptionAction extends YourLocationActions {
|
||||||
|
|
@ -55,13 +66,18 @@ class ToggledSubscriptionAction extends YourLocationActions {
|
||||||
}
|
}
|
||||||
|
|
||||||
class SubscribeAction extends YourLocationActions {
|
class SubscribeAction extends YourLocationActions {
|
||||||
ObjectId id;
|
SubscribeAction();
|
||||||
|
}
|
||||||
|
|
||||||
SubscribeAction(this.id);
|
class SubscribeConfirmAction extends YourLocationActions {
|
||||||
|
YourLocation loc;
|
||||||
|
|
||||||
|
SubscribeConfirmAction(this.loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
class UnSubscribeAction extends YourLocationActions {
|
class UnSubscribeAction extends YourLocationActions {
|
||||||
ObjectId id;
|
YourLocation loc;
|
||||||
|
|
||||||
UnSubscribeAction(this.id);
|
UnSubscribeAction(this.loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,16 +4,14 @@ import 'package:redux/redux.dart';
|
||||||
import 'actions.dart';
|
import 'actions.dart';
|
||||||
|
|
||||||
final yourLocationsReducer = combineReducers<List<YourLocation>>([
|
final yourLocationsReducer = combineReducers<List<YourLocation>>([
|
||||||
new TypedReducer<List<YourLocation>, AddedYourLocationAction>(_addedYourLocation),
|
new TypedReducer<List<YourLocation>, AddedYourLocationAction>(
|
||||||
|
_addedYourLocation),
|
||||||
new TypedReducer<List<YourLocation>, DeletedYourLocationAction>(
|
new TypedReducer<List<YourLocation>, DeletedYourLocationAction>(
|
||||||
_deletedYourLocation),
|
_deletedYourLocation),
|
||||||
new TypedReducer<List<YourLocation>, UpdateYourLocationAction>(
|
new TypedReducer<List<YourLocation>, UpdateLocalYourLocationAction>(
|
||||||
_updateYourLocation),
|
_updateLocalYourLocation),
|
||||||
new TypedReducer<List<YourLocation>, ToggledSubscriptionAction>(_toggledSubscriptionAction)
|
new TypedReducer<List<YourLocation>, ToggledSubscriptionAction>(
|
||||||
|
_toggledSubscriptionAction)
|
||||||
/* new TypedReducer<List<YourLocation>, ToggleSubscriptionAction>(_toggleSubscriptionAction),
|
|
||||||
new TypedReducer<List<YourLocation>, SubscribeAction>(_setLoadedYourLocations),
|
|
||||||
new TypedReducer<List<YourLocation>, UnSubscribeAction>(_setNoYourLocations), */
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
List<YourLocation> _addedYourLocation(
|
List<YourLocation> _addedYourLocation(
|
||||||
|
|
@ -28,25 +26,18 @@ List<YourLocation> _deletedYourLocation(
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<YourLocation> _updateYourLocation(
|
List<YourLocation> _updateLocalYourLocation(
|
||||||
List<YourLocation> yourLocations, UpdateYourLocationAction action) {
|
List<YourLocation> yourLocations, UpdateLocalYourLocationAction action) {
|
||||||
return yourLocations
|
return yourLocations
|
||||||
.map((yourLocation) =>
|
.map((yourLocation) =>
|
||||||
yourLocation.id == action.id ? action.loc : yourLocation)
|
yourLocation.id == action.loc.id ? action.loc : yourLocation)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<YourLocation> _toggledSubscriptionAction(List<YourLocation> yourLocations, ToggledSubscriptionAction action) {
|
List<YourLocation> _toggledSubscriptionAction(
|
||||||
|
List<YourLocation> yourLocations, ToggledSubscriptionAction action) {
|
||||||
return yourLocations
|
return yourLocations
|
||||||
.map((yourLocation) => yourLocation.id == action.loc.id ? action.loc : yourLocation)
|
.map((yourLocation) =>
|
||||||
.toList();
|
yourLocation.id == action.loc.id ? action.loc : yourLocation)
|
||||||
|
.toList();
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
List<YourLocation> _setLoadedYourLocations(List<YourLocation> yourLocations, YourLocationsLoadedAction action) {
|
|
||||||
return action.yourLocations;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<YourLocation> _setNoYourLocations(List<YourLocation> yourLocations, YourLocationsNotLoadedAction action) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import 'dart:convert' show json;
|
|
||||||
import 'dart:core';
|
import 'dart:core';
|
||||||
|
|
||||||
import 'package:comunes_flutter/comunes_flutter.dart';
|
import 'package:comunes_flutter/comunes_flutter.dart';
|
||||||
|
|
@ -9,12 +8,9 @@ import 'package:flutter/material.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:flutter_redux/flutter_redux.dart';
|
import 'package:flutter_redux/flutter_redux.dart';
|
||||||
import 'package:http/http.dart' as http;
|
|
||||||
import 'package:just_debounce_it/just_debounce_it.dart';
|
|
||||||
import 'package:latlong/latlong.dart';
|
import 'package:latlong/latlong.dart';
|
||||||
|
|
||||||
import 'colors.dart';
|
import 'colors.dart';
|
||||||
import 'customBottomAppBar.dart';
|
|
||||||
import 'dummyMapPlugin.dart';
|
import 'dummyMapPlugin.dart';
|
||||||
import 'fireMarkType.dart';
|
import 'fireMarkType.dart';
|
||||||
import 'fireMarker.dart';
|
import 'fireMarker.dart';
|
||||||
|
|
@ -24,6 +20,7 @@ import 'models/appState.dart';
|
||||||
import 'models/fireMapState.dart';
|
import 'models/fireMapState.dart';
|
||||||
import 'redux/actions.dart';
|
import 'redux/actions.dart';
|
||||||
import 'slider.dart';
|
import 'slider.dart';
|
||||||
|
import 'yourLocationMapBottom.dart';
|
||||||
import 'zoomMapPlugin.dart';
|
import 'zoomMapPlugin.dart';
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
|
|
@ -32,12 +29,14 @@ class _ViewModel {
|
||||||
final OnSubscribeFunction onSubs;
|
final OnSubscribeFunction onSubs;
|
||||||
final OnSubscribeConfirmedFunction onSubsConfirmed;
|
final OnSubscribeConfirmedFunction onSubsConfirmed;
|
||||||
final OnUnSubscribeFunction onUnSubs;
|
final OnUnSubscribeFunction onUnSubs;
|
||||||
|
final OnSubscribeDistanceChangeFunction onSlide;
|
||||||
|
|
||||||
_ViewModel(
|
_ViewModel(
|
||||||
{@required this.mapState,
|
{@required this.mapState,
|
||||||
@required this.onSubs,
|
@required this.onSubs,
|
||||||
@required this.onSubsConfirmed,
|
@required this.onSubsConfirmed,
|
||||||
@required this.onUnSubs});
|
@required this.onUnSubs,
|
||||||
|
@required this.onSlide});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool operator ==(Object other) =>
|
bool operator ==(Object other) =>
|
||||||
|
|
@ -47,78 +46,57 @@ class _ViewModel {
|
||||||
mapState == other.mapState &&
|
mapState == other.mapState &&
|
||||||
onSubs == other.onSubs &&
|
onSubs == other.onSubs &&
|
||||||
onSubsConfirmed == other.onSubsConfirmed &&
|
onSubsConfirmed == other.onSubsConfirmed &&
|
||||||
onUnSubs == other.onUnSubs;
|
onUnSubs == other.onUnSubs &&
|
||||||
|
onSlide == other.onSlide;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
int get hashCode =>
|
int get hashCode =>
|
||||||
mapState.hashCode ^
|
mapState.hashCode ^
|
||||||
onSubs.hashCode ^
|
onSubs.hashCode ^
|
||||||
onSubsConfirmed.hashCode ^
|
onSubsConfirmed.hashCode ^
|
||||||
onUnSubs.hashCode;
|
onUnSubs.hashCode ^
|
||||||
|
onSlide.hashCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
class GenericMap extends StatefulWidget {
|
class YourLocationMap extends StatefulWidget {
|
||||||
GenericMap();
|
YourLocationMap();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_GenericMapState createState() => _GenericMapState();
|
_YourLocationMapState createState() => _YourLocationMapState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _GenericMapState extends State<GenericMap> {
|
class _YourLocationMapState extends State<YourLocationMap> {
|
||||||
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
|
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
|
||||||
final FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
|
final FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
|
||||||
|
|
||||||
int numFires;
|
_YourLocationMapState();
|
||||||
int kmAround = 100;
|
|
||||||
List<dynamic> fires = [];
|
|
||||||
List<dynamic> falsePos = [];
|
|
||||||
List<dynamic> industries = [];
|
|
||||||
|
|
||||||
void updateFireStats(YourLocation location) {
|
|
||||||
var url = '${globals.firesApiUrl}fires-in-full/${globals
|
|
||||||
.firesApiKey}/${location.lat}/${location.lon}/$kmAround';
|
|
||||||
http.read(url).then((result) {
|
|
||||||
setState(() {
|
|
||||||
var resultDecoded = json.decode(result);
|
|
||||||
// print(resultDecoded);
|
|
||||||
numFires = resultDecoded['real'];
|
|
||||||
fires = resultDecoded['fires'];
|
|
||||||
falsePos = resultDecoded['falsePos'];
|
|
||||||
industries = resultDecoded['industries'];
|
|
||||||
|
|
||||||
if (globals.isDevelopment) {
|
|
||||||
var firesCount = fires.length;
|
|
||||||
var industriesCount = industries.length;
|
|
||||||
var falsePosCount = falsePos.length;
|
|
||||||
print(
|
|
||||||
'real: $numFires, fire: $firesCount falsePos: $falsePosCount industries: $industriesCount');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
_GenericMapState();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
// print('Build map with operation: $operation');
|
|
||||||
return new StoreConnector<AppState, _ViewModel>(
|
return new StoreConnector<AppState, _ViewModel>(
|
||||||
distinct: false, // FIXME
|
distinct: false, // FIXME
|
||||||
converter: (store) {
|
converter: (store) {
|
||||||
return new _ViewModel(
|
return new _ViewModel(
|
||||||
onSubs: (loc) {
|
onSubs: (loc) {
|
||||||
store.dispatch(new SubscribeAction(loc.id));
|
store.dispatch(new SubscribeAction());
|
||||||
},
|
},
|
||||||
onSubsConfirmed: (loc) {
|
onSubsConfirmed: (loc) {
|
||||||
store.dispatch(new SubscribeAction(loc.id));
|
loc.subscribed = true;
|
||||||
|
store.dispatch(new SubscribeConfirmAction(loc));
|
||||||
},
|
},
|
||||||
onUnSubs: (loc) {
|
onUnSubs: (loc) {
|
||||||
store.dispatch(new UnSubscribeAction(loc.id));
|
loc.subscribed = false;
|
||||||
|
store.dispatch(new UnSubscribeAction(loc));
|
||||||
|
},
|
||||||
|
onSlide: (loc) {
|
||||||
|
store.dispatch(new UpdateLocalYourLocationAction(loc));
|
||||||
},
|
},
|
||||||
mapState: store.state.fireMapState);
|
mapState: store.state.fireMapState);
|
||||||
},
|
},
|
||||||
builder: (context, view) {
|
builder: (context, view) {
|
||||||
YourLocation location = view.mapState.yourLocation;
|
YourLocation location = view.mapState.yourLocation;
|
||||||
|
assert(location != null);
|
||||||
MapOptions mapOptions = new MapOptions(
|
MapOptions mapOptions = new MapOptions(
|
||||||
center: new LatLng(location.lat, location.lon),
|
center: new LatLng(location.lat, location.lon),
|
||||||
plugins: globals.isDevelopment
|
plugins: globals.isDevelopment
|
||||||
|
|
@ -137,7 +115,9 @@ class _GenericMapState extends State<GenericMap> {
|
||||||
// mapController.fitBounds(bounds);
|
// mapController.fitBounds(bounds);
|
||||||
// mapController.center
|
// mapController.center
|
||||||
|
|
||||||
FireMapStatus operation = view.mapState.status;
|
FireMapState mapState = view.mapState;
|
||||||
|
FireMapStatus operation = mapState.status;
|
||||||
|
print('Build map with operation: $operation');
|
||||||
final btnText = operation == FireMapStatus.view
|
final btnText = operation == FireMapStatus.view
|
||||||
? S.of(context).toFiresNotifications
|
? S.of(context).toFiresNotifications
|
||||||
: operation == FireMapStatus.subscriptionConfirm
|
: operation == FireMapStatus.subscriptionConfirm
|
||||||
|
|
@ -148,7 +128,6 @@ class _GenericMapState extends State<GenericMap> {
|
||||||
: operation == FireMapStatus.subscriptionConfirm
|
: operation == FireMapStatus.subscriptionConfirm
|
||||||
? Icons.check
|
? Icons.check
|
||||||
: Icons.notifications_off;
|
: Icons.notifications_off;
|
||||||
updateFireStats(location);
|
|
||||||
return new Scaffold(
|
return new Scaffold(
|
||||||
key: _scaffoldKey,
|
key: _scaffoldKey,
|
||||||
appBar: new AppBar(
|
appBar: new AppBar(
|
||||||
|
|
@ -156,23 +135,19 @@ class _GenericMapState extends State<GenericMap> {
|
||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton.extended(
|
floatingActionButton: FloatingActionButton.extended(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
switch (operation) {
|
||||||
switch (operation) {
|
case FireMapStatus.view:
|
||||||
case FireMapStatus.view:
|
view.onSubs(location);
|
||||||
view.onSubs(location);
|
break;
|
||||||
break;
|
case FireMapStatus.subscriptionConfirm:
|
||||||
case FireMapStatus.subscriptionConfirm:
|
view.onSubsConfirmed(location);
|
||||||
view.onSubsConfirmed(location);
|
// IOS specific
|
||||||
// IOS specific
|
_firebaseMessaging.requestNotificationPermissions();
|
||||||
_firebaseMessaging.requestNotificationPermissions();
|
break;
|
||||||
operation = FireMapStatus.unsubscribe;
|
case FireMapStatus.unsubscribe:
|
||||||
break;
|
view.onUnSubs(location);
|
||||||
case FireMapStatus.unsubscribe:
|
break;
|
||||||
view.onUnSubs(location);
|
}
|
||||||
operation = FireMapStatus.view;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
},
|
},
|
||||||
// https://github.com/flutter/flutter/issues/17583
|
// https://github.com/flutter/flutter/issues/17583
|
||||||
heroTag: "firesmap" + location.id.toHexString(),
|
heroTag: "firesmap" + location.id.toHexString(),
|
||||||
|
|
@ -185,24 +160,7 @@ class _GenericMapState extends State<GenericMap> {
|
||||||
),
|
),
|
||||||
floatingActionButtonLocation:
|
floatingActionButtonLocation:
|
||||||
FloatingActionButtonLocation.centerFloat,
|
FloatingActionButtonLocation.centerFloat,
|
||||||
bottomNavigationBar: new CustomBottomAppBar(
|
bottomNavigationBar: new YourLocationMapBottom(),
|
||||||
fabLocation: FloatingActionButtonLocation.centerFloat,
|
|
||||||
showNotch: false,
|
|
||||||
color: fires100,
|
|
||||||
// height: 170.0,
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
actions: listWithoutNulls(<Widget>[
|
|
||||||
operation == FireMapStatus.subscriptionConfirm ||
|
|
||||||
numFires == null
|
|
||||||
? null
|
|
||||||
: new Text(numFires > 0
|
|
||||||
? S.of(context).firesAroundThisArea(
|
|
||||||
numFires.toString(), kmAround.toString())
|
|
||||||
: S
|
|
||||||
.of(context)
|
|
||||||
.noFiresAroundThisArea(kmAround.toString())),
|
|
||||||
SizedBox(width: 10.0)
|
|
||||||
])),
|
|
||||||
body: LayoutBuilder(
|
body: LayoutBuilder(
|
||||||
builder: (context, constraints) =>
|
builder: (context, constraints) =>
|
||||||
Stack(fit: StackFit.expand, children: <Widget>[
|
Stack(fit: StackFit.expand, children: <Widget>[
|
||||||
|
|
@ -231,8 +189,8 @@ class _GenericMapState extends State<GenericMap> {
|
||||||
? new ZoomMapPluginOptions()
|
? new ZoomMapPluginOptions()
|
||||||
: new DummyMapPluginOptions(),
|
: new DummyMapPluginOptions(),
|
||||||
new MarkerLayerOptions(
|
new MarkerLayerOptions(
|
||||||
markers: buildMarkers(location, this.fires,
|
markers: buildMarkers(location, mapState.fires,
|
||||||
this.industries, this.falsePos),
|
mapState.industries, mapState.falsePos),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)),
|
)),
|
||||||
|
|
@ -243,19 +201,22 @@ class _GenericMapState extends State<GenericMap> {
|
||||||
child: new CenteredRow(
|
child: new CenteredRow(
|
||||||
// Fit sample:
|
// Fit sample:
|
||||||
// https://github.com/apptreesoftware/flutter_map/blob/master/flutter_map_example/lib/pages/map_controller.dart
|
// https://github.com/apptreesoftware/flutter_map/blob/master/flutter_map_example/lib/pages/map_controller.dart
|
||||||
children: operation ==
|
children:
|
||||||
FireMapStatus.subscriptionConfirm
|
operation == FireMapStatus.subscriptionConfirm
|
||||||
? <Widget>[
|
? <Widget>[
|
||||||
new FireDistanceSlider(
|
new FireDistanceSlider(
|
||||||
initialValue: kmAround,
|
initialValue: location.distance,
|
||||||
onSlide: (distance) {
|
onSlide: (distance) {
|
||||||
|
location.distance = distance;
|
||||||
|
view.onSlide(location);
|
||||||
|
/* FIXME
|
||||||
setState(() {
|
setState(() {
|
||||||
kmAround = distance;
|
kmAround = distance;
|
||||||
Debounce.seconds(1, updateFireStats);
|
Debounce.seconds(1, updateFireStats);
|
||||||
});
|
}); */
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
: [],
|
: [],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
]),
|
]),
|
||||||
58
lib/yourLocationMapBottom.dart
Normal file
58
lib/yourLocationMapBottom.dart
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
import 'package:comunes_flutter/comunes_flutter.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_redux/flutter_redux.dart';
|
||||||
|
|
||||||
|
import 'colors.dart';
|
||||||
|
import 'customBottomAppBar.dart';
|
||||||
|
import 'generated/i18n.dart';
|
||||||
|
import 'models/appState.dart';
|
||||||
|
import 'models/fireMapState.dart';
|
||||||
|
|
||||||
|
@immutable
|
||||||
|
class _ViewModel {
|
||||||
|
final FireMapState state;
|
||||||
|
|
||||||
|
_ViewModel({@required this.state});
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) =>
|
||||||
|
identical(this, other) ||
|
||||||
|
other is _ViewModel &&
|
||||||
|
runtimeType == other.runtimeType &&
|
||||||
|
state == other.state;
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => state.hashCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
class YourLocationMapBottom extends StatelessWidget {
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return new StoreConnector<AppState, _ViewModel>(
|
||||||
|
distinct: true, // FIXME
|
||||||
|
converter: (store) {
|
||||||
|
return new _ViewModel(state: store.state.fireMapState);
|
||||||
|
},
|
||||||
|
builder: (context, view) {
|
||||||
|
int kmAround = view.state.yourLocation.distance;
|
||||||
|
return new CustomBottomAppBar(
|
||||||
|
fabLocation: FloatingActionButtonLocation.centerFloat,
|
||||||
|
showNotch: false,
|
||||||
|
color: fires100,
|
||||||
|
// height: 170.0,
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
actions: listWithoutNulls(<Widget>[
|
||||||
|
view.state.status == FireMapStatus.subscriptionConfirm ||
|
||||||
|
view.state.numFires == null
|
||||||
|
? null
|
||||||
|
: new Text(view.state.numFires > 0
|
||||||
|
? S.of(context).firesAroundThisArea(
|
||||||
|
view.state.numFires.toString(), kmAround.toString())
|
||||||
|
: S
|
||||||
|
.of(context)
|
||||||
|
.noFiresAroundThisArea(kmAround.toString())),
|
||||||
|
SizedBox(width: 10.0)
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue