Implement local HTTP Api usage

This commit is contained in:
2023-02-14 18:06:18 +01:00
parent 83ad7d59d2
commit 7432972b3c
27 changed files with 1300 additions and 59 deletions

View File

@ -1,12 +1,19 @@
import 'dart:io';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_login/flutter_login.dart';
import 'package:marianum_mobile/api/apiError.dart';
import 'package:marianum_mobile/api/webuntis/webuntisError.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../api/webuntis/queries/authenticate/authenticateParams.dart';
import '../../api/webuntis/queries/authenticate/authenticate.dart';
import '../../data/accountModel.dart';
class Login extends StatefulWidget {
const Login({Key? key}) : super(key: key);
@ -15,32 +22,75 @@ class Login extends StatefulWidget {
}
class _LoginState extends State<Login> {
Duration get loginTime => const Duration(milliseconds: 2250);
bool displayDisclaimerText = true;
final Future<SharedPreferences> _storage = SharedPreferences.getInstance();
String? checkInput(value){
return (value ?? "").length < 5 ? "Eingabe zu kurz" : null;
String? _checkInput(value){
return (value ?? "").length == 0 ? "Eingabe erforderlich" : null;
}
Future<String?> _login(LoginData data) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setBool("loggedIn", false);
try {
await Authenticate(
AuthenticateParams(
user: data.name,
password: data.password,
)
).run().then((value) => {
log(value.sessionId)
});
} on WebuntisError catch(e) {
return e.toString();
} on ApiError catch(e) {
return e.toString();
}
setState(() {
displayDisclaimerText = false;
});
preferences.setBool("loggedIn", true);
preferences.setString("username", data.name);
preferences.setString("password", data.password);
return null;
}
Future<String> _resetPassword(String name) {
return Future.delayed(Duration.zero).then((_) {
return "Diese Funktion steht nicht zur Verfügung!";
});
}
@override
Widget build(BuildContext context) {
return FlutterLogin(
logo: Image.file(File("assets/logo/icon.png")).image,
userValidator: checkInput,
passwordValidator: checkInput,
userValidator: _checkInput,
passwordValidator: _checkInput,
onSubmitAnimationCompleted: () => Provider.of<AccountModel>(context, listen: false).login(),
onLogin: _authUser,
savedEmail: "test",
onLogin: _login,
onSignup: null,
onRecoverPassword: _recoverPassword,
onRecoverPassword: _resetPassword,
hideForgotPasswordButton: true,
theme: LoginTheme(
primaryColor: Theme.of(context).primaryColor,
accentColor: Colors.white,
errorColor: Theme.of(context).primaryColor,
footerBottomPadding: 10,
textFieldStyle: const TextStyle(
fontWeight: FontWeight.w500
),
cardTheme: const CardTheme(
elevation: 10,
shape: InputBorder.none,
),
),
@ -52,42 +102,23 @@ class _LoginState extends State<Login> {
disableCustomPageTransformer: true,
headerWidget: const Padding(
padding: EdgeInsets.only(bottom: 10),
headerWidget: Padding(
padding: const EdgeInsets.only(bottom: 30),
child: Center(
child: Text(
"Dies ist ein Inoffizieller Nextclient & Webuntis Client und wird nicht vom Marianum selbst betrieben.\nBitte bedenke, dass deine persönlichen Anmelde & Infodaten durch dritte, nicht vom Marianum betriebene, Systeme geleitet werden!",
child: Visibility(
visible: displayDisclaimerText,
child: const Text(
"Dies ist ein Inoffizieller Nextclient & Webuntis Client und wird nicht vom Marianum selbst betrieben.\nKeinerlei Gewähr für Vollständigkeit, Richtigkeit und Aktualität!",
textAlign: TextAlign.center,
),
),
),
),
footer: "Marianum Fulda - Die persönliche Schule!",
title: "Marianum",
footer: "Marianum Fulda - Die persönliche Schule",
title: "Marianum Fulda",
hideForgotPasswordButton: true,
userType: LoginUserType.name,
);
}
Future<String?> _authUser(LoginData data) async {
final SharedPreferences preferences = await _storage;
preferences.setBool("loggedIn", true);
preferences.setString("username", data.name);
preferences.setString("password", data.password);
debugPrint('Name: ${data.name}, Password: ${data.password}');
return Future.delayed(loginTime).then((_) {
Provider.of<AccountModel>(context, listen: false).login();
return null;
});
}
Future<String> _recoverPassword(String name) {
return Future.delayed(loginTime).then((_) {
return "Diese Funktion steht nicht zur Verfügung!";
});
}
}

View File

@ -0,0 +1,145 @@
import 'dart:developer';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jiffy/jiffy.dart';
import 'package:marianum_mobile/api/webuntis/queries/getRooms/getRooms.dart';
import 'package:marianum_mobile/data/incommingPackets/timetablePacket.dart';
import 'package:marianum_mobile/widget/loadingSpinner.dart';
import 'package:marianum_mobile/widget/offlineError.dart';
import 'package:timetable_view/timetable_view.dart';
import '../../../api/webuntis/queries/getTimetable/getTimetableParams.dart';
import '../../../api/webuntis/queries/getTimetable/getTimetableResponse.dart';
import '../../../api/webuntis/queries/getTimetable/getTimetable.dart';
class TestTimeTable extends StatefulWidget {
const TestTimeTable({Key? key}) : super(key: key);
@override
State<TestTimeTable> createState() => _TestTimeTableState();
}
class _TestTimeTableState extends State<TestTimeTable> {
late Future<GetTimetableResponse> data;
@override
void initState() {
data = GetTimetable(
GetTimetableParams(
options: GetTimetableParamsOptions(
element: GetTimetableParamsOptionsElement(
id: 92,
type: 5,
keyType: GetTimetableParamsOptionsElementKeyType.id,
),
startDate: 20230206,
endDate: 20230212,
)
)
).run();
GetRooms().run().then((value) => {
log(value.rawResponse.body)
});
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
builder: (BuildContext context, AsyncSnapshot<GetTimetableResponse> snapshot) {
if(snapshot.hasData) {
return Center(
child: TimetableView(
laneEventsList: _buildLaneEvents(snapshot.data!),
onEventTap: (TableEvent event) {},
timetableStyle: CustomTableStyle(context),
onEmptySlotTap: (int laneIndex, TableEventTime start, TableEventTime end) => {},
),
);
} else if(snapshot.hasError) {
return const OfflineBanner(text: "Der Stundenplan konnte nicht geladen werden!");
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
future: data,
);
}
List<LaneEvents> _buildLaneEvents(GetTimetableResponse data) {
List<LaneEvents> laneEvents = List<LaneEvents>.empty(growable: true);
Jiffy.locale("de");
List<int> dayList = data.result.map((e) => e.date).toSet().toList();
dayList.sort((a, b) => a-b);
dayList.forEach((day) {
//Every Day
laneEvents.add(
LaneEvents(
lane: Lane(
laneIndex: day,
name: "${Jiffy(day.toString()).format("dd.MM.yy")}\n${Jiffy(day.toString()).format("EEEE")}",
textStyle: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
fontSize: 14
)
),
events: List<TableEvent>.generate(
data.result.where((element) => element.date == day).length,
(index) {
GetTimetableResponseObject tableEvent = data.result.where((element) => element.date == day).elementAt(index);
return TableEvent(
title: "${tableEvent.substText}",
eventId: tableEvent.id,
laneIndex: day,
startTime: parseTime(tableEvent.startTime),
endTime: parseTime(tableEvent.endTime),
padding: const EdgeInsets.all(5),
backgroundColor: Theme.of(context).primaryColor,
location: "\n${tableEvent.statflags}",
);
}
)
)
);
});
return laneEvents;
}
TableEventTime parseTime(int input) {
String time = input.toString().length < 4 ? "0$input" : input.toString();
return TableEventTime(hour: int.parse(time.substring(0, 2)), minute: int.parse(time.substring(2, 4)));
}
}
class CustomTableStyle extends TimetableStyle {
dynamic context;
CustomTableStyle(this.context);
@override
int get startHour => 07;
@override
int get endHour => 17;
@override
double get laneWidth => 100;
@override
Color get cornerColor => Theme.of(context).primaryColor;
@override
Color get timeItemTextColor => Theme.of(context).primaryColor;
@override
double get timeItemHeight => 60;
@override
double get timeItemWidth => 40;
}