2023-03-17 09:41:08 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-03-18 20:24:48 +01:00
|
|
|
import 'package:outbag_app/backend/user.dart';
|
2023-03-24 13:22:48 +01:00
|
|
|
import 'package:outbag_app/screens/room/edit.dart';
|
2023-03-20 21:19:25 +01:00
|
|
|
import 'package:outbag_app/screens/room/join.dart';
|
2023-03-24 21:10:42 +01:00
|
|
|
import 'package:outbag_app/screens/room/members.dart';
|
2023-03-18 20:24:48 +01:00
|
|
|
import 'package:outbag_app/screens/room/new.dart';
|
2023-03-23 14:48:53 +01:00
|
|
|
import 'package:outbag_app/tools/fetch_wrapper.dart';
|
2023-03-23 10:51:34 +01:00
|
|
|
import 'package:provider/provider.dart';
|
2023-03-17 09:41:08 +01:00
|
|
|
import './screens/home.dart';
|
|
|
|
import './screens/welcome.dart';
|
2023-03-23 10:19:14 +01:00
|
|
|
import './screens/room/main.dart';
|
2023-03-17 21:08:45 +01:00
|
|
|
import './screens/auth.dart';
|
|
|
|
import './backend/request.dart';
|
2023-03-17 09:41:08 +01:00
|
|
|
import 'package:routemaster/routemaster.dart';
|
|
|
|
|
|
|
|
// routes when user is not logged in
|
2023-03-17 21:08:45 +01:00
|
|
|
final routesUnauthorized = RouteMap(routes: {
|
2023-03-23 10:51:34 +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 MaterialPage(child: WelcomePage()));
|
2023-03-18 20:24:48 +01:00
|
|
|
|
|
|
|
// routes when user is logged in
|
|
|
|
final routesLoggedIn = RouteMap(routes: {
|
2023-03-23 10:51:34 +01:00
|
|
|
'/': (_) => const MaterialPage(child: HomePage()),
|
|
|
|
'/add-room/new': (_) => const MaterialPage(child: NewRoomPage()),
|
|
|
|
'/add-room': (_) => const MaterialPage(child: JoinRoomPage()),
|
|
|
|
'/r/:server/:tag/': (info) {
|
|
|
|
final server = info.pathParameters['server'] ?? "";
|
|
|
|
final tag = info.pathParameters['tag'] ?? "";
|
2023-03-22 21:17:03 +01:00
|
|
|
|
2023-03-23 10:51:34 +01:00
|
|
|
return MaterialPage(child: RoomPage(server, tag));
|
|
|
|
},
|
2023-03-24 13:22:48 +01:00
|
|
|
'/r/:server/:tag/edit': (info) {
|
|
|
|
final server = info.pathParameters['server'] ?? "";
|
|
|
|
final tag = info.pathParameters['tag'] ?? "";
|
|
|
|
|
|
|
|
return MaterialPage(child: EditRoomPage(server, tag));
|
|
|
|
},
|
2023-03-24 21:10:42 +01:00
|
|
|
'/r/:server/:tag/members': (info) {
|
|
|
|
final server = info.pathParameters['server'] ?? "";
|
|
|
|
final tag = info.pathParameters['tag'] ?? "";
|
|
|
|
|
|
|
|
return MaterialPage(child: ManageRoomMembersPage(server, tag));
|
|
|
|
},
|
|
|
|
|
2023-03-23 10:51:34 +01:00
|
|
|
}, onUnknownRoute: (_) => const Redirect('/'));
|
2023-03-17 09:41:08 +01:00
|
|
|
|
|
|
|
void main() {
|
2023-03-17 21:08:45 +01:00
|
|
|
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 {
|
2023-03-18 20:24:48 +01:00
|
|
|
// assume user is logged in
|
|
|
|
// unless not userdata is found
|
|
|
|
// or the userdata turns out to be wrong
|
|
|
|
bool isAuthorized = true;
|
2023-03-23 10:51:34 +01:00
|
|
|
AccountMeta? info;
|
2023-03-17 09:41:08 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
2023-03-24 16:33:37 +01:00
|
|
|
// wait for user to be authorized
|
|
|
|
User.listen((data) async {
|
|
|
|
try {
|
|
|
|
await User.fromDisk();
|
|
|
|
setState(() {
|
|
|
|
isAuthorized = true;
|
|
|
|
});
|
|
|
|
} catch (_) {}
|
|
|
|
});
|
|
|
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => fetchInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetchInfo() {
|
2023-03-23 10:19:14 +01:00
|
|
|
// try to obtain user account information
|
2023-03-17 09:41:08 +01:00
|
|
|
// with existing details
|
2023-03-23 10:19:14 +01:00
|
|
|
// NOTE: also functions as a way to verify ther data
|
2023-03-24 16:33:37 +01:00
|
|
|
doNetworkRequest(null,
|
2023-03-23 14:48:53 +01:00
|
|
|
req: (user) => postWithCreadentials(
|
|
|
|
target: (user?.server)!,
|
|
|
|
path: 'getMyAccount',
|
|
|
|
credentials: user!,
|
|
|
|
body: {}),
|
|
|
|
onOK: (body) {
|
|
|
|
final info = AccountMeta.fromJSON(body['data']);
|
|
|
|
setState(() {
|
|
|
|
isAuthorized = true;
|
|
|
|
this.info = info;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onServerErr: (body) {
|
|
|
|
// credentials are wrong
|
|
|
|
// log out
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
isAuthorized = false;
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
onNetworkErr: () {
|
|
|
|
// user is currently offline
|
|
|
|
// approve login,
|
|
|
|
// until user goes back offline
|
|
|
|
// NOTE TODO: check user data once online
|
|
|
|
setState(() {
|
|
|
|
isAuthorized = true;
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
onUserErr: () {
|
|
|
|
// invalid credentials
|
|
|
|
// log out
|
|
|
|
setState(() {
|
|
|
|
isAuthorized = false;
|
|
|
|
});
|
2023-03-24 16:33:37 +01:00
|
|
|
// do not show snackbar,
|
|
|
|
// because the user was probably never logged in
|
|
|
|
return false;
|
2023-03-17 21:08:45 +01:00
|
|
|
});
|
2023-03-17 09:41:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-03-23 10:51:34 +01:00
|
|
|
return MultiProvider(
|
|
|
|
providers: [
|
2023-03-23 14:48:53 +01:00
|
|
|
Provider<AccountMeta?>.value(
|
|
|
|
value: info,
|
|
|
|
),
|
2023-03-23 10:51:34 +01:00
|
|
|
],
|
|
|
|
child: 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(
|
2023-03-22 21:17:03 +01:00
|
|
|
routesBuilder: (context) =>
|
2023-03-23 10:51:34 +01:00
|
|
|
isAuthorized ? routesLoggedIn : routesUnauthorized),
|
|
|
|
routeInformationParser: const RoutemasterParser(),
|
|
|
|
));
|
2023-03-17 09:41:08 +01:00
|
|
|
}
|
|
|
|
}
|