Revolution #1

Merged
nuark merged 28 commits from revolution into master 2023-05-08 08:50:16 +03:00
Showing only changes of commit 619a43eaed - Show all commits

View file

@ -1,34 +1,49 @@
import 'package:animated_background/animated_background.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:tuuli_app/api/api_client.dart';
class CheckupPage extends StatefulWidget {
const CheckupPage({super.key});
import 'package:tuuli_app/api_controller.dart';
class CheckupPageController extends GetxController {
@override
State<StatefulWidget> createState() => _CheckupPageState();
}
void onInit() {
super.onInit();
}
class _CheckupPageState extends State<CheckupPage>
with TickerProviderStateMixin {
@override
void initState() {
super.initState();
Future<void> checkCredentials() async {
if (ApiController.to.token.isEmpty) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Get.offAllNamed("/login");
});
} else {
try {
final resp =
await ApiController.to.apiClient.listTablesApiListTablesGet(
accessToken: ApiController.to.token,
);
WidgetsBinding.instance.addPostFrameCallback((_) {
checkCredentials();
if (resp.statusCode == 200) {
Get.offAllNamed("/home");
} else {
Get.offAllNamed("/login");
}
});
} catch (e) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Get.offAllNamed("/login");
});
}
}
}
}
class CheckupPage extends GetView<CheckupPageController> {
const CheckupPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: AnimatedBackground(
behaviour: RandomParticleBehaviour(),
vsync: this,
child: Center(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
@ -37,29 +52,16 @@ class _CheckupPageState extends State<CheckupPage>
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 16),
const SizedBox.square(
FutureBuilder(
future: controller.checkCredentials(),
builder: (ctx, _) => const SizedBox.square(
dimension: 32,
child: CircularProgressIndicator(),
),
),
],
),
),
),
);
}
Future<void> checkCredentials() async {
final accessToken = GetStorage().read<String?>("accessToken");
if (accessToken == null) {
Get.offAllNamed("/login");
} else {
final apiClient = Get.find<ApiClient>();
(await apiClient.tablesList()).unfold((data) {
Get.offAllNamed("/home");
}, (error) async {
await GetStorage().remove("accessToken");
Get.offAllNamed("/login");
});
}
}
}