Skip to content

Commit

Permalink
refactor to use dio. better error handling. translations
Browse files Browse the repository at this point in the history
  • Loading branch information
guivazcabral committed Jun 11, 2019
1 parent 4e3f209 commit e30af0f
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 49 deletions.
13 changes: 12 additions & 1 deletion lib/localization/fogos_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,19 @@ class FogosLocalizations {
name: 'textFiresList',
desc: 'Fires List',
);


String get textProblemLoadingData => Intl.message(
"Houve um problema a carregar a informação.",
name: 'textProblemLoadingData',
desc: 'There was a problem loading data.',
);

String get textInternetConnection => Intl.message(
"Certifique-se que está ligado à Internet.",
name: 'textInternetConnection',
desc: 'Make sure you are connected to the Internet',
);

String textFireStatus(FireStatus status) {
switch (status) {
case FireStatus.arrival:
Expand Down
14 changes: 13 additions & 1 deletion lib/localization/generated/l10n/intl_messages_en.arb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"@@locale": "en",
"@@last_modified": "2019-05-30T09:56:25.562305",
"@@last_modified": "2019-06-11T11:18:27.773623",
"appTitle": "Fogos.pt",
"@appTitle": {
"description": "App title",
Expand Down Expand Up @@ -421,5 +421,17 @@
"description": "Fires List",
"type": "text",
"placeholders": {}
},
"textProblemLoadingData": "There was a problem loading data.",
"@textProblemLoadingData": {
"description": "There was a problem loading data.",
"type": "text",
"placeholders": {}
},
"textInternetConnection": "Make sure you are connected to the Internet.",
"@textInternetConnection": {
"description": "Make sure you are connected to the Internet",
"type": "text",
"placeholders": {}
}
}
16 changes: 14 additions & 2 deletions lib/localization/generated/l10n/intl_messages_pt.arb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"@@locale": "pt",
"@@last_modified": "2019-05-30T09:56:25.562305",
"@@last_modified": "2019-06-11T11:18:27.773623",
"appTitle": "Fogos.pt",
"@appTitle": {
"description": "App title",
Expand Down Expand Up @@ -421,5 +421,17 @@
"description": "Fires List",
"type": "text",
"placeholders": {}
},
"textProblemLoadingData": "Houve um problema a carregar a informação.",
"@textProblemLoadingData": {
"description": "There was a problem loading data.",
"type": "text",
"placeholders": {}
},
"textInternetConnection": "Certifique-se que está ligado à Internet.",
"@textInternetConnection": {
"description": "Make sure you are connected to the Internet",
"type": "text",
"placeholders": {}
}
}
}
102 changes: 72 additions & 30 deletions lib/middleware/fires_middleware.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'dart:io';

import 'package:fogosmobile/models/fire_details.dart';
import 'package:fogosmobile/utils/model_utils.dart';
import 'package:redux/redux.dart';
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';
import 'dart:convert';

