diff --git a/lib/arrival_fetch.dart b/lib/arrival_fetch.dart new file mode 100644 index 0000000..f8fd69c --- /dev/null +++ b/lib/arrival_fetch.dart @@ -0,0 +1,49 @@ +import 'dart:convert'; +import 'dart:async'; + +import 'package:http/http.dart' as http; + +Future> fetchArrivals() async { + try { + 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); + // })); + 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'); + } + } catch (error) { + throw Exception('Failed to fetch data: $error'); + } +} + + +class Arrival { + final String timeOfDay; + final int weekDay; + final int tramLine; + final bool direction; + + Arrival({ + required this.timeOfDay, + required this.weekDay, + required this.tramLine, + required this.direction, + }); + + factory Arrival.fromJson(Map json) { + 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, + ); + } +} + diff --git a/lib/albut.dart b/lib/arrival_post.dart similarity index 99% rename from lib/albut.dart rename to lib/arrival_post.dart index f5f1d04..f6ad2bb 100644 --- a/lib/albut.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 4223167..4837f43 100644 --- a/lib/fetcher.dart +++ b/lib/fetcher.dart @@ -1,10 +1,16 @@ import 'package:flutter/material.dart'; -import 'package:witl/home_screen.dart'; -import 'package:witl/input_data.dart'; +import 'package:witl/arrival_fetch.dart'; -class FetchAPI extends StatelessWidget { +class FetchAPI extends StatefulWidget { const FetchAPI({super.key}); + @override + State createState() => _FetchAPIState(); +} + +class _FetchAPIState extends State { + late Future> fetchedArrivals; + @override Widget build(BuildContext context) { return Scaffold( @@ -14,33 +20,71 @@ class FetchAPI extends StatelessWidget { body: Column( mainAxisAlignment: MainAxisAlignment.start, 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("Arrival Time: ${snapshot.data![index].timeOfDay}"), + titleAlignment: ListTileTitleAlignment.center, + subtitle: Text("Week Day: ${weekNumToString(snapshot.data![index].weekDay)}"), + ); + }, + ); + } + }, + ), + ), + ), + const SizedBox(height: 30,), InkWell( - onTap:() { - Navigator.push( - context, - MaterialPageRoute(builder: (context) => const HomeScreen()) - ); + onTap: () { + Navigator.pushNamedAndRemoveUntil(context, '/', (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") + child: const Text("Homescreen"), ), ), + const SizedBox(height: 35,), ], ), ); } + + @override + void initState() { + super.initState(); + 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 {