[mob][photos] Use loading indicator instead of blocking progress

This commit is contained in:
Neeraj Gupta 2024-04-19 15:52:41 +05:30
parent 564ca77a8b
commit 08040d9154

View file

@ -3,11 +3,11 @@ import "dart:async";
import "package:flutter/material.dart";
import "package:like_button/like_button.dart";
import "package:logging/logging.dart";
import "package:photos/core/configuration.dart";
import "package:photos/generated/l10n.dart";
import 'package:photos/models/file/file.dart';
import "package:photos/services/favorites_service.dart";
import "package:photos/ui/common/progress_dialog.dart";
import "package:photos/utils/dialog_util.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/utils/toast_util.dart";
class FavoriteWidget extends StatefulWidget {
@ -18,13 +18,13 @@ class FavoriteWidget extends StatefulWidget {
super.key,
});
// State<ShareCollectionPage> createState() => _ShareCollectionPageState();
@override
State<StatefulWidget> createState() => _FavoriteWidgetState();
}
class _FavoriteWidgetState extends State<FavoriteWidget> {
late Logger _logger;
bool _isLoading = false;
@override
void initState() {
@ -42,61 +42,67 @@ class _FavoriteWidgetState extends State<FavoriteWidget> {
future: _fetchData(),
builder: (context, snapshot) {
final bool isLiked = snapshot.data ?? false;
return LikeButton(
size: 24,
isLiked: isLiked,
onTap: (oldValue) async {
final isLiked = !oldValue;
bool hasError = false;
if (isLiked) {
final shouldBlockUser = widget.file.uploadedFileID == null;
late ProgressDialog dialog;
if (shouldBlockUser) {
dialog = createProgressDialog(
context,
S.of(context).addingToFavorites,
);
await dialog.show();
}
try {
await FavoritesService.instance.addToFavorites(
context,
widget.file,
);
} catch (e, s) {
_logger.severe(e, s);
hasError = true;
showToast(context, S.of(context).sorryCouldNotAddToFavorites);
} finally {
if (shouldBlockUser) {
await dialog.hide();
}
}
} else {
try {
await FavoritesService.instance
.removeFromFavorites(context, widget.file);
} catch (e, s) {
_logger.severe(e, s);
hasError = true;
showToast(
context,
S.of(context).sorryCouldNotRemoveFromFavorites,
);
}
}
return hasError ? oldValue : isLiked;
},
likeBuilder: (isLiked) {
return Icon(
isLiked ? Icons.favorite_rounded : Icons.favorite_border_rounded,
color: isLiked
? Colors.pinkAccent
: Colors.white, //same for both themes
size: 24,
);
},
);
return _isLoading
? const EnteLoadingWidget(
size: 12,
) // Add this line
: LikeButton(
size: 24,
isLiked: isLiked,
onTap: (oldValue) async {
if (widget.file.uploadedFileID == null ||
widget.file.ownerID !=
Configuration.instance.getUserID()!) {
setState(() {
_isLoading = true; // Add this line
});
}
final isLiked = !oldValue;
bool hasError = false;
if (isLiked) {
try {
await FavoritesService.instance.addToFavorites(
context,
widget.file,
);
} catch (e, s) {
_logger.severe(e, s);
hasError = true;
showToast(
context, S.of(context).sorryCouldNotAddToFavorites);
}
} else {
try {
await FavoritesService.instance
.removeFromFavorites(context, widget.file);
} catch (e, s) {
_logger.severe(e, s);
hasError = true;
showToast(
context,
S.of(context).sorryCouldNotRemoveFromFavorites,
);
}
}
setState(() {
_isLoading = false; // Add this line
});
return hasError ? oldValue : isLiked;
},
likeBuilder: (isLiked) {
debugPrint(
"File Upload ID ${widget.file.uploadedFileID} & collection ${widget.file.collectionID}");
return Icon(
isLiked
? Icons.favorite_rounded
: Icons.favorite_border_rounded,
color: isLiked
? Colors.pinkAccent
: Colors.white, //same for both themes
size: 24,
);
},
);
},
);
}