import 'package:fogosmobile/models/app_state.dart';
Expand All @@ -23,8 +21,7 @@ List<Middleware<AppState>> firesMiddleware() {
TypedMiddleware<AppState, LoadFiresAction>(loadFires),
TypedMiddleware<AppState, LoadFireAction>(loadFire),
TypedMiddleware<AppState, LoadFireMeansHistoryAction>(loadFireMeansHistory),
TypedMiddleware<AppState, LoadFireDetailsHistoryAction>(
loadFireDetailsHistory),
TypedMiddleware<AppState, LoadFireDetailsHistoryAction>(loadFireDetailsHistory),
TypedMiddleware<AppState, LoadFireRiskAction>(loadFireRisk),
];
}
Expand All @@ -36,16 +33,16 @@ Middleware<AppState> _createLoadFires() {

try {
String url = Endpoints.getFires;
final response = await http.get(url);
final responseData = json.decode(response.body)['data'];
List<Fire> fires =
responseData.map<Fire>((model) => Fire.fromJson(model)).toList();
print("load fires");
Response response = await Dio().get(url);
final responseData = json.decode(response.data)["data"];
List<Fire> fires = responseData.map<Fire>((model) => Fire.fromJson(model)).toList();
fires = calculateFireImportance(fires);
store.dispatch(new FiresLoadedAction(fires));
} catch (e) {
store.dispatch(new FiresLoadedAction([]));
if (!(e is SocketException)) {
store.dispatch(new AddErrorAction('fires'));
if (!(e is DioError)) {
print('throwing error');
throw e;
}
}
Expand All @@ -57,16 +54,28 @@ Middleware<AppState> _createLoadFire() {
return (Store store, action, NextDispatcher next) async {
next(action);

String url = '${Endpoints.getFire}${action.fireId}';

try {
String url = '${Endpoints.getFire}${action.fireId}';
final response = await http.get(url);
final responseData = json.decode(response.body)['data'];
Response response = await Dio().get(url);
final responseData = json.decode(response.data)["data"];
if (responseData == null) {
throw new StateError('No fire data could be loaded: $url');
}

Fire fire = Fire.fromJson(responseData);
store.dispatch(new FireLoadedAction(fire));
} catch (e) {
store.dispatch(new FireLoadedAction(null));
store.dispatch(new AddErrorAction('fire'));
if (!(e is SocketException)) {
if (e is DioError) {
if (e.response != null) {
if (e.response.statusCode >= 400) {
throw new StateError('Server responded with ${e.response.statusCode}: $url');
}
}
} else {
print('throwing error');
throw e;
}
}
Expand All @@ -76,18 +85,28 @@ Middleware<AppState> _createLoadFire() {
Middleware<AppState> _createLoadFireMeansHistory() {
return (Store store, action, NextDispatcher next) async {
next(action);
print('create means history');
String url = '${Endpoints.getFireMeansHistory}${action.fireId}';

try {
String url = '${Endpoints.getFireMeansHistory}${action.fireId}';
final response = await http.get(url);
final responseData = json.decode(response.body)['data'];
Response response = await Dio().get(url);
final responseData = json.decode(response.data)["data"];
if (responseData == null) {
throw new StateError('No getFireMeansHistory could be loaded: $url');
}
MeansHistory data = MeansHistory.fromJson(responseData);
store.dispatch(new RemoveErrorAction('fireMeansHistory'));
store.dispatch(new FireMeansHistoryLoadedAction(data));
} catch (e) {
store.dispatch(new FireMeansHistoryLoadedAction(null));
store.dispatch(new AddErrorAction('fireMeansHistory'));
if (!(e is SocketException)) {
if (e is DioError) {
if (e.response != null) {
if (e.response.statusCode >= 400) {
throw new StateError('Server responded with ${e.response.statusCode}: $url');
}
}
} else {
print('throwing error');
throw e;
}
}
Expand All @@ -97,18 +116,28 @@ Middleware<AppState> _createLoadFireMeansHistory() {
Middleware<AppState> _createLoadFireDetailsHistory() {
return (Store store, action, NextDispatcher next) async {
next(action);
print('create details history');
String url = '${Endpoints.getFireDetailsHistory}${action.fireId}';

try {
String url = '${Endpoints.getFireDetailsHistory}${action.fireId}';
final response = await http.get(url);
final responseData = json.decode(response.body)['data'];
Response response = await Dio().get(url);
final responseData = json.decode(response.data)["data"];
if (responseData == null) {
throw new StateError('No getFireDetailsHistory could be loaded: $url');
}
DetailsHistory data = DetailsHistory.fromJson(responseData);
store.dispatch(new RemoveErrorAction('fireDetailsHistory'));
store.dispatch(new FireDetailsHistoryLoadedAction(data));
} catch (e) {
store.dispatch(new FireDetailsHistoryLoadedAction(null));
store.dispatch(new AddErrorAction('fireDetailsHistory'));
if (!(e is SocketException)) {
if (e is DioError) {
if (e.response != null) {
if (e.response.statusCode >= 400) {
throw new StateError('Server responded with ${e.response.statusCode}: $url');
}
}
} else {
print('throwing error');
throw e;
}
}
Expand All @@ -118,17 +147,30 @@ Middleware<AppState> _createLoadFireDetailsHistory() {
Middleware<AppState> _createLoadFireRisk() {
return (Store store, action, NextDispatcher next) async {
next(action);
print('create risk');

String url = '${Endpoints.getFireRisk}${action.fireId}';

try {
String url = '${Endpoints.getFireRisk}${action.fireId}';
final response = await http.get(url);
String responseData = json.decode(response.body)['data'][0]['hoje'];
Response response = await Dio().get(url);
final responseData = json.decode(response.data)["data"][0]['hoje'];

if (responseData == null) {
throw new StateError('No getFireRisk could be loaded: $url');
}

store.dispatch(new RemoveErrorAction('fireRisk'));
store.dispatch(new FireRiskLoadedAction(responseData));
} catch (e) {
store.dispatch(new FireRiskLoadedAction(null));
store.dispatch(new AddErrorAction('fireRisk'));
if (!(e is SocketException)) {
if (e is DioError) {
if (e.response != null) {
if (e.response.statusCode >= 400) {
throw new StateError('Server responded with ${e.response.statusCode}: $url');
}
}
} else {
print('throwing error');
throw e;
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/screens/components/details_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_svg/svg.dart';
import 'package:fogosmobile/actions/fires_actions.dart';
import 'package:fogosmobile/localization/fogos_localizations.dart';
import 'package:fogosmobile/models/app_state.dart';
import 'package:fogosmobile/models/fire_details.dart';
import 'package:fogosmobile/screens/utils/widget_utils.dart';
Expand All @@ -21,7 +22,7 @@ class DetailsHistoryStats extends StatelessWidget {

if (stats == null) {
if (state.errors != null && state.errors.contains('fireDetailsHistory')) {
return Center(child: Text('There was an error loading this chart.'));
return Center(child: Text(FogosLocalizations.of(context).textProblemLoadingData));
}
return Center(child: CircularProgressIndicator());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/components/fireRisk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FireRisk extends StatelessWidget {

if (stats == null) {
if (state.errors != null && state.errors.contains('fireRisk')) {
return Center(child: Text('There was an error loading this chart.'));
return Center(child: Text(FogosLocalizations.of(context).textProblemLoadingData));
}
return Center(child: CircularProgressIndicator());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/components/fire_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FireDetails extends StatelessWidget {
Fire fire = state.fire;
if (fire == null) {
if (state.errors != null && state.errors.contains('fire')) {
return Center(child: Text('There was an error loading this info.'));
return Center(child: Text(FogosLocalizations.of(context).textProblemLoadingData));
}

return ModalProgressHUD(
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/components/meansStatistics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MeansStatistics extends StatelessWidget {

if (stats == null) {
if (state.errors != null && state.errors.contains('fireMeansHistory')) {
return Center(child: Text('There was an error loading this chart.'));
return Center(child: Text(FogosLocalizations.of(context).textProblemLoadingData));
}
return Center(child: CircularProgressIndicator());
}
Expand Down
65 changes: 59 additions & 6 deletions lib/screens/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:fogosmobile/actions/fires_actions.dart';
import 'package:fogosmobile/localization/fogos_localizations.dart';
import 'package:fogosmobile/models/app_state.dart';
import 'package:fogosmobile/models/fire.dart';
import 'package:fogosmobile/screens/components/fire_details.dart';
Expand Down Expand Up @@ -51,6 +52,62 @@ class HomePage extends StatelessWidget {
builder: (BuildContext context, AppState state) {
final store = StoreProvider.of<AppState>(context);

if (state.fires.length < 1) {
if (state.errors != null && state.errors.contains('fires')) {
return Stack(
children: <Widget>[
new FlutterMap(
mapController: mapController,
options: new MapOptions(
center: _center,
zoom: 7.0,
minZoom: 1.0,
maxZoom: 20.0,
),
layers: [
new TileLayerOptions(
urlTemplate: MAPBOX_URL_TEMPLATE,
additionalOptions: {
'accessToken': MAPBOX_ACCESS_TOKEN,
'id': MAPBOX_ID,
},
),
],
),
MapboxCopyright(),
Center(
child: Container(
height: 150,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4.0),
color: Colors.black54,
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
FogosLocalizations.of(context).textProblemLoadingData,
style: TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
Text(
FogosLocalizations.of(context).textInternetConnection,
style: TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
],
),
),
),
)
],
);
}
}

if (state.fires != null) {
for (final Fire fire in state.fires) {
if (state.activeFilters.contains(fire.status)) {
Expand All @@ -61,13 +118,9 @@ class HomePage extends StatelessWidget {
point: new LatLng(fire.lat, fire.lng),
builder: (BuildContext context) {
return new Container(
decoration: BoxDecoration(
color: getFireColor(fire),
shape: BoxShape.circle),
decoration: BoxDecoration(color: getFireColor(fire), shape: BoxShape.circle),
child: IconButton(
icon: new SvgPicture.asset(
getCorrectStatusImage(
fire.statusCode, fire.important),
icon: new SvgPicture.asset(getCorrectStatusImage(fire.statusCode, fire.important),
semanticsLabel: 'Acme Logo'),
onPressed: () async {
store.dispatch(ClearFireAction());
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/warnings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Warnings extends StatelessWidget {
List warnings = state.warnings;
if (warnings == null) {
if (state.errors != null && state.errors.contains('warnings')) {
return Center(child: Text('There was an error loading this info.'));
return Center(child: Text(FogosLocalizations.of(context).textProblemLoadingData));
}

return Center(child: CircularProgressIndicator());
Expand Down
Loading

0 comments on commit e30af0f

Please sign in to comment.