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,19 +1,70 @@
|
|||
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() {
|
||||
runApp(const MainApp());
|
||||
void main() async {
|
||||
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 {
|
||||
const MainApp({super.key});
|
||||
class UpdateForgeApp extends HookWidget {
|
||||
final ApiService api;
|
||||
final bool hasToken;
|
||||
|
||||
const UpdateForgeApp({super.key, required this.api, required this.hasToken});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Center(
|
||||
child: Text('Hello World!'),
|
||||
),
|
||||
final loggedIn = useState(hasToken && api.isAuthenticated);
|
||||
|
||||
Future<void> saveCredentials() async {
|
||||
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