Skip to content

Commit

Permalink
Merge PR #1745
Browse files Browse the repository at this point in the history
  • Loading branch information
fdennis committed Dec 11, 2024
2 parents e3fd7bd + b17725a commit 18189be
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion lib/desktop/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:local_notifier/local_notifier.dart';
import 'package:logging/logging.dart';
import 'package:material_symbols_icons/symbols.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:screen_retriever/screen_retriever.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart';
Expand Down Expand Up @@ -125,7 +127,26 @@ Future<Widget> initialize(List<String> argv) async {
_initLogging(args);

await windowManager.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
SharedPreferences prefs;
try {
prefs = await SharedPreferences.getInstance();
} catch (error) {
Directory appSupportDirectory = await getApplicationSupportDirectory();
String appDataPath =
path.join(appSupportDirectory.path, 'shared_preferences.json');
_log.warning(
'Failed to load the preferences file at $appDataPath. Attempting to repair it.');
await _repairPreferences(appDataPath);

try {
prefs = await SharedPreferences.getInstance();
} catch (error) {
_log.warning(
'Failed to repair the preferences file. Deleting the file and proceeding with a fresh configuration.');
await File(appDataPath).delete();
prefs = await SharedPreferences.getInstance();
}
}
final windowManagerHelper = WindowManagerHelper.withPreferences(prefs);
final isHidden = _getIsHidden(args, prefs);

Expand Down Expand Up @@ -413,3 +434,13 @@ class _HelperWaiterState extends ConsumerState<_HelperWaiter> {
}
}
}

Future<void> _repairPreferences(String appDataPath) async {
List<int> contents = await File(appDataPath).readAsBytes();
var contentsGrowable = List<int>.from(contents); // Make the list growable

// Remove any NUL characters
contentsGrowable.removeWhere((item) => item == 0);

await File(appDataPath).writeAsBytes(contentsGrowable);
}

0 comments on commit 18189be

Please sign in to comment.