2022-11-18 21:50:15 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-11-23 02:59:08 +00:00
|
|
|
import 'package:go_router/go_router.dart';
|
2022-11-21 21:21:45 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2022-11-18 21:50:15 +00:00
|
|
|
|
|
|
|
import '../../globals.dart';
|
|
|
|
import '../../models/timeline_entry.dart';
|
2022-11-21 21:21:45 +00:00
|
|
|
import '../../services/timeline_manager.dart';
|
2022-11-18 21:50:15 +00:00
|
|
|
import '../../utils/snackbar_builder.dart';
|
|
|
|
|
|
|
|
class InteractionsBarControl extends StatefulWidget {
|
|
|
|
final TimelineEntry entry;
|
2022-11-21 21:21:45 +00:00
|
|
|
|
2022-11-22 19:42:26 +00:00
|
|
|
final bool isMine;
|
|
|
|
|
|
|
|
const InteractionsBarControl(
|
|
|
|
{super.key, required this.entry, required this.isMine});
|
2022-11-18 21:50:15 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<InteractionsBarControl> createState() => _InteractionsBarControlState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _InteractionsBarControlState extends State<InteractionsBarControl> {
|
2022-11-21 21:21:45 +00:00
|
|
|
static final _logger = Logger('$InteractionsBarControl');
|
2022-11-18 21:50:15 +00:00
|
|
|
|
2022-11-22 16:43:16 +00:00
|
|
|
bool get isPost => widget.entry.parentId.isEmpty;
|
|
|
|
|
2022-11-21 21:21:45 +00:00
|
|
|
bool get isFavorited => widget.entry.isFavorited;
|
|
|
|
|
2022-11-22 16:43:16 +00:00
|
|
|
bool get isReshared => widget.entry.isReshare;
|
|
|
|
|
2022-11-21 21:21:45 +00:00
|
|
|
int get reshares => widget.entry.engagementSummary.rebloggedCount;
|
|
|
|
|
|
|
|
int get comments => widget.entry.engagementSummary.repliesCount;
|
|
|
|
|
|
|
|
int get likes => widget.entry.engagementSummary.favoritesCount;
|
2022-11-18 21:50:15 +00:00
|
|
|
|
|
|
|
Future<void> toggleFavorited() async {
|
|
|
|
final newState = !isFavorited;
|
2022-11-21 21:21:45 +00:00
|
|
|
_logger.finest('Trying to toggle favorite from $isFavorited to $newState');
|
|
|
|
final result = await getIt<TimelineManager>()
|
2022-11-18 21:50:15 +00:00
|
|
|
.toggleFavorited(widget.entry.id, newState);
|
|
|
|
result.match(onSuccess: (update) {
|
|
|
|
setState(() {
|
2022-11-21 21:21:45 +00:00
|
|
|
_logger.finest(
|
|
|
|
'Success toggling! $isFavorited -> ${update.entry.isFavorited}');
|
2022-11-18 21:50:15 +00:00
|
|
|
});
|
|
|
|
}, onError: (error) {
|
|
|
|
buildSnackbar(context, 'Error toggling like status: $error');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-22 16:43:16 +00:00
|
|
|
Future<void> resharePost() async {
|
|
|
|
final id = widget.entry.id;
|
|
|
|
_logger.finest('Trying to reshare $id');
|
|
|
|
final result = await getIt<TimelineManager>().resharePost(id);
|
|
|
|
result.match(onSuccess: (update) {
|
|
|
|
setState(() {
|
|
|
|
_logger.finest('Success resharing post by ${widget.entry.author}');
|
|
|
|
});
|
|
|
|
}, onError: (error) {
|
|
|
|
buildSnackbar(context, 'Error resharing post by ${widget.entry.author}');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-23 02:59:08 +00:00
|
|
|
Future<void> addComment() async {
|
|
|
|
context.push('/comment/new?parent_id=${widget.entry.id}');
|
|
|
|
}
|
|
|
|
|
2022-11-22 16:43:16 +00:00
|
|
|
Future<void> unResharePost() async {
|
|
|
|
final id = widget.entry.id;
|
|
|
|
_logger.finest('Trying to un-reshare $id');
|
|
|
|
final result = await getIt<TimelineManager>().unResharePost(id);
|
|
|
|
result.match(onSuccess: (update) {
|
|
|
|
setState(() {
|
|
|
|
_logger.finest('Success un-resharing post by ${widget.entry.author}');
|
|
|
|
});
|
|
|
|
}, onError: (error) {
|
|
|
|
buildSnackbar(
|
|
|
|
context, 'Error un-resharing post by ${widget.entry.author}');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:50:15 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-11-21 21:21:45 +00:00
|
|
|
_logger.finest('Building: ${widget.entry.toShortString()}');
|
2022-11-18 21:50:15 +00:00
|
|
|
return Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text('$likes likes, $reshares reshares, $comments comments'),
|
2022-11-22 16:43:16 +00:00
|
|
|
Row(children: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () async => await toggleFavorited(),
|
|
|
|
icon: isFavorited
|
|
|
|
? const Icon(
|
|
|
|
Icons.thumb_up,
|
|
|
|
semanticLabel: 'Like',
|
|
|
|
)
|
|
|
|
: Icon(Icons.thumb_up_outlined)),
|
|
|
|
if (isPost)
|
|
|
|
IconButton(
|
2022-11-22 19:42:26 +00:00
|
|
|
onPressed: widget.isMine
|
|
|
|
? null
|
|
|
|
: () async => isReshared
|
|
|
|
? await unResharePost()
|
|
|
|
: await resharePost(),
|
2022-11-22 16:43:16 +00:00
|
|
|
icon:
|
2022-11-23 02:59:08 +00:00
|
|
|
Icon(isReshared ? Icons.repeat_on_outlined : Icons.repeat)),
|
|
|
|
IconButton(onPressed: addComment, icon: Icon(Icons.add_comment)),
|
2022-11-22 16:43:16 +00:00
|
|
|
]),
|
2022-11-18 21:50:15 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|