490 lines
17 KiB
Dart
490 lines
17 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 'package:flutter/material.dart';
|
|
import 'package:smilet/Aktivitet/NewActivity.dart';
|
|
import 'package:smilet/Aktivitet/Participate.dart';
|
|
import 'package:smilet/LoginPage.dart';
|
|
import 'package:smilet/User.dart';
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:weather_icons/weather_icons.dart';
|
|
|
|
class Activity extends StatefulWidget {
|
|
final String schoolName;
|
|
final User user;
|
|
const Activity({super.key, required this.schoolName, required this.user});
|
|
|
|
@override
|
|
_ActivityState createState() => _ActivityState();
|
|
}
|
|
|
|
class _ActivityState extends State<Activity> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
var futureBuilder = FutureBuilder(
|
|
future: _getActivities(user.getUsername().toString()),
|
|
builder: (BuildContext context, AsyncSnapshot snapshot) {
|
|
switch (snapshot.connectionState) {
|
|
case ConnectionState.none:
|
|
return _emptyActivities(context, snapshot);
|
|
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 _emptyActivities(context, snapshot);
|
|
}
|
|
}
|
|
},
|
|
);
|
|
|
|
return Center(
|
|
child: Column(children: <Widget>[
|
|
SizedBox(height: 15),
|
|
Text('Dina kommande aktiviteter',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 25,
|
|
fontWeight: FontWeight.bold,
|
|
)),
|
|
Expanded(child: futureBuilder),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(10.0),
|
|
child: SizedBox(
|
|
height: 70,
|
|
width: 150,
|
|
child: ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => NewActivity(user: user)));
|
|
},
|
|
child: Text('Skapa aktivitet')),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(10.0),
|
|
child: SizedBox(
|
|
height: 70,
|
|
width: 160,
|
|
child: ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => Participate(
|
|
user: user,
|
|
)));
|
|
},
|
|
child: Text('Delta i aktivitet'))),
|
|
)
|
|
],
|
|
),
|
|
]));
|
|
}
|
|
|
|
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 ShaderMask(
|
|
shaderCallback: (Rect rect) {
|
|
return LinearGradient(
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
colors: [
|
|
Colors.green,
|
|
Colors.transparent,
|
|
Colors.transparent,
|
|
Colors.green
|
|
],
|
|
stops: [0.0, 0.01, 0.9, 0.99],
|
|
).createShader(rect);
|
|
},
|
|
blendMode: BlendMode.dstOut,
|
|
child: 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, 255, 255, 255),
|
|
borderRadius: BorderRadius.all(Radius.circular(20)),
|
|
),
|
|
child: Column(
|
|
children: <Widget>[
|
|
SizedBox(height: 5),
|
|
|
|
// Väderapi row, anropar metod som anropar metoder = return Row()
|
|
_weather(schoolCode, dateTime),
|
|
SizedBox(height: 5),
|
|
Divider(
|
|
height: 1,
|
|
color: Colors.grey,
|
|
),
|
|
SizedBox(height: 5),
|
|
Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: Text(
|
|
title,
|
|
textAlign: TextAlign.left,
|
|
style: TextStyle(
|
|
fontSize: 30,
|
|
fontWeight: FontWeight.bold,
|
|
color: const Color.fromARGB(255, 0, 0, 0)),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 10),
|
|
child: Text(
|
|
date,
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(
|
|
fontSize: 30,
|
|
fontWeight: FontWeight.bold,
|
|
color: const Color.fromARGB(255, 0, 0, 0)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: Text(
|
|
place,
|
|
textAlign: TextAlign.left,
|
|
style: TextStyle(
|
|
fontSize: 20,
|
|
color: const Color.fromARGB(255, 0, 0, 0)),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 10),
|
|
child: Text(
|
|
time,
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(
|
|
fontSize: 20,
|
|
color: const Color.fromARGB(255, 0, 0, 0)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
SizedBox(
|
|
height: 5,
|
|
),
|
|
Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: Text(
|
|
"Skapare: $creatorUser",
|
|
textAlign: TextAlign.left,
|
|
style: TextStyle(
|
|
fontSize: 15,
|
|
color: const Color.fromARGB(255, 0, 0, 0)),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 10),
|
|
child: Text(
|
|
"Antal deltagare: ${amountRegistered.toString()}",
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(
|
|
fontSize: 15,
|
|
color: const Color.fromARGB(255, 0, 0, 0)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
SizedBox(
|
|
height: 5,
|
|
),
|
|
Row(
|
|
children: <Widget>[
|
|
if (creatorUser != user.getUsername())
|
|
Expanded(
|
|
child: ElevatedButton(
|
|
style: TextButton.styleFrom(
|
|
backgroundColor:
|
|
Color.fromARGB(255, 249, 138, 11),
|
|
),
|
|
child: Text(
|
|
"Avanmäl",
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
onPressed: () async {
|
|
await _removeUserFromActivity(
|
|
id, user.getUsername());
|
|
setState(() {});
|
|
}),
|
|
),
|
|
if (creatorUser == user.getUsername())
|
|
Expanded(
|
|
child: ElevatedButton(
|
|
style: TextButton.styleFrom(
|
|
backgroundColor:
|
|
Color.fromARGB(255, 233, 52, 52),
|
|
),
|
|
child: Text(
|
|
"Ta bort aktivitet",
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
onPressed: () async {
|
|
await _removeActivity(id);
|
|
setState(() {});
|
|
}),
|
|
),
|
|
],
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
},
|
|
separatorBuilder: (BuildContext context, int index) =>
|
|
const Divider(),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
Widget _emptyActivities(BuildContext context, AsyncSnapshot snapshot) {
|
|
return Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
Future<List> _getActivities(String username) async {
|
|
var response = await http.get(
|
|
Uri.parse('https://group-15-3.pvt.dsv.su.se/activity/user/$username'));
|
|
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> _removeUserFromActivity(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&remove=true'));
|
|
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();
|
|
}
|
|
}
|
|
|
|
Future<void> _removeActivity(int id) async {
|
|
var response = await http
|
|
.get(Uri.parse('https://group-15-3.pvt.dsv.su.se/activity/remove/$id'));
|
|
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();
|
|
}
|
|
}
|
|
|
|
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;
|
|
MaterialColor color;
|
|
if (weather == "Clear") {
|
|
icon = Icons.sunny;
|
|
color = Colors.yellow;
|
|
} else if (weather == "Rain") {
|
|
icon = Icons.cloudy_snowing;
|
|
color = Colors.blue;
|
|
} else if (weather == "Thunderstorm") {
|
|
icon = Icons.thunderstorm;
|
|
color = Colors.blue;
|
|
} else if (weather == "Snow") {
|
|
icon = Icons.snowing;
|
|
color = Colors.lightBlue;
|
|
} else if (weather == "Drizzle") {
|
|
icon = Icons.water_drop;
|
|
color = Colors.blue;
|
|
} else {
|
|
if (weatherCloud.contains('few')) {
|
|
icon = WeatherIcons.day_cloudy;
|
|
color = Colors.grey;
|
|
} else if (weatherCloud.contains('scattered')) {
|
|
icon = WeatherIcons.cloud;
|
|
color = Colors.grey;
|
|
} else if (weatherCloud.contains('broken')) {
|
|
icon = WeatherIcons.cloudy;
|
|
color = Colors.grey;
|
|
} else {
|
|
icon = Icons.cloud;
|
|
color = Colors.grey;
|
|
}
|
|
}
|
|
|
|
if (list.isNotEmpty) {
|
|
return Row(
|
|
children: <Widget>[
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 5),
|
|
child: Icon(
|
|
Icons.event,
|
|
color: Color.fromARGB(255, 249, 138, 11),
|
|
)),
|
|
Expanded(
|
|
child: Text(
|
|
temp,
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(fontSize: 20, color: Colors.black),
|
|
)),
|
|
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, 249, 138, 11),
|
|
)),
|
|
Expanded(
|
|
child: Text(
|
|
'',
|
|
textAlign: TextAlign.right,
|
|
style: TextStyle(fontSize: 20, color: Colors.black),
|
|
)),
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 10, left: 10),
|
|
child: Icon(
|
|
Icons.error,
|
|
color: Color.fromARGB(255, 249, 138, 11),
|
|
size: 30,
|
|
))
|
|
]);
|
|
}
|
|
}
|
|
}
|