410 lines
14 KiB
Dart
410 lines
14 KiB
Dart
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, file_names, use_build_context_synchronously, library_private_types_in_public_api
|
|
|
|
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:smilet/LoginPage.dart';
|
|
import 'package:smilet/MainMenu/Meny.dart';
|
|
import 'dart:async';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:smilet/User.dart';
|
|
import 'package:weather_icons/weather_icons.dart';
|
|
|
|
Future<List> _getActivities(String schoolCode) async {
|
|
var response = await http.get(Uri.parse(
|
|
'https://group-15-3.pvt.dsv.su.se/activity/available?schoolCode=$schoolCode&username=${user.getUsername()}'));
|
|
if (response.statusCode == 200) {
|
|
// If the server did return a 200 OK response,
|
|
// then parse the JSON.
|
|
return jsonDecode(utf8.decode(response.bodyBytes)) as List<dynamic>;
|
|
} else {
|
|
// If the server did not return a 200 OK response,
|
|
// then throw an exception.
|
|
throw Exception();
|
|
}
|
|
}
|
|
|
|
Future<void> _updateActivity(int id, String username) async {
|
|
var response = await http.get(Uri.parse(
|
|
'https://group-15-3.pvt.dsv.su.se/activity/update?id=$id&username=$username'));
|
|
if (response.statusCode == 200) {
|
|
// If the server did return a 200 OK response,
|
|
// then parse the JSON.
|
|
} else {
|
|
// If the server did not return a 200 OK response,
|
|
// then throw an exception.
|
|
throw Exception();
|
|
}
|
|
}
|
|
|
|
class Participate extends StatefulWidget {
|
|
final User user;
|
|
const Participate({super.key, required this.user});
|
|
|
|
@override
|
|
_ParticipateState createState() => _ParticipateState();
|
|
}
|
|
|
|
class _ParticipateState extends State<Participate> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
var futureBuilder = FutureBuilder(
|
|
future: _getActivities(user.getSchoolCode().toString()),
|
|
builder: (BuildContext context, AsyncSnapshot snapshot) {
|
|
switch (snapshot.connectionState) {
|
|
case ConnectionState.none:
|
|
return Text('connectionstate none');
|
|
case ConnectionState.waiting:
|
|
return _emptyActivities(context, snapshot);
|
|
default:
|
|
if (snapshot.hasError) {
|
|
return Text('snapshot error ${snapshot.error.toString()}');
|
|
} else if (snapshot.hasData) {
|
|
return _generateActivities(context, snapshot);
|
|
} else {
|
|
return Text('snapshot null');
|
|
}
|
|
}
|
|
},
|
|
);
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
centerTitle: true,
|
|
title: Image.asset(
|
|
'assets/images/ml_logo.png',
|
|
height: 40,
|
|
),
|
|
),
|
|
body: Center(
|
|
child: Column(children: <Widget>[
|
|
SizedBox(height: 15),
|
|
Text(
|
|
'${schoolDetails["institution"].toString().toUpperCase()}s aktiviteter',
|
|
style: TextStyle(
|
|
fontSize: 25,
|
|
fontWeight: FontWeight.bold,
|
|
)),
|
|
Expanded(child: futureBuilder),
|
|
])));
|
|
}
|
|
|
|
Widget _generateActivities(BuildContext context, AsyncSnapshot snapshot) {
|
|
List<dynamic> items = snapshot.data;
|
|
if (items.isEmpty) {
|
|
return Center(
|
|
child: Text(
|
|
"Inga kommande aktiviteter!",
|
|
style: TextStyle(fontSize: 20),
|
|
),
|
|
);
|
|
} else {
|
|
return ListView.separated(
|
|
padding: const EdgeInsets.all(8),
|
|
itemCount: items.length + 1,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
if (index == 0) {
|
|
return Container();
|
|
} else {
|
|
Map map = items[index - 1];
|
|
int id = map["id"];
|
|
String title = map["title"].toString();
|
|
String place = map["place"].toString();
|
|
int dayIndex = 8;
|
|
int monthIndex = 5;
|
|
String dateTime = map["dateTime"];
|
|
|
|
// Filtrera bort 0 från månad
|
|
if (dateTime.substring(5, 7).startsWith("0")) {
|
|
monthIndex = 6;
|
|
}
|
|
|
|
// Filtrera bort 0 från dag
|
|
if (dateTime.substring(8, 10).startsWith("0")) {
|
|
dayIndex = 9;
|
|
}
|
|
|
|
String date =
|
|
"${dateTime.substring(dayIndex, 10)}/${dateTime.substring(monthIndex, 7)}";
|
|
String time = dateTime.substring(11, 16);
|
|
String creatorUser = map["creatorUser"].toString();
|
|
int amountRegistered = map["amountRegistered"];
|
|
String schoolCode = map["schoolCode"];
|
|
|
|
return Container(
|
|
padding: const EdgeInsets.all(8),
|
|
decoration: BoxDecoration(
|
|
color: Color.fromARGB(255, 59, 154, 233),
|
|
borderRadius: BorderRadius.all(Radius.circular(20)),
|
|
),
|
|
child: Column(
|
|
children: <Widget>[
|
|
SizedBox(height: 5),
|
|
|
|
// Weather + row med eventicon
|
|
_weather(schoolCode, dateTime),
|
|
|
|
SizedBox(height: 5),
|
|
Divider(
|
|
height: 1,
|
|
color: Colors.white,
|
|
),
|
|
SizedBox(height: 5),
|
|
Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: Text(
|
|
title,
|
|
textAlign: TextAlign.left,
|
|
style: TextStyle(
|
|
fontSize: 30,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 5),
|
|
child: Text(
|
|
date,
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(
|
|
fontSize: 30,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: Text(
|
|
place,
|
|
textAlign: TextAlign.left,
|
|
style: TextStyle(fontSize: 20, color: Colors.white),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 5),
|
|
child: Text(
|
|
time,
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(fontSize: 20, color: Colors.white),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
SizedBox(
|
|
height: 5,
|
|
),
|
|
Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: Text(
|
|
"Skapare: $creatorUser",
|
|
textAlign: TextAlign.left,
|
|
style: TextStyle(fontSize: 15, color: Colors.white),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 5),
|
|
child: Text(
|
|
"Antal deltagare: ${amountRegistered.toString()}",
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(fontSize: 15, color: Colors.white),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
SizedBox(
|
|
height: 5,
|
|
),
|
|
Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: ElevatedButton(
|
|
style: TextButton.styleFrom(
|
|
backgroundColor: Color.fromARGB(255, 2, 205, 93),
|
|
),
|
|
child: Text(
|
|
"Anmäl",
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
onPressed: () async {
|
|
await _updateActivity(id, user.getUsername());
|
|
setState(() {});
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => Meny(
|
|
user: user,
|
|
schoolDetails: schoolDetails,
|
|
)));
|
|
}),
|
|
),
|
|
],
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
},
|
|
separatorBuilder: (BuildContext context, int index) => const Divider(),
|
|
);
|
|
}
|
|
}
|
|
|
|
Widget _emptyActivities(BuildContext context, AsyncSnapshot snapshot) {
|
|
return Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
Future<Map<String, dynamic>> _checkApi(String five) async {
|
|
var response = await http.get(Uri.parse(five));
|
|
if (response.statusCode == 200) {
|
|
// If the server did return a 200 OK response,
|
|
// then parse the JSON.
|
|
return jsonDecode(response.body) as Map<String, dynamic>;
|
|
} else {
|
|
// If the server did not return a 200 OK response,
|
|
// then throw an exception.
|
|
throw Exception;
|
|
}
|
|
}
|
|
|
|
Widget _weather(String schoolCode, String dateTime) {
|
|
String fiveSthlm =
|
|
'https://api.openweathermap.org/data/2.5/forecast?lat=59.334591&lon=18.063240&appid=cc1aa0082d9d3ec25f5d1007e1014725&units=metric';
|
|
String fiveUppsala =
|
|
'https://api.openweathermap.org/data/2.5/forecast?lat=59.858227&lon=17.632252&appid=cc1aa0082d9d3ec25f5d1007e1014725&units=metric';
|
|
String schoolUrl;
|
|
if ((schoolCode == 'dXUtZGF0YQ') || (schoolCode == 'dXUtYmlv')) {
|
|
schoolUrl = fiveUppsala;
|
|
} else {
|
|
schoolUrl = fiveSthlm;
|
|
}
|
|
var weatherBuilder = FutureBuilder(
|
|
future: _checkApi(schoolUrl),
|
|
builder: (BuildContext contextTwo, AsyncSnapshot snapshotTwo) {
|
|
switch (snapshotTwo.connectionState) {
|
|
case ConnectionState.none:
|
|
return Text('none');
|
|
case ConnectionState.waiting:
|
|
return Text('Laddar');
|
|
default:
|
|
if (snapshotTwo.hasError) {
|
|
return Text('error'); //${snapshot.error}
|
|
} else if (snapshotTwo.hasData) {
|
|
return _weatherBuilder(contextTwo, snapshotTwo, dateTime);
|
|
} else {
|
|
return Text('null');
|
|
}
|
|
}
|
|
},
|
|
);
|
|
return weatherBuilder;
|
|
}
|
|
|
|
Widget _weatherBuilder(
|
|
BuildContext contextTwo, AsyncSnapshot snapshotTwo, String dateTime) {
|
|
Map<String, dynamic> itemsTwo = snapshotTwo.data;
|
|
List<dynamic> list = [];
|
|
String temp = '';
|
|
String weather = '';
|
|
String weatherCloud = '';
|
|
|
|
for (int i = 0; i < itemsTwo["cnt"]; i++) {
|
|
int difference = DateTime.parse(itemsTwo["list"][i]["dt_txt"])
|
|
.difference(DateTime.parse(dateTime))
|
|
.inHours;
|
|
if (difference <= 1 && difference >= -1) {
|
|
list.add(itemsTwo["list"][i]);
|
|
}
|
|
}
|
|
|
|
if (list.isNotEmpty) {
|
|
temp = "${list[0]["main"]["temp"].round()}\u2103";
|
|
weather = list[0]["weather"][0]["main"];
|
|
weatherCloud = list[0]["weather"][0]["description"];
|
|
}
|
|
|
|
IconData icon;
|
|
Color color;
|
|
if (weather == "Clear") {
|
|
icon = Icons.sunny;
|
|
color = const Color.fromARGB(255, 255, 230, 6);
|
|
} else if (weather == "Rain") {
|
|
icon = Icons.cloudy_snowing;
|
|
color = Color.fromARGB(255, 2, 62, 166);
|
|
} else if (weather == "Thunderstorm") {
|
|
icon = Icons.thunderstorm;
|
|
color = Color.fromARGB(255, 2, 62, 166);
|
|
} else if (weather == "Snow") {
|
|
icon = Icons.snowing;
|
|
color = Colors.white;
|
|
} else if (weather == "Drizzle") {
|
|
icon = Icons.water_drop;
|
|
color = Color.fromARGB(255, 2, 62, 166);
|
|
} else {
|
|
if (weatherCloud.contains('few')) {
|
|
icon = WeatherIcons.day_cloudy;
|
|
color = Colors.white;
|
|
} else if (weatherCloud.contains('scattered')) {
|
|
icon = WeatherIcons.cloud;
|
|
color = Colors.white;
|
|
} else if (weatherCloud.contains('broken')) {
|
|
icon = WeatherIcons.cloudy;
|
|
color = Colors.white;
|
|
} else {
|
|
icon = Icons.cloud;
|
|
color = Colors.white;
|
|
}
|
|
}
|
|
|
|
if (list.isNotEmpty) {
|
|
return Row(
|
|
children: <Widget>[
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 5),
|
|
child: Icon(
|
|
Icons.event,
|
|
color: Color.fromARGB(255, 254, 169, 72),
|
|
)),
|
|
Expanded(
|
|
child: Text(
|
|
temp,
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(fontSize: 20, color: Colors.white),
|
|
)),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 10, left: 10),
|
|
child: Icon(
|
|
icon,
|
|
color: color,
|
|
size: 30,
|
|
))
|
|
],
|
|
);
|
|
} else {
|
|
return Row(children: <Widget>[
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 5),
|
|
child: Icon(
|
|
Icons.event,
|
|
color: Color.fromARGB(255, 254, 169, 72),
|
|
)),
|
|
Expanded(
|
|
child: Text(
|
|
'',
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(fontSize: 20, color: Colors.white),
|
|
)),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 10, left: 10),
|
|
child: Icon(
|
|
Icons.error,
|
|
color: Color.fromARGB(255, 249, 138, 11),
|
|
size: 30,
|
|
))
|
|
]);
|
|
}
|
|
}
|
|
}
|