1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 08:16:28 +00:00
comunicmobile/lib/ui/widgets/FrienshipStatusWidget.dart

116 lines
3.4 KiB
Dart
Raw Normal View History

2019-06-15 14:01:58 +00:00
import 'package:comunic/helpers/friends_helper.dart';
import 'package:comunic/models/friend_status.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
/// Friendship status widget
///
/// @author Pierre HUBERT
const WhiteTextColorStyle = TextStyle(color: Colors.white);
class FriendshipStatusWidget extends StatefulWidget {
final FriendStatus status;
final void Function() onFriendshipUpdated;
const FriendshipStatusWidget({
Key? key,
required this.status,
required this.onFriendshipUpdated,
2022-03-11 15:40:56 +00:00
}) : super(key: key);
2019-06-15 14:01:58 +00:00
@override
_FriendshipStatusWidgetState createState() => _FriendshipStatusWidgetState();
}
class _FriendshipStatusWidgetState extends State<FriendshipStatusWidget> {
final FriendsHelper _friendsHelper = FriendsHelper();
bool _sentRequest = false;
int get friendID => widget.status.userID;
void setSentRequest(bool sent) => setState(() => _sentRequest = sent);
@override
void didUpdateWidget(FriendshipStatusWidget oldWidget) {
super.didUpdateWidget(oldWidget);
setSentRequest(false);
}
@override
Widget build(BuildContext context) {
if (_sentRequest) return Container();
// No request sent yet
if (widget.status.noRequestExchanged) {
2021-03-13 14:38:43 +00:00
return ElevatedButton(
child: Text(tr("Send request")!.toUpperCase()),
2019-06-15 14:01:58 +00:00
onPressed: () =>
executeRequest(() => _friendsHelper.sendRequest(friendID)),
);
}
// Already sent a friendship request
if (widget.status.sentRequest) {
2021-03-13 14:38:43 +00:00
return ElevatedButton(
2019-06-15 14:01:58 +00:00
child: Text(
tr("Cancel request")!.toUpperCase(),
2019-06-15 14:01:58 +00:00
style: WhiteTextColorStyle,
),
2021-03-13 14:38:43 +00:00
style:
ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.red)),
2019-06-15 14:01:58 +00:00
onPressed: () =>
executeRequest(() => _friendsHelper.cancelRequest(friendID)),
);
}
// Received a friendship request
if (widget.status.receivedRequest) {
return Column(
children: <Widget>[
2021-03-13 14:42:19 +00:00
ElevatedButton(
2019-06-15 14:01:58 +00:00
child: Text(
tr("Accept request")!.toUpperCase(),
2019-06-15 14:01:58 +00:00
style: WhiteTextColorStyle,
),
2021-03-13 14:42:19 +00:00
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green)),
2019-06-15 14:01:58 +00:00
onPressed: () => executeRequest(
() => _friendsHelper.respondRequest(friendID, true)),
),
2021-03-13 14:42:19 +00:00
ElevatedButton(
2019-06-15 14:01:58 +00:00
child: Text(
tr("Reject request")!.toUpperCase(),
2019-06-15 14:01:58 +00:00
style: WhiteTextColorStyle,
),
2021-03-13 14:42:19 +00:00
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red)),
2019-06-15 14:01:58 +00:00
onPressed: () => executeRequest(
() => _friendsHelper.respondRequest(friendID, false)),
)
],
);
}
// The two users are friends, offers to follow him
2021-03-13 14:38:43 +00:00
return ElevatedButton(
child: Text((widget.status.following ? tr("Following") : tr("Follow"))!
2019-06-15 14:01:58 +00:00
.toUpperCase()),
onPressed: () => executeRequest(() =>
_friendsHelper.setFollowing(friendID, !widget.status.following)),
);
}
/// Send friendship request
Future<void> executeRequest(Future<bool> Function() func) async {
setSentRequest(true);
if (!await func())
showSimpleSnack(context, tr("Could not update your membership!")!);
2019-06-15 14:01:58 +00:00
widget.onFriendshipUpdated();
}
}