fluffychat/lib/components/list_items/chat_list_item.dart

153 lines
4.9 KiB
Dart
Raw Normal View History

2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/message_content.dart';
2020-01-03 09:20:30 +01:00
import 'package:fluffychat/utils/chat_time.dart';
2020-01-01 19:10:13 +01:00
import 'package:fluffychat/utils/app_route.dart';
2020-01-08 20:43:30 +01:00
import 'package:fluffychat/utils/room_name_calculator.dart';
2020-01-01 19:10:13 +01:00
import 'package:fluffychat/views/chat.dart';
import 'package:flutter/material.dart';
2020-01-05 12:27:03 +01:00
import 'package:toast/toast.dart';
2020-01-01 19:10:13 +01:00
import '../avatar.dart';
2020-01-05 12:27:03 +01:00
import '../matrix.dart';
2020-01-01 19:10:13 +01:00
class ChatListItem extends StatelessWidget {
final Room room;
final bool activeChat;
2020-01-05 12:27:03 +01:00
final Function onForget;
2020-01-01 19:10:13 +01:00
2020-01-05 12:27:03 +01:00
const ChatListItem(this.room, {this.activeChat = false, this.onForget});
void clickAction(BuildContext context) async {
if (!activeChat) {
if (room.membership == Membership.invite &&
await Matrix.of(context).tryRequestWithLoadingDialog(room.join()) ==
false) {
return;
}
if (room.membership == Membership.ban) {
Toast.show("You have been banned from this chat", context, duration: 5);
return;
}
if (room.membership == Membership.leave) {
await showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("Archived Room"),
content: Text("This room has been archived."),
actions: <Widget>[
FlatButton(
child: Text("Close".toUpperCase(),
style: TextStyle(color: Colors.blueGrey)),
onPressed: () => Navigator.of(context).pop(),
),
FlatButton(
child: Text("Forget".toUpperCase(),
style: TextStyle(color: Colors.red)),
onPressed: () async {
await Matrix.of(context)
.tryRequestWithLoadingDialog(room.forget());
await Navigator.of(context).pop();
if (this.onForget != null) this.onForget();
},
),
FlatButton(
child: Text("Rejoin".toUpperCase(),
style: TextStyle(color: Colors.blue)),
onPressed: () async {
await Matrix.of(context)
.tryRequestWithLoadingDialog(room.join());
await Navigator.of(context).pop();
},
),
],
),
);
}
if (room.membership == Membership.join) {
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, Chat(room.id)),
(r) => r.isFirst,
);
}
}
}
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
return Material(
color: activeChat ? Color(0xFFE8E8E8) : Colors.white,
child: ListTile(
leading: Avatar(room.avatar),
2020-01-03 09:09:14 +01:00
title: Row(
children: <Widget>[
Expanded(
child: Text(
2020-01-08 20:43:30 +01:00
RoomNameCalculator(room).name,
2020-01-03 09:09:14 +01:00
maxLines: 1,
2020-01-03 12:04:24 +01:00
overflow: TextOverflow.ellipsis,
),
),
SizedBox(width: 16),
Text(
ChatTime(room.timeCreated).toEventTimeString(),
style: TextStyle(
color: Color(0xFF555555),
2020-01-03 14:44:39 +01:00
fontSize: 13,
2020-01-03 09:09:14 +01:00
),
),
],
),
subtitle: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
2020-01-05 12:27:03 +01:00
Expanded(
child: room.membership == Membership.invite
? Text(
"You are invited to this chat",
style: TextStyle(
color: Theme.of(context).primaryColor,
),
)
: MessageContent(
room.lastEvent,
textOnly: true,
),
),
2020-01-03 09:09:14 +01:00
SizedBox(width: 8),
room.pushRuleState == PushRuleState.notify
? Container()
: Icon(
Icons.notifications_off,
color: Colors.grey,
2020-01-03 14:44:39 +01:00
size: 16,
2020-01-03 09:09:14 +01:00
),
room.notificationCount > 0
? Container(
2020-01-03 14:44:39 +01:00
padding: EdgeInsets.symmetric(horizontal: 5),
2020-01-03 09:09:14 +01:00
height: 20,
decoration: BoxDecoration(
color: room.highlightCount > 0
? Colors.red
2020-01-03 11:57:00 +01:00
: Theme.of(context).primaryColor,
2020-01-03 09:09:14 +01:00
borderRadius: BorderRadius.circular(20),
),
child: Center(
child: Text(
room.notificationCount.toString(),
style: TextStyle(color: Colors.white),
),
),
)
: Text(" "),
],
2020-01-02 23:38:46 +01:00
),
2020-01-05 12:27:03 +01:00
onTap: () => clickAction(context),
2020-01-01 19:10:13 +01:00
),
);
}
}