This commit is contained in:
Christian Pauly 2020-05-05 14:55:19 +02:00
parent c396e10ea7
commit e02a8d294e
12 changed files with 123 additions and 119 deletions

View File

@ -153,7 +153,7 @@ class ChatListItem extends StatelessWidget {
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
room.getLocalizedDisplayname(context),
room.getLocalizedDisplayname(I18n.of(context)),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
@ -189,7 +189,7 @@ class ChatListItem extends StatelessWidget {
),
)
: Text(
room.lastEvent.getLocalizedBody(context,
room.lastEvent.getLocalizedBody(I18n.of(context),
withSenderNamePrefix: true, hideReply: true),
maxLines: 1,
overflow: TextOverflow.ellipsis,

View File

@ -1,5 +1,6 @@
import 'package:bubble/bubble.dart';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart';
import 'package:fluffychat/utils/event_extension.dart';
@ -21,7 +22,7 @@ class StateMessage extends StatelessWidget {
color: Theme.of(context).backgroundColor.withOpacity(0.66),
alignment: Alignment.center,
child: Text(
event.getLocalizedBody(context),
event.getLocalizedBody(I18n.of(context)),
textAlign: TextAlign.center,
style: TextStyle(
color: Theme.of(context).textTheme.body1.color,

View File

@ -58,7 +58,7 @@ class MessageContent extends StatelessWidget {
);
}
return LinkText(
text: event.getLocalizedBody(context, hideReply: true),
text: event.getLocalizedBody(I18n.of(context), hideReply: true),
textStyle: TextStyle(
color: textColor,
decoration: event.redacted ? TextDecoration.lineThrough : null,

View File

@ -1,4 +1,5 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:fluffychat/utils/event_extension.dart';
import 'package:flutter/material.dart';
@ -35,8 +36,11 @@ class ReplyContent extends StatelessWidget {
),
),
Text(
replyEvent?.getLocalizedBody(context,
withSenderNamePrefix: false, hideReply: true) ??
replyEvent?.getLocalizedBody(
I18n.of(context),
withSenderNamePrefix: false,
hideReply: true,
) ??
"",
overflow: TextOverflow.ellipsis,
maxLines: 1,

View File

@ -12,30 +12,28 @@ extension LocalizedBody on Event {
MessageTypes.None,
};
String getLocalizedBody(BuildContext context,
String getLocalizedBody(I18n i18n,
{bool withSenderNamePrefix = false, bool hideReply = false}) {
if (this.redacted) {
return I18n.of(context)
.removedBy(redactedBecause.sender.calcDisplayname());
return i18n.removedBy(redactedBecause.sender.calcDisplayname());
}
String localizedBody = body;
final String senderName = this.sender.calcDisplayname();
switch (this.type) {
case EventTypes.Sticker:
localizedBody = I18n.of(context).sentASticker(senderName);
localizedBody = i18n.sentASticker(senderName);
break;
case EventTypes.Redaction:
localizedBody = I18n.of(context).redactedAnEvent(senderName);
localizedBody = i18n.redactedAnEvent(senderName);
break;
case EventTypes.RoomAliases:
localizedBody = I18n.of(context).changedTheRoomAliases(senderName);
localizedBody = i18n.changedTheRoomAliases(senderName);
break;
case EventTypes.RoomCanonicalAlias:
localizedBody =
I18n.of(context).changedTheRoomInvitationLink(senderName);
localizedBody = i18n.changedTheRoomInvitationLink(senderName);
break;
case EventTypes.RoomCreate:
localizedBody = I18n.of(context).createdTheChat(senderName);
localizedBody = i18n.createdTheChat(senderName);
break;
case EventTypes.RoomJoinRules:
JoinRules joinRules = JoinRules.values.firstWhere(
@ -44,10 +42,10 @@ extension LocalizedBody on Event {
content["join_rule"],
orElse: () => null);
if (joinRules == null) {
localizedBody = I18n.of(context).changedTheJoinRules(senderName);
localizedBody = i18n.changedTheJoinRules(senderName);
} else {
localizedBody = I18n.of(context).changedTheJoinRulesTo(
senderName, joinRules.getLocalizedString(context));
localizedBody = i18n.changedTheJoinRulesTo(
senderName, joinRules.getLocalizedString(i18n));
}
break;
case EventTypes.RoomMember:
@ -61,36 +59,35 @@ extension LocalizedBody on Event {
: "";
if (newMembership != oldMembership) {
if (oldMembership == "invite" && newMembership == "join") {
text = I18n.of(context).acceptedTheInvitation(targetName);
text = i18n.acceptedTheInvitation(targetName);
} else if (oldMembership == "invite" && newMembership == "leave") {
if (this.stateKey == this.senderId) {
text = I18n.of(context).rejectedTheInvitation(targetName);
text = i18n.rejectedTheInvitation(targetName);
} else {
text = I18n.of(context)
.hasWithdrawnTheInvitationFor(senderName, targetName);
text = i18n.hasWithdrawnTheInvitationFor(senderName, targetName);
}
} else if (oldMembership == "leave" && newMembership == "join") {
text = I18n.of(context).joinedTheChat(targetName);
text = i18n.joinedTheChat(targetName);
} else if (oldMembership == "join" && newMembership == "ban") {
text = I18n.of(context).kickedAndBanned(senderName, targetName);
text = i18n.kickedAndBanned(senderName, targetName);
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey != this.senderId) {
text = I18n.of(context).kicked(senderName, targetName);
text = i18n.kicked(senderName, targetName);
} else if (oldMembership == "join" &&
newMembership == "leave" &&
this.stateKey == this.senderId) {
text = I18n.of(context).userLeftTheChat(targetName);
text = i18n.userLeftTheChat(targetName);
} else if (oldMembership == "invite" && newMembership == "ban") {
text = I18n.of(context).bannedUser(senderName, targetName);
text = i18n.bannedUser(senderName, targetName);
} else if (oldMembership == "leave" && newMembership == "ban") {
text = I18n.of(context).bannedUser(senderName, targetName);
text = i18n.bannedUser(senderName, targetName);
} else if (oldMembership == "ban" && newMembership == "leave") {
text = I18n.of(context).unbannedUser(senderName, targetName);
text = i18n.unbannedUser(senderName, targetName);
} else if (newMembership == "invite") {
text = I18n.of(context).invitedUser(senderName, targetName);
text = i18n.invitedUser(senderName, targetName);
} else if (newMembership == "join") {
text = I18n.of(context).joinedTheChat(targetName);
text = i18n.joinedTheChat(targetName);
}
} else if (newMembership == "join") {
final String newAvatar = this.content["avatar_url"] ?? "";
@ -107,29 +104,27 @@ extension LocalizedBody on Event {
// Has the user avatar changed?
if (newAvatar != oldAvatar) {
text = I18n.of(context).changedTheProfileAvatar(targetName);
text = i18n.changedTheProfileAvatar(targetName);
}
// Has the user avatar changed?
else if (newDisplayname != oldDisplayname) {
text = I18n.of(context)
.changedTheDisplaynameTo(targetName, newDisplayname);
text = i18n.changedTheDisplaynameTo(targetName, newDisplayname);
}
}
localizedBody = text;
break;
case EventTypes.RoomPowerLevels:
localizedBody = I18n.of(context).changedTheChatPermissions(senderName);
localizedBody = i18n.changedTheChatPermissions(senderName);
break;
case EventTypes.RoomName:
localizedBody =
I18n.of(context).changedTheChatNameTo(senderName, content["name"]);
localizedBody = i18n.changedTheChatNameTo(senderName, content["name"]);
break;
case EventTypes.RoomTopic:
localizedBody = I18n.of(context)
.changedTheChatDescriptionTo(senderName, content["topic"]);
localizedBody =
i18n.changedTheChatDescriptionTo(senderName, content["topic"]);
break;
case EventTypes.RoomAvatar:
localizedBody = I18n.of(context).changedTheChatAvatar(senderName);
localizedBody = i18n.changedTheChatAvatar(senderName);
break;
case EventTypes.GuestAccess:
GuestAccess guestAccess = GuestAccess.values.firstWhere(
@ -138,11 +133,10 @@ extension LocalizedBody on Event {
content["guest_access"],
orElse: () => null);
if (guestAccess == null) {
localizedBody =
I18n.of(context).changedTheGuestAccessRules(senderName);
localizedBody = i18n.changedTheGuestAccessRules(senderName);
} else {
localizedBody = I18n.of(context).changedTheGuestAccessRulesTo(
senderName, guestAccess.getLocalizedString(context));
localizedBody = i18n.changedTheGuestAccessRulesTo(
senderName, guestAccess.getLocalizedString(i18n));
}
break;
case EventTypes.HistoryVisibility:
@ -153,40 +147,38 @@ extension LocalizedBody on Event {
content["history_visibility"],
orElse: () => null);
if (historyVisibility == null) {
localizedBody =
I18n.of(context).changedTheHistoryVisibility(senderName);
localizedBody = i18n.changedTheHistoryVisibility(senderName);
} else {
localizedBody = I18n.of(context).changedTheHistoryVisibilityTo(
senderName, historyVisibility.getLocalizedString(context));
localizedBody = i18n.changedTheHistoryVisibilityTo(
senderName, historyVisibility.getLocalizedString(i18n));
}
break;
case EventTypes.Encryption:
localizedBody =
I18n.of(context).activatedEndToEndEncryption(senderName);
localizedBody = i18n.activatedEndToEndEncryption(senderName);
if (!room.client.encryptionEnabled) {
localizedBody += ". " + I18n.of(context).needPantalaimonWarning;
localizedBody += ". " + i18n.needPantalaimonWarning;
}
break;
case EventTypes.Encrypted:
case EventTypes.Message:
switch (this.messageType) {
case MessageTypes.Image:
localizedBody = I18n.of(context).sentAPicture(senderName);
localizedBody = i18n.sentAPicture(senderName);
break;
case MessageTypes.File:
localizedBody = I18n.of(context).sentAFile(senderName);
localizedBody = i18n.sentAFile(senderName);
break;
case MessageTypes.Audio:
localizedBody = I18n.of(context).sentAnAudio(senderName);
localizedBody = i18n.sentAnAudio(senderName);
break;
case MessageTypes.Video:
localizedBody = I18n.of(context).sentAVideo(senderName);
localizedBody = i18n.sentAVideo(senderName);
break;
case MessageTypes.Location:
localizedBody = I18n.of(context).sharedTheLocation(senderName);
localizedBody = i18n.sharedTheLocation(senderName);
break;
case MessageTypes.Sticker:
localizedBody = I18n.of(context).sentASticker(senderName);
localizedBody = i18n.sentASticker(senderName);
break;
case MessageTypes.Emote:
localizedBody = "* $body";
@ -195,25 +187,23 @@ extension LocalizedBody on Event {
String errorText;
switch (body) {
case DecryptError.CHANNEL_CORRUPTED:
errorText = I18n.of(context).channelCorruptedDecryptError + ".";
errorText = i18n.channelCorruptedDecryptError + ".";
break;
case DecryptError.NOT_ENABLED:
errorText = I18n.of(context).encryptionNotEnabled + ".";
errorText = i18n.encryptionNotEnabled + ".";
break;
case DecryptError.UNKNOWN_ALGORITHM:
errorText = I18n.of(context).unknownEncryptionAlgorithm + ".";
errorText = i18n.unknownEncryptionAlgorithm + ".";
break;
case DecryptError.UNKNOWN_SESSION:
errorText = I18n.of(context).noPermission + ".";
errorText = i18n.noPermission + ".";
break;
default:
errorText = body;
break;
}
localizedBody = "🔒 " +
I18n.of(context).couldNotDecryptMessage +
": " +
errorText;
localizedBody =
"🔒 " + i18n.couldNotDecryptMessage + ": " + errorText;
break;
case MessageTypes.Text:
case MessageTypes.Notice:
@ -224,7 +214,7 @@ extension LocalizedBody on Event {
}
break;
default:
localizedBody = I18n.of(context).unknownEvent(this.typeKey);
localizedBody = i18n.unknownEvent(this.typeKey);
}
// Hide reply fallback
@ -237,9 +227,8 @@ extension LocalizedBody on Event {
if (withSenderNamePrefix &&
this.type == EventTypes.Message &&
textOnlyMessageTypes.contains(this.messageType)) {
final String senderNameOrYou = this.senderId == room.client.userID
? I18n.of(context).you
: senderName;
final String senderNameOrYou =
this.senderId == room.client.userID ? i18n.you : senderName;
localizedBody = "$senderNameOrYou: $localizedBody";
}

View File

@ -160,21 +160,25 @@ abstract class FirebaseController {
: i18n.unreadMessages(unreadEvents.toString());
// Calculate the body
final String body = event.getLocalizedBody(context,
withSenderNamePrefix: true, hideReply: true);
final String body = event.getLocalizedBody(
i18n,
withSenderNamePrefix: true,
hideReply: true,
);
// The person object for the android message style notification
final person = Person(
name: room.getLocalizedDisplayname(context),
name: room.getLocalizedDisplayname(i18n),
icon: room.avatar == null
? null
: await downloadAndSaveAvatar(
room.avatar,
client,
width: 126,
height: 126,
: BitmapFilePathAndroidIcon(
await downloadAndSaveAvatar(
room.avatar,
client,
width: 126,
height: 126,
),
),
iconSource: IconSource.FilePath,
);
// Show notification
@ -182,7 +186,6 @@ abstract class FirebaseController {
'fluffychat_push',
'FluffyChat push channel',
'Push notifications for FluffyChat',
style: AndroidNotificationStyle.Messaging,
styleInformation: MessagingStyleInformation(
person,
conversationTitle: title,
@ -200,8 +203,8 @@ abstract class FirebaseController {
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await _flutterLocalNotificationsPlugin.show(0,
room.getLocalizedDisplayname(context), body, platformChannelSpecifics,
await _flutterLocalNotificationsPlugin.show(
0, room.getLocalizedDisplayname(i18n), body, platformChannelSpecifics,
payload: roomId);
} catch (exception) {
debugPrint("[Push] Error while processing notification: " +

View File

@ -1,20 +1,19 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart';
extension LocalizedRoomDisplayname on Room {
String getLocalizedDisplayname(BuildContext context) {
String getLocalizedDisplayname(I18n i18n) {
if ((this.name?.isEmpty ?? true) &&
(this.canonicalAlias?.isEmpty ?? true) &&
!this.isDirectChat &&
(this.mHeroes != null && this.mHeroes.isNotEmpty)) {
return I18n.of(context).groupWith(this.displayname);
return i18n.groupWith(this.displayname);
}
if ((this.name?.isEmpty ?? true) &&
(this.canonicalAlias?.isEmpty ?? true) &&
!this.isDirectChat &&
(this.mHeroes?.isEmpty ?? true)) {
return I18n.of(context).emptyChat;
return i18n.emptyChat;
}
return this.displayname;
}

View File

@ -1,18 +1,17 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:flutter/material.dart';
extension HistoryVisibilityDisplayString on HistoryVisibility {
String getLocalizedString(BuildContext context) {
String getLocalizedString(I18n i18n) {
switch (this) {
case HistoryVisibility.invited:
return I18n.of(context).fromTheInvitation;
return i18n.fromTheInvitation;
case HistoryVisibility.joined:
return I18n.of(context).fromJoining;
return i18n.fromJoining;
case HistoryVisibility.shared:
return I18n.of(context).visibleForAllParticipants;
return i18n.visibleForAllParticipants;
case HistoryVisibility.world_readable:
return I18n.of(context).visibleForEveryone;
return i18n.visibleForEveryone;
default:
return this.toString().replaceAll("HistoryVisibility.", "");
}
@ -20,12 +19,12 @@ extension HistoryVisibilityDisplayString on HistoryVisibility {
}
extension GuestAccessDisplayString on GuestAccess {
String getLocalizedString(BuildContext context) {
String getLocalizedString(I18n i18n) {
switch (this) {
case GuestAccess.can_join:
return I18n.of(context).guestsCanJoin;
return i18n.guestsCanJoin;
case GuestAccess.forbidden:
return I18n.of(context).guestsAreForbidden;
return i18n.guestsAreForbidden;
default:
return this.toString().replaceAll("GuestAccess.", "");
}
@ -33,12 +32,12 @@ extension GuestAccessDisplayString on GuestAccess {
}
extension JoinRulesDisplayString on JoinRules {
String getLocalizedString(BuildContext context) {
String getLocalizedString(I18n i18n) {
switch (this) {
case JoinRules.public:
return I18n.of(context).anyoneCanJoin;
return i18n.anyoneCanJoin;
case JoinRules.invite:
return I18n.of(context).invitedUsersOnly;
return i18n.invitedUsersOnly;
default:
return this.toString().replaceAll("JoinRules.", "");
}

View File

@ -244,11 +244,12 @@ class _ChatState extends State<_Chat> {
String _getSelectedEventString(BuildContext context) {
String copyString = "";
if (selectedEvents.length == 1) {
return selectedEvents.first.getLocalizedBody(context);
return selectedEvents.first.getLocalizedBody(I18n.of(context));
}
for (Event event in selectedEvents) {
if (copyString.isNotEmpty) copyString += "\n\n";
copyString += event.getLocalizedBody(context, withSenderNamePrefix: true);
copyString +=
event.getLocalizedBody(I18n.of(context), withSenderNamePrefix: true);
}
return copyString;
}
@ -360,7 +361,7 @@ class _ChatState extends State<_Chat> {
? CrossAxisAlignment.center
: CrossAxisAlignment.start,
children: <Widget>[
Text(room.getLocalizedDisplayname(context)),
Text(room.getLocalizedDisplayname(I18n.of(context))),
AnimatedContainer(
duration: Duration(milliseconds: 500),
height: typingText.isEmpty ? 0 : 20,

View File

@ -35,7 +35,7 @@ class _ChatDetailsState extends State<ChatDetails> {
final String displayname = await SimpleDialogs(context).enterText(
titleText: I18n.of(context).changeTheNameOfTheGroup,
labelText: I18n.of(context).changeTheNameOfTheGroup,
hintText: widget.room.getLocalizedDisplayname(context),
hintText: widget.room.getLocalizedDisplayname(I18n.of(context)),
);
if (displayname == null) return;
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
@ -186,7 +186,7 @@ class _ChatDetailsState extends State<ChatDetails> {
),
ChatSettingsPopupMenu(widget.room, false)
],
title: Text(widget.room.getLocalizedDisplayname(context),
title: Text(widget.room.getLocalizedDisplayname(I18n.of(context)),
style: TextStyle(
color:
Theme.of(context).appBarTheme.textTheme.title.color)),
@ -251,8 +251,8 @@ class _ChatDetailsState extends State<ChatDetails> {
child: Icon(Icons.people),
),
title: Text(I18n.of(context).changeTheNameOfTheGroup),
subtitle: Text(
widget.room.getLocalizedDisplayname(context)),
subtitle: Text(widget.room
.getLocalizedDisplayname(I18n.of(context))),
onTap: () => setDisplaynameAction(context),
),
if (widget.room.canSendEvent("m.room.canonical_alias") &&
@ -281,7 +281,8 @@ class _ChatDetailsState extends State<ChatDetails> {
title: Text(
I18n.of(context).whoIsAllowedToJoinThisGroup),
subtitle: Text(
widget.room.joinRules.getLocalizedString(context),
widget.room.joinRules
.getLocalizedString(I18n.of(context)),
),
),
onSelected: (JoinRules joinRule) =>
@ -293,14 +294,14 @@ class _ChatDetailsState extends State<ChatDetails> {
if (widget.room.canChangeJoinRules)
PopupMenuItem<JoinRules>(
value: JoinRules.public,
child: Text(
JoinRules.public.getLocalizedString(context)),
child: Text(JoinRules.public
.getLocalizedString(I18n.of(context))),
),
if (widget.room.canChangeJoinRules)
PopupMenuItem<JoinRules>(
value: JoinRules.invite,
child: Text(
JoinRules.invite.getLocalizedString(context)),
child: Text(JoinRules.invite
.getLocalizedString(I18n.of(context))),
),
],
),
@ -316,7 +317,7 @@ class _ChatDetailsState extends State<ChatDetails> {
Text(I18n.of(context).visibilityOfTheChatHistory),
subtitle: Text(
widget.room.historyVisibility
.getLocalizedString(context),
.getLocalizedString(I18n.of(context)),
),
),
onSelected: (HistoryVisibility historyVisibility) =>
@ -329,25 +330,25 @@ class _ChatDetailsState extends State<ChatDetails> {
PopupMenuItem<HistoryVisibility>(
value: HistoryVisibility.invited,
child: Text(HistoryVisibility.invited
.getLocalizedString(context)),
.getLocalizedString(I18n.of(context))),
),
if (widget.room.canChangeHistoryVisibility)
PopupMenuItem<HistoryVisibility>(
value: HistoryVisibility.joined,
child: Text(HistoryVisibility.joined
.getLocalizedString(context)),
.getLocalizedString(I18n.of(context))),
),
if (widget.room.canChangeHistoryVisibility)
PopupMenuItem<HistoryVisibility>(
value: HistoryVisibility.shared,
child: Text(HistoryVisibility.shared
.getLocalizedString(context)),
.getLocalizedString(I18n.of(context))),
),
if (widget.room.canChangeHistoryVisibility)
PopupMenuItem<HistoryVisibility>(
value: HistoryVisibility.world_readable,
child: Text(HistoryVisibility.world_readable
.getLocalizedString(context)),
.getLocalizedString(I18n.of(context))),
),
],
),
@ -364,7 +365,7 @@ class _ChatDetailsState extends State<ChatDetails> {
Text(I18n.of(context).areGuestsAllowedToJoin),
subtitle: Text(
widget.room.guestAccess
.getLocalizedString(context),
.getLocalizedString(I18n.of(context)),
),
),
onSelected: (GuestAccess guestAccess) =>
@ -379,7 +380,7 @@ class _ChatDetailsState extends State<ChatDetails> {
value: GuestAccess.can_join,
child: Text(
GuestAccess.can_join
.getLocalizedString(context),
.getLocalizedString(I18n.of(context)),
),
),
if (widget.room.canChangeGuestAccess)
@ -387,7 +388,7 @@ class _ChatDetailsState extends State<ChatDetails> {
value: GuestAccess.forbidden,
child: Text(
GuestAccess.forbidden
.getLocalizedString(context),
.getLocalizedString(I18n.of(context)),
),
),
],

View File

@ -168,7 +168,14 @@ packages:
name: flutter_local_notifications
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.4+3"
version: "1.4.3"
flutter_local_notifications_platform_interface:
dependency: transitive
description:
name: flutter_local_notifications_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
flutter_localizations:
dependency: "direct main"
description: flutter

View File

@ -39,7 +39,7 @@ dependencies:
sqflite: ^1.2.0
flutter_advanced_networkimage: any
firebase_messaging: ^6.0.13
flutter_local_notifications: ^0.8.4
flutter_local_notifications: ^1.4.3
link_text: ^0.1.1
path_provider: ^1.5.1
webview_flutter: ^0.3.19+9