Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,49 @@ class _AppState extends ConsumerState<App> with WindowListener {
void onWindowClose() async {
bool isPreventClose = await windowManager.isPreventClose();
if (isPreventClose) {
if (ref.watch(
settingsProvider.select((value) => value.promptBeforeClosing)) &&
ref.watch(hasUnsavedChangesProvider)) {
final hasUnsaved = ref.watch(hasUnsavedChangesProvider);
final autoSave =
ref.watch(settingsProvider.select((value) => value.autoSaveOnExit));
final promptBefore = ref
.watch(settingsProvider.select((value) => value.promptBeforeClosing));

// Priority 1: Auto-save if enabled and there are unsaved changes
if (autoSave && hasUnsaved) {
try {
await ref.read(collectionStateNotifierProvider.notifier).saveData();
await windowManager.destroy();
} catch (e) {
// If autosave fails, show error and prevent close to avoid data loss
if (context.mounted) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: const Text('Error Saving'),
content: Text(
'Failed to save your changes: $e\n\nPlease try saving manually or close without saving.'),
actions: [
OutlinedButton(
child: const Text('Close Without Saving'),
onPressed: () async {
Navigator.of(context).pop();
await windowManager.destroy();
},
),
FilledButton(
child: const Text('Try Again'),
onPressed: () {
Navigator.of(context).pop();
onWindowClose(); // Retry
},
),
],
),
);
}
}
}
// Priority 2: Show prompt if enabled and there are unsaved changes
else if (promptBefore && hasUnsaved) {
showDialog(
context: context,
builder: (_) => AlertDialog(
Expand Down Expand Up @@ -89,7 +129,9 @@ class _AppState extends ConsumerState<App> with WindowListener {
],
),
);
} else {
}
// Priority 3: Just close if no unsaved changes or both settings disabled
else {
await windowManager.destroy();
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/models/settings_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class SettingsModel {
this.defaultCodeGenLang = CodegenLanguage.curl,
this.saveResponses = true,
this.promptBeforeClosing = true,
this.autoSaveOnExit = true,
this.activeEnvironmentId,
this.historyRetentionPeriod = HistoryRetentionPeriod.oneWeek,
this.workspaceFolderPath,
Expand All @@ -30,6 +31,7 @@ class SettingsModel {
final CodegenLanguage defaultCodeGenLang;
final bool saveResponses;
final bool promptBeforeClosing;
final bool autoSaveOnExit;
final String? activeEnvironmentId;
final HistoryRetentionPeriod historyRetentionPeriod;
final String? workspaceFolderPath;
Expand All @@ -46,6 +48,7 @@ class SettingsModel {
CodegenLanguage? defaultCodeGenLang,
bool? saveResponses,
bool? promptBeforeClosing,
bool? autoSaveOnExit,
String? activeEnvironmentId,
HistoryRetentionPeriod? historyRetentionPeriod,
String? workspaceFolderPath,
Expand All @@ -63,6 +66,7 @@ class SettingsModel {
offset: offset ?? this.offset,
saveResponses: saveResponses ?? this.saveResponses,
promptBeforeClosing: promptBeforeClosing ?? this.promptBeforeClosing,
autoSaveOnExit: autoSaveOnExit ?? this.autoSaveOnExit,
activeEnvironmentId: activeEnvironmentId ?? this.activeEnvironmentId,
historyRetentionPeriod:
historyRetentionPeriod ?? this.historyRetentionPeriod,
Expand All @@ -85,6 +89,7 @@ class SettingsModel {
offset: offset,
saveResponses: saveResponses,
promptBeforeClosing: promptBeforeClosing,
autoSaveOnExit: autoSaveOnExit,
activeEnvironmentId: activeEnvironmentId,
historyRetentionPeriod: historyRetentionPeriod,
workspaceFolderPath: workspaceFolderPath,
Expand Down Expand Up @@ -132,6 +137,7 @@ class SettingsModel {
}
final saveResponses = data["saveResponses"] as bool?;
final promptBeforeClosing = data["promptBeforeClosing"] as bool?;
final autoSaveOnExit = data["autoSaveOnExit"] as bool?;
final activeEnvironmentId = data["activeEnvironmentId"] as String?;
final historyRetentionPeriodStr = data["historyRetentionPeriod"] as String?;
HistoryRetentionPeriod? historyRetentionPeriod;
Expand Down Expand Up @@ -160,6 +166,7 @@ class SettingsModel {
defaultCodeGenLang: defaultCodeGenLang,
saveResponses: saveResponses,
promptBeforeClosing: promptBeforeClosing,
autoSaveOnExit: autoSaveOnExit,
activeEnvironmentId: activeEnvironmentId,
historyRetentionPeriod:
historyRetentionPeriod ?? HistoryRetentionPeriod.oneWeek,
Expand All @@ -182,6 +189,7 @@ class SettingsModel {
"defaultCodeGenLang": defaultCodeGenLang.name,
"saveResponses": saveResponses,
"promptBeforeClosing": promptBeforeClosing,
"autoSaveOnExit": autoSaveOnExit,
"activeEnvironmentId": activeEnvironmentId,
"historyRetentionPeriod": historyRetentionPeriod.name,
"workspaceFolderPath": workspaceFolderPath,
Expand Down Expand Up @@ -209,6 +217,7 @@ class SettingsModel {
other.defaultCodeGenLang == defaultCodeGenLang &&
other.saveResponses == saveResponses &&
other.promptBeforeClosing == promptBeforeClosing &&
other.autoSaveOnExit == autoSaveOnExit &&
other.activeEnvironmentId == activeEnvironmentId &&
other.historyRetentionPeriod == historyRetentionPeriod &&
other.workspaceFolderPath == workspaceFolderPath &&
Expand All @@ -229,6 +238,7 @@ class SettingsModel {
defaultCodeGenLang,
saveResponses,
promptBeforeClosing,
autoSaveOnExit,
activeEnvironmentId,
historyRetentionPeriod,
workspaceFolderPath,
Expand Down
2 changes: 2 additions & 0 deletions lib/providers/settings_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ThemeStateNotifier extends StateNotifier<SettingsModel> {
CodegenLanguage? defaultCodeGenLang,
bool? saveResponses,
bool? promptBeforeClosing,
bool? autoSaveOnExit,
String? activeEnvironmentId,
HistoryRetentionPeriod? historyRetentionPeriod,
String? workspaceFolderPath,
Expand All @@ -45,6 +46,7 @@ class ThemeStateNotifier extends StateNotifier<SettingsModel> {
defaultCodeGenLang: defaultCodeGenLang,
saveResponses: saveResponses,
promptBeforeClosing: promptBeforeClosing,
autoSaveOnExit: autoSaveOnExit,
activeEnvironmentId: activeEnvironmentId,
historyRetentionPeriod: historyRetentionPeriod,
workspaceFolderPath: workspaceFolderPath,
Expand Down
11 changes: 11 additions & 0 deletions lib/screens/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ class SettingsPage extends ConsumerWidget {
.update(promptBeforeClosing: value);
},
),
CheckboxListTile(
title: const Text("Auto-save on Exit"),
subtitle: const Text(
"Automatically save all changes when closing the app"),
value: settings.autoSaveOnExit,
onChanged: (value) {
ref
.read(settingsProvider.notifier)
.update(autoSaveOnExit: value);
},
),
ListTile(
hoverColor: kColorTransparent,
title: const Text('History Retention Period'),
Expand Down