2020-01-05 12:27:03 +01:00
|
|
|
import 'dart:async';
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'dart:io';
|
2020-10-03 13:11:07 +02:00
|
|
|
|
2020-11-14 10:08:13 +01:00
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
2021-07-13 18:26:55 +02:00
|
|
|
import 'package:flutter_app_badger/flutter_app_badger.dart';
|
2021-05-23 13:11:55 +02:00
|
|
|
|
2021-06-18 10:29:48 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2020-10-04 17:01:54 +02:00
|
|
|
import 'package:file_picker_cross/file_picker_cross.dart';
|
2021-04-09 16:29:48 +02:00
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
2021-05-22 09:08:23 +02:00
|
|
|
import 'package:fluffychat/pages/views/chat_view.dart';
|
2021-05-22 08:57:49 +02:00
|
|
|
import 'package:fluffychat/pages/recording_dialog.dart';
|
2021-05-15 12:15:03 +02:00
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2021-04-03 13:09:20 +02:00
|
|
|
|
2020-12-25 09:58:34 +01:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
2021-05-22 08:53:52 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-05-22 09:24:39 +02:00
|
|
|
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/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/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2020-10-03 13:11:07 +02:00
|
|
|
import 'package:flutter/scheduler.dart';
|
2020-02-09 15:15:29 +01:00
|
|
|
import 'package:flutter/services.dart';
|
2020-10-03 13:11:07 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
2021-07-24 10:35:18 +02:00
|
|
|
import 'package:record/record.dart';
|
2020-09-19 19:21:33 +02:00
|
|
|
import 'package:scroll_to_index/scroll_to_index.dart';
|
2021-01-16 16:39:07 +01:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2021-05-23 13:11:55 +02:00
|
|
|
import 'package:vrouter/vrouter.dart';
|
2021-06-06 17:28:00 +02:00
|
|
|
import '../utils/localized_exception_extension.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-04-24 09:15:59 +02:00
|
|
|
import 'send_file_dialog.dart';
|
2021-07-23 14:24:52 +02:00
|
|
|
import 'sticker_picker_dialog.dart';
|
2021-05-22 09:24:39 +02:00
|
|
|
import '../utils/matrix_sdk_extensions.dart/filtered_timeline_extension.dart';
|
|
|
|
import '../utils/matrix_sdk_extensions.dart/matrix_file_extension.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-01-16 12:46:38 +01:00
|
|
|
class Chat extends StatefulWidget {
|
2021-05-23 13:11:55 +02:00
|
|
|
final Widget sideView;
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-05-23 13:11:55 +02:00
|
|
|
Chat({Key key, this.sideView}) : super(key: key);
|
2020-01-23 12:11:57 +01:00
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
2021-04-15 13:03:14 +02:00
|
|
|
ChatController createState() => ChatController();
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
class ChatController extends State<Chat> {
|
2020-01-01 19:10:13 +01:00
|
|
|
Room room;
|
|
|
|
|
|
|
|
Timeline timeline;
|
|
|
|
|
2020-01-08 14:19:15 +01:00
|
|
|
MatrixState matrix;
|
|
|
|
|
2021-05-23 13:11:55 +02:00
|
|
|
String get roomId => context.vRouter.pathParameters['roomid'];
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
final AutoScrollController scrollController = AutoScrollController();
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2020-01-24 11:51:23 +01:00
|
|
|
FocusNode inputFocus = FocusNode();
|
|
|
|
|
2020-01-05 12:27:03 +01:00
|
|
|
Timer typingCoolDown;
|
|
|
|
Timer typingTimeout;
|
|
|
|
bool currentlyTyping = false;
|
|
|
|
|
2020-04-02 13:14:39 +02:00
|
|
|
List<Event> selectedEvents = [];
|
2020-02-09 15:15:29 +01:00
|
|
|
|
2020-12-23 11:23:19 +01:00
|
|
|
List<Event> filteredEvents;
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
final Set<String> unfolded = {};
|
2020-11-22 19:34:19 +01:00
|
|
|
|
2020-02-09 15:15:29 +01:00
|
|
|
Event replyEvent;
|
|
|
|
|
2020-08-12 11:30:31 +02:00
|
|
|
Event editEvent;
|
|
|
|
|
2020-02-09 16:58:49 +01:00
|
|
|
bool showScrollDownButton = false;
|
|
|
|
|
2020-02-09 15:15:29 +01:00
|
|
|
bool get selectMode => selectedEvents.isNotEmpty;
|
|
|
|
|
2020-02-14 14:34:28 +01:00
|
|
|
final int _loadHistoryCount = 100;
|
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
String inputText = '';
|
2020-02-22 09:03:44 +01:00
|
|
|
|
2020-10-23 17:45:22 +02:00
|
|
|
String pendingText = '';
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
bool get canLoadMore => timeline.events.last.type != EventTypes.RoomCreate;
|
2020-05-09 09:30:03 +02:00
|
|
|
|
2021-05-20 13:46:52 +02:00
|
|
|
bool showEmojiPicker = false;
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void startCallAction() async {
|
2021-01-16 16:39:07 +01:00
|
|
|
final url =
|
|
|
|
'${AppConfig.jitsiInstance}${Uri.encodeComponent(Matrix.of(context).client.generateUniqueTransactionId())}';
|
|
|
|
|
|
|
|
final success = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => room.sendEvent({
|
|
|
|
'msgtype': Matrix.callNamespace,
|
|
|
|
'body': url,
|
|
|
|
}));
|
|
|
|
if (success.error != null) return;
|
|
|
|
await launch(url);
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:34:28 +01:00
|
|
|
void requestHistory() async {
|
2021-04-15 13:03:14 +02:00
|
|
|
if (canLoadMore) {
|
2020-12-27 11:59:03 +01:00
|
|
|
try {
|
|
|
|
await timeline.requestHistory(historyCount: _loadHistoryCount);
|
|
|
|
} catch (err) {
|
2021-06-06 17:56:01 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(
|
|
|
|
(err as Object).toLocalizedString(context),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2020-12-27 11:59:03 +01:00
|
|
|
}
|
2020-09-19 19:21:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _updateScrollController() {
|
2020-11-16 11:31:31 +01:00
|
|
|
if (!mounted) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-15 13:03:14 +02:00
|
|
|
if (scrollController.position.pixels ==
|
|
|
|
scrollController.position.maxScrollExtent &&
|
2020-09-19 19:21:33 +02:00
|
|
|
timeline.events.isNotEmpty &&
|
|
|
|
timeline.events[timeline.events.length - 1].type !=
|
|
|
|
EventTypes.RoomCreate) {
|
|
|
|
requestHistory();
|
|
|
|
}
|
2021-04-15 13:03:14 +02:00
|
|
|
if (scrollController.position.pixels > 0 && showScrollDownButton == false) {
|
2020-09-19 19:21:33 +02:00
|
|
|
setState(() => showScrollDownButton = true);
|
2021-04-15 13:03:14 +02:00
|
|
|
} else if (scrollController.position.pixels == 0 &&
|
2020-09-19 19:21:33 +02:00
|
|
|
showScrollDownButton == true) {
|
|
|
|
setState(() => showScrollDownButton = false);
|
2020-02-14 14:37:31 +01:00
|
|
|
}
|
2020-02-14 14:34:28 +01:00
|
|
|
}
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
|
|
|
void initState() {
|
2021-04-15 13:03:14 +02:00
|
|
|
scrollController.addListener(_updateScrollController);
|
2020-01-01 19:10:13 +01:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2020-01-05 12:27:03 +01:00
|
|
|
void updateView() {
|
2020-01-23 12:11:57 +01:00
|
|
|
if (!mounted) return;
|
2020-12-23 11:23:19 +01:00
|
|
|
setState(
|
|
|
|
() {
|
2021-04-15 13:03:14 +02:00
|
|
|
filteredEvents = timeline.getFilteredEvents(unfolded: unfolded);
|
2020-12-23 11:23:19 +01:00
|
|
|
},
|
|
|
|
);
|
2020-01-05 12:27:03 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void unfold(String eventId) {
|
2021-02-25 09:11:17 +01:00
|
|
|
var i = filteredEvents.indexWhere((e) => e.eventId == eventId);
|
|
|
|
setState(() {
|
|
|
|
while (i < filteredEvents.length - 1 && filteredEvents[i].isState) {
|
2021-04-15 13:03:14 +02:00
|
|
|
unfolded.add(filteredEvents[i].eventId);
|
2021-02-25 09:11:17 +01:00
|
|
|
i++;
|
|
|
|
}
|
2021-04-15 13:03:14 +02:00
|
|
|
filteredEvents = timeline.getFilteredEvents(unfolded: unfolded);
|
2021-02-25 09:11:17 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
Future<bool> getTimeline() async {
|
2020-01-24 12:05:37 +01:00
|
|
|
if (timeline == null) {
|
|
|
|
timeline = await room.getTimeline(onUpdate: updateView);
|
|
|
|
if (timeline.events.isNotEmpty) {
|
2021-04-15 13:03:14 +02:00
|
|
|
// ignore: unawaited_futures
|
|
|
|
room.setUnread(false).catchError((err) {
|
2020-11-16 11:31:31 +01:00
|
|
|
if (err is MatrixException && err.errcode == 'M_FORBIDDEN') {
|
|
|
|
// ignore if the user is not in the room (still joining)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw err;
|
2021-04-15 13:03:14 +02:00
|
|
|
});
|
|
|
|
}
|
2020-09-19 19:21:33 +02:00
|
|
|
|
|
|
|
// when the scroll controller is attached we want to scroll to an event id, if specified
|
|
|
|
// and update the scroll controller...which will trigger a request history, if the
|
|
|
|
// "load more" button is visible on the screen
|
|
|
|
SchedulerBinding.instance.addPostFrameCallback((_) async {
|
2020-11-16 11:31:31 +01:00
|
|
|
if (mounted) {
|
2021-05-23 13:11:55 +02:00
|
|
|
final event = VRouter.of(context).queryParameters['event'];
|
|
|
|
if (event != null) {
|
|
|
|
scrollToEventId(event);
|
2020-11-16 11:31:31 +01:00
|
|
|
}
|
|
|
|
_updateScrollController();
|
2020-09-19 19:21:33 +02:00
|
|
|
}
|
|
|
|
});
|
2020-01-24 12:05:37 +01:00
|
|
|
}
|
2021-04-17 09:51:08 +02:00
|
|
|
filteredEvents = timeline.getFilteredEvents(unfolded: unfolded);
|
2021-05-15 11:55:04 +02:00
|
|
|
if (room.notificationCount != null &&
|
|
|
|
room.notificationCount > 0 &&
|
|
|
|
timeline != null &&
|
|
|
|
timeline.events.isNotEmpty &&
|
|
|
|
Matrix.of(context).webHasFocus) {
|
|
|
|
// ignore: unawaited_futures
|
2021-05-20 13:59:55 +02:00
|
|
|
room.setReadMarker(
|
2021-05-15 11:55:04 +02:00
|
|
|
timeline.events.first.eventId,
|
|
|
|
readReceiptLocationEventId: timeline.events.first.eventId,
|
|
|
|
);
|
2021-05-15 12:15:03 +02:00
|
|
|
if (PlatformInfos.isIOS) {
|
|
|
|
// Workaround for iOS not clearing notifications with fcm_shared_isolate
|
|
|
|
if (!room.client.rooms.any((r) =>
|
|
|
|
r.membership == Membership.invite ||
|
|
|
|
(r.notificationCount != null && r.notificationCount > 0))) {
|
|
|
|
// ignore: unawaited_futures
|
|
|
|
FlutterLocalNotificationsPlugin().cancelAll();
|
2021-07-13 18:26:55 +02:00
|
|
|
FlutterAppBadger.removeBadge();
|
2021-05-15 12:15:03 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-15 11:55:04 +02:00
|
|
|
}
|
2020-01-01 19:10:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2020-02-22 08:27:08 +01:00
|
|
|
timeline?.cancelSubscriptions();
|
2020-01-23 12:11:57 +01:00
|
|
|
timeline = null;
|
2020-01-01 19:10:13 +01:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:41:28 +01:00
|
|
|
TextEditingController sendController = TextEditingController();
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-06-14 00:03:06 +02:00
|
|
|
Future<void> send() async {
|
2021-01-16 19:44:46 +01:00
|
|
|
if (sendController.text.trim().isEmpty) return;
|
2021-06-14 00:03:06 +02:00
|
|
|
var parseCommands = true;
|
|
|
|
|
|
|
|
final commandMatch = RegExp(r'^\/(\w+)').firstMatch(sendController.text);
|
|
|
|
if (commandMatch != null &&
|
|
|
|
!room.client.commands.keys.contains(commandMatch[1].toLowerCase())) {
|
|
|
|
final l10n = L10n.of(context);
|
|
|
|
final dialogResult = await showOkCancelAlertDialog(
|
|
|
|
context: context,
|
|
|
|
useRootNavigator: false,
|
|
|
|
title: l10n.commandInvalid,
|
|
|
|
message: l10n.commandMissing(commandMatch[0]),
|
|
|
|
okLabel: l10n.sendAsText,
|
|
|
|
cancelLabel: l10n.cancel,
|
|
|
|
);
|
|
|
|
if (dialogResult == null || dialogResult == OkCancelResult.cancel) return;
|
|
|
|
parseCommands = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore: unawaited_futures
|
2020-08-12 11:30:31 +02:00
|
|
|
room.sendTextEvent(sendController.text,
|
2021-06-14 00:03:06 +02:00
|
|
|
inReplyTo: replyEvent,
|
|
|
|
editEventId: editEvent?.eventId,
|
|
|
|
parseCommands: parseCommands);
|
2021-05-13 12:48:05 +02:00
|
|
|
sendController.value = TextEditingValue(
|
|
|
|
text: pendingText,
|
|
|
|
selection: TextSelection.collapsed(offset: 0),
|
|
|
|
);
|
2020-02-23 08:52:28 +01:00
|
|
|
|
2020-08-12 11:30:31 +02:00
|
|
|
setState(() {
|
2020-10-23 17:45:22 +02:00
|
|
|
inputText = pendingText;
|
2020-08-12 11:30:31 +02:00
|
|
|
replyEvent = null;
|
|
|
|
editEvent = null;
|
2020-10-23 17:45:22 +02:00
|
|
|
pendingText = '';
|
2020-08-12 11:30:31 +02:00
|
|
|
});
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void sendFileAction() async {
|
2020-10-04 17:01:54 +02:00
|
|
|
final result =
|
|
|
|
await FilePickerCross.importFromStorage(type: FileTypeCross.any);
|
|
|
|
if (result == null) return;
|
2020-09-04 12:56:25 +02:00
|
|
|
await showDialog(
|
2020-10-04 17:01:54 +02:00
|
|
|
context: context,
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-01-18 10:43:00 +01:00
|
|
|
builder: (c) => SendFileDialog(
|
2020-10-04 17:01:54 +02:00
|
|
|
file: MatrixFile(
|
|
|
|
bytes: result.toUint8List(),
|
|
|
|
name: result.fileName,
|
|
|
|
).detectFileType,
|
|
|
|
room: room,
|
|
|
|
),
|
|
|
|
);
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void sendImageAction() async {
|
2020-10-06 18:22:50 +02:00
|
|
|
final result =
|
|
|
|
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
|
|
|
|
if (result == null) return;
|
2020-09-04 12:56:25 +02:00
|
|
|
await showDialog(
|
2020-10-04 17:01:54 +02:00
|
|
|
context: context,
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-01-18 10:43:00 +01:00
|
|
|
builder: (c) => SendFileDialog(
|
2020-10-06 18:22:50 +02:00
|
|
|
file: MatrixImageFile(
|
|
|
|
bytes: result.toUint8List(),
|
|
|
|
name: result.fileName,
|
|
|
|
),
|
2020-10-04 17:01:54 +02:00
|
|
|
room: room,
|
|
|
|
),
|
|
|
|
);
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void openCameraAction() async {
|
2021-07-18 12:17:56 +02:00
|
|
|
// Make sure the textfield is unfocused before opening the camera
|
|
|
|
FocusScope.of(context).requestFocus(FocusNode());
|
2021-04-14 10:37:15 +02:00
|
|
|
final file = await ImagePicker().getImage(source: ImageSource.camera);
|
2020-01-01 19:10:13 +01:00
|
|
|
if (file == null) return;
|
2020-10-04 17:01:54 +02:00
|
|
|
final bytes = await file.readAsBytes();
|
2020-09-04 12:56:25 +02:00
|
|
|
await showDialog(
|
2020-10-04 17:01:54 +02:00
|
|
|
context: context,
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-01-18 10:43:00 +01:00
|
|
|
builder: (c) => SendFileDialog(
|
2020-10-04 17:01:54 +02:00
|
|
|
file: MatrixImageFile(
|
|
|
|
bytes: bytes,
|
|
|
|
name: file.path,
|
|
|
|
),
|
|
|
|
room: room,
|
|
|
|
),
|
|
|
|
);
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:24:52 +02:00
|
|
|
void sendStickerAction() async {
|
|
|
|
final sticker = await showModalBottomSheet<ImagePackImageContent>(
|
|
|
|
context: context,
|
|
|
|
useRootNavigator: false,
|
|
|
|
builder: (c) => StickerPickerDialog(room: room),
|
|
|
|
);
|
|
|
|
if (sticker == null) return;
|
|
|
|
final eventContent = <String, dynamic>{
|
|
|
|
'body': sticker.body,
|
|
|
|
if (sticker.info != null) 'info': sticker.info,
|
|
|
|
'url': sticker.url.toString(),
|
|
|
|
};
|
|
|
|
// send the sticker
|
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => room.sendEvent(
|
|
|
|
eventContent,
|
|
|
|
type: EventTypes.Sticker,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void voiceMessageAction() async {
|
2021-07-24 10:35:18 +02:00
|
|
|
if (await Record().hasPermission() == false) return;
|
2021-01-23 11:17:34 +01:00
|
|
|
final result = await showDialog<String>(
|
|
|
|
context: context,
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-02-24 12:17:23 +01:00
|
|
|
builder: (c) => RecordingDialog(),
|
2021-01-23 11:17:34 +01:00
|
|
|
);
|
2020-03-15 11:27:51 +01:00
|
|
|
if (result == null) return;
|
2020-05-13 15:58:59 +02:00
|
|
|
final audioFile = File(result);
|
2020-09-20 10:35:19 +02:00
|
|
|
// as we already explicitly say send in the recording dialog,
|
|
|
|
// we do not need the send file dialog anymore. We can just send this straight away.
|
2020-12-25 09:58:34 +01:00
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => room.sendFileEvent(
|
2020-09-20 10:35:19 +02:00
|
|
|
MatrixAudioFile(
|
|
|
|
bytes: audioFile.readAsBytesSync(), name: audioFile.path),
|
|
|
|
),
|
|
|
|
);
|
2020-03-15 11:27:51 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
String _getSelectedEventString() {
|
2020-05-13 15:58:59 +02:00
|
|
|
var copyString = '';
|
2020-04-02 13:14:39 +02:00
|
|
|
if (selectedEvents.length == 1) {
|
2020-10-03 13:11:07 +02:00
|
|
|
return selectedEvents.first
|
2020-11-21 15:30:15 +01:00
|
|
|
.getDisplayEvent(timeline)
|
2020-10-03 13:11:07 +02:00
|
|
|
.getLocalizedBody(MatrixLocals(L10n.of(context)));
|
2020-04-02 13:14:39 +02:00
|
|
|
}
|
2021-04-14 10:37:15 +02:00
|
|
|
for (final event in selectedEvents) {
|
2020-05-13 15:58:59 +02:00
|
|
|
if (copyString.isNotEmpty) copyString += '\n\n';
|
2020-11-21 15:30:15 +01:00
|
|
|
copyString += event.getDisplayEvent(timeline).getLocalizedBody(
|
|
|
|
MatrixLocals(L10n.of(context)),
|
2020-10-03 13:11:07 +02:00
|
|
|
withSenderNamePrefix: true);
|
2020-02-09 15:15:29 +01:00
|
|
|
}
|
|
|
|
return copyString;
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void copyEventsAction() {
|
|
|
|
Clipboard.setData(ClipboardData(text: _getSelectedEventString()));
|
2020-02-09 15:15:29 +01:00
|
|
|
setState(() => selectedEvents.clear());
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void reportEventAction() async {
|
2021-02-01 21:26:02 +01:00
|
|
|
final event = selectedEvents.single;
|
|
|
|
final score = await showConfirmationDialog<int>(
|
|
|
|
context: context,
|
|
|
|
title: L10n.of(context).howOffensiveIsThisContent,
|
|
|
|
actions: [
|
|
|
|
AlertDialogAction(
|
2021-02-01 22:00:41 +01:00
|
|
|
key: -100,
|
2021-02-01 21:26:02 +01:00
|
|
|
label: L10n.of(context).extremeOffensive,
|
|
|
|
),
|
|
|
|
AlertDialogAction(
|
2021-02-01 22:00:41 +01:00
|
|
|
key: -50,
|
2021-02-01 21:26:02 +01:00
|
|
|
label: L10n.of(context).offensive,
|
|
|
|
),
|
|
|
|
AlertDialogAction(
|
|
|
|
key: 0,
|
|
|
|
label: L10n.of(context).inoffensive,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
if (score == null) return;
|
|
|
|
final reason = await showTextInputDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-02-01 21:26:02 +01:00
|
|
|
context: context,
|
|
|
|
title: L10n.of(context).whyDoYouWantToReportThis,
|
2021-02-23 11:03:54 +01:00
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
cancelLabel: L10n.of(context).cancel,
|
2021-02-01 21:26:02 +01:00
|
|
|
textFields: [DialogTextField(hintText: L10n.of(context).reason)]);
|
|
|
|
if (reason == null || reason.single.isEmpty) return;
|
|
|
|
final result = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
2021-05-20 13:59:55 +02:00
|
|
|
future: () => Matrix.of(context).client.reportContent(
|
2021-02-01 21:26:02 +01:00
|
|
|
event.roomId,
|
|
|
|
event.eventId,
|
|
|
|
reason.single,
|
|
|
|
score,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (result.error != null) return;
|
|
|
|
setState(() => selectedEvents.clear());
|
2021-05-23 13:11:55 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2021-04-03 13:09:20 +02:00
|
|
|
SnackBar(content: Text(L10n.of(context).contentHasBeenReported)));
|
2021-02-01 21:26:02 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void redactEventsAction() async {
|
2021-04-14 10:37:15 +02:00
|
|
|
final confirmed = await showOkCancelAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2020-11-14 10:08:13 +01:00
|
|
|
context: context,
|
|
|
|
title: L10n.of(context).messageWillBeRemovedWarning,
|
|
|
|
okLabel: L10n.of(context).remove,
|
2021-02-18 14:23:22 +01:00
|
|
|
cancelLabel: L10n.of(context).cancel,
|
2020-11-14 10:08:13 +01:00
|
|
|
) ==
|
|
|
|
OkCancelResult.ok;
|
2020-02-09 15:15:29 +01:00
|
|
|
if (!confirmed) return;
|
2021-04-14 10:37:15 +02:00
|
|
|
for (final event in selectedEvents) {
|
2020-12-25 09:58:34 +01:00
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
2021-05-20 13:59:55 +02:00
|
|
|
future: () =>
|
|
|
|
event.status > 0 ? event.redactEvent() : event.remove());
|
2020-02-09 15:15:29 +01:00
|
|
|
}
|
|
|
|
setState(() => selectedEvents.clear());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get canRedactSelectedEvents {
|
2021-04-14 10:37:15 +02:00
|
|
|
for (final event in selectedEvents) {
|
2020-02-09 15:15:29 +01:00
|
|
|
if (event.canRedact == false) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void forwardEventsAction() async {
|
2020-02-09 15:15:29 +01:00
|
|
|
if (selectedEvents.length == 1) {
|
|
|
|
Matrix.of(context).shareContent = selectedEvents.first.content;
|
|
|
|
} else {
|
|
|
|
Matrix.of(context).shareContent = {
|
2020-05-13 15:58:59 +02:00
|
|
|
'msgtype': 'm.text',
|
2021-04-15 13:03:14 +02:00
|
|
|
'body': _getSelectedEventString(),
|
2020-02-09 15:15:29 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
setState(() => selectedEvents.clear());
|
2021-07-08 17:10:20 +02:00
|
|
|
VRouter.of(context).to('/rooms');
|
2020-02-09 15:15:29 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void sendAgainAction() {
|
2020-08-12 11:30:31 +02:00
|
|
|
final event = selectedEvents.first;
|
|
|
|
if (event.status == -1) {
|
|
|
|
event.sendAgain();
|
|
|
|
}
|
|
|
|
final allEditEvents = event
|
2021-04-21 14:19:54 +02:00
|
|
|
.aggregatedEvents(timeline, RelationshipTypes.edit)
|
2020-08-12 11:30:31 +02:00
|
|
|
.where((e) => e.status == -1);
|
|
|
|
for (final e in allEditEvents) {
|
|
|
|
e.sendAgain();
|
|
|
|
}
|
2020-02-09 15:15:29 +01:00
|
|
|
setState(() => selectedEvents.clear());
|
|
|
|
}
|
|
|
|
|
2020-10-17 11:15:55 +02:00
|
|
|
void replyAction({Event replyTo}) {
|
2020-02-09 15:15:29 +01:00
|
|
|
setState(() {
|
2020-10-17 11:15:55 +02:00
|
|
|
replyEvent = replyTo ?? selectedEvents.first;
|
2020-02-09 15:15:29 +01:00
|
|
|
selectedEvents.clear();
|
|
|
|
});
|
2020-02-16 11:36:18 +01:00
|
|
|
inputFocus.requestFocus();
|
2020-02-09 15:15:29 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void scrollToEventId(String eventId) async {
|
2020-12-23 11:23:19 +01:00
|
|
|
var eventIndex = filteredEvents.indexWhere((e) => e.eventId == eventId);
|
2020-09-19 19:21:33 +02:00
|
|
|
if (eventIndex == -1) {
|
|
|
|
// event id not found...maybe we can fetch it?
|
|
|
|
// the try...finally is here to start and close the loading dialog reliably
|
2020-11-14 10:08:13 +01:00
|
|
|
final task = Future.microtask(() async {
|
2020-09-19 19:21:33 +02:00
|
|
|
// okay, we first have to fetch if the event is in the room
|
|
|
|
try {
|
|
|
|
final event = await timeline.getEventById(eventId);
|
|
|
|
if (event == null) {
|
|
|
|
// event is null...meaning something is off
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err is MatrixException && err.errcode == 'M_NOT_FOUND') {
|
|
|
|
// event wasn't found, as the server gave a 404 or something
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
// okay, we know that the event *is* in the room
|
|
|
|
while (eventIndex == -1) {
|
2021-04-15 13:03:14 +02:00
|
|
|
if (!canLoadMore) {
|
2020-09-19 19:21:33 +02:00
|
|
|
// we can't load any more events but still haven't found ours yet...better stop here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await timeline.requestHistory(historyCount: _loadHistoryCount);
|
|
|
|
} catch (err) {
|
|
|
|
if (err is TimeoutException) {
|
|
|
|
// loading the history timed out...so let's do nothing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rethrow;
|
|
|
|
}
|
2020-12-23 11:23:19 +01:00
|
|
|
eventIndex = filteredEvents.indexWhere((e) => e.eventId == eventId);
|
2020-09-19 19:21:33 +02:00
|
|
|
}
|
2020-11-14 10:08:13 +01:00
|
|
|
});
|
|
|
|
if (context != null) {
|
2020-12-25 09:58:34 +01:00
|
|
|
await showFutureLoadingDialog(context: context, future: () => task);
|
2020-11-14 10:08:13 +01:00
|
|
|
} else {
|
|
|
|
await task;
|
2020-09-19 19:21:33 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 11:31:31 +01:00
|
|
|
if (!mounted) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-15 13:03:14 +02:00
|
|
|
await scrollController.scrollToIndex(eventIndex,
|
2020-09-19 19:21:33 +02:00
|
|
|
preferPosition: AutoScrollPosition.middle);
|
|
|
|
_updateScrollController();
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void scrollDown() => scrollController.jumpTo(0);
|
|
|
|
|
2021-05-20 13:46:52 +02:00
|
|
|
void onEmojiSelected(category, emoji) {
|
|
|
|
setState(() => showEmojiPicker = false);
|
2020-12-12 17:19:53 +01:00
|
|
|
if (emoji == null) return;
|
2020-12-19 16:12:47 +01:00
|
|
|
// make sure we don't send the same emoji twice
|
2021-05-20 13:46:52 +02:00
|
|
|
if (_allReactionEvents
|
2020-12-19 16:12:47 +01:00
|
|
|
.any((e) => e.content['m.relates_to']['key'] == emoji.emoji)) return;
|
2021-04-15 13:03:14 +02:00
|
|
|
return sendEmojiAction(emoji.emoji);
|
2020-12-12 17:19:53 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 13:46:52 +02:00
|
|
|
Iterable<Event> _allReactionEvents;
|
|
|
|
|
|
|
|
void cancelEmojiPicker() => setState(() => showEmojiPicker = false);
|
|
|
|
|
|
|
|
void pickEmojiAction(Iterable<Event> allReactionEvents) async {
|
|
|
|
_allReactionEvents = allReactionEvents;
|
|
|
|
setState(() => showEmojiPicker = true);
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void sendEmojiAction(String emoji) async {
|
2020-12-27 19:24:47 +01:00
|
|
|
await showFutureLoadingDialog(
|
2020-12-25 09:58:34 +01:00
|
|
|
context: context,
|
|
|
|
future: () => room.sendReaction(
|
2020-12-27 19:24:47 +01:00
|
|
|
selectedEvents.single.eventId,
|
2020-12-12 17:19:53 +01:00
|
|
|
emoji,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
setState(() => selectedEvents.clear());
|
|
|
|
}
|
|
|
|
|
2021-05-20 13:46:52 +02:00
|
|
|
void clearSelectedEvents() => setState(() {
|
|
|
|
selectedEvents.clear();
|
|
|
|
showEmojiPicker = false;
|
|
|
|
});
|
2020-01-05 12:27:03 +01:00
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void editSelectedEventAction() {
|
|
|
|
setState(() {
|
|
|
|
pendingText = sendController.text;
|
|
|
|
editEvent = selectedEvents.first;
|
|
|
|
inputText = sendController.text = editEvent
|
|
|
|
.getDisplayEvent(timeline)
|
|
|
|
.getLocalizedBody(MatrixLocals(L10n.of(context)),
|
|
|
|
withSenderNamePrefix: false, hideReply: true);
|
|
|
|
selectedEvents.clear();
|
|
|
|
});
|
|
|
|
inputFocus.requestFocus();
|
|
|
|
}
|
2021-03-28 09:20:34 +02:00
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void onEventActionPopupMenuSelected(selected) {
|
|
|
|
switch (selected) {
|
|
|
|
case 'copy':
|
|
|
|
copyEventsAction();
|
|
|
|
break;
|
|
|
|
case 'redact':
|
|
|
|
redactEventsAction();
|
|
|
|
break;
|
|
|
|
case 'report':
|
|
|
|
reportEventAction();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-07 10:18:51 +01:00
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void goToNewRoomAction() async {
|
|
|
|
if (OkCancelResult.ok !=
|
|
|
|
await showOkCancelAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-04-15 13:03:14 +02:00
|
|
|
context: context,
|
|
|
|
title: L10n.of(context).goToTheNewRoom,
|
|
|
|
message: room
|
|
|
|
.getState(EventTypes.RoomTombstone)
|
|
|
|
.parsedTombstoneContent
|
|
|
|
.body,
|
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
cancelLabel: L10n.of(context).cancel,
|
|
|
|
)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final result = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => room.client.joinRoom(room
|
|
|
|
.getState(EventTypes.RoomTombstone)
|
|
|
|
.parsedTombstoneContent
|
|
|
|
.replacementRoom),
|
|
|
|
);
|
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: room.leave,
|
|
|
|
);
|
|
|
|
if (result.error == null) {
|
2021-07-08 17:10:20 +02:00
|
|
|
VRouter.of(context).to('/rooms/${result.result}');
|
2021-04-15 13:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-07 10:18:51 +01:00
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void onSelectMessage(Event event) {
|
|
|
|
if (!event.redacted) {
|
|
|
|
if (selectedEvents.contains(event)) {
|
|
|
|
setState(
|
|
|
|
() => selectedEvents.remove(event),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
setState(
|
|
|
|
() => selectedEvents.add(event),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
selectedEvents.sort(
|
|
|
|
(a, b) => a.originServerTs.compareTo(b.originServerTs),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-11-08 15:40:06 +01:00
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
int findChildIndexCallback(Key key, Map<String, int> thisEventsKeyMap) {
|
|
|
|
// this method is called very often. As such, it has to be optimized for speed.
|
|
|
|
if (!(key is ValueKey)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final eventId = (key as ValueKey).value;
|
|
|
|
if (!(eventId is String)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// first fetch the last index the event was at
|
|
|
|
final index = thisEventsKeyMap[eventId];
|
|
|
|
if (index == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// we need to +1 as 0 is the typing thing at the bottom
|
|
|
|
return index + 1;
|
|
|
|
}
|
2021-02-25 09:11:17 +01:00
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void onInputBarSubmitted(String text) {
|
|
|
|
send();
|
|
|
|
FocusScope.of(context).requestFocus(inputFocus);
|
|
|
|
}
|
2020-11-07 10:18:51 +01:00
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void onAddPopupMenuButtonSelected(String choice) {
|
|
|
|
if (choice == 'file') {
|
|
|
|
sendFileAction();
|
2021-07-23 14:24:52 +02:00
|
|
|
}
|
|
|
|
if (choice == 'image') {
|
2021-04-15 13:03:14 +02:00
|
|
|
sendImageAction();
|
|
|
|
}
|
|
|
|
if (choice == 'camera') {
|
|
|
|
openCameraAction();
|
|
|
|
}
|
2021-07-23 14:24:52 +02:00
|
|
|
if (choice == 'sticker') {
|
|
|
|
sendStickerAction();
|
|
|
|
}
|
2021-04-15 13:03:14 +02:00
|
|
|
if (choice == 'voice') {
|
|
|
|
voiceMessageAction();
|
|
|
|
}
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
2020-08-12 11:30:31 +02:00
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void onInputBarChanged(String text) {
|
|
|
|
typingCoolDown?.cancel();
|
|
|
|
typingCoolDown = Timer(Duration(seconds: 2), () {
|
|
|
|
typingCoolDown = null;
|
|
|
|
currentlyTyping = false;
|
2021-05-20 13:59:55 +02:00
|
|
|
room.setTyping(false);
|
2021-04-15 13:03:14 +02:00
|
|
|
});
|
|
|
|
typingTimeout ??= Timer(Duration(seconds: 30), () {
|
|
|
|
typingTimeout = null;
|
|
|
|
currentlyTyping = false;
|
|
|
|
});
|
|
|
|
if (!currentlyTyping) {
|
|
|
|
currentlyTyping = true;
|
2021-05-20 13:59:55 +02:00
|
|
|
room.setTyping(true, timeout: Duration(seconds: 30).inMilliseconds);
|
2021-04-15 13:03:14 +02:00
|
|
|
}
|
2021-05-20 13:33:06 +02:00
|
|
|
setState(() => inputText = text);
|
2021-04-15 13:03:14 +02:00
|
|
|
}
|
2020-08-12 11:30:31 +02:00
|
|
|
|
2021-04-15 13:03:14 +02:00
|
|
|
void cancelReplyEventAction() => setState(() {
|
|
|
|
if (editEvent != null) {
|
|
|
|
inputText = sendController.text = pendingText;
|
|
|
|
pendingText = '';
|
|
|
|
}
|
|
|
|
replyEvent = null;
|
|
|
|
editEvent = null;
|
|
|
|
});
|
2020-08-12 11:30:31 +02:00
|
|
|
|
|
|
|
@override
|
2021-05-23 18:46:15 +02:00
|
|
|
Widget build(BuildContext context) => ChatView(this);
|
2020-08-12 11:30:31 +02:00
|
|
|
}
|