-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add loading state, error handling, and retry mechanism for memory operations #3503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,8 @@ class MemoryEditSheet extends StatefulWidget { | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class _MemoryEditSheetState extends State<MemoryEditSheet> { | ||||||||||||||||||||||||||||||||||||||||||||||||
| late final TextEditingController contentController; | ||||||||||||||||||||||||||||||||||||||||||||||||
| bool _isSaving = false; | ||||||||||||||||||||||||||||||||||||||||||||||||
| bool _saveFailed = false; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||
| void initState() { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -108,30 +110,47 @@ class _MemoryEditSheetState extends State<MemoryEditSheet> { | |||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| const SizedBox(height: 24), | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (_saveFailed) ...[ | ||||||||||||||||||||||||||||||||||||||||||||||||
| const Text( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 'Failed to save. Please check your connection.', | ||||||||||||||||||||||||||||||||||||||||||||||||
| style: TextStyle( | ||||||||||||||||||||||||||||||||||||||||||||||||
| color: Colors.redAccent, | ||||||||||||||||||||||||||||||||||||||||||||||||
| fontSize: 13, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| textAlign: TextAlign.center, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| const SizedBox(height: 8), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||
| SizedBox( | ||||||||||||||||||||||||||||||||||||||||||||||||
| width: double.infinity, | ||||||||||||||||||||||||||||||||||||||||||||||||
| child: ElevatedButton( | ||||||||||||||||||||||||||||||||||||||||||||||||
| onPressed: () { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (contentController.text.trim().isNotEmpty) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| widget.provider.editMemory(widget.memory, contentController.text, widget.memory.category); | ||||||||||||||||||||||||||||||||||||||||||||||||
| Navigator.pop(context); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||
| onPressed: _isSaving ? null : _handleSave, | ||||||||||||||||||||||||||||||||||||||||||||||||
| style: ElevatedButton.styleFrom( | ||||||||||||||||||||||||||||||||||||||||||||||||
| backgroundColor: Colors.deepPurpleAccent, | ||||||||||||||||||||||||||||||||||||||||||||||||
| backgroundColor: _saveFailed ? Colors.orange : Colors.deepPurpleAccent, | ||||||||||||||||||||||||||||||||||||||||||||||||
| foregroundColor: Colors.white, | ||||||||||||||||||||||||||||||||||||||||||||||||
| padding: const EdgeInsets.symmetric(vertical: 14), | ||||||||||||||||||||||||||||||||||||||||||||||||
| shape: RoundedRectangleBorder( | ||||||||||||||||||||||||||||||||||||||||||||||||
| borderRadius: BorderRadius.circular(12), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| disabledBackgroundColor: Colors.deepPurpleAccent.withOpacity(0.5), | ||||||||||||||||||||||||||||||||||||||||||||||||
| disabledForegroundColor: Colors.white.withOpacity(0.7), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| child: const Text( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 'Save Memory', | ||||||||||||||||||||||||||||||||||||||||||||||||
| style: TextStyle( | ||||||||||||||||||||||||||||||||||||||||||||||||
| fontSize: 16, | ||||||||||||||||||||||||||||||||||||||||||||||||
| fontWeight: FontWeight.w600, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| child: _isSaving | ||||||||||||||||||||||||||||||||||||||||||||||||
| ? const SizedBox( | ||||||||||||||||||||||||||||||||||||||||||||||||
| height: 20, | ||||||||||||||||||||||||||||||||||||||||||||||||
| width: 20, | ||||||||||||||||||||||||||||||||||||||||||||||||
| child: CircularProgressIndicator( | ||||||||||||||||||||||||||||||||||||||||||||||||
| strokeWidth: 2, | ||||||||||||||||||||||||||||||||||||||||||||||||
| valueColor: AlwaysStoppedAnimation<Color>(Colors.white), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
| : Text( | ||||||||||||||||||||||||||||||||||||||||||||||||
| _saveFailed ? 'Retry' : 'Save Memory', | ||||||||||||||||||||||||||||||||||||||||||||||||
| style: const TextStyle( | ||||||||||||||||||||||||||||||||||||||||||||||||
| fontSize: 16, | ||||||||||||||||||||||||||||||||||||||||||||||||
| fontWeight: FontWeight.w600, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -140,6 +159,39 @@ class _MemoryEditSheetState extends State<MemoryEditSheet> { | |||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Future<void> _handleSave() async { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (contentController.text.trim().isEmpty) return; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| setState(() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| _isSaving = true; | ||||||||||||||||||||||||||||||||||||||||||||||||
| _saveFailed = false; | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| bool success; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||
| success = await widget.provider.editMemory( | ||||||||||||||||||||||||||||||||||||||||||||||||
| widget.memory, | ||||||||||||||||||||||||||||||||||||||||||||||||
| contentController.text, | ||||||||||||||||||||||||||||||||||||||||||||||||
| widget.memory.category, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| success = false; | ||||||||||||||||||||||||||||||||||||||||||||||||
| debugPrint('Error saving memory: $e'); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+172
to
+181
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!mounted) return; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| setState(() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| _isSaving = false; | ||||||||||||||||||||||||||||||||||||||||||||||||
| _saveFailed = !success; | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (success) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| Navigator.pop(context); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Future<void> _showDeleteConfirmation(BuildContext context) async { | ||||||||||||||||||||||||||||||||||||||||||||||||
| final shouldDelete = await DeleteConfirmation.show(context); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (shouldDelete) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.