2023-03-17 09:41:08 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-03-25 14:29:28 +01:00
|
|
|
import 'package:outbag_app/backend/themes.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-25 10:27:21 +01:00
|
|
|
import 'package:outbag_app/screens/room/permissions.dart';
|
2023-03-18 20:24:48 +01:00
|
|
|
import 'package:outbag_app/screens/room/new.dart';
|
2023-03-25 14:29:28 +01:00
|
|
|
import 'package:outbag_app/screens/settings/main.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';
|
|
|
|
|
|
|
|
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
|
2023-03-23 10:51:34 +01:00
|
|
|
AccountMeta? info;
|
2023-03-25 17:18:46 +01:00
|
|
|
User? user;
|
2023-03-17 09:41:08 +01:00
|
|
|
|
2023-03-25 14:29:28 +01:00
|
|
|
AppTheme theme = AppTheme.auto;
|
|
|
|
|
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
|
2023-03-25 14:29:28 +01:00
|
|
|
User.listen((_) async {
|
2023-03-24 16:33:37 +01:00
|
|
|
try {
|
2023-03-25 17:18:46 +01:00
|
|
|
final user = await User.fromDisk();
|
|
|
|
setState(() {
|
|
|
|
this.user = user;
|
|
|
|
});
|
|
|
|
fetchInfo();
|
|
|
|
} catch (_) {
|
|
|
|
// no userdata found
|
2023-03-24 16:33:37 +01:00
|
|
|
setState(() {
|
2023-03-25 17:18:46 +01:00
|
|
|
user = null;
|
2023-03-24 16:33:37 +01:00
|
|
|
});
|
2023-03-25 17:18:46 +01:00
|
|
|
}
|
2023-03-24 16:33:37 +01:00
|
|
|
});
|
|
|
|
|
2023-03-25 14:29:28 +01:00
|
|
|
AppTheme.listen((_) async {
|
|
|
|
final theme = await AppTheme.fromDisk();
|
|
|
|
setState(() {
|
|
|
|
this.theme = theme;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
(() async {
|
|
|
|
final theme = await AppTheme.fromDisk();
|
|
|
|
setState(() {
|
|
|
|
this.theme = theme;
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
2023-03-25 17:18:46 +01:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
|
|
|
try {
|
|
|
|
final user = await User.fromDisk();
|
|
|
|
setState(() {
|
|
|
|
this.user = user;
|
|
|
|
});
|
|
|
|
fetchInfo();
|
|
|
|
} catch (_) {
|
|
|
|
// user unavailable
|
|
|
|
// invalid credentials
|
|
|
|
// log out
|
|
|
|
setState(() {
|
|
|
|
user = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-03-24 16:33:37 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 17:18:46 +01:00
|
|
|
void fetchInfo() async {
|
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-25 17:18:46 +01:00
|
|
|
req: () => postWithCreadentials(
|
2023-03-23 14:48:53 +01:00
|
|
|
target: (user?.server)!,
|
|
|
|
path: 'getMyAccount',
|
|
|
|
credentials: user!,
|
|
|
|
body: {}),
|
|
|
|
onOK: (body) {
|
|
|
|
final info = AccountMeta.fromJSON(body['data']);
|
|
|
|
setState(() {
|
|
|
|
this.info = info;
|
|
|
|
});
|
|
|
|
},
|
2023-03-25 17:18:46 +01:00
|
|
|
onServerErr: (_) {
|
2023-03-23 14:48:53 +01:00
|
|
|
// credentials are wrong
|
|
|
|
// log out
|
|
|
|
|
|
|
|
setState(() {
|
2023-03-25 17:18:46 +01:00
|
|
|
info = null;
|
|
|
|
user = null;
|
2023-03-23 14:48:53 +01:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
onNetworkErr: () {
|
|
|
|
// user is currently offline
|
|
|
|
// approve login,
|
|
|
|
// until user goes back offline
|
|
|
|
// NOTE TODO: check user data once online
|
|
|
|
return true;
|
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-25 17:18:46 +01:00
|
|
|
Provider<AppTheme>.value(value: theme),
|
|
|
|
// conditional user provider
|
|
|
|
// used to only provide a user outside of the login area
|
|
|
|
...(user!=null)?[
|
|
|
|
Provider<User>.value(value:user!)
|
|
|
|
]:[]
|
2023-03-23 10:51:34 +01:00
|
|
|
],
|
|
|
|
child: MaterialApp.router(
|
|
|
|
title: "Outbag",
|
2023-03-25 14:29:28 +01:00
|
|
|
themeMode: theme.mode,
|
2023-03-23 10:51:34 +01:00
|
|
|
theme: ThemeData(useMaterial3: true, brightness: Brightness.light),
|
|
|
|
darkTheme: ThemeData(useMaterial3: true, brightness: Brightness.dark),
|
|
|
|
routerDelegate: RoutemasterDelegate(
|
2023-03-25 17:18:46 +01:00
|
|
|
routesBuilder: (context) => RouteMap(
|
|
|
|
routes: {
|
|
|
|
// pre authentification routes
|
|
|
|
...(user == null)
|
|
|
|
? {
|
|
|
|
'/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)),
|
|
|
|
}
|
|
|
|
: {},
|
|
|
|
|
|
|
|
// routes that need the user
|
|
|
|
// to exis
|
|
|
|
...(user!=null)?{
|
|
|
|
'/': (_) => const MaterialPage(child: HomePage()),
|
|
|
|
|
|
|
|
// user-space settings
|
|
|
|
'/settings': (_) =>
|
|
|
|
const MaterialPage(child: SettingsPage()),
|
|
|
|
|
|
|
|
// server dashboard
|
|
|
|
// TODO: add permission check
|
|
|
|
// so routes are only available
|
|
|
|
// if user is allowed to view dashboard
|
|
|
|
...{
|
|
|
|
|
|
|
|
},
|
|
|
|
// room related settings
|
|
|
|
'/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'] ?? "";
|
|
|
|
|
|
|
|
return MaterialPage(child: RoomPage(server, tag));
|
|
|
|
},
|
|
|
|
'/r/:server/:tag/edit': (info) {
|
|
|
|
final server = info.pathParameters['server'] ?? "";
|
|
|
|
final tag = info.pathParameters['tag'] ?? "";
|
|
|
|
|
|
|
|
return MaterialPage(child: EditRoomPage(server, tag));
|
|
|
|
},
|
|
|
|
'/r/:server/:tag/members': (info) {
|
|
|
|
final server = info.pathParameters['server'] ?? "";
|
|
|
|
final tag = info.pathParameters['tag'] ?? "";
|
|
|
|
|
|
|
|
return MaterialPage(
|
|
|
|
child: ManageRoomMembersPage(server, tag));
|
|
|
|
},
|
|
|
|
'/r/:server/:tag/permissions': (info) {
|
|
|
|
final server = info.pathParameters['server'] ?? "";
|
|
|
|
final tag = info.pathParameters['tag'] ?? "";
|
|
|
|
|
|
|
|
return MaterialPage(
|
|
|
|
child: EditRoomPermissionSetPage(server, tag));
|
|
|
|
},
|
|
|
|
}:{}
|
|
|
|
},
|
|
|
|
onUnknownRoute: (_) => (user == null)
|
|
|
|
? const MaterialPage(child: WelcomePage())
|
|
|
|
: const Redirect('/'))),
|
2023-03-23 10:51:34 +01:00
|
|
|
routeInformationParser: const RoutemasterParser(),
|
|
|
|
));
|
2023-03-17 09:41:08 +01:00
|
|
|
}
|
|
|
|
}
|