fluffychat/lib/views/chat_details.dart

531 lines
24 KiB
Dart
Raw Normal View History

2020-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-01-16 12:46:38 +01:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2020-12-11 14:14:33 +01:00
import 'package:fluffychat/app_config.dart';
import 'package:fluffychat/components/avatar.dart';
2021-01-16 12:46:38 +01:00
import 'package:fluffychat/components/matrix.dart';
2020-11-22 21:51:30 +01:00
import 'package:fluffychat/utils/fluffy_share.dart';
import 'package:flushbar/flushbar_helper.dart';
2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
2020-10-04 17:01:54 +02:00
import 'package:file_picker_cross/file_picker_cross.dart';
2020-01-01 19:10:13 +01:00
import 'package:fluffychat/components/chat_settings_popup_menu.dart';
import 'package:fluffychat/components/content_banner.dart';
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-01-01 19:10:13 +01:00
import 'package:fluffychat/components/list_items/participant_list_item.dart';
import 'package:fluffychat/utils/matrix_locals.dart';
2020-10-04 17:01:54 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-01-01 19:10:13 +01:00
import 'package:image_picker/image_picker.dart';
2020-09-05 13:45:03 +02:00
import 'package:matrix_link_text/link_text.dart';
2020-09-05 13:45:03 +02:00
import '../utils/url_launcher.dart';
2020-01-01 19:10:13 +01:00
class ChatDetails extends StatefulWidget {
2021-01-16 12:46:38 +01:00
final String roomId;
2020-01-01 19:10:13 +01:00
2021-01-16 12:46:38 +01:00
const ChatDetails(this.roomId);
2020-01-01 19:10:13 +01:00
@override
_ChatDetailsState createState() => _ChatDetailsState();
}
class _ChatDetailsState extends State<ChatDetails> {
2021-01-16 12:46:38 +01:00
Room room;
2020-01-01 19:10:13 +01:00
List<User> members;
2020-02-16 11:09:28 +01:00
void setDisplaynameAction(BuildContext context) async {
2020-11-14 10:08:13 +01:00
final input = await showTextInputDialog(
context: context,
title: L10n.of(context).changeTheNameOfTheGroup,
textFields: [
DialogTextField(
2021-01-16 12:46:38 +01:00
initialText: room.getLocalizedDisplayname(
2020-11-14 10:08:13 +01:00
MatrixLocals(
L10n.of(context),
),
),
)
],
2020-02-16 11:09:28 +01:00
);
2020-11-14 10:08:13 +01:00
if (input == null) return;
2020-12-25 09:58:34 +01:00
final success = await showFutureLoadingDialog(
context: context,
2021-01-16 12:46:38 +01:00
future: () => room.setName(input.single),
2020-01-01 19:10:13 +01:00
);
2020-12-25 09:58:34 +01:00
if (success.error == null) {
await FlushbarHelper.createSuccess(
message: L10n.of(context).displaynameHasBeenChanged)
.show(context);
2020-01-01 19:10:13 +01:00
}
}
2020-02-16 11:09:28 +01:00
void setCanonicalAliasAction(context) async {
2020-11-14 10:08:13 +01:00
final input = await showTextInputDialog(
context: context,
title: L10n.of(context).setInvitationLink,
textFields: [
DialogTextField(
hintText: '#localpart:domain',
initialText: L10n.of(context).alias.toLowerCase(),
)
],
2020-02-16 11:09:28 +01:00
);
2020-11-14 10:08:13 +01:00
if (input == null) return;
2021-01-16 12:46:38 +01:00
final domain = room.client.userID.domain;
2020-11-14 10:08:13 +01:00
final canonicalAlias = '%23' + input.single + '%3A' + domain;
2021-01-16 12:46:38 +01:00
final aliasEvent = room.getState('m.room.aliases', domain);
2020-05-13 15:58:59 +02:00
final aliases =
aliasEvent != null ? aliasEvent.content['aliases'] ?? [] : [];
2020-01-19 15:07:42 +01:00
if (aliases.indexWhere((s) => s == canonicalAlias) == -1) {
2020-05-13 15:58:59 +02:00
var newAliases = List<String>.from(aliases);
2020-01-19 15:07:42 +01:00
newAliases.add(canonicalAlias);
2020-12-25 09:58:34 +01:00
final response = await showFutureLoadingDialog(
context: context,
2021-01-16 12:46:38 +01:00
future: () => room.client.requestRoomAliasInformations(canonicalAlias),
2020-01-19 15:07:42 +01:00
);
2020-12-25 09:58:34 +01:00
if (response.error != null) {
final success = await showFutureLoadingDialog(
context: context,
2021-01-16 12:46:38 +01:00
future: () => room.client.createRoomAlias(canonicalAlias, room.id),
2020-01-19 15:07:42 +01:00
);
2020-12-25 09:58:34 +01:00
if (success.error != null) return;
2020-01-19 15:07:42 +01:00
}
}
2020-12-25 09:58:34 +01:00
await showFutureLoadingDialog(
context: context,
2021-01-16 12:46:38 +01:00
future: () => room.client.sendState(room.id, 'm.room.canonical_alias', {
2020-11-14 10:08:13 +01:00
'alias': input.single,
2020-06-10 10:07:01 +02:00
}),
2020-01-19 15:07:42 +01:00
);
}
2020-02-16 11:09:28 +01:00
void setTopicAction(BuildContext context) async {
2020-11-14 10:08:13 +01:00
final input = await showTextInputDialog(
context: context,
title: L10n.of(context).setGroupDescription,
textFields: [
DialogTextField(
hintText: L10n.of(context).setGroupDescription,
2021-01-16 12:46:38 +01:00
initialText: room.topic,
2020-11-27 11:30:21 +01:00
minLines: 1,
maxLines: 4,
2020-11-14 10:08:13 +01:00
)
],
2020-02-16 11:09:28 +01:00
);
2020-11-14 10:08:13 +01:00
if (input == null) return;
2020-12-25 09:58:34 +01:00
final success = await showFutureLoadingDialog(
context: context,
2021-01-16 12:46:38 +01:00
future: () => room.setDescription(input.single),
2020-01-17 12:06:03 +01:00
);
2020-12-25 09:58:34 +01:00
if (success.error == null) {
await FlushbarHelper.createSuccess(
message: L10n.of(context).groupDescriptionHasBeenChanged)
.show(context);
2020-01-17 12:06:03 +01:00
}
}
2020-01-01 19:10:13 +01:00
void setAvatarAction(BuildContext context) async {
2020-10-04 17:01:54 +02:00
MatrixFile file;
if (PlatformInfos.isMobile) {
final result = await ImagePicker().getImage(
source: ImageSource.gallery,
imageQuality: 50,
maxWidth: 1600,
maxHeight: 1600);
if (result == null) return;
file = MatrixFile(
bytes: await result.readAsBytes(),
name: result.path,
);
} else {
final result = await FilePickerCross.importFromStorage(
type: FileTypeCross.image,
);
if (result == null) return;
file = MatrixFile(
bytes: result.toUint8List(),
name: result.fileName,
);
}
2020-12-25 09:58:34 +01:00
final success = await showFutureLoadingDialog(
context: context,
2021-01-16 12:46:38 +01:00
future: () => room.setAvatar(file),
2020-01-01 19:10:13 +01:00
);
2020-12-25 09:58:34 +01:00
if (success.error == null) {
await FlushbarHelper.createSuccess(
message: L10n.of(context).avatarHasBeenChanged)
.show(context);
2020-01-01 19:10:13 +01:00
}
}
void requestMoreMembersAction(BuildContext context) async {
2020-12-25 09:58:34 +01:00
final participants = await showFutureLoadingDialog(
2021-01-16 12:46:38 +01:00
context: context, future: () => room.requestParticipants());
2020-12-25 09:58:34 +01:00
if (participants.error == null) {
setState(() => members = participants.result);
}
2020-01-01 19:10:13 +01:00
}
@override
Widget build(BuildContext context) {
2021-01-16 12:46:38 +01:00
room ??= Matrix.of(context).client.getRoomById(widget.roomId);
if (room == null) {
2020-01-20 13:46:39 +01:00
return Scaffold(
appBar: AppBar(
2021-01-16 14:24:52 +01:00
leading: BackButton(),
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).oopsSomethingWentWrong),
2020-01-20 13:46:39 +01:00
),
body: Center(
2020-05-07 07:52:40 +02:00
child: Text(L10n.of(context).youAreNoLongerParticipatingInThisChat),
2020-01-20 13:46:39 +01:00
),
2020-01-05 12:27:03 +01:00
);
}
2021-01-16 12:46:38 +01:00
members ??= room.getParticipants();
2020-04-02 13:14:39 +02:00
members.removeWhere((u) => u.membership == Membership.leave);
2020-05-13 15:58:59 +02:00
final actualMembersCount =
2021-01-16 12:46:38 +01:00
room.mInvitedMemberCount + room.mJoinedMemberCount;
2020-05-13 15:58:59 +02:00
final canRequestMoreMembers = members.length < actualMembersCount;
2021-01-16 12:46:38 +01:00
return StreamBuilder(
stream: room.onUpdate.stream,
builder: (context, snapshot) {
return Scaffold(
body: NestedScrollView(
headerSliverBuilder:
(BuildContext context, bool innerBoxIsScrolled) => <Widget>[
SliverAppBar(
2021-02-07 10:36:42 +01:00
elevation: Theme.of(context).appBarTheme.elevation,
2021-01-16 14:24:52 +01:00
leading: BackButton(),
2021-01-16 12:46:38 +01:00
expandedHeight: 300.0,
floating: true,
pinned: true,
actions: <Widget>[
if (room.canonicalAlias?.isNotEmpty ?? false)
IconButton(
2021-02-20 07:40:42 +01:00
tooltip: L10n.of(context).share,
2021-01-16 12:46:38 +01:00
icon: Icon(Icons.share_outlined),
onPressed: () => FluffyShare.share(
AppConfig.inviteLinkPrefix + room.canonicalAlias,
context),
),
ChatSettingsPopupMenu(room, false)
],
title: Text(
room.getLocalizedDisplayname(
MatrixLocals(L10n.of(context))),
style: TextStyle(
color: Theme.of(context)
.appBarTheme
.textTheme
.headline6
.color)),
backgroundColor: Theme.of(context).appBarTheme.color,
flexibleSpace: FlexibleSpaceBar(
background: ContentBanner(room.avatar,
onEdit: room.canSendEvent('m.room.avatar')
? () => setAvatarAction(context)
: null),
2020-05-09 07:17:55 +02:00
),
2021-01-16 12:46:38 +01:00
),
],
body: ListView.builder(
itemCount: members.length + 1 + (canRequestMoreMembers ? 1 : 0),
itemBuilder: (BuildContext context, int i) => i == 0
? Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ListTile(
leading: room.canSendEvent('m.room.topic')
? CircleAvatar(
backgroundColor: Theme.of(context)
.scaffoldBackgroundColor,
foregroundColor: Colors.grey,
child: Icon(Icons.edit_outlined),
radius: Avatar.defaultSize / 2,
2021-01-16 12:46:38 +01:00
)
: null,
title: Text('${L10n.of(context).groupDescription}:',
style: TextStyle(
color: Theme.of(context).accentColor,
2021-01-16 12:46:38 +01:00
fontWeight: FontWeight.bold)),
subtitle: LinkText(
text: room.topic?.isEmpty ?? true
? L10n.of(context).addGroupDescription
: room.topic,
linkStyle: TextStyle(color: Colors.blueAccent),
textStyle: TextStyle(
fontSize: 14,
color:
Theme.of(context).textTheme.bodyText2.color,
),
onLinkTap: (url) =>
UrlLauncher(context, url).launchUrl(),
),
onTap: room.canSendEvent('m.room.topic')
? () => setTopicAction(context)
: null,
),
Divider(thickness: 1),
ListTile(
title: Text(
L10n.of(context).settings,
style: TextStyle(
color: Theme.of(context).accentColor,
2021-01-16 12:46:38 +01:00
fontWeight: FontWeight.bold,
2020-05-09 07:17:55 +02:00
),
2020-01-17 10:39:46 +01:00
),
2021-01-16 12:46:38 +01:00
),
if (room.canSendEvent('m.room.name'))
2020-05-09 07:17:55 +02:00
ListTile(
2021-01-16 12:46:38 +01:00
leading: CircleAvatar(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
foregroundColor: Colors.grey,
child: Icon(Icons.people_outlined),
),
2020-05-09 07:17:55 +02:00
title: Text(
2021-01-16 12:46:38 +01:00
L10n.of(context).changeTheNameOfTheGroup),
subtitle: Text(room.getLocalizedDisplayname(
MatrixLocals(L10n.of(context)))),
onTap: () => setDisplaynameAction(context),
),
if (room.canSendEvent('m.room.canonical_alias') &&
room.joinRules == JoinRules.public)
ListTile(
leading: CircleAvatar(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
foregroundColor: Colors.grey,
child: Icon(Icons.link_outlined),
2020-05-09 07:17:55 +02:00
),
2021-01-16 12:46:38 +01:00
onTap: () => setCanonicalAliasAction(context),
title: Text(L10n.of(context).setInvitationLink),
subtitle: Text(
(room.canonicalAlias?.isNotEmpty ?? false)
? room.canonicalAlias
: L10n.of(context).none),
2020-01-03 12:07:04 +01:00
),
2021-01-16 12:46:38 +01:00
ListTile(
leading: CircleAvatar(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
foregroundColor: Colors.grey,
child: Icon(Icons.insert_emoticon_outlined),
),
title: Text(L10n.of(context).emoteSettings),
subtitle: Text(L10n.of(context).setCustomEmotes),
onTap: () async {
// okay, we need to test if there are any emote state events other than the default one
// if so, we need to be directed to a selection screen for which pack we want to look at
// otherwise, we just open the normal one.
if ((room.states
.states['im.ponies.room_emotes'] ??
<String, Event>{})
.keys
.any((String s) => s.isNotEmpty)) {
await AdaptivePageLayout.of(context)
.pushNamed('/rooms/${room.id}/emotes');
} else {
2021-02-01 16:25:28 +01:00
await AdaptivePageLayout.of(context).pushNamed(
'/settings/emotes',
arguments: {'room': room});
2021-01-16 12:46:38 +01:00
}
},
),
PopupMenuButton(
child: ListTile(
leading: CircleAvatar(
2020-05-09 07:17:55 +02:00
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
foregroundColor: Colors.grey,
2021-01-16 12:46:38 +01:00
child: Icon(Icons.public_outlined)),
title: Text(
L10n.of(context).whoIsAllowedToJoinThisGroup),
subtitle: Text(
room.joinRules.getLocalizedString(
MatrixLocals(L10n.of(context))),
2020-05-09 07:17:55 +02:00
),
2021-01-16 12:46:38 +01:00
),
onSelected: (JoinRules joinRule) =>
showFutureLoadingDialog(
context: context,
future: () => room.setJoinRules(joinRule),
),
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<JoinRules>>[
if (room.canChangeJoinRules)
PopupMenuItem<JoinRules>(
value: JoinRules.public,
child: Text(JoinRules.public
.getLocalizedString(
MatrixLocals(L10n.of(context)))),
2020-05-09 07:17:55 +02:00
),
2021-01-16 12:46:38 +01:00
if (room.canChangeJoinRules)
PopupMenuItem<JoinRules>(
value: JoinRules.invite,
child: Text(JoinRules.invite
.getLocalizedString(
MatrixLocals(L10n.of(context)))),
),
],
),
PopupMenuButton(
child: ListTile(
2020-05-12 09:02:33 +02:00
leading: CircleAvatar(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
foregroundColor: Colors.grey,
2021-01-16 12:46:38 +01:00
child: Icon(Icons.visibility_outlined),
2020-02-16 10:48:20 +01:00
),
2021-01-16 12:46:38 +01:00
title: Text(
L10n.of(context).visibilityOfTheChatHistory),
subtitle: Text(
room.historyVisibility.getLocalizedString(
MatrixLocals(L10n.of(context))),
2020-05-09 07:17:55 +02:00
),
),
2021-01-16 12:46:38 +01:00
onSelected: (HistoryVisibility historyVisibility) =>
showFutureLoadingDialog(
context: context,
future: () =>
room.setHistoryVisibility(historyVisibility),
),
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<HistoryVisibility>>[
if (room.canChangeHistoryVisibility)
PopupMenuItem<HistoryVisibility>(
value: HistoryVisibility.invited,
child: Text(HistoryVisibility.invited
.getLocalizedString(
MatrixLocals(L10n.of(context)))),
),
if (room.canChangeHistoryVisibility)
PopupMenuItem<HistoryVisibility>(
value: HistoryVisibility.joined,
child: Text(HistoryVisibility.joined
.getLocalizedString(
MatrixLocals(L10n.of(context)))),
),
if (room.canChangeHistoryVisibility)
PopupMenuItem<HistoryVisibility>(
value: HistoryVisibility.shared,
child: Text(HistoryVisibility.shared
.getLocalizedString(
MatrixLocals(L10n.of(context)))),
),
if (room.canChangeHistoryVisibility)
PopupMenuItem<HistoryVisibility>(
value: HistoryVisibility.world_readable,
child: Text(HistoryVisibility.world_readable
.getLocalizedString(
MatrixLocals(L10n.of(context)))),
),
],
),
if (room.joinRules == JoinRules.public)
2020-05-09 07:17:55 +02:00
PopupMenuButton(
child: ListTile(
leading: CircleAvatar(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
foregroundColor: Colors.grey,
2021-01-16 12:46:38 +01:00
child: Icon(Icons.info_outline),
2020-05-09 07:17:55 +02:00
),
2021-01-16 12:46:38 +01:00
title: Text(
L10n.of(context).areGuestsAllowedToJoin),
2020-05-09 07:17:55 +02:00
subtitle: Text(
2021-01-16 12:46:38 +01:00
room.guestAccess.getLocalizedString(
MatrixLocals(L10n.of(context))),
2020-02-16 10:48:20 +01:00
),
),
2021-01-16 12:46:38 +01:00
onSelected: (GuestAccess guestAccess) =>
showFutureLoadingDialog(
2020-12-25 09:58:34 +01:00
context: context,
2021-01-16 12:46:38 +01:00
future: () => room.setGuestAccess(guestAccess),
2020-05-09 07:17:55 +02:00
),
itemBuilder: (BuildContext context) =>
2021-01-16 12:46:38 +01:00
<PopupMenuEntry<GuestAccess>>[
if (room.canChangeGuestAccess)
PopupMenuItem<GuestAccess>(
value: GuestAccess.can_join,
child: Text(
GuestAccess.can_join.getLocalizedString(
MatrixLocals(L10n.of(context))),
),
2020-05-09 07:17:55 +02:00
),
2021-01-16 12:46:38 +01:00
if (room.canChangeGuestAccess)
PopupMenuItem<GuestAccess>(
value: GuestAccess.forbidden,
child: Text(
GuestAccess.forbidden.getLocalizedString(
MatrixLocals(L10n.of(context))),
),
2020-05-09 07:17:55 +02:00
),
],
),
2021-01-16 12:46:38 +01:00
ListTile(
title: Text(L10n.of(context).editChatPermissions),
subtitle:
Text(L10n.of(context).whoCanPerformWhichAction),
leading: CircleAvatar(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
foregroundColor: Colors.grey,
child: Icon(Icons.edit_attributes_outlined),
2020-12-05 13:03:57 +01:00
),
2021-01-16 12:46:38 +01:00
onTap: () => AdaptivePageLayout.of(context)
.pushNamed('/rooms/${room.id}/permissions'),
),
Divider(thickness: 1),
ListTile(
title: Text(
actualMembersCount > 1
? L10n.of(context).countParticipants(
actualMembersCount.toString())
: L10n.of(context).emptyChat,
style: TextStyle(
color: Theme.of(context).accentColor,
2021-01-16 12:46:38 +01:00
fontWeight: FontWeight.bold,
2020-05-09 07:17:55 +02:00
),
),
2021-01-16 12:46:38 +01:00
),
room.canInvite
? ListTile(
title: Text(L10n.of(context).inviteContact),
leading: CircleAvatar(
child: Icon(Icons.add_outlined),
backgroundColor:
Theme.of(context).primaryColor,
foregroundColor: Colors.white,
radius: Avatar.defaultSize / 2,
2021-01-16 12:46:38 +01:00
),
onTap: () => AdaptivePageLayout.of(context)
.pushNamed('/rooms/${room.id}/invite'),
)
: Container(),
],
)
: i < members.length + 1
? ParticipantListItem(members[i - 1])
: ListTile(
title: Text(L10n.of(context)
.loadCountMoreParticipants(
(actualMembersCount - members.length)
.toString())),
leading: CircleAvatar(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
child: Icon(
Icons.refresh,
color: Colors.grey,
2020-02-16 10:48:20 +01:00
),
2020-05-09 07:17:55 +02:00
),
2021-01-16 12:46:38 +01:00
onTap: () => requestMoreMembersAction(context),
),
2020-05-09 07:17:55 +02:00
),
2021-01-16 12:46:38 +01:00
),
);
});
2020-01-01 19:10:13 +01:00
}
}