fluffychat/lib/pages/chat/pinned_events.dart

145 lines
5.2 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
2022-02-15 12:47:22 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_linkify/flutter_linkify.dart';
import 'package:matrix/matrix.dart';
2022-02-15 12:47:22 +01:00
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/pages/chat/chat.dart';
2022-12-30 17:54:01 +01:00
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
2022-02-15 12:47:22 +01:00
import 'package:fluffychat/utils/url_launcher.dart';
class PinnedEvents extends StatelessWidget {
final ChatController controller;
const PinnedEvents(this.controller, {Key? key}) : super(key: key);
2022-02-15 12:47:22 +01:00
Future<void> _displayPinnedEventsDialog(
BuildContext context,
List<Event?> events,
) async {
2022-02-15 12:47:22 +01:00
final eventId = events.length == 1
? events.single?.eventId
2023-01-08 12:32:35 +01:00
: await showConfirmationDialog<String>(
2022-02-15 12:47:22 +01:00
context: context,
2023-01-08 12:32:35 +01:00
title: L10n.of(context)!.pinMessage,
2022-02-15 12:47:22 +01:00
actions: events
.map(
(event) => AlertDialogAction(
key: event?.eventId ?? '',
label: event?.calcLocalizedBodyFallback(
MatrixLocals(L10n.of(context)!),
withSenderNamePrefix: true,
hideReply: true,
) ??
'UNKNOWN',
),
)
.toList(),
);
2022-02-15 12:47:22 +01:00
if (eventId != null) controller.scrollToEventId(eventId);
}
@override
Widget build(BuildContext context) {
final pinnedEventIds = controller.room.pinnedEventIds;
if (pinnedEventIds.isEmpty) {
return const SizedBox.shrink();
}
final completers = pinnedEventIds.map<Completer<Event?>>((e) {
final completer = Completer<Event?>();
controller.room
.getEventById(e)
.then((value) => completer.complete(value));
return completer;
});
return FutureBuilder<List<Event?>>(
future: Future.wait(completers.map((e) => e.future).toList()),
builder: (context, snapshot) {
final pinnedEvents = snapshot.data;
final event = (pinnedEvents != null && pinnedEvents.isNotEmpty)
? snapshot.data?.last
: null;
2022-02-15 12:47:22 +01:00
if (event == null || pinnedEvents == null) {
return Container();
}
final fontSize = AppConfig.messageFontSize * AppConfig.fontSizeFactor;
return Material(
color: Theme.of(context).colorScheme.surfaceVariant,
child: InkWell(
onTap: () => _displayPinnedEventsDialog(
context,
pinnedEvents,
),
child: Row(
children: [
IconButton(
splashRadius: 20,
iconSize: 20,
color: Theme.of(context).colorScheme.onSurfaceVariant,
icon: const Icon(Icons.push_pin),
tooltip: L10n.of(context)!.unpin,
onPressed:
controller.room.canSendEvent(EventTypes.RoomPinnedEvents)
? () => controller.unpinEvent(event.eventId)
: null,
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: FutureBuilder<String>(
future: event.calcLocalizedBody(
MatrixLocals(L10n.of(context)!),
withSenderNamePrefix: true,
hideReply: true,
),
builder: (context, snapshot) {
return Linkify(
text: snapshot.data ??
event.calcLocalizedBodyFallback(
MatrixLocals(L10n.of(context)!),
withSenderNamePrefix: true,
hideReply: true,
),
2023-05-23 08:22:34 +02:00
options: const LinkifyOptions(humanize: false),
maxLines: 2,
style: TextStyle(
color:
Theme.of(context).colorScheme.onSurfaceVariant,
overflow: TextOverflow.ellipsis,
fontSize: fontSize,
decoration: event.redacted
? TextDecoration.lineThrough
: null,
),
linkStyle: TextStyle(
color:
Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: fontSize,
decoration: TextDecoration.underline,
decorationColor:
Theme.of(context).colorScheme.onSurfaceVariant,
),
onOpen: (url) =>
UrlLauncher(context, url.url).launchUrl(),
);
},
),
),
),
],
),
),
);
},
);
}
}