Skip to content

Commit

Permalink
Add: isPasting and isUploading flags, with setters and notifyListener…
Browse files Browse the repository at this point in the history
…s in NoteModel
  • Loading branch information
shukebeta committed Oct 9, 2024
1 parent 384940b commit 2349950
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/models/note_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ class NoteModel with ChangeNotifier {
focusNode = FocusNode();
}

bool isPasting = false;
void setPasting(bool value) {
if (isPasting != value) {
isPasting = value;
notifyListeners();
}
}

bool isUploading = false;
void setUploading(bool value) {
if (isUploading != value) {
isUploading = value;
notifyListeners();
}
}

bool get isPrivate => _isPrivate;

bool get isMarkdown => _isMarkdown;
Expand Down
10 changes: 8 additions & 2 deletions lib/screens/components/note_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class NoteEditState extends State<NoteEdit> {
maintainState: true,
child: IconButton(
onPressed: noteModel.isMarkdown ? () => _pickAndUploadImage(context, noteModel) : null,
icon: const Icon(Icons.add_photo_alternate),
icon: noteModel.isUploading ? const CircularProgressIndicator() : const Icon(Icons.add_photo_alternate),
iconSize: 24.0,
padding: const EdgeInsets.all(12.0),
),
Expand All @@ -166,7 +166,7 @@ class NoteEditState extends State<NoteEdit> {
onPressed: () async {
await _pasteFromClipboard(context, noteModel);
},
icon: const Icon(Icons.paste),
icon: noteModel.isPasting ? const CircularProgressIndicator() : const Icon(Icons.paste),
iconSize: 24.0,
padding: const EdgeInsets.all(12.0),
),
Expand All @@ -180,12 +180,15 @@ class NoteEditState extends State<NoteEdit> {
MultipartFile? imageFile = await imageService.pickImage();

if (imageFile != null) {
noteModel.setUploading(true);
await imageService.uploadImage(
imageFile,
(text) {
noteModel.setUploading(false);
noteModel.content += noteModel.content.isEmpty ? text : '\n$text';
},
(error) {
noteModel.setUploading(false);
Util.showError(scaffoldMessengerState, error);
},
);
Expand All @@ -194,11 +197,14 @@ class NoteEditState extends State<NoteEdit> {

Future<void> _pasteFromClipboard(BuildContext context, NoteModel noteModel) async {
final scaffoldMessengerState = ScaffoldMessenger.of(context);
noteModel.setPasting(true);
await imageService.pasteFromClipboard(
(text) {
noteModel.setPasting(false);
noteModel.content += noteModel.content.isEmpty ? text : '\n$text';
},
(error) {
noteModel.setPasting(false);
Util.showError(scaffoldMessengerState, error);
},
);
Expand Down

0 comments on commit 2349950

Please sign in to comment.