Some comments never hurt anyone

This commit is contained in:
2023-12-30 23:05:06 +02:00
parent 23218c22d7
commit 7a0bd79e37
7 changed files with 53 additions and 12 deletions

View File

@@ -9,7 +9,7 @@ class Settings extends StatefulWidget {
} }
class _Settings extends State<Settings> { class _Settings extends State<Settings> {
late String selectedTheme = 'Dark'; late String selectedTheme = 'Dark'; // Default app theme
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -20,9 +20,12 @@ class _Settings extends State<Settings> {
body: ListView( body: ListView(
children:[ children:[
// List of Tiles in the Settings Screen
// Starting with App Theme
ListTile( ListTile(
title: const Text("App Theme"), title: const Text("App Theme"),
subtitle: const Text("Sets the theme of the app between: Dark, Light, System Default."), subtitle: const Text("Sets the theme of the app between: Dark, Light, System Default."),
// User Selection
trailing: DropdownButton<String> ( trailing: DropdownButton<String> (
value: selectedTheme, value: selectedTheme,
items: <String>["Light", "Dark", "System Default"].map((String value) { items: <String>["Light", "Dark", "System Default"].map((String value) {
@@ -31,6 +34,7 @@ class _Settings extends State<Settings> {
child: Text(value), child: Text(value),
); );
}).toList(), }).toList(),
// Updating Selection
onChanged: (String? updatedTheme) { onChanged: (String? updatedTheme) {
setState(() { setState(() {
selectedTheme = updatedTheme ?? selectedTheme; selectedTheme = updatedTheme ?? selectedTheme;

View File

@@ -13,9 +13,16 @@ class Arrival {
factory Arrival.fromJson(Map<String, dynamic> json) { factory Arrival.fromJson(Map<String, dynamic> json) {
return Arrival( return Arrival(
// Arrival Time
timeOfDay: json['time_of_day'] as String, timeOfDay: json['time_of_day'] as String,
// Day 1 -> 7 !! Starting from Monday !!
weekDay: json['week_day'] as int, weekDay: json['week_day'] as int,
// Line 1 or 2
tramLine: json['tram_line'] as int, tramLine: json['tram_line'] as int,
// True -> Raml, False -> Victoria
direction: json['direction'] as bool, direction: json['direction'] as bool,
); );
} }

View File

@@ -6,18 +6,16 @@ import 'package:witl/arrival.dart';
Future<List<Arrival>> fetchArrivals() async { Future<List<Arrival>> fetchArrivals() async {
try { try {
// Grabbing http reponse
final response = await http.get(Uri.parse('http://141.144.238.26:48502/arrivals/all')); final response = await http.get(Uri.parse('http://141.144.238.26:48502/arrivals/all'));
if (response.statusCode == 200) { if (response.statusCode == 200) {
// List<dynamic> data = jsonDecode(response.body); // Some JSON fuckery xd
// List<Arrival> arrivals = List<Arrival>.from(data.map((dynamic arrivalJson) {
// return Arrival.fromJson(arrivalJson);
// }));
Iterable I = json.decode(response.body); Iterable I = json.decode(response.body);
List<Arrival> arrivals = List<Arrival>.from(I.map((model)=>Arrival.fromJson(model))); List<Arrival> arrivals = List<Arrival>.from(I.map((model)=>Arrival.fromJson(model)));
return arrivals.reversed.toList(); return arrivals.reversed.toList();
} else { } else {
throw Exception('Failed to load Arrival'); throw Exception('Failed to load Arrivals list');
} }
} catch (error) { } catch (error) {
throw Exception('Failed to fetch data: $error'); throw Exception('Failed to fetch data: $error');

View File

@@ -4,11 +4,14 @@ import 'package:http/http.dart' as http;
import 'package:witl/arrival.dart'; import 'package:witl/arrival.dart';
Future<Arrival> insertArrival(String date, int weekday, int line, bool direction) async { Future<Arrival> insertArrival(String date, int weekday, int line, bool direction) async {
// POST response as JSON
final response = await http.post( final response = await http.post(
Uri.parse('http://141.144.238.26:48502/arrivals/insert'), Uri.parse('http://141.144.238.26:48502/arrivals/insert'),
headers: <String, String> { headers: <String, String> {
// Headers used
'Content-Type': 'application/json; charset=UTF-8', 'Content-Type': 'application/json; charset=UTF-8',
}, },
// Encoding a singular json entry
body: jsonEncode(<String, dynamic> { body: jsonEncode(<String, dynamic> {
'time_of_day': date, 'time_of_day': date,
'week_day': weekday, 'week_day': weekday,

View File

@@ -18,6 +18,7 @@ class _FetchAPIState extends State<FetchAPI> {
appBar: AppBar( appBar: AppBar(
title: const Text("Fetching API"), title: const Text("Fetching API"),
actions: [ actions: [
// Refresh fetched list
IconButton( IconButton(
onPressed: () { onPressed: () {
setState(() { setState(() {
@@ -26,10 +27,15 @@ class _FetchAPIState extends State<FetchAPI> {
}, },
icon: const Icon(Icons.refresh_sharp), icon: const Icon(Icons.refresh_sharp),
), ),
// Spacer
const SizedBox(width: 30,), const SizedBox(width: 30,),
], ],
), ),
// RefreshIndicator is used so that user can pull down
// in order to update instead of pressing the refresh button
body: RefreshIndicator( body: RefreshIndicator(
// Method to Use no argument parenthesis
onRefresh: _refreshData, onRefresh: _refreshData,
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
@@ -41,13 +47,18 @@ class _FetchAPIState extends State<FetchAPI> {
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) { if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator(); return const CircularProgressIndicator();
} else if (snapshot.hasError) { }
else if (snapshot.hasError) {
return Text('Failed to fetch data.\nError: ${snapshot.error}'); return Text('Failed to fetch data.\nError: ${snapshot.error}');
} else { }
else {
return ListView.builder( return ListView.builder(
// List items to display
itemCount: snapshot.data!.length, itemCount: snapshot.data!.length,
// Building them
itemBuilder: (context, index) { itemBuilder: (context, index) {
return ListTile( return ListTile(
// Really should be more informative about what's displayed
title: Text("Arrival Time: ${snapshot.data![index].timeOfDay}"), title: Text("Arrival Time: ${snapshot.data![index].timeOfDay}"),
titleAlignment: ListTileTitleAlignment.center, titleAlignment: ListTileTitleAlignment.center,
subtitle: Text("Week Day: ${weekNumToString(snapshot.data![index].weekDay)}"), subtitle: Text("Week Day: ${weekNumToString(snapshot.data![index].weekDay)}"),
@@ -64,8 +75,12 @@ class _FetchAPIState extends State<FetchAPI> {
), ),
const SizedBox(height: 30,), const SizedBox(height: 30,),
InkWell( InkWell(
// Navigating Home
onTap: () { onTap: () {
Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false); Navigator.pushNamedAndRemoveUntil(
context,
'/',
(route) => false);
}, },
child: Container( child: Container(
padding: const EdgeInsets.all(20.0), padding: const EdgeInsets.all(20.0),
@@ -81,6 +96,7 @@ class _FetchAPIState extends State<FetchAPI> {
Future<void> _refreshData() async { Future<void> _refreshData() async {
setState(() { setState(() {
// Refresh arrivals list by invoking the fetch method
fetchedArrivals = fetchArrivals(); fetchedArrivals = fetchArrivals();
}); });
} }
@@ -131,7 +147,7 @@ showArrivalInfo(BuildContext context, Arrival data) {
], ],
); );
// show the dialog // show the dialogue
showDialog( showDialog(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {

View File

@@ -13,23 +13,32 @@ class HomeScreen extends StatelessWidget {
title: Row( title: Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
// Our Amazing Logo
Image.asset('assets/images/choo.png', Image.asset('assets/images/choo.png',
fit: BoxFit.contain, fit: BoxFit.contain,
height: 64, height: 64,
), ),
// Spacing
const SizedBox(width: 10,), const SizedBox(width: 10,),
// App Title
const Text("WITL"), const Text("WITL"),
// Fill out the area blankly
// So that the icon can be shoved far right
const Expanded(child: SizedBox(),), const Expanded(child: SizedBox(),),
// Navigate to Settings
IconButton( IconButton(
onPressed: () {Navigator.push( onPressed: () {Navigator.push(
context, context,
MaterialPageRoute(builder: (context) => const Settings()) MaterialPageRoute(builder: (context) => const Settings())
);}, );},
icon: const Icon(Icons.settings)) icon: const Icon(Icons.settings)),
// Space from the end
const SizedBox(width: 30,),
] ]
), ),
), ),
@@ -39,12 +48,15 @@ class HomeScreen extends StatelessWidget {
const Row( const Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children:[ children:[
// Our Motto (?)
Text("We may not be able to tell you why.\nBut surely are able to predict when."), Text("We may not be able to tell you why.\nBut surely are able to predict when."),
], ],
), ),
// Spacing
const SizedBox(height: 80), const SizedBox(height: 80),
// To the fetching screen
InkWell( InkWell(
onTap:() { onTap:() {
Navigator.push( Navigator.push(
@@ -60,6 +72,7 @@ class HomeScreen extends StatelessWidget {
const SizedBox(height: 30), const SizedBox(height: 30),
// To Data Input
InkWell( InkWell(
onTap:() { onTap:() {
Navigator.push( Navigator.push(

View File

@@ -159,7 +159,7 @@ class _InputDataState extends State<InputData> {
insertArrival('${now.hour}:${now.minute}:${now.second}', now.weekday, selectedLine, payloadDirection); insertArrival('${now.hour}:${now.minute}:${now.second}', now.weekday, selectedLine, payloadDirection);
//prompting of submission //prompting of submission
showAlertDialog(context, selectedLine, selectedColor, selectedDirection, userFormattedString); showSubmissionSuccess(context, selectedLine, selectedColor, selectedDirection, userFormattedString);
}, },
child: Container( child: Container(