From b2cd91274f759e73194d39140c5c1b4fe0e821dc Mon Sep 17 00:00:00 2001 From: DenserMeerkat Date: Mon, 22 Jul 2024 00:56:44 +0530 Subject: [PATCH] wip: manage history dialog --- lib/consts.dart | 18 +++- lib/models/settings_model.dart | 23 ++++- lib/providers/settings_providers.dart | 2 + lib/screens/envvar/environments_pane.dart | 2 +- lib/screens/history/history_pane.dart | 4 + .../history_widgets/his_bottombar.dart | 2 +- .../history_widgets/his_sidebar_header.dart | 16 +++- lib/screens/home_page/collection_pane.dart | 2 +- lib/widgets/dialog_history_retention.dart | 85 +++++++++++++++++++ lib/widgets/request_widgets.dart | 4 + lib/widgets/widgets.dart | 1 + 11 files changed, 149 insertions(+), 10 deletions(-) create mode 100644 lib/widgets/dialog_history_retention.dart diff --git a/lib/consts.dart b/lib/consts.dart index ed2bc4e1..5e8790c0 100644 --- a/lib/consts.dart +++ b/lib/consts.dart @@ -109,6 +109,7 @@ const kPv2 = EdgeInsets.symmetric(vertical: 2); const kPv6 = EdgeInsets.symmetric(vertical: 6); const kPv8 = EdgeInsets.symmetric(vertical: 8); const kPv10 = EdgeInsets.symmetric(vertical: 10); +const kPv20 = EdgeInsets.symmetric(vertical: 20); const kPh2 = EdgeInsets.symmetric(horizontal: 2); const kPt28o8 = EdgeInsets.only(top: 28, left: 8.0, right: 8.0, bottom: 8.0); const kPt5o10 = @@ -116,9 +117,8 @@ const kPt5o10 = const kPh4 = EdgeInsets.symmetric(horizontal: 4); const kPh8 = EdgeInsets.symmetric(horizontal: 8); const kPh12 = EdgeInsets.symmetric(horizontal: 12); -const kPh20 = EdgeInsets.symmetric( - horizontal: 20, -); +const kPh20 = EdgeInsets.symmetric(horizontal: 20); +const kPh24 = EdgeInsets.symmetric(horizontal: 24); const kPh20t40 = EdgeInsets.only( left: 20, right: 20, @@ -178,6 +178,7 @@ const kHSpacer40 = SizedBox(width: 40); const kVSpacer5 = SizedBox(height: 5); const kVSpacer8 = SizedBox(height: 8); const kVSpacer10 = SizedBox(height: 10); +const kVSpacer16 = SizedBox(height: 16); const kVSpacer20 = SizedBox(height: 20); const kVSpacer40 = SizedBox(height: 40); @@ -331,6 +332,17 @@ class ButtonData { final String tooltip; } +enum HistoryRetentionPeriod { + oneWeek("1 Week", Icons.calendar_view_week_rounded), + oneMonth("1 Month", Icons.calendar_view_month_rounded), + threeMonths("3 Months", Icons.calendar_month_rounded), + forever("Forever", Icons.all_inclusive_rounded); + + const HistoryRetentionPeriod(this.label, this.icon); + final String label; + final IconData icon; +} + enum ItemMenuOption { edit("Rename"), delete("Delete"), diff --git a/lib/models/settings_model.dart b/lib/models/settings_model.dart index 846bdf52..e82ffdca 100644 --- a/lib/models/settings_model.dart +++ b/lib/models/settings_model.dart @@ -13,6 +13,7 @@ class SettingsModel { this.saveResponses = true, this.promptBeforeClosing = true, this.activeEnvironmentId, + this.historyRetentionPeriod = HistoryRetentionPeriod.oneWeek, }); final bool isDark; @@ -24,6 +25,7 @@ class SettingsModel { final bool saveResponses; final bool promptBeforeClosing; final String? activeEnvironmentId; + final HistoryRetentionPeriod historyRetentionPeriod; SettingsModel copyWith({ bool? isDark, @@ -35,6 +37,7 @@ class SettingsModel { bool? saveResponses, bool? promptBeforeClosing, String? activeEnvironmentId, + HistoryRetentionPeriod? historyRetentionPeriod, }) { return SettingsModel( isDark: isDark ?? this.isDark, @@ -47,6 +50,8 @@ class SettingsModel { saveResponses: saveResponses ?? this.saveResponses, promptBeforeClosing: promptBeforeClosing ?? this.promptBeforeClosing, activeEnvironmentId: activeEnvironmentId ?? this.activeEnvironmentId, + historyRetentionPeriod: + historyRetentionPeriod ?? this.historyRetentionPeriod, ); } @@ -80,6 +85,18 @@ class SettingsModel { final saveResponses = data["saveResponses"] as bool?; final promptBeforeClosing = data["promptBeforeClosing"] as bool?; final activeEnvironmentId = data["activeEnvironmentId"] as String?; + final historyRetentionPeriodStr = data["historyRetentionPeriod"] as String?; + HistoryRetentionPeriod historyRetentionPeriod = + HistoryRetentionPeriod.oneWeek; + if (historyRetentionPeriodStr != null) { + try { + historyRetentionPeriod = + HistoryRetentionPeriod.values.byName(historyRetentionPeriodStr); + } catch (e) { + // pass + } + } + const sm = SettingsModel(); return sm.copyWith( @@ -92,6 +109,7 @@ class SettingsModel { saveResponses: saveResponses, promptBeforeClosing: promptBeforeClosing, activeEnvironmentId: activeEnvironmentId, + historyRetentionPeriod: historyRetentionPeriod, ); } @@ -108,6 +126,7 @@ class SettingsModel { "saveResponses": saveResponses, "promptBeforeClosing": promptBeforeClosing, "activeEnvironmentId": activeEnvironmentId, + "historyRetentionPeriod": historyRetentionPeriod.name, }; } @@ -129,7 +148,8 @@ class SettingsModel { other.defaultCodeGenLang == defaultCodeGenLang && other.saveResponses == saveResponses && other.promptBeforeClosing == promptBeforeClosing && - other.activeEnvironmentId == activeEnvironmentId; + other.activeEnvironmentId == activeEnvironmentId && + other.historyRetentionPeriod == historyRetentionPeriod; } @override @@ -145,6 +165,7 @@ class SettingsModel { saveResponses, promptBeforeClosing, activeEnvironmentId, + historyRetentionPeriod, ); } } diff --git a/lib/providers/settings_providers.dart b/lib/providers/settings_providers.dart index 0fff037b..6293e04b 100644 --- a/lib/providers/settings_providers.dart +++ b/lib/providers/settings_providers.dart @@ -30,6 +30,7 @@ class ThemeStateNotifier extends StateNotifier { bool? saveResponses, bool? promptBeforeClosing, String? activeEnvironmentId, + HistoryRetentionPeriod? historyRetentionPeriod, }) async { state = state.copyWith( isDark: isDark, @@ -41,6 +42,7 @@ class ThemeStateNotifier extends StateNotifier { saveResponses: saveResponses, promptBeforeClosing: promptBeforeClosing, activeEnvironmentId: activeEnvironmentId, + historyRetentionPeriod: historyRetentionPeriod, ); await hiveHandler.saveSettings(state.toJson()); } diff --git a/lib/screens/envvar/environments_pane.dart b/lib/screens/envvar/environments_pane.dart index 340cc0e8..dd6b5af4 100644 --- a/lib/screens/envvar/environments_pane.dart +++ b/lib/screens/envvar/environments_pane.dart @@ -190,8 +190,8 @@ class EnvironmentItem extends ConsumerWidget { ref.read(activeEnvironmentIdStateProvider.notifier).state = id; }, onTap: () { - ref.read(mobileScaffoldKeyStateProvider).currentState?.closeDrawer(); ref.read(selectedEnvironmentIdStateProvider.notifier).state = id; + ref.read(mobileScaffoldKeyStateProvider).currentState?.closeDrawer(); }, focusNode: ref.watch(nameTextFieldFocusNodeProvider), onChangedNameEditor: (value) { diff --git a/lib/screens/history/history_pane.dart b/lib/screens/history/history_pane.dart index 8b090b95..ba89c565 100644 --- a/lib/screens/history/history_pane.dart +++ b/lib/screens/history/history_pane.dart @@ -150,6 +150,10 @@ class _HistoryExpansionTileState extends ConsumerState ref .read(historyMetaStateNotifier.notifier) .loadHistoryRequest(item.first.historyId); + ref + .read(mobileScaffoldKeyStateProvider) + .currentState + ?.closeDrawer(); }, ), ); diff --git a/lib/screens/history/history_widgets/his_bottombar.dart b/lib/screens/history/history_widgets/his_bottombar.dart index 9945b7bc..da6b0ffe 100644 --- a/lib/screens/history/history_widgets/his_bottombar.dart +++ b/lib/screens/history/history_widgets/his_bottombar.dart @@ -82,7 +82,7 @@ class HistorySheetButton extends StatelessWidget { isScrollControlled: true, builder: (context) { return ConstrainedBox( - constraints: const BoxConstraints(maxWidth: 500), + constraints: const BoxConstraints(maxWidth: 400), child: const HistorRequestsScrollableSheet()); }, ); diff --git a/lib/screens/history/history_widgets/his_sidebar_header.dart b/lib/screens/history/history_widgets/his_sidebar_header.dart index e431c4b7..6af1e1e8 100644 --- a/lib/screens/history/history_widgets/his_sidebar_header.dart +++ b/lib/screens/history/history_widgets/his_sidebar_header.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:apidash/extensions/extensions.dart'; import 'package:apidash/providers/providers.dart'; +import 'package:apidash/widgets/widgets.dart'; import 'package:apidash/consts.dart'; class HistorySidebarHeader extends ConsumerWidget { @@ -21,13 +22,22 @@ class HistorySidebarHeader extends ConsumerWidget { ), const Spacer(), IconButton( - tooltip: "Auto Delete Settings", + tooltip: "Manage History", style: IconButton.styleFrom( foregroundColor: Theme.of(context).colorScheme.primary, ), - onPressed: () {}, + onPressed: () { + showHistoryRetentionDialog( + context, + ref.read(settingsProvider.select( + (value) => value.historyRetentionPeriod)), (value) { + ref.read(settingsProvider.notifier).update( + historyRetentionPeriod: value, + ); + }); + }, icon: const Icon( - Icons.auto_delete_outlined, + Icons.manage_history_rounded, size: 20, ), ), diff --git a/lib/screens/home_page/collection_pane.dart b/lib/screens/home_page/collection_pane.dart index 521fdad8..7c40ef3f 100644 --- a/lib/screens/home_page/collection_pane.dart +++ b/lib/screens/home_page/collection_pane.dart @@ -220,8 +220,8 @@ class RequestItem extends ConsumerWidget { selectedId: selectedId, editRequestId: editRequestId, onTap: () { - ref.read(mobileScaffoldKeyStateProvider).currentState?.closeDrawer(); ref.read(selectedIdStateProvider.notifier).state = id; + ref.read(mobileScaffoldKeyStateProvider).currentState?.closeDrawer(); }, // onDoubleTap: () { // ref.read(selectedIdStateProvider.notifier).state = id; diff --git a/lib/widgets/dialog_history_retention.dart b/lib/widgets/dialog_history_retention.dart new file mode 100644 index 00000000..67f513ac --- /dev/null +++ b/lib/widgets/dialog_history_retention.dart @@ -0,0 +1,85 @@ +import 'package:flutter/material.dart'; +import 'package:apidash/consts.dart'; + +showHistoryRetentionDialog( + BuildContext context, + HistoryRetentionPeriod historyRetentionPeriod, + Function(HistoryRetentionPeriod) onRetentionPeriodChange, +) { + HistoryRetentionPeriod selectedRetentionPeriod = historyRetentionPeriod; + + showDialog( + context: context, + builder: (context) { + return AlertDialog( + icon: const Icon(Icons.manage_history_rounded), + iconColor: Theme.of(context).colorScheme.primary, + title: const Text("Manage History"), + titleTextStyle: Theme.of(context).textTheme.titleLarge, + contentPadding: kPv20, + content: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 320), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Padding( + padding: kPh24, + child: Text( + "Select the duration for which you want to retain your request history", + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: Theme.of(context) + .colorScheme + .onSurface + .withOpacity(0.8), + ), + ), + ), + kVSpacer10, + ...HistoryRetentionPeriod.values + .map((e) => RadioListTile( + title: Text( + e.label, + style: TextStyle( + color: selectedRetentionPeriod == e + ? Theme.of(context).colorScheme.primary + : null), + ), + secondary: Icon(e.icon, + color: selectedRetentionPeriod == e + ? Theme.of(context).colorScheme.primary + : Theme.of(context) + .colorScheme + .onSurface + .withOpacity(0.6)), + value: e, + groupValue: selectedRetentionPeriod, + onChanged: (value) { + if (value != null) { + selectedRetentionPeriod = value; + (context as Element).markNeedsBuild(); + } + }, + )) + ], + ), + ), + actions: [ + TextButton( + child: const Text('Cancel'), + onPressed: () { + Navigator.pop(context); + }, + ), + TextButton( + child: const Text('Confirm'), + onPressed: () { + onRetentionPeriodChange(selectedRetentionPeriod); + Navigator.pop(context); + }, + ), + ], + ); + }, + ); +} diff --git a/lib/widgets/request_widgets.dart b/lib/widgets/request_widgets.dart index 1e7b7ccf..5ad400aa 100644 --- a/lib/widgets/request_widgets.dart +++ b/lib/widgets/request_widgets.dart @@ -52,6 +52,10 @@ class _RequestPaneState extends State mainAxisAlignment: MainAxisAlignment.end, children: [ FilledButton.tonalIcon( + style: FilledButton.styleFrom( + padding: kPh12, + minimumSize: const Size(44, 44), + ), onPressed: widget.onPressedCodeButton, icon: Icon( widget.codePaneVisible diff --git a/lib/widgets/widgets.dart b/lib/widgets/widgets.dart index 5383916d..fcb97a6b 100644 --- a/lib/widgets/widgets.dart +++ b/lib/widgets/widgets.dart @@ -15,6 +15,7 @@ export 'checkbox.dart'; export 'code_previewer.dart'; export 'codegen_previewer.dart'; export 'dialog_about.dart'; +export 'dialog_history_retention.dart'; export 'dialog_import.dart'; export 'dialog_rename.dart'; export 'drag_and_drop_area.dart';