2023-03-17 09:41:08 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-03-28 14:54:39 +02:00
|
|
|
import 'package:go_router/go_router.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-29 15:14:27 +02:00
|
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.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
|
|
|
|
|
|
|
void main() {
|
2023-03-17 21:08:45 +01:00
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2023-03-17 09:41:08 +01:00
|
|
|
runApp(const OutbagApp());
|
|
|
|
}
|
|
|
|
|
2023-03-28 14:54:39 +02:00
|
|
|
final GlobalKey<NavigatorState> _rootNavigatorKey =
|
|
|
|
GlobalKey<NavigatorState>(debugLabel: 'root');
|
|
|
|
final GlobalKey<NavigatorState> _userShellNavigatorKey =
|
|
|
|
GlobalKey<NavigatorState>(debugLabel: 'user');
|
|
|
|
|
2023-03-17 09:41:08 +01:00
|
|
|
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-28 14:54:39 +02:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
|
|
|
// wait for user to be authorized
|
|
|
|
User.listen((_) async {
|
|
|
|
try {
|
|
|
|
final user = await User.fromDisk();
|
|
|
|
setState(() {
|
|
|
|
this.user = user;
|
|
|
|
});
|
|
|
|
fetchInfo(user);
|
|
|
|
} catch (_) {
|
|
|
|
// no userdata found
|
|
|
|
setState(() {
|
|
|
|
user = null;
|
|
|
|
});
|
|
|
|
}
|
2023-03-25 14:29:28 +01:00
|
|
|
});
|
|
|
|
|
2023-03-28 14:54:39 +02:00
|
|
|
AppTheme.listen((_) async {
|
|
|
|
final theme = await AppTheme.fromDisk();
|
|
|
|
setState(() {
|
|
|
|
this.theme = theme;
|
|
|
|
});
|
2023-03-25 14:29:28 +01:00
|
|
|
});
|
|
|
|
|
2023-03-28 14:54:39 +02:00
|
|
|
// load theme
|
|
|
|
try {
|
|
|
|
final theme = await AppTheme.fromDisk();
|
|
|
|
setState(() {
|
|
|
|
this.theme = theme;
|
|
|
|
});
|
|
|
|
} catch (_) {}
|
|
|
|
|
|
|
|
// load user
|
2023-03-25 17:18:46 +01:00
|
|
|
try {
|
|
|
|
final user = await User.fromDisk();
|
|
|
|
setState(() {
|
|
|
|
this.user = user;
|
|
|
|
});
|
2023-03-28 14:54:39 +02:00
|
|
|
fetchInfo(user);
|
2023-03-25 17:18:46 +01:00
|
|
|
} catch (_) {
|
|
|
|
// user unavailable
|
|
|
|
// invalid credentials
|
|
|
|
// log out
|
|
|
|
setState(() {
|
|
|
|
user = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-03-24 16:33:37 +01:00
|
|
|
}
|
|
|
|
|
2023-03-28 14:54:39 +02:00
|
|
|
void fetchInfo(User user) 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-28 14:54:39 +02:00
|
|
|
target: user.server,
|
2023-03-23 14:48:53 +01:00
|
|
|
path: 'getMyAccount',
|
2023-03-28 14:54:39 +02:00
|
|
|
credentials: user,
|
2023-03-23 14:48:53 +01:00
|
|
|
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;
|
2023-03-28 14:54:39 +02:00
|
|
|
this.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),
|
2023-03-23 10:51:34 +01:00
|
|
|
],
|
|
|
|
child: MaterialApp.router(
|
|
|
|
title: "Outbag",
|
2023-03-29 15:14:27 +02:00
|
|
|
localizationsDelegates: const [
|
|
|
|
GlobalMaterialLocalizations.delegate,
|
|
|
|
GlobalWidgetsLocalizations.delegate,
|
|
|
|
GlobalCupertinoLocalizations.delegate,
|
|
|
|
AppLocalizations.delegate
|
|
|
|
],
|
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
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),
|
2023-03-28 14:54:39 +02:00
|
|
|
routerConfig: GoRouter(
|
|
|
|
navigatorKey: _rootNavigatorKey,
|
|
|
|
initialLocation: '/',
|
|
|
|
redirect: (context, state) async {
|
|
|
|
if (user == null) {
|
|
|
|
// prelogin
|
|
|
|
if (!state.subloc.startsWith('/welcome')) {
|
|
|
|
// prevent unauthorized user from accessing home
|
|
|
|
return '/welcome';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// post login
|
|
|
|
if (state.subloc.startsWith('/welcome')) {
|
|
|
|
// prevent authorized user from accessing /welcome
|
|
|
|
return '/';
|
2023-03-25 17:18:46 +01:00
|
|
|
}
|
2023-03-28 14:54:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
routes: <RouteBase>[
|
|
|
|
// unauthorized routes
|
|
|
|
GoRoute(
|
|
|
|
name: 'welcome',
|
|
|
|
path: '/welcome',
|
|
|
|
builder: (context, state) => const WelcomePage(),
|
|
|
|
routes: <RouteBase>[
|
|
|
|
GoRoute(
|
|
|
|
name: 'signin',
|
|
|
|
path: 'signin',
|
|
|
|
builder: (context, state) =>
|
|
|
|
const AuthPage(mode: Mode.signin),
|
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
name: 'signup',
|
|
|
|
path: 'signup',
|
|
|
|
builder: (context, state) =>
|
|
|
|
const AuthPage(mode: Mode.signup),
|
|
|
|
),
|
|
|
|
GoRoute(
|
|
|
|
name: 'signup-ota',
|
|
|
|
path: 'signup-ota',
|
|
|
|
builder: (context, state) =>
|
|
|
|
const AuthPage(mode: Mode.signupOTA),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
|
|
|
|
// authorized routes
|
|
|
|
ShellRoute(
|
|
|
|
navigatorKey: _userShellNavigatorKey,
|
|
|
|
builder: (context, state, child) =>
|
|
|
|
Provider.value(value: user!, child: child),
|
|
|
|
routes: <RouteBase>[
|
|
|
|
GoRoute(
|
|
|
|
path: '/',
|
|
|
|
name: 'home',
|
|
|
|
builder: (context, state) => const HomePage(),
|
|
|
|
routes: [
|
|
|
|
GoRoute(
|
|
|
|
name: 'settings',
|
|
|
|
path: 'settings',
|
|
|
|
builder: (context, state) =>
|
|
|
|
const SettingsPage()),
|
|
|
|
GoRoute(
|
|
|
|
path: 'join-room',
|
|
|
|
name: 'add-room',
|
|
|
|
builder: (context, state) =>
|
|
|
|
const JoinRoomPage(),
|
|
|
|
routes: <RouteBase>[
|
|
|
|
GoRoute(
|
|
|
|
path: 'new',
|
|
|
|
name: 'new-room',
|
|
|
|
builder: (context, state) =>
|
|
|
|
const NewRoomPage()),
|
|
|
|
]),
|
|
|
|
GoRoute(
|
|
|
|
name: 'room',
|
|
|
|
path: 'r/:server/:id',
|
|
|
|
builder: (context, state) => RoomPage(
|
|
|
|
state.params['server'] ?? '',
|
|
|
|
state.params['id'] ?? ''),
|
|
|
|
routes: <RouteBase>[
|
2023-03-28 16:14:38 +02:00
|
|
|
GoRoute(
|
|
|
|
name: 'edit-room',
|
|
|
|
path: 'edit',
|
|
|
|
builder: (context, state) =>
|
|
|
|
EditRoomPage(
|
|
|
|
state.params['server'] ??
|
|
|
|
'',
|
|
|
|
state.params['id'] ?? '')),
|
|
|
|
GoRoute(
|
|
|
|
name: 'room-members',
|
|
|
|
path: 'members',
|
|
|
|
builder: (context, state) =>
|
|
|
|
ManageRoomMembersPage(
|
|
|
|
state.params['server'] ??
|
|
|
|
'',
|
|
|
|
state.params['id'] ?? '')),
|
|
|
|
GoRoute(
|
|
|
|
name: 'room-permissions',
|
|
|
|
path: 'roles',
|
|
|
|
builder: (context, state) =>
|
|
|
|
EditRoomPermissionSetPage(
|
|
|
|
state.params['server'] ??
|
|
|
|
'',
|
|
|
|
state.params['id'] ?? '')),
|
2023-03-28 14:54:39 +02:00
|
|
|
])
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
|
|
|
|
// routes that can be accessed
|
|
|
|
// with and without an account
|
|
|
|
// i.e the about screen
|
|
|
|
GoRoute(
|
|
|
|
path: '/about',
|
|
|
|
name: 'about',
|
|
|
|
builder: (context, state) => const Text('About'))
|
|
|
|
]),
|
2023-03-23 10:51:34 +01:00
|
|
|
));
|
2023-03-17 09:41:08 +01:00
|
|
|
}
|
|
|
|
}
|