From 3dcb8215f28fc164ccdd40bf4ee02fb3632d1413 Mon Sep 17 00:00:00 2001 From: Supermjork Date: Tue, 26 Dec 2023 18:59:07 +0200 Subject: [PATCH 1/8] Updated navigation --- lib/fetcher.dart | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/lib/fetcher.dart b/lib/fetcher.dart index 4223167..eb5894b 100644 --- a/lib/fetcher.dart +++ b/lib/fetcher.dart @@ -1,6 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:witl/home_screen.dart'; -import 'package:witl/input_data.dart'; class FetchAPI extends StatelessWidget { const FetchAPI({super.key}); @@ -11,33 +9,19 @@ class FetchAPI extends StatelessWidget { appBar: AppBar( title: const Text("Fetching API"), ), - body: Column( - mainAxisAlignment: MainAxisAlignment.start, + body: ListView( children: [ InkWell( - onTap:() { - Navigator.push( + onTap: () { + Navigator.pushNamedAndRemoveUntil( context, - MaterialPageRoute(builder: (context) => const HomeScreen()) - ); + '/', + (route) => false + ); }, child: Container( - padding: const EdgeInsets.all(20.0), - child: const Text("To Homescreen") - ), - ), - const SizedBox(height: 20), - InkWell( - onTap:() { - Navigator.push( - context, - MaterialPageRoute(builder: (context) => const InputData()) - ); - }, - child: Container( - padding: const EdgeInsets.all(20.0), - child: const Text("To Input Data") - ), + padding: const EdgeInsets.all(20.0), + child: const Text("Homescreen")), ), ], ), From 70895a74c52a63cb51709569540595020f3fb7eb Mon Sep 17 00:00:00 2001 From: Supermjork Date: Tue, 26 Dec 2023 19:23:43 +0200 Subject: [PATCH 2/8] Fetching but Type Conversion Err --- lib/album_fetch.dart | 53 +++++++++++++++++++++++++++++ lib/{albut.dart => album_post.dart} | 0 lib/fetcher.dart | 28 ++++++++++++++- 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 lib/album_fetch.dart rename lib/{albut.dart => album_post.dart} (100%) diff --git a/lib/album_fetch.dart b/lib/album_fetch.dart new file mode 100644 index 0000000..658c896 --- /dev/null +++ b/lib/album_fetch.dart @@ -0,0 +1,53 @@ + +import 'dart:convert'; +import 'dart:async'; + +import 'package:http/http.dart' as http; + +Future fetchArrivals() async { + final response = await http + .get(Uri.parse(DB_URL)); + + if (response.statusCode == 200) { + // If the server did return a 200 OK response, + // then parse the JSON. + return FetchAlbum.fromJson(jsonDecode(response.body) as Map); + } else { + // If the server did not return a 200 OK response, + // then throw an exception. + throw Exception('Failed to load album'); + } +} + +class FetchAlbum { + final String timeOfDay; + final int weekDay; + final String tramLine; + final bool direction; + + const FetchAlbum({ + required this.timeOfDay, + required this.weekDay, + required this.tramLine, + required this.direction, + }); + + factory FetchAlbum.fromJson(Map json) { + return switch (json) { + { + 'time_of_day': String jsonTime, + 'week_day': int jsonDay, + 'tram_line': String jsonTramLine, + 'direction': bool jsonDirec, + } => + FetchAlbum( + // Class : json declaration + timeOfDay: jsonTime, + weekDay: jsonDay, + tramLine: jsonTramLine, + direction: jsonDirec, + ), + _ => throw const FormatException('Failed to load album.'), + }; + } +} diff --git a/lib/albut.dart b/lib/album_post.dart similarity index 100% rename from lib/albut.dart rename to lib/album_post.dart diff --git a/lib/fetcher.dart b/lib/fetcher.dart index eb5894b..9238d39 100644 --- a/lib/fetcher.dart +++ b/lib/fetcher.dart @@ -1,8 +1,22 @@ import 'package:flutter/material.dart'; +import 'package:witl/album_fetch.dart'; -class FetchAPI extends StatelessWidget { +class FetchAPI extends StatefulWidget { const FetchAPI({super.key}); + @override + State createState() => _FetchAPIState(); +} + +class _FetchAPIState extends State { + late Future fetchedAlbum; + + @override + void initState() { + super.initState(); + fetchedAlbum = fetchArrivals(); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -11,6 +25,18 @@ class FetchAPI extends StatelessWidget { ), body: ListView( children: [ + FutureBuilder ( + future: fetchedAlbum, + builder: (context, snapshot) { + if (snapshot.hasData) { + return Text(snapshot.data!.timeOfDay); + } else if (snapshot.hasError) { + return Text('${snapshot.error}'); + } + + return const CircularProgressIndicator(); + }, + ), InkWell( onTap: () { Navigator.pushNamedAndRemoveUntil( From 60d890b24a7ca7fff50aae2c56474a669fcdfb00 Mon Sep 17 00:00:00 2001 From: Supermjork Date: Tue, 26 Dec 2023 19:50:22 +0200 Subject: [PATCH 3/8] DAMN FORMATTING --- lib/album_fetch.dart | 28 ++++++++++++++++------------ lib/fetcher.dart | 39 ++++++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/lib/album_fetch.dart b/lib/album_fetch.dart index 658c896..d26ceab 100644 --- a/lib/album_fetch.dart +++ b/lib/album_fetch.dart @@ -1,53 +1,57 @@ - import 'dart:convert'; import 'dart:async'; import 'package:http/http.dart' as http; -Future fetchArrivals() async { +Future> fetchArrivals() async { final response = await http - .get(Uri.parse(DB_URL)); + .get(Uri.parse('http://141.144.238.26:48502/arrivals/all')); if (response.statusCode == 200) { // If the server did return a 200 OK response, // then parse the JSON. - return FetchAlbum.fromJson(jsonDecode(response.body) as Map); + List data = jsonDecode(response.body); + List posts = List.from(data.map((dynamic postJson) { + return Arrival.fromJson(postJson); + })); } else { // If the server did not return a 200 OK response, // then throw an exception. - throw Exception('Failed to load album'); + throw Exception('Failed to load Arrival'); } + + throw Exception("API Unavailable"); } -class FetchAlbum { +class Arrival { final String timeOfDay; final int weekDay; - final String tramLine; + final int tramLine; final bool direction; - const FetchAlbum({ + const Arrival({ required this.timeOfDay, required this.weekDay, required this.tramLine, required this.direction, }); - factory FetchAlbum.fromJson(Map json) { + factory Arrival.fromJson(Map json) { return switch (json) { { 'time_of_day': String jsonTime, 'week_day': int jsonDay, - 'tram_line': String jsonTramLine, + 'tram_line': int jsonTramLine, 'direction': bool jsonDirec, } => - FetchAlbum( + Arrival( // Class : json declaration timeOfDay: jsonTime, weekDay: jsonDay, tramLine: jsonTramLine, direction: jsonDirec, ), - _ => throw const FormatException('Failed to load album.'), + _ => throw const FormatException('Failed to load Arrival.'), }; } } diff --git a/lib/fetcher.dart b/lib/fetcher.dart index 9238d39..638b017 100644 --- a/lib/fetcher.dart +++ b/lib/fetcher.dart @@ -2,19 +2,19 @@ import 'package:flutter/material.dart'; import 'package:witl/album_fetch.dart'; class FetchAPI extends StatefulWidget { - const FetchAPI({super.key}); + const FetchAPI({Key? key}) : super(key: key); @override State createState() => _FetchAPIState(); } class _FetchAPIState extends State { - late Future fetchedAlbum; + late Future> fetchedArrivals; @override void initState() { super.initState(); - fetchedAlbum = fetchArrivals(); + fetchedArrivals = fetchArrivals(); } @override @@ -25,32 +25,37 @@ class _FetchAPIState extends State { ), body: ListView( children: [ - FutureBuilder ( - future: fetchedAlbum, + FutureBuilder>( + future: fetchedArrivals, builder: (context, snapshot) { - if (snapshot.hasData) { - return Text(snapshot.data!.timeOfDay); + if (snapshot.connectionState == ConnectionState.waiting) { + return const CircularProgressIndicator(); } else if (snapshot.hasError) { - return Text('${snapshot.error}'); + return Text('Failed to fetch data.\nError: ${snapshot.error}'); + } else { + return ListView.builder( + itemCount: snapshot.data!.length, + itemBuilder: (context, index) { + return ListTile( + title: Text(snapshot.data![index].timeOfDay), + ); + }, + ); } - - return const CircularProgressIndicator(); }, ), InkWell( onTap: () { Navigator.pushNamedAndRemoveUntil( - context, - '/', - (route) => false - ); + context, '/', (route) => false); }, child: Container( - padding: const EdgeInsets.all(20.0), - child: const Text("Homescreen")), + padding: const EdgeInsets.all(20.0), + child: const Text("Homescreen"), + ), ), ], ), ); } -} \ No newline at end of file +} From 09a8774423bcbfb697f902b7d3dbff1503e43616 Mon Sep 17 00:00:00 2001 From: Supermjork Date: Tue, 26 Dec 2023 20:23:27 +0200 Subject: [PATCH 4/8] embty tile --- lib/album_fetch.dart | 54 ++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/lib/album_fetch.dart b/lib/album_fetch.dart index d26ceab..72ddd53 100644 --- a/lib/album_fetch.dart +++ b/lib/album_fetch.dart @@ -4,32 +4,31 @@ import 'dart:async'; import 'package:http/http.dart' as http; Future> fetchArrivals() async { - final response = await http - .get(Uri.parse('http://141.144.238.26:48502/arrivals/all')); + try { + final response = await http.get(Uri.parse('http://141.144.238.26:48502/arrivals/all')); - if (response.statusCode == 200) { - // If the server did return a 200 OK response, - // then parse the JSON. - List data = jsonDecode(response.body); - List posts = List.from(data.map((dynamic postJson) { - return Arrival.fromJson(postJson); - })); - } else { - // If the server did not return a 200 OK response, - // then throw an exception. - throw Exception('Failed to load Arrival'); + if (response.statusCode == 200) { + List data = jsonDecode(response.body); + List arrivals = List.from(data.map((dynamic arrivalJson) { + return Arrival.fromJson(arrivalJson); + })); + return arrivals; + } else { + throw Exception('Failed to load Arrival'); + } + } catch (error) { + throw Exception('Failed to fetch data: $error'); } - - throw Exception("API Unavailable"); } + class Arrival { final String timeOfDay; final int weekDay; final int tramLine; final bool direction; - const Arrival({ + Arrival({ required this.timeOfDay, required this.weekDay, required this.tramLine, @@ -37,21 +36,12 @@ class Arrival { }); factory Arrival.fromJson(Map json) { - return switch (json) { - { - 'time_of_day': String jsonTime, - 'week_day': int jsonDay, - 'tram_line': int jsonTramLine, - 'direction': bool jsonDirec, - } => - Arrival( - // Class : json declaration - timeOfDay: jsonTime, - weekDay: jsonDay, - tramLine: jsonTramLine, - direction: jsonDirec, - ), - _ => throw const FormatException('Failed to load Arrival.'), - }; + return Arrival( + timeOfDay: json['time_of_day'] as String, + weekDay: json['week_day'] as int, + tramLine: json['tram_line'] as int, + direction: json['direction'] as bool, + ); } } + From e9b9e49509daa58b6fa3fab24b588f1412403a03 Mon Sep 17 00:00:00 2001 From: linlyboi Date: Tue, 26 Dec 2023 21:08:23 +0200 Subject: [PATCH 5/8] ListView construction properly done --- lib/fetcher.dart | 51 ++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/fetcher.dart b/lib/fetcher.dart index 638b017..3f447f8 100644 --- a/lib/fetcher.dart +++ b/lib/fetcher.dart @@ -23,31 +23,34 @@ class _FetchAPIState extends State { appBar: AppBar( title: const Text("Fetching API"), ), - body: ListView( - children: [ - FutureBuilder>( - future: fetchedArrivals, - builder: (context, snapshot) { - if (snapshot.connectionState == ConnectionState.waiting) { - return const CircularProgressIndicator(); - } else if (snapshot.hasError) { - return Text('Failed to fetch data.\nError: ${snapshot.error}'); - } else { - return ListView.builder( - itemCount: snapshot.data!.length, - itemBuilder: (context, index) { - return ListTile( - title: Text(snapshot.data![index].timeOfDay), + body: Row( + children: [ + Expanded( + child: SizedBox( + child: FutureBuilder>( + future: fetchedArrivals, + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + return const CircularProgressIndicator(); + } else if (snapshot.hasError) { + return Text('Failed to fetch data.\nError: ${snapshot.error}'); + } else { + return ListView.builder( + itemCount: snapshot.data!.length, + itemBuilder: (context, index) { + return ListTile( + title: Text(snapshot.data![index].timeOfDay), + ); + }, ); - }, - ); - } - }, + } + }, + ), + ), ), InkWell( onTap: () { - Navigator.pushNamedAndRemoveUntil( - context, '/', (route) => false); + Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false); }, child: Container( padding: const EdgeInsets.all(20.0), @@ -58,4 +61,10 @@ class _FetchAPIState extends State { ), ); } + + @override + void initState() { + super.initState(); + fetchedArrivals = fetchArrivals(); + } } From f56650722826c2deff36e34a7ed3943191796605 Mon Sep 17 00:00:00 2001 From: linlyboi Date: Tue, 26 Dec 2023 21:08:42 +0200 Subject: [PATCH 6/8] unsure if this nuke was needed but it cleaner now --- lib/album_fetch.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/album_fetch.dart b/lib/album_fetch.dart index 72ddd53..f8fd69c 100644 --- a/lib/album_fetch.dart +++ b/lib/album_fetch.dart @@ -8,10 +8,12 @@ Future> fetchArrivals() async { final response = await http.get(Uri.parse('http://141.144.238.26:48502/arrivals/all')); if (response.statusCode == 200) { - List data = jsonDecode(response.body); - List arrivals = List.from(data.map((dynamic arrivalJson) { - return Arrival.fromJson(arrivalJson); - })); + // List data = jsonDecode(response.body); + // List arrivals = List.from(data.map((dynamic arrivalJson) { + // return Arrival.fromJson(arrivalJson); + // })); + Iterable I = json.decode(response.body); + List arrivals = List.from(I.map((model)=>Arrival.fromJson(model))); return arrivals; } else { throw Exception('Failed to load Arrival'); From e7f29132258af07400d75a6ac2a81706df8f707a Mon Sep 17 00:00:00 2001 From: linlyboi Date: Tue, 26 Dec 2023 21:09:30 +0200 Subject: [PATCH 7/8] yeah --- lib/fetcher.dart | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/fetcher.dart b/lib/fetcher.dart index 3f447f8..de30f0e 100644 --- a/lib/fetcher.dart +++ b/lib/fetcher.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:witl/album_fetch.dart'; class FetchAPI extends StatefulWidget { - const FetchAPI({Key? key}) : super(key: key); + const FetchAPI({super.key}); @override State createState() => _FetchAPIState(); @@ -11,12 +11,6 @@ class FetchAPI extends StatefulWidget { class _FetchAPIState extends State { late Future> fetchedArrivals; - @override - void initState() { - super.initState(); - fetchedArrivals = fetchArrivals(); - } - @override Widget build(BuildContext context) { return Scaffold( From d25327602a3ad5459acb2c099a724ed87df14c3b Mon Sep 17 00:00:00 2001 From: Supermjork Date: Tue, 26 Dec 2023 21:49:27 +0200 Subject: [PATCH 8/8] Fetch fetches, layout bettered --- lib/{album_fetch.dart => arrival_fetch.dart} | 0 lib/{album_post.dart => arrival_post.dart} | 1 + lib/fetcher.dart | 34 +++++++++++++++++--- lib/input_data.dart | 2 +- 4 files changed, 32 insertions(+), 5 deletions(-) rename lib/{album_fetch.dart => arrival_fetch.dart} (100%) rename lib/{album_post.dart => arrival_post.dart} (99%) diff --git a/lib/album_fetch.dart b/lib/arrival_fetch.dart similarity index 100% rename from lib/album_fetch.dart rename to lib/arrival_fetch.dart diff --git a/lib/album_post.dart b/lib/arrival_post.dart similarity index 99% rename from lib/album_post.dart rename to lib/arrival_post.dart index f5f1d04..f6ad2bb 100644 --- a/lib/album_post.dart +++ b/lib/arrival_post.dart @@ -1,4 +1,5 @@ import 'dart:convert'; + import 'package:http/http.dart' as http; Future createAlbum(String title) async { diff --git a/lib/fetcher.dart b/lib/fetcher.dart index de30f0e..4837f43 100644 --- a/lib/fetcher.dart +++ b/lib/fetcher.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:witl/album_fetch.dart'; +import 'package:witl/arrival_fetch.dart'; class FetchAPI extends StatefulWidget { const FetchAPI({super.key}); @@ -17,8 +17,9 @@ class _FetchAPIState extends State { appBar: AppBar( title: const Text("Fetching API"), ), - body: Row( - children: [ + body: Column( + mainAxisAlignment: MainAxisAlignment.start, + children: [ Expanded( child: SizedBox( child: FutureBuilder>( @@ -33,7 +34,9 @@ class _FetchAPIState extends State { itemCount: snapshot.data!.length, itemBuilder: (context, index) { return ListTile( - title: Text(snapshot.data![index].timeOfDay), + title: Text("Arrival Time: ${snapshot.data![index].timeOfDay}"), + titleAlignment: ListTileTitleAlignment.center, + subtitle: Text("Week Day: ${weekNumToString(snapshot.data![index].weekDay)}"), ); }, ); @@ -42,6 +45,7 @@ class _FetchAPIState extends State { ), ), ), + const SizedBox(height: 30,), InkWell( onTap: () { Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false); @@ -51,6 +55,7 @@ class _FetchAPIState extends State { child: const Text("Homescreen"), ), ), + const SizedBox(height: 35,), ], ), ); @@ -62,3 +67,24 @@ class _FetchAPIState extends State { fetchedArrivals = fetchArrivals(); } } + +String weekNumToString(int weekday) { + switch (weekday) { + case 1: + return "Monday"; + case 2: + return "Tuesday"; + case 3: + return "Wednesday"; + case 4: + return "Thursday"; + case 5: + return "Friday"; + case 6: + return "Saturday"; + case 7: + return "Sunday"; + } + + throw "AAAAAAAAA"; +} \ No newline at end of file diff --git a/lib/input_data.dart b/lib/input_data.dart index 539adb6..e611882 100644 --- a/lib/input_data.dart +++ b/lib/input_data.dart @@ -5,7 +5,7 @@ class InputData extends StatefulWidget { const InputData({super.key}); @override - _InputDataState createState() => _InputDataState(); + State createState() => _InputDataState(); } class _InputDataState extends State {