migrate mozilla caches into profiles

This commit is contained in:
Fabian Freund
2026-02-18 07:43:21 +01:00
parent ccab46384f
commit 4d7c185460
5 changed files with 75 additions and 194 deletions
@@ -18,70 +18,38 @@
* 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:go_router/go_router.dart';
/// Result of the switch profile dialog.
/// - `shouldSwitch`: Whether the user confirmed the switch.
/// - `clearCache`: Whether to clear the shared cache.
typedef SwitchProfileDialogResult = ({bool shouldSwitch, bool clearCache});
/// Shows a confirmation dialog for switching user profiles.
///
/// Returns a [SwitchProfileDialogResult] if the user confirms, or null if dismissed.
/// If [duplicateMozillaProfile] is provided, shows an option to clear shared cache.
Future<SwitchProfileDialogResult?> showSwitchProfileDialog(
/// Returns `true` if the user confirms the switch, or null if dismissed.
Future<bool?> showSwitchProfileDialog(
BuildContext context, {
required String profileName,
String? duplicateMozillaProfile,
}) {
return showDialog<SwitchProfileDialogResult>(
return showDialog<bool>(
context: context,
builder: (context) => HookBuilder(
builder: (context) {
final clearCache = useState(false);
return AlertDialog(
icon: const Icon(Icons.warning),
title: const Text('Switch User'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Switching to User '$profileName' will require a restart of the Browser.",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
if (duplicateMozillaProfile != null)
SwitchListTile(
contentPadding: EdgeInsets.zero,
value: clearCache.value,
title: const Text('Clear Shared Cache'),
subtitle: const Text(
'This User has been created based on an exisiting Mozilla Profile Identifier. Clearing cache will affect all linked accounts.',
),
onChanged: (value) {
clearCache.value = value;
},
),
],
),
actions: [
TextButton(
onPressed: () {
context.pop((shouldSwitch: false, clearCache: false));
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
context.pop((shouldSwitch: true, clearCache: clearCache.value));
},
child: const Text('Switch Profile'),
),
],
);
},
builder: (context) => AlertDialog(
icon: const Icon(Icons.warning),
title: const Text('Switch User'),
content: Text(
"Switching to User '$profileName' will require a restart of the Browser.",
style: const TextStyle(fontWeight: FontWeight.bold),
),
actions: [
TextButton(
onPressed: () {
context.pop(false);
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
context.pop(true);
},
child: const Text('Switch Profile'),
),
],
),
);
}
@@ -26,12 +26,11 @@ import 'package:weblibre/features/user/domain/repositories/profile.dart';
import 'package:weblibre/utils/exit_app.dart';
import 'package:weblibre/utils/ui_helper.dart' as ui_helper;
/// Handles the profile switching flow with confirmation dialog and cache clearing options.
/// Handles the profile switching flow with confirmation dialog.
///
/// This function:
/// - Checks if the profile is already active
/// - Shows a confirmation dialog with browser restart warning
/// - Optionally clears shared cache for duplicate Mozilla profiles
/// - Switches to the selected profile and exits the app
Future<void> handleSwitchProfile(
BuildContext context,
@@ -48,22 +47,14 @@ Future<void> handleSwitchProfile(
return;
}
final duplicateMozillaProfile = await filesystem
.checkForDuplicateMozillaProfile(profile.uuidValue);
if (!context.mounted) return;
final result = await showSwitchProfileDialog(
final shouldSwitch = await showSwitchProfileDialog(
context,
profileName: profile.name,
duplicateMozillaProfile: duplicateMozillaProfile,
);
if (result?.shouldSwitch == true) {
if (duplicateMozillaProfile != null && result?.clearCache == true) {
await filesystem.clearMozillaProfileCache(duplicateMozillaProfile);
}
if (shouldSwitch == true) {
await ref
.read(profileRepositoryProvider.notifier)
.switchProfile(profile.id);
@@ -65,12 +65,6 @@ class ProfileRepository extends _$ProfileRepository {
return false;
}
// Clean up Mozilla cache directories before deleting the profile
final mozillaProfileIds = filesystem.getMozillaProfileIds(uuid);
for (final profileId in mozillaProfileIds) {
await filesystem.clearMozillaProfileCache(profileId);
}
await filesystem.getProfileDir(uuid).delete(recursive: true);
ref.invalidateSelf();