actions-test/lib/main.dart

107 lines
3.2 KiB
Dart
Raw Normal View History

2023-03-17 09:41:08 +01:00
import 'package:flutter/material.dart';
import 'package:outbag_app/backend/storage.dart';
2023-03-17 09:41:08 +01:00
import './screens/home.dart';
import './screens/welcome.dart';
import './screens/room.dart';
import './screens/auth.dart';
import './backend/request.dart';
2023-03-17 09:41:08 +01:00
import 'package:routemaster/routemaster.dart';
import 'package:localstore/localstore.dart';
2023-03-17 09:41:08 +01:00
// routes when user is logged in
final routesLoggedIn = RouteMap(routes: {
'/': (_) => const MaterialPage(child: HomePage()),
'/r/:server/:tag/:page': (info) => MaterialPage(
child: RoomPage(info.pathParameters['server'] ?? "",
info.pathParameters['tag'] ?? "",
page: info.pathParameters['page'] ?? ""),
)
}, onUnknownRoute: (_) => const Redirect('/'));
2023-03-17 09:41:08 +01:00
// routes when user is not logged in
final routesUnauthorized = RouteMap(routes: {
2023-03-17 09:41:08 +01:00
'/welcome/': (_) => const MaterialPage(child: WelcomePage()),
'/signup': (_) => const MaterialPage(child: AuthPage(mode: Mode.signup)),
'/signupOTA': (_) =>
const MaterialPage(child: AuthPage(mode: Mode.signupOTA)),
'/signin': (_) => const MaterialPage(child: AuthPage(mode: Mode.signin)),
}, onUnknownRoute: (_) => const Redirect('/welcome'));
2023-03-17 09:41:08 +01:00
void main() {
WidgetsFlutterBinding.ensureInitialized();
2023-03-17 09:41:08 +01:00
runApp(const OutbagApp());
}
class OutbagApp extends StatefulWidget {
const OutbagApp({super.key});
@override
State<StatefulWidget> createState() => _OutbagAppState();
}
class _OutbagAppState extends State {
bool isAuthorized = false;
@override
void initState() {
super.initState();
// TODO: try to login user,
// with existing details
(() async {
try {
final credentials = await LoginDetails.fromDisk();
final resp = await postUnauthorized(
target: credentials.server,
path: 'signin',
body: {
'name': credentials.username,
'server': credentials.server.tag,
'accountKey': credentials.password
});
if (resp.res == Result.ok) {
setState(() {
isAuthorized = true;
});
}
} catch (_) {
// user is currently offline
// approve login,
// until user goes back offline
// NOTE TODO: check user data once online
setState(() {
isAuthorized = true;
});
}
})();
// wait for user to be authorized
final db = Localstore.instance;
final stream = db.collection('meta').stream;
stream.listen((data) async {
try {
await LoginDetails.fromDisk();
setState(() {
isAuthorized = true;
});
} catch (_) {}
});
2023-03-17 09:41:08 +01:00
}
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: "Outbag",
// TODO: change back to system (or load from disk)
//themeMode: ThemeMode.system,
themeMode: ThemeMode.dark,
theme: ThemeData(useMaterial3: true, brightness: Brightness.light),
darkTheme: ThemeData(useMaterial3: true, brightness: Brightness.dark),
routerDelegate: RoutemasterDelegate(
routesBuilder: (context) =>
isAuthorized ? routesLoggedIn : routesUnauthorized),
routeInformationParser: const RoutemasterParser(),
);
}
}