diff --git a/assets/img/ic_exos_logo.png b/assets/img/ic_exos_logo.png new file mode 100644 index 0000000..c61f153 Binary files /dev/null and b/assets/img/ic_exos_logo.png differ diff --git a/flutter_01.png b/flutter_01.png new file mode 100644 index 0000000..62ec240 Binary files /dev/null and b/flutter_01.png differ diff --git a/lib/route/home.dart b/lib/route/home.dart index 66c5f22..8dd8dad 100644 --- a/lib/route/home.dart +++ b/lib/route/home.dart @@ -1,8 +1,10 @@ import 'package:escape_earth/bottom_home.dart'; 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/RocketHero.dart'; +import 'package:escape_earth/view/RocketView.dart'; import 'package:flutter/material.dart'; class Home extends StatefulWidget { @@ -26,7 +28,7 @@ class HomeState extends State { children: [ Padding( padding: EdgeInsets.only(bottom: BOTTOM_BAR_HEIGHT), - child: currentFragment, + child: SafeArea(child: currentFragment), ), Align( alignment: Alignment.bottomCenter, @@ -75,7 +77,103 @@ class HomeState extends State { class HomeBody extends StatelessWidget { @override Widget build(BuildContext context) { - // TODO: implement HomeBody - return Container(); + return FutureBuilder( + future: RocketService.getNextLaunch(), + builder: (context, snap) { + 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: [ + Padding( + padding: EdgeInsets.only(top: 30.0), + child: Image.asset("assets/img/ic_exos_logo.png", width: 250.0), + ), + Expanded( + child: Center( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text("NEXT LAUNCH IN:", style: TextStyle(fontSize: 18.0)), + Padding(padding: EdgeInsets.all(10.0)), + Counter( + until: DateTime.parse(snap.data.date), + ), + ], + ))), + Padding( + padding: EdgeInsets.only( + left: 16.0, right: 16.0, bottom: 70.0, top: 16.0), + child: RocketView(launch: snap.data), + ), + ], + ); + }, + ); + } +} + +class Counter extends StatefulWidget { + final DateTime until; + + Counter({ + Key key, + @required this.until, + }) : super(key: key); + + @override + CounterState createState() => CounterState(); +} + +class CounterState extends State { + bool isRunning; + + @override + void initState() { + super.initState(); + isRunning = true; + _run(); + } + + void _run() async { + while (isRunning) { + await Future.delayed(const Duration(seconds: 1)); + setState(() {}); + } + } + + @override + Widget build(BuildContext context) { + return Text( + _calculateString(), + style: TextStyle(fontWeight: FontWeight.bold, fontSize: 43.0), + ); + } + + String _calculateString() { + final now = DateTime.now().millisecondsSinceEpoch; + final diff = widget.until.millisecondsSinceEpoch - now; + + final day = diff ~/ 86400000; + final hours = diff ~/ 3600000 % 24; + final minutes = diff ~/ 60000 % 60; + final seconds = diff ~/ 1000 % 60; + + return "${day}d ${hours}h ${minutes}m ${seconds}s"; + } + + @override + void dispose() { + isRunning = false; + super.dispose(); } } diff --git a/lib/service/RocketService.dart b/lib/service/RocketService.dart index af81bbb..265a8d3 100644 --- a/lib/service/RocketService.dart +++ b/lib/service/RocketService.dart @@ -5,6 +5,19 @@ import 'package:escape_earth/model/RocketLaunch.dart'; import 'package:http/http.dart' as http; class RocketService { + static Future getNextLaunch() async { + 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], + ); + } + static Future> getLaunches({ String query }) async { http.Response rawLaunchesResponse; if (query == null || query == "") { @@ -29,7 +42,7 @@ class RocketService { launchCompany: allAgencies[i], name: launchesContent[i]["name"], date: launchesContent[i]["windowstart"], - videoUrl: launchesContent[i]["vidURLs"] == null ? null : launchesContent[i]["vidURLs"][0], + videoUrl: (launchesContent[i]["vidURLs"] ?? [null])[0], )); } return result; diff --git a/lib/view/NewsView.dart b/lib/view/NewsView.dart index ef7edd7..0cb63b6 100644 --- a/lib/view/NewsView.dart +++ b/lib/view/NewsView.dart @@ -6,12 +6,10 @@ import 'package:escape_earth/localdata.dart'; class NewsList extends StatelessWidget { @override Widget build(BuildContext context) { - return SafeArea( - child: ListView.builder( - itemCount: latestNews.length, - padding: EdgeInsets.only(left: 16.0, right: 16.0), - itemBuilder: (context, i) => NewsItem(news: latestNews[i])), - ); + return ListView.builder( + itemCount: latestNews.length, + padding: EdgeInsets.only(left: 16.0, right: 16.0), + itemBuilder: (context, i) => NewsItem(news: latestNews[i])); } } @@ -72,7 +70,8 @@ class NewsItem extends StatelessWidget { children: [ Icon(Icons.calendar_today, color: Colors.black), Padding(padding: EdgeInsets.all(2.5)), - Text(_dateToCalendarString(news.date), style: TextStyle(color: Colors.black)), + Text(_dateToCalendarString(news.date), + style: TextStyle(color: Colors.black)), ]), ), ], @@ -83,5 +82,6 @@ class NewsItem extends StatelessWidget { ); } - String _dateToCalendarString(DateTime date) => "${date.month}/${date.day}/${date.year}"; + String _dateToCalendarString(DateTime date) => + "${date.month}/${date.day}/${date.year}"; }