feat: add main app entry with flutter hooks
This commit is contained in:
parent
c0c3e79640
commit
8b373e3360
1 changed files with 61 additions and 10 deletions
|
|
@ -1,20 +1,71 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
|
import 'config/theme.dart';
|
||||||
|
import 'services/api_service.dart';
|
||||||
|
import 'services/background_service.dart';
|
||||||
|
import 'screens/login_screen.dart';
|
||||||
|
import 'screens/apps_screen.dart';
|
||||||
|
|
||||||
void main() {
|
void main() async {
|
||||||
runApp(const MainApp());
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
const storage = FlutterSecureStorage();
|
||||||
|
final savedUrl = await storage.read(key: 'server_url');
|
||||||
|
final savedToken = await storage.read(key: 'auth_token');
|
||||||
|
|
||||||
|
final api = ApiService(baseUrl: savedUrl ?? 'https://localhost');
|
||||||
|
|
||||||
|
if (savedToken != null) {
|
||||||
|
api.setToken(savedToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
final bgService = BackgroundService();
|
||||||
|
await bgService.initialize();
|
||||||
|
await bgService.schedulePeriodicCheck();
|
||||||
|
|
||||||
|
runApp(UpdateForgeApp(api: api, hasToken: savedToken != null));
|
||||||
}
|
}
|
||||||
|
|
||||||
class MainApp extends StatelessWidget {
|
class UpdateForgeApp extends HookWidget {
|
||||||
const MainApp({super.key});
|
final ApiService api;
|
||||||
|
final bool hasToken;
|
||||||
|
|
||||||
|
const UpdateForgeApp({super.key, required this.api, required this.hasToken});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return const MaterialApp(
|
final loggedIn = useState(hasToken && api.isAuthenticated);
|
||||||
home: Scaffold(
|
|
||||||
body: Center(
|
Future<void> saveCredentials() async {
|
||||||
child: Text('Hello World!'),
|
const storage = FlutterSecureStorage();
|
||||||
),
|
await storage.write(key: 'server_url', value: api.baseUrl);
|
||||||
),
|
await storage.write(key: 'auth_token', value: api.token);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> clearCredentials() async {
|
||||||
|
const storage = FlutterSecureStorage();
|
||||||
|
await storage.delete(key: 'auth_token');
|
||||||
|
api.setToken('');
|
||||||
|
loggedIn.value = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MaterialApp(
|
||||||
|
title: 'UpdateForge',
|
||||||
|
theme: buildAppTheme(),
|
||||||
|
debugShowCheckedModeBanner: false,
|
||||||
|
home: loggedIn.value
|
||||||
|
? AppsScreen(
|
||||||
|
api: api,
|
||||||
|
onLogout: clearCredentials,
|
||||||
|
)
|
||||||
|
: LoginScreen(
|
||||||
|
api: api,
|
||||||
|
onLogin: () async {
|
||||||
|
await saveCredentials();
|
||||||
|
loggedIn.value = true;
|
||||||
|
},
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue