336 lines
8.4 KiB
Dart
336 lines
8.4 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:notas/data/app_database.dart';
|
|
import 'package:notas/data/local_vault_service.dart';
|
|
import 'package:notas/data/note_repository.dart';
|
|
import 'package:notas/platform/app_platform.dart';
|
|
import 'package:notas/platform/window_state.dart';
|
|
import 'package:notas/screens/home_screen.dart';
|
|
import 'package:notas/screens/settings_screen.dart';
|
|
import 'package:notas/screens/vault_access_screen.dart';
|
|
import 'package:notas/theme/app_theme.dart';
|
|
import 'package:notas/widgets/app_title_bar.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:window_manager/window_manager.dart';
|
|
|
|
enum _AppSection {
|
|
home,
|
|
settings,
|
|
}
|
|
|
|
class NotesApp extends StatefulWidget {
|
|
const NotesApp({super.key});
|
|
|
|
@override
|
|
State<NotesApp> createState() => _NotesAppState();
|
|
}
|
|
|
|
class _NotesAppState extends State<NotesApp> with WindowListener {
|
|
static const Duration _screenTransitionDuration = Duration(milliseconds: 280);
|
|
|
|
final LocalVaultService _vaultService = LocalVaultService.instance;
|
|
final GlobalKey<ScaffoldMessengerState> _scaffoldMessengerKey =
|
|
GlobalKey<ScaffoldMessengerState>();
|
|
|
|
AppDatabase? _database;
|
|
NoteRepository? _repository;
|
|
bool _isBootstrapping = true;
|
|
bool _isUnlocking = false;
|
|
_AppSection _currentSection = _AppSection.home;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (isDesktop) {
|
|
windowManager.addListener(this);
|
|
}
|
|
_bootstrapVault();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
if (isDesktop) {
|
|
windowManager.removeListener(this);
|
|
}
|
|
_database?.close();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _bootstrapVault() async {
|
|
try {
|
|
final String? encryptionKey = await _vaultService.readEncryptionKey();
|
|
|
|
if (encryptionKey != null) {
|
|
await _openVault(encryptionKey);
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isBootstrapping = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _openVault(String encryptionKey) async {
|
|
await _database?.close();
|
|
|
|
final AppDatabase database = AppDatabase(encryptionKey: encryptionKey);
|
|
if (!mounted) {
|
|
await database.close();
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_database = database;
|
|
_repository = NoteRepository(database: database);
|
|
});
|
|
}
|
|
|
|
void _openSettings() {
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_currentSection = _AppSection.settings;
|
|
});
|
|
}
|
|
|
|
void _openHome() {
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_currentSection = _AppSection.home;
|
|
});
|
|
}
|
|
|
|
Future<void> _resetLocalVaultData() async {
|
|
final AppDatabase? database = _database;
|
|
|
|
setState(() {
|
|
_repository = null;
|
|
_database = null;
|
|
_isBootstrapping = true;
|
|
});
|
|
|
|
await database?.close();
|
|
|
|
await _vaultService.clearEncryptionKey();
|
|
|
|
final Directory supportDir = await getApplicationSupportDirectory();
|
|
final String dbPath = p.join(supportDir.path, 'notes.sqlite');
|
|
final List<String> filesToDelete = <String>[
|
|
dbPath,
|
|
'$dbPath-wal',
|
|
'$dbPath-shm',
|
|
'$dbPath-journal',
|
|
];
|
|
|
|
for (final String filePath in filesToDelete) {
|
|
final File file = File(filePath);
|
|
if (await file.exists()) {
|
|
await file.delete();
|
|
}
|
|
}
|
|
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isBootstrapping = false;
|
|
_isUnlocking = false;
|
|
});
|
|
}
|
|
|
|
Future<void> _enterWithoutAccount() async {
|
|
if (_isUnlocking) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isUnlocking = true;
|
|
});
|
|
|
|
try {
|
|
final String encryptionKey = await _vaultService.createEncryptionKey();
|
|
await _openVault(encryptionKey);
|
|
} catch (error) {
|
|
_scaffoldMessengerKey.currentState?.showSnackBar(
|
|
SnackBar(content: Text('No se pudo crear el vault local: $error')),
|
|
);
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isUnlocking = false;
|
|
_isBootstrapping = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void _showAccountPlaceholder(String actionLabel) {
|
|
_scaffoldMessengerKey.currentState?.showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
'$actionLabel todavía no está conectado con la API. Usa "Entrar sin cuenta" para empezar en local.',
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _saveWindowSize() async {
|
|
if (await windowManager.isFullScreen()) {
|
|
return;
|
|
}
|
|
|
|
if (await windowManager.isMaximized()) {
|
|
return;
|
|
}
|
|
|
|
final Size currentSize = await windowManager.getSize();
|
|
await WindowStateStore.instance.saveWindowSize(currentSize);
|
|
}
|
|
|
|
Widget _buildLoadingScreen() {
|
|
return MaterialApp(
|
|
title: 'Mis Notas',
|
|
debugShowCheckedModeBanner: false,
|
|
theme: AppTheme.theme,
|
|
home: const Scaffold(
|
|
body: SafeArea(
|
|
child: Column(
|
|
children: [
|
|
AppTitleBar(),
|
|
Expanded(
|
|
child: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
CircularProgressIndicator(),
|
|
SizedBox(height: 16),
|
|
Text('Preparando el vault local...'),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildAppShell({required Widget home}) {
|
|
return MaterialApp(
|
|
title: 'Mis Notas',
|
|
debugShowCheckedModeBanner: false,
|
|
scaffoldMessengerKey: _scaffoldMessengerKey,
|
|
theme: AppTheme.theme,
|
|
home: home,
|
|
);
|
|
}
|
|
|
|
Widget _buildMainShell(NoteRepository repository) {
|
|
final Widget activeScreen = _currentSection == _AppSection.home
|
|
? HomeScreen(
|
|
key: const ValueKey<String>('home-screen'),
|
|
repository: repository,
|
|
onOpenSettings: _openSettings,
|
|
)
|
|
: SettingsScreen(
|
|
key: const ValueKey<String>('settings-screen'),
|
|
onDeleteAllData: _resetLocalVaultData,
|
|
onBackToHome: _openHome,
|
|
);
|
|
|
|
return MaterialApp(
|
|
title: 'Mis Notas',
|
|
debugShowCheckedModeBanner: false,
|
|
scaffoldMessengerKey: _scaffoldMessengerKey,
|
|
theme: AppTheme.theme,
|
|
home: Scaffold(
|
|
body: Container(
|
|
decoration: const BoxDecoration(
|
|
gradient: LinearGradient(
|
|
colors: [
|
|
Color(0xFF191A1D),
|
|
Color(0xFF222326),
|
|
Color(0xFF101114),
|
|
],
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
),
|
|
),
|
|
child: SafeArea(
|
|
child: Column(
|
|
children: [
|
|
const AppTitleBar(),
|
|
Expanded(
|
|
child: AnimatedSwitcher(
|
|
duration: _screenTransitionDuration,
|
|
switchInCurve: Curves.easeOutCubic,
|
|
switchOutCurve: Curves.easeInCubic,
|
|
transitionBuilder: (Widget child, Animation<double> animation) {
|
|
final Animation<Offset> offsetAnimation = Tween<Offset>(
|
|
begin: const Offset(0.08, 0.0),
|
|
end: Offset.zero,
|
|
).animate(animation);
|
|
|
|
return FadeTransition(
|
|
opacity: animation,
|
|
child: SlideTransition(position: offsetAnimation, child: child),
|
|
);
|
|
},
|
|
child: activeScreen,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void onWindowResize() {
|
|
_saveWindowSize();
|
|
}
|
|
|
|
@override
|
|
void onWindowResized() {
|
|
_saveWindowSize();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_isBootstrapping) {
|
|
return _buildLoadingScreen();
|
|
}
|
|
|
|
final NoteRepository? repository = _repository;
|
|
|
|
if (repository != null) {
|
|
return _buildMainShell(repository);
|
|
}
|
|
|
|
return _buildAppShell(
|
|
home: VaultAccessScreen(
|
|
isBusy: _isUnlocking,
|
|
onCreateAccountPressed: (String email, String password) async {
|
|
_showAccountPlaceholder('Crear cuenta');
|
|
},
|
|
onSignInPressed: (String email, String password) async {
|
|
_showAccountPlaceholder('Iniciar sesión');
|
|
},
|
|
onContinueWithoutAccount: _enterWithoutAccount,
|
|
),
|
|
);
|
|
}
|
|
} |