relatica/lib/controls/timeline/interactions_bar_control.dart

103 lines
3.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.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;
const InteractionsBarControl({super.key, required this.entry});
@override
State<InteractionsBarControl> createState() => _InteractionsBarControlState();
}
class _InteractionsBarControlState extends State<InteractionsBarControl> {
static final _logger = Logger('$InteractionsBarControl');
2022-11-22 16:43:16 +00:00
bool get isPost => widget.entry.parentId.isEmpty;
bool get isFavorited => widget.entry.isFavorited;
2022-11-22 16:43:16 +00:00
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<void> toggleFavorited() async {
final newState = !isFavorited;
_logger.finest('Trying to toggle favorite from $isFavorited to $newState');
final result = await getIt<TimelineManager>()
.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');
});
}
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}');
});
}
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}');
});
}
@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'),
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(
onPressed: () async =>
isReshared ? await unResharePost() : await resharePost(),
icon:
Icon(isReshared ? Icons.repeat_on_outlined : Icons.repeat))
]),
],
);
}
}