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