Skip to content

Commit

Permalink
Add offline error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone Sestito committed Oct 22, 2018
1 parent 0e12e23 commit 32004f1
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 26 deletions.
5 changes: 2 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,13 @@ class SplashState extends State<Splash> with SingleTickerProviderStateMixin {
final Animation curve =
CurvedAnimation(parent: controller, curve: Curves.elasticIn);
rocketAnimation = IntTween(begin: 0, end: 360).animate(curve);
controller.forward();
controller.addStatusListener((status) async {
controller..addStatusListener((status) async {
if (status == AnimationStatus.completed) {
// Wait after the animation
await Future.delayed(Duration(milliseconds: 600));
widget.callback();
}
});
})..forward();
}

@override
Expand Down
11 changes: 6 additions & 5 deletions lib/route/collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import 'package:escape_earth/route/launches_result.dart';
import 'package:escape_earth/service/RocketService.dart';
import 'package:escape_earth/view/OfflineView.dart';
import 'package:escape_earth/view/RocketView.dart';
import 'package:escape_earth/view/RoundSearch.dart';
import 'package:flutter/material.dart';
Expand All @@ -32,17 +33,17 @@ class CollectionRocketRoute extends StatelessWidget {
future: RocketService.getLaunches(),
initialData: null,
builder: (context, snap) {
if (snap.error != null) {
debugPrint(snap.error.toString());
return OfflineView();
}

if (snap.data == null || snap.data.length == 0) {
return Center(
child: CircularProgressIndicator(),
);
}

if (snap.error != null) {
print(snap.error);
return Center(child: Icon(Icons.portable_wifi_off));
}

return ListView.builder(
padding: EdgeInsets.only(top: 90.0, right: 16.0, left: 16.0),
itemCount: snap.data.length,
Expand Down
11 changes: 6 additions & 5 deletions lib/route/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:escape_earth/route/collection.dart';
import 'package:escape_earth/route/qa.dart';
import 'package:escape_earth/service/RocketService.dart';
import 'package:escape_earth/view/NewsView.dart';
import 'package:escape_earth/view/OfflineView.dart';
import 'package:escape_earth/view/RocketHero.dart';
import 'package:escape_earth/view/RocketView.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -99,17 +100,17 @@ class HomeBody extends StatelessWidget {
return FutureBuilder(
future: RocketService.getNextLaunch(),
builder: (context, snap) {
if (snap.error != null) {
debugPrint(snap.error.toString());
return OfflineView();
}

if (snap.data == null) {
return Center(
child: CircularProgressIndicator(),
);
}

if (snap.error != null) {
print(snap.error);
return Center(child: Icon(Icons.portable_wifi_off));
}

return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expand Down
14 changes: 13 additions & 1 deletion lib/route/launches_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import 'package:escape_earth/service/RocketService.dart';
import 'package:escape_earth/view/OfflineView.dart';
import 'package:escape_earth/view/RocketView.dart';
import 'package:flutter/material.dart';

Expand All @@ -39,12 +40,23 @@ class LaunchesResultRoute extends StatelessWidget {
future: RocketService.getLaunches(query: query),
initialData: null,
builder: (context, snap) {
if (snap.data == null || snap.data.length == 0) {
if (snap.error != null) {
debugPrint(snap.error.toString());
return OfflineView();
}

if (snap.data == null) {
return Center(
child: CircularProgressIndicator(),
);
}

if (snap.data.length == 0) {
return Center(
child: Text("No results :("),
);
}

return ListView.builder(
padding: EdgeInsets.only(top: 48.0),
itemCount: snap.data.length,
Expand Down
40 changes: 28 additions & 12 deletions lib/service/RocketService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,51 @@ import 'dart:async';
import 'dart:convert';
import 'package:escape_earth/model/Agency.dart';
import 'package:escape_earth/model/RocketLaunch.dart';
import 'package:escape_earth/utils.dart';
import 'package:http/http.dart' as http;

class RocketService {
static Future<RocketLaunch> getNextLaunch() async {
final response = await http.get("https://launchlibrary.net/1.4/launch?next=1");
if (!(await isConnected())) {
throw StateError("Device offline.");
}

final response =
await http.get("https://launchlibrary.net/1.4/launch?next=1");
final content = json.decode(response.body)["launches"][0];
final agency = await getAgencyById(content["lsp"]);
return RocketLaunch(
country: agency.countryCode,
launchCompany: agency,
name: content["name"],
date: content["windowstart"],
videoUrl: (content["vidURLs"] ?? [null])[0],
);
country: agency.countryCode,
launchCompany: agency,
name: content["name"],
date: content["windowstart"],
videoUrl: (content["vidURLs"] ?? [null])[0],
);
}

static Future<List<RocketLaunch>> getLaunches({ String query }) async {
static Future<List<RocketLaunch>> getLaunches({String query}) async {
if (!(await isConnected())) {
throw StateError("Device offline.");
}

http.Response rawLaunchesResponse;
if (query == null || query == "") {
rawLaunchesResponse = await http.get("https://launchlibrary.net/1.4/launch?next=25");
rawLaunchesResponse =
await http.get("https://launchlibrary.net/1.4/launch?next=25");
} else {
rawLaunchesResponse = await http.get("https://launchlibrary.net/1.4/launch?next=25&name=$query");
rawLaunchesResponse = await http
.get("https://launchlibrary.net/1.4/launch?next=25&name=$query");
}

final List<dynamic> launchesContent =
json.decode(rawLaunchesResponse.body)["launches"];

final agRequests = <Future>[];
for (Map item in launchesContent) {
agRequests.add(getAgencyById(item["lsp"]));
}
final allAgencies = await Future.wait(agRequests);

final result = <RocketLaunch>[];
for (int i = 0; i < launchesContent.length; i++) {
result.add(RocketLaunch(
Expand All @@ -68,6 +80,10 @@ class RocketService {
}

static Future<Agency> getAgencyById(String id) async {
if (!(await isConnected())) {
throw StateError("Device offline.");
}

final raw = await http.get("https://launchlibrary.net/1.4/agency/$id");
final agency = json.decode(raw.body)["agencies"][0];
return Agency(
Expand Down
7 changes: 7 additions & 0 deletions lib/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:connectivity/connectivity.dart';

Future<bool> isConnected() async {
final connectivityResult = await (Connectivity().checkConnectivity());
return connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi;
}
20 changes: 20 additions & 0 deletions lib/view/OfflineView.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';

class OfflineView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Icon(Icons.signal_wifi_off, size: 50.0),
Padding(padding: EdgeInsets.all(20.0)),
Text(
"You're offline.",
style: TextStyle(fontSize: 21.0),
textAlign: TextAlign.center,
),
],
);
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ environment:
dependencies:
http: ^0.12.0
url_launcher: ^4.0.1
connectivity: ^0.3.2
flutter:
sdk: flutter

Expand Down

0 comments on commit 32004f1

Please sign in to comment.