prepare for multiple apps
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'package:fancy_password_field/fancy_password_field.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:saf_util/saf_util.dart';
|
||||
import 'package:weblibre/core/routing/routes.dart';
|
||||
import 'package:weblibre/domain/entities/profile.dart';
|
||||
import 'package:weblibre/features/user/domain/presentation/dialogs/password_confirmation_dialog.dart';
|
||||
import 'package:weblibre/features/user/domain/providers/backup_directory.dart';
|
||||
import 'package:weblibre/features/user/domain/services/user_backup.dart';
|
||||
import 'package:weblibre/utils/ui_helper.dart';
|
||||
|
||||
class ProfileBackupScreen extends HookConsumerWidget {
|
||||
final Profile profile;
|
||||
|
||||
const ProfileBackupScreen({super.key, required this.profile});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final formKey = useMemoized(() => GlobalKey<FormState>());
|
||||
|
||||
final passwordTextController = useTextEditingController();
|
||||
final passwordController = useMemoized(() => FancyPasswordController());
|
||||
|
||||
final integrityVerification = useState(true);
|
||||
final skipCaches = useState(false);
|
||||
final skipPasswordConfirmation = useState(false);
|
||||
|
||||
final backupFuture = useState<Future<bool>?>(null);
|
||||
final backupState = useFuture(backupFuture.value);
|
||||
|
||||
useEffect(() {
|
||||
if (backupState.hasError) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
showErrorMessage(context, backupState.error!.toString());
|
||||
});
|
||||
} else if (backupState.hasData) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
showInfoMessage(context, 'Backup created successfully');
|
||||
ProfileListRoute().go(context);
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
}, [backupState.hasError, backupState.hasData, backupState.error]);
|
||||
|
||||
final disableInteraction =
|
||||
backupState.connectionState == ConnectionState.waiting;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Create Backup')),
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8),
|
||||
child: Form(
|
||||
key: formKey,
|
||||
child: ListView(
|
||||
children: [
|
||||
FancyPasswordField(
|
||||
controller: passwordTextController,
|
||||
enabled: !disableInteraction,
|
||||
passwordController: passwordController,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
enableIMEPersonalizedLearning: false,
|
||||
keyboardType: TextInputType.visiblePassword,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Password',
|
||||
floatingLabelBehavior: FloatingLabelBehavior.always,
|
||||
),
|
||||
validationRules: {MinCharactersValidationRule(5)},
|
||||
validator: (value) {
|
||||
//Make sure since onChange is sometimes unreliable
|
||||
passwordController.onChange(value ?? '');
|
||||
|
||||
return passwordController.areAllRulesValidated
|
||||
? null
|
||||
: 'Not Validated';
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
value: integrityVerification.value,
|
||||
onChanged: disableInteraction
|
||||
? null
|
||||
: (value) {
|
||||
integrityVerification.value = value;
|
||||
},
|
||||
title: const Text('Verify Backup Integrity'),
|
||||
subtitle: const Text(
|
||||
'Automatically check that backups are complete and restorable',
|
||||
),
|
||||
),
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
value: skipCaches.value,
|
||||
onChanged: disableInteraction
|
||||
? null
|
||||
: (value) {
|
||||
skipCaches.value = value;
|
||||
},
|
||||
title: const Text('Skip Cache Directories'),
|
||||
subtitle: const Text(
|
||||
'Leave out temporary browser caches like page, icon, and thumbnail data to keep backups smaller',
|
||||
),
|
||||
),
|
||||
ExpansionTile(
|
||||
enabled: !disableInteraction,
|
||||
childrenPadding: EdgeInsets.zero,
|
||||
tilePadding: EdgeInsets.zero,
|
||||
title: const Text('Advanced'),
|
||||
children: [
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
value: skipPasswordConfirmation.value,
|
||||
onChanged: disableInteraction
|
||||
? null
|
||||
: (value) {
|
||||
skipPasswordConfirmation.value = value;
|
||||
},
|
||||
title: const Text('Skip Password Confirmation Prompt'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (disableInteraction)
|
||||
const Column(
|
||||
children: [
|
||||
LinearProgressIndicator(),
|
||||
Text('Creating Backup'),
|
||||
],
|
||||
)
|
||||
else
|
||||
FilledButton.icon(
|
||||
icon: const Icon(MdiIcons.safe),
|
||||
onPressed: () async {
|
||||
if (formKey.currentState?.validate() ?? false) {
|
||||
if (!skipPasswordConfirmation.value) {
|
||||
final confirmation =
|
||||
await showPasswordConfirmationDialog(context);
|
||||
|
||||
if (confirmation != passwordTextController.text) {
|
||||
if (context.mounted) {
|
||||
showErrorMessage(
|
||||
context,
|
||||
'Passwords do not match',
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (ref.read(backupDirectoryUriProvider) == null) {
|
||||
final dir = await SafUtil().pickDirectory(
|
||||
writePermission: true,
|
||||
persistablePermission: true,
|
||||
);
|
||||
if (dir == null) return;
|
||||
ref
|
||||
.read(backupDirectoryUriProvider.notifier)
|
||||
.set(Uri.parse(dir.uri));
|
||||
}
|
||||
|
||||
backupFuture.value = ref
|
||||
.read(userBackupServiceProvider.notifier)
|
||||
.createUserBackup(
|
||||
profile,
|
||||
password: passwordTextController.text,
|
||||
integrityCheck: integrityVerification.value,
|
||||
skipCaches: skipCaches.value,
|
||||
);
|
||||
}
|
||||
},
|
||||
label: const Text('Backup'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:saf_util/saf_util.dart';
|
||||
import 'package:weblibre/core/providers/format.dart';
|
||||
import 'package:weblibre/core/routing/routes.dart';
|
||||
import 'package:weblibre/features/user/domain/providers.dart';
|
||||
import 'package:weblibre/features/user/domain/providers/backup_directory.dart';
|
||||
import 'package:weblibre/features/user/domain/services/user_backup.dart';
|
||||
import 'package:weblibre/presentation/widgets/failure_widget.dart';
|
||||
|
||||
final _filenamePattern = RegExp(
|
||||
r'^backup_(?<profile>.+?)_(?<timestamp>\d{4}-\d{2}-\d{2}_\d{6})\.weblibre$',
|
||||
);
|
||||
|
||||
class ProfileBackupListScreen extends HookConsumerWidget {
|
||||
const ProfileBackupListScreen({super.key});
|
||||
|
||||
Future<void> _pickDirectory(WidgetRef ref) async {
|
||||
final dir = await SafUtil().pickDirectory(
|
||||
writePermission: true,
|
||||
persistablePermission: true,
|
||||
);
|
||||
|
||||
if (dir != null) {
|
||||
final dirUri = Uri.parse(dir.uri);
|
||||
ref.read(backupDirectoryUriProvider.notifier).set(dirUri);
|
||||
|
||||
final migrated = await ref
|
||||
.read(userBackupServiceProvider.notifier)
|
||||
.migrateOldBackups(dirUri);
|
||||
|
||||
if (migrated > 0) {
|
||||
ref.invalidate(backupListProvider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final dirUri = ref.watch(backupDirectoryUriProvider);
|
||||
final backupListAsync = ref.watch(backupListProvider);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Backups'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(MdiIcons.folderCog),
|
||||
tooltip: 'Change backup directory',
|
||||
onPressed: () => _pickDirectory(ref),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
child: dirUri == null
|
||||
? Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32.0),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(MdiIcons.folderOpen, size: 64),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'Select a directory to store your backups.',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'Choose a location outside of the app to keep your backups safe across reinstalls.',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
FilledButton.icon(
|
||||
icon: const Icon(MdiIcons.folderPlus),
|
||||
label: const Text('Select Backup Directory'),
|
||||
onPressed: () => _pickDirectory(ref),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
: backupListAsync.when(
|
||||
data: (backupList) {
|
||||
if (backupList.isEmpty) {
|
||||
return const Center(child: Text('No backups found'));
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
itemCount: backupList.length,
|
||||
itemBuilder: (context, index) {
|
||||
final file = backupList[index];
|
||||
final match = _filenamePattern.firstMatch(file.name);
|
||||
|
||||
if (match != null) {
|
||||
final profileName = match.group(1)!;
|
||||
final datePart = match.group(2)!;
|
||||
|
||||
final dateTime = UserBackupService.dateFormatter.decode(
|
||||
datePart,
|
||||
);
|
||||
|
||||
return ListTile(
|
||||
key: ValueKey(file.uri),
|
||||
title: Text(profileName),
|
||||
subtitle: Text(
|
||||
ref
|
||||
.read(formatProvider.notifier)
|
||||
.fullDateTime(dateTime),
|
||||
),
|
||||
onTap: () async {
|
||||
await RestoreProfileRoute(
|
||||
backupFileUri: file.uri,
|
||||
).push(context);
|
||||
},
|
||||
);
|
||||
} else {
|
||||
return ListTile(
|
||||
key: ValueKey(file.uri),
|
||||
title: Text(file.name),
|
||||
onTap: () async {
|
||||
await RestoreProfileRoute(
|
||||
backupFileUri: file.uri,
|
||||
).push(context);
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
error: (error, stackTrace) => FailureWidget(
|
||||
title: 'Failed to get backups',
|
||||
exception: error,
|
||||
onRetry: () {
|
||||
ref.invalidate(backupListProvider);
|
||||
},
|
||||
),
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,320 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:weblibre/core/filesystem.dart';
|
||||
import 'package:weblibre/core/routing/routes.dart';
|
||||
import 'package:weblibre/domain/entities/profile.dart';
|
||||
import 'package:weblibre/features/settings/presentation/widgets/sections.dart';
|
||||
import 'package:weblibre/features/user/data/models/auth_settings.dart';
|
||||
import 'package:weblibre/features/user/domain/presentation/dialogs/delete_profile_dialog.dart';
|
||||
import 'package:weblibre/features/user/domain/presentation/utils/profile_switch_handler.dart';
|
||||
import 'package:weblibre/features/user/domain/providers/profile_auth.dart';
|
||||
import 'package:weblibre/features/user/domain/repositories/profile.dart';
|
||||
import 'package:weblibre/features/user/domain/services/local_authentication.dart';
|
||||
import 'package:weblibre/utils/form_validators.dart';
|
||||
|
||||
const _timeoutOptions = <DropdownMenuItem<Duration?>>[
|
||||
DropdownMenuItem(value: Duration(minutes: 1), child: Text('1 minute')),
|
||||
DropdownMenuItem(value: Duration(minutes: 5), child: Text('5 minutes')),
|
||||
DropdownMenuItem(value: Duration(minutes: 15), child: Text('15 minutes')),
|
||||
DropdownMenuItem(value: Duration(hours: 1), child: Text('1 hour')),
|
||||
];
|
||||
|
||||
class ProfileEditScreen extends HookConsumerWidget {
|
||||
final Profile? profile;
|
||||
|
||||
const ProfileEditScreen({super.key, required this.profile});
|
||||
|
||||
Future<void> _handleSave(
|
||||
BuildContext context,
|
||||
WidgetRef ref,
|
||||
GlobalKey<FormState> formKey,
|
||||
String name,
|
||||
AuthSettings authSettings,
|
||||
) async {
|
||||
if (!(formKey.currentState?.validate() ?? false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Require biometric confirmation when enabling/changing auth
|
||||
if (profile != null &&
|
||||
(profile!.authSettings.authenticationRequired ||
|
||||
authSettings.authenticationRequired)) {
|
||||
final authResult = await ref
|
||||
.read(localAuthenticationServiceProvider.notifier)
|
||||
.authenticate(
|
||||
authKey: profileAccessAuthKey(profile!.id),
|
||||
localizedReason: 'Require authentication for profile',
|
||||
);
|
||||
|
||||
if (!authResult) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (profile != null) {
|
||||
await ref
|
||||
.read(profileRepositoryProvider.notifier)
|
||||
.updateProfileMetadata(
|
||||
profile!.copyWith(name: name, authSettings: authSettings),
|
||||
);
|
||||
|
||||
if (context.mounted) {
|
||||
context.pop();
|
||||
}
|
||||
} else {
|
||||
await ref
|
||||
.read(profileRepositoryProvider.notifier)
|
||||
.createProfile(name: name, authSettings: authSettings);
|
||||
|
||||
if (context.mounted) {
|
||||
context.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final formKey = useMemoized(() => GlobalKey<FormState>());
|
||||
final nameTextController = useTextEditingController(text: profile?.name);
|
||||
final authSettings = useState(
|
||||
profile?.authSettings ?? AuthSettings.withDefaults(),
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: (profile != null)
|
||||
? const Text('Edit User')
|
||||
: const Text('Create User'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () async {
|
||||
await _handleSave(
|
||||
context,
|
||||
ref,
|
||||
formKey,
|
||||
nameTextController.text,
|
||||
authSettings.value,
|
||||
);
|
||||
},
|
||||
icon: const Icon(Icons.check),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
child: Form(
|
||||
key: formKey,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12.0),
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: nameTextController,
|
||||
decoration: const InputDecoration(
|
||||
label: Text('Name'),
|
||||
floatingLabelBehavior: FloatingLabelBehavior.always,
|
||||
),
|
||||
validator: validateProfileName,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
_AuthSection(
|
||||
authSettings: authSettings.value,
|
||||
onAuthSettingsChanged: (newSettings) {
|
||||
authSettings.value = newSettings;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
if (profile != null) ...[
|
||||
_ProfileActionsSection(profile: profile!),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _AuthSection extends StatelessWidget {
|
||||
final AuthSettings authSettings;
|
||||
final ValueChanged<AuthSettings> onAuthSettingsChanged;
|
||||
|
||||
const _AuthSection({
|
||||
required this.authSettings,
|
||||
required this.onAuthSettingsChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SettingSection(name: 'Authentication'),
|
||||
SwitchListTile.adaptive(
|
||||
value: authSettings.authenticationRequired,
|
||||
title: const Text('Require Authentication'),
|
||||
subtitle: const Text(
|
||||
'Lock this profile when switching away from the app',
|
||||
),
|
||||
secondary: const Icon(MdiIcons.fingerprint),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
onChanged: (value) {
|
||||
onAuthSettingsChanged(
|
||||
authSettings.copyWith.authenticationRequired(value),
|
||||
);
|
||||
},
|
||||
),
|
||||
if (authSettings.authenticationRequired) ...[
|
||||
const SizedBox(height: 8),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const ListTile(
|
||||
title: Text('Auto-lock Behavior'),
|
||||
subtitle: Text('Choose when to lock the profile'),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: Icon(MdiIcons.lockClock),
|
||||
),
|
||||
RadioGroup<AutoLockMode>(
|
||||
groupValue: authSettings.autoLockMode,
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
onAuthSettingsChanged(
|
||||
authSettings.copyWith.autoLockMode(value),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: const Column(
|
||||
children: [
|
||||
RadioListTile.adaptive(
|
||||
value: AutoLockMode.background,
|
||||
title: Text('Lock on Background'),
|
||||
subtitle: Text(
|
||||
'Lock immediately when app goes to background',
|
||||
),
|
||||
),
|
||||
RadioListTile.adaptive(
|
||||
value: AutoLockMode.timeout,
|
||||
title: Text('Lock After Timeout'),
|
||||
subtitle: Text('Lock after a period of inactivity'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (authSettings.autoLockMode == AutoLockMode.timeout)
|
||||
ListTile(
|
||||
title: const Text('Timeout Duration'),
|
||||
subtitle: const Text('How long to wait before locking'),
|
||||
leading: const Icon(MdiIcons.timerOutline),
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
trailing: DropdownButton<Duration?>(
|
||||
value: authSettings.timeout,
|
||||
items: _timeoutOptions,
|
||||
underline: const SizedBox.shrink(),
|
||||
onChanged: (Duration? value) {
|
||||
if (value != null) {
|
||||
onAuthSettingsChanged(authSettings.copyWith.timeout(value));
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ProfileActionsSection extends ConsumerWidget {
|
||||
final Profile profile;
|
||||
|
||||
const _ProfileActionsSection({required this.profile});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SettingSection(name: 'Profile Actions'),
|
||||
const SizedBox(height: 8),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: OutlinedButton.icon(
|
||||
label: const Text('Backup'),
|
||||
icon: const Icon(MdiIcons.safe),
|
||||
onPressed: () async {
|
||||
await BackupProfileRoute(
|
||||
profile: jsonEncode(profile.toJson()),
|
||||
).push(context);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (filesystem.selectedProfile != profile.uuidValue)
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: OutlinedButton.icon(
|
||||
label: const Text('Switch to this Profile'),
|
||||
icon: const Icon(MdiIcons.accountSwitch),
|
||||
onPressed: () async {
|
||||
await handleSwitchProfile(context, ref, profile);
|
||||
},
|
||||
),
|
||||
),
|
||||
if (filesystem.selectedProfile != profile.uuidValue)
|
||||
const SizedBox(height: 12),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: OutlinedButton.icon(
|
||||
style: OutlinedButton.styleFrom(
|
||||
side: BorderSide(color: Theme.of(context).colorScheme.error),
|
||||
foregroundColor: Theme.of(context).colorScheme.error,
|
||||
iconColor: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
label: const Text('Delete'),
|
||||
icon: const Icon(Icons.delete),
|
||||
onPressed: () async {
|
||||
final result = await showDeleteProfileDialog(context);
|
||||
|
||||
if (result == true) {
|
||||
await ref
|
||||
.read(profileRepositoryProvider.notifier)
|
||||
.deleteProfile(profile.uuidValue.uuid);
|
||||
|
||||
if (context.mounted) {
|
||||
context.pop();
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:weblibre/core/filesystem.dart';
|
||||
import 'package:weblibre/core/routing/routes.dart';
|
||||
import 'package:weblibre/features/user/domain/repositories/profile.dart';
|
||||
import 'package:weblibre/presentation/widgets/failure_widget.dart';
|
||||
|
||||
class ProfileListScreen extends HookConsumerWidget {
|
||||
const ProfileListScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final usersAsync = ref.watch(profileRepositoryProvider);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Users'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () async {
|
||||
await ProfileBackupListRoute().push(context);
|
||||
},
|
||||
icon: const Icon(MdiIcons.backupRestore),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
child: usersAsync.when(
|
||||
skipLoadingOnReload: true,
|
||||
data: (profiles) => ListView.builder(
|
||||
itemCount: profiles.length,
|
||||
itemBuilder: (context, index) {
|
||||
final profile = profiles[index];
|
||||
final isSelected =
|
||||
filesystem.selectedProfile == profile.uuidValue;
|
||||
|
||||
return ListTile(
|
||||
enabled: !isSelected,
|
||||
leading: const Icon(Icons.person),
|
||||
title: Text(profile.name),
|
||||
subtitle: isSelected ? const Text('Active') : null,
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () async {
|
||||
await EditProfileRoute(
|
||||
profile: jsonEncode(profile.toJson()),
|
||||
).push(context);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
error: (error, stackTrace) => Center(
|
||||
child: FailureWidget(
|
||||
title: 'Failed to load Profiles',
|
||||
exception: error,
|
||||
),
|
||||
),
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () async {
|
||||
await CreateProfileRoute().push(context);
|
||||
},
|
||||
child: const Icon(Icons.person_add),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2026 Fabian Freund.
|
||||
*
|
||||
* This file is part of WebLibre
|
||||
* (see https://weblibre.eu).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:weblibre/core/routing/routes.dart';
|
||||
import 'package:weblibre/features/user/domain/presentation/dialogs/override_profile_dialog.dart';
|
||||
import 'package:weblibre/features/user/domain/services/user_backup.dart';
|
||||
import 'package:weblibre/utils/form_validators.dart';
|
||||
import 'package:weblibre/utils/ui_helper.dart';
|
||||
|
||||
enum RestoreTarget { createOrOverride, createNew }
|
||||
|
||||
class ProfileRestoreScreen extends HookConsumerWidget {
|
||||
final Uri backupFileUri;
|
||||
|
||||
const ProfileRestoreScreen({super.key, required this.backupFileUri});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final formKey = useMemoized(() => GlobalKey<FormState>());
|
||||
|
||||
final passwordTextController = useTextEditingController();
|
||||
final nameTextController = useTextEditingController();
|
||||
|
||||
final restoreFuture = useState<Future<bool>?>(null);
|
||||
final restoreState = useFuture(restoreFuture.value);
|
||||
|
||||
final restoreTarget = useState(RestoreTarget.createNew);
|
||||
|
||||
useEffect(() {
|
||||
if (restoreState.hasError) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
showErrorMessage(context, restoreState.error!.toString());
|
||||
});
|
||||
} else if (restoreState.hasData) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
showInfoMessage(context, 'Backup restored successfully');
|
||||
ProfileListRoute().go(context);
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
}, [restoreState.hasError, restoreState.hasData, restoreState.error]);
|
||||
|
||||
final disableInteraction =
|
||||
restoreState.connectionState == ConnectionState.waiting;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Restore Backup')),
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8),
|
||||
child: Form(
|
||||
key: formKey,
|
||||
child: ListView(
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: passwordTextController,
|
||||
enabled: !disableInteraction,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
enableIMEPersonalizedLearning: false,
|
||||
keyboardType: TextInputType.visiblePassword,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Password',
|
||||
floatingLabelBehavior: FloatingLabelBehavior.always,
|
||||
),
|
||||
validator: (value) {
|
||||
return validateRequired(
|
||||
value,
|
||||
message: 'Password required',
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
RadioGroup(
|
||||
groupValue: restoreTarget.value,
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
restoreTarget.value = value;
|
||||
}
|
||||
},
|
||||
child: Column(
|
||||
children: [
|
||||
RadioListTile(
|
||||
enabled: !disableInteraction,
|
||||
value: RestoreTarget.createNew,
|
||||
title: const Text('Create New User'),
|
||||
subtitle: const Text('Restore backup as a new user'),
|
||||
),
|
||||
RadioListTile(
|
||||
enabled: !disableInteraction,
|
||||
value: RestoreTarget.createOrOverride,
|
||||
title: const Text('Restore & Replace'),
|
||||
subtitle: const Text(
|
||||
'Restore backup and overwrite existing user if present',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (restoreTarget.value == RestoreTarget.createNew)
|
||||
TextFormField(
|
||||
controller: nameTextController,
|
||||
enabled: !disableInteraction,
|
||||
decoration: const InputDecoration(
|
||||
label: Text('Name'),
|
||||
floatingLabelBehavior: FloatingLabelBehavior.always,
|
||||
),
|
||||
validator: validateProfileName,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (disableInteraction)
|
||||
const Column(
|
||||
children: [
|
||||
LinearProgressIndicator(),
|
||||
Text('Restoring Backup'),
|
||||
],
|
||||
)
|
||||
else
|
||||
FilledButton.icon(
|
||||
icon: const Icon(MdiIcons.backupRestore),
|
||||
onPressed: () {
|
||||
if (formKey.currentState?.validate() ?? false) {
|
||||
restoreFuture.value = switch (restoreTarget.value) {
|
||||
RestoreTarget.createOrOverride =>
|
||||
ref
|
||||
.read(userBackupServiceProvider.notifier)
|
||||
.restoreAndCreateOrOverride(
|
||||
backupFileUri,
|
||||
password: passwordTextController.text,
|
||||
confirmOverrideCallback: () {
|
||||
if (context.mounted) {
|
||||
return showOverrideProfileDialog(context);
|
||||
} else {
|
||||
throw Exception('Override failed');
|
||||
}
|
||||
},
|
||||
),
|
||||
RestoreTarget.createNew =>
|
||||
ref
|
||||
.read(userBackupServiceProvider.notifier)
|
||||
.restoreAndCreateNew(
|
||||
backupFileUri,
|
||||
profileName: nameTextController.text,
|
||||
password: passwordTextController.text,
|
||||
),
|
||||
};
|
||||
}
|
||||
},
|
||||
label: const Text('Restore'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user