fluffychat/lib/pages/story/story_view.dart

413 lines
16 KiB
Dart
Raw Normal View History

2021-12-24 14:18:09 +01:00
import 'package:flutter/material.dart';
2021-12-25 08:56:35 +01:00
import 'package:flutter/services.dart';
2021-12-24 14:18:09 +01:00
import 'package:flutter_blurhash/flutter_blurhash.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_linkify/flutter_linkify.dart';
2021-12-24 14:18:09 +01:00
import 'package:matrix/matrix.dart';
import 'package:video_player/video_player.dart';
2022-12-23 15:10:26 +01:00
import 'package:fluffychat/config/app_config.dart';
2021-12-24 14:18:09 +01:00
import 'package:fluffychat/pages/story/story_page.dart';
2021-12-25 14:07:48 +01:00
import 'package:fluffychat/utils/date_time_extension.dart';
2021-12-24 14:18:09 +01:00
import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/string_color.dart';
2021-12-26 12:46:18 +01:00
import 'package:fluffychat/utils/url_launcher.dart';
2021-12-25 08:56:35 +01:00
import 'package:fluffychat/widgets/avatar.dart';
import '../../config/themes.dart';
2021-12-24 14:18:09 +01:00
class StoryView extends StatelessWidget {
final StoryPageController controller;
const StoryView(this.controller, {Key? key}) : super(key: key);
2022-02-20 10:24:23 +01:00
static const List<Shadow> textShadows = [
Shadow(
color: Colors.black,
offset: Offset(5, 5),
blurRadius: 20,
),
Shadow(
color: Colors.black,
offset: Offset(5, 5),
blurRadius: 20,
),
Shadow(
color: Colors.black,
offset: Offset(-5, -5),
blurRadius: 20,
),
Shadow(
color: Colors.black,
offset: Offset(-5, -5),
blurRadius: 20,
),
];
2021-12-24 14:18:09 +01:00
@override
Widget build(BuildContext context) {
2021-12-25 14:07:48 +01:00
final currentEvent = controller.currentEvent;
2021-12-24 14:18:09 +01:00
return Scaffold(
2021-12-27 17:16:35 +01:00
backgroundColor: Colors.blueGrey.shade900,
2021-12-24 14:18:09 +01:00
appBar: AppBar(
2021-12-25 08:56:35 +01:00
titleSpacing: 0,
2021-12-26 11:30:44 +01:00
leading: IconButton(
2022-09-06 20:21:11 +02:00
color: Colors.white,
2021-12-26 11:30:44 +01:00
icon: const Icon(Icons.close),
onPressed: Navigator.of(context).pop,
),
2022-01-19 08:30:43 +01:00
title: ListTile(
contentPadding: EdgeInsets.zero,
title: Text(
controller.title,
style: const TextStyle(
color: Colors.white,
shadows: [
Shadow(
color: Colors.black,
offset: Offset(0, 0),
blurRadius: 5,
),
],
2021-12-26 11:30:44 +01:00
),
2021-12-25 08:56:35 +01:00
),
2022-01-19 08:30:43 +01:00
subtitle: currentEvent != null
? Text(
currentEvent.originServerTs.localizedTime(context),
style: const TextStyle(
color: Colors.white70,
shadows: [
Shadow(
color: Colors.black,
offset: Offset(0, 0),
blurRadius: 5,
),
],
),
)
: null,
2022-07-08 10:41:36 +02:00
leading: Hero(
tag: 'stories_${controller.roomId}',
child: Avatar(
mxContent: controller.avatar,
name: controller.title,
),
2022-01-19 08:30:43 +01:00
),
2021-12-25 08:56:35 +01:00
),
actions: currentEvent == null
? null
: [
if (!controller.isOwnStory)
2022-01-19 08:30:43 +01:00
IconButton(
2022-09-06 20:21:11 +02:00
color: Colors.white,
2022-01-19 08:30:43 +01:00
icon: Icon(Icons.adaptive.share_outlined),
onPressed: controller.share,
2021-12-26 12:46:18 +01:00
),
PopupMenuButton<PopupStoryAction>(
2022-09-06 20:21:11 +02:00
color: Colors.white,
2022-01-19 08:30:43 +01:00
onSelected: controller.onPopupStoryAction,
2022-12-23 05:58:40 +01:00
icon: Icon(
Icons.adaptive.more_outlined,
color: Colors.white,
),
2022-01-19 08:30:43 +01:00
itemBuilder: (context) => [
if (controller.currentEvent?.canRedact ?? false)
PopupMenuItem(
value: PopupStoryAction.delete,
child: Text(L10n.of(context)!.delete),
),
PopupMenuItem(
value: PopupStoryAction.report,
child: Text(L10n.of(context)!.reportMessage),
),
2022-02-20 10:24:23 +01:00
if (!controller.isOwnStory)
PopupMenuItem(
value: PopupStoryAction.message,
child: Text(L10n.of(context)!.sendAMessage),
),
2022-01-19 08:30:43 +01:00
],
),
],
2021-12-25 08:56:35 +01:00
systemOverlayStyle: SystemUiOverlayStyle.light,
iconTheme: const IconThemeData(color: Colors.white),
elevation: 0,
backgroundColor: Colors.transparent,
2021-12-24 14:18:09 +01:00
),
extendBodyBehindAppBar: true,
2021-12-25 14:07:48 +01:00
body: FutureBuilder(
2021-12-24 14:18:09 +01:00
future: controller.loadStory,
builder: (context, snapshot) {
final error = snapshot.error;
if (error != null) {
return Center(child: Text(error.toLocalizedString(context)));
}
2021-12-25 14:07:48 +01:00
final events = controller.events;
if (snapshot.connectionState != ConnectionState.done) {
2021-12-24 14:18:09 +01:00
return const Center(
child: CircularProgressIndicator.adaptive(
strokeWidth: 2,
),
);
2021-12-24 14:18:09 +01:00
}
if (events.isEmpty) {
2021-12-25 08:56:35 +01:00
return Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Avatar(
mxContent: controller.avatar,
name: controller.title,
size: 128,
fontSize: 64,
),
const SizedBox(height: 32),
Text(
L10n.of(context)!.thisUserHasNotPostedAnythingYet,
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 20,
color: Colors.white,
),
2021-12-25 08:56:35 +01:00
),
],
),
2021-12-24 14:18:09 +01:00
);
}
final event = events[controller.index];
2022-02-20 10:24:23 +01:00
final backgroundColor = controller.storyThemeData.color1 ??
event.content.tryGet<String>('body')?.color ??
2021-12-24 14:18:09 +01:00
Theme.of(context).primaryColor;
2022-02-20 10:24:23 +01:00
final backgroundColorDark = controller.storyThemeData.color2 ??
2021-12-24 14:18:09 +01:00
event.content.tryGet<String>('body')?.darkColor ??
2022-02-20 10:24:23 +01:00
Theme.of(context).primaryColorDark;
2021-12-24 14:18:09 +01:00
if (event.messageType == MessageTypes.Text) {
controller.loadingModeOff();
}
2022-01-19 08:30:43 +01:00
final hash = event.infoMap['xyz.amorgan.blurhash'];
2022-03-10 17:28:53 +01:00
return Stack(
children: [
if (hash is String)
BlurHash(
hash: hash,
imageFit: BoxFit.cover,
),
2022-04-11 07:00:01 +02:00
if ({MessageTypes.Video, MessageTypes.Audio}
.contains(event.messageType) &&
2022-03-10 17:28:53 +01:00
PlatformInfos.isMobile)
Positioned(
top: 80,
bottom: 64,
left: 0,
right: 0,
child: FutureBuilder<VideoPlayerController?>(
future: controller.loadVideoControllerFuture ??=
controller.loadVideoController(event),
2022-02-20 10:24:23 +01:00
builder: (context, snapshot) {
2022-03-10 17:28:53 +01:00
final videoPlayerController = snapshot.data;
if (videoPlayerController == null) {
2022-02-20 10:24:23 +01:00
controller.loadingModeOn();
return const SizedBox.shrink();
2022-02-20 10:24:23 +01:00
}
controller.loadingModeOff();
2022-03-10 17:28:53 +01:00
return Center(child: VideoPlayer(videoPlayerController));
2022-02-20 10:24:23 +01:00
},
2021-12-24 14:18:09 +01:00
),
2022-03-10 17:28:53 +01:00
),
if (event.messageType == MessageTypes.Image ||
(event.messageType == MessageTypes.Video &&
!PlatformInfos.isMobile))
FutureBuilder<MatrixFile>(
future: controller.downloadAndDecryptAttachment(
event,
event.messageType == MessageTypes.Video,
),
2022-03-10 17:28:53 +01:00
builder: (context, snapshot) {
final matrixFile = snapshot.data;
if (matrixFile == null) {
controller.loadingModeOn();
return const SizedBox.shrink();
2022-03-10 17:28:53 +01:00
}
controller.loadingModeOff();
2022-04-03 10:22:19 +02:00
return Container(
constraints: const BoxConstraints.expand(),
alignment: controller.storyThemeData.fit == BoxFit.cover
? null
: Alignment.center,
2022-03-10 17:28:53 +01:00
child: Image.memory(
matrixFile.bytes,
fit: controller.storyThemeData.fit,
),
);
},
),
2022-04-08 10:12:58 +02:00
GestureDetector(
onTapDown: controller.hold,
onTapUp: controller.unhold,
onTapCancel: controller.unhold,
onVerticalDragStart: controller.hold,
onVerticalDragEnd: controller.unhold,
onHorizontalDragStart: controller.hold,
onHorizontalDragEnd: controller.unhold,
child: AnimatedContainer(
duration: FluffyThemes.animationDuration,
curve: FluffyThemes.animationCurve,
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 80,
),
2022-04-08 10:12:58 +02:00
decoration: BoxDecoration(
gradient: event.messageType == MessageTypes.Text
? LinearGradient(
colors: [
backgroundColorDark,
backgroundColor,
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
)
: null,
),
alignment: Alignment(
controller.storyThemeData.alignmentX.toDouble() / 100,
controller.storyThemeData.alignmentY.toDouble() / 100,
),
child: SafeArea(
child: Linkify(
2022-04-03 10:22:19 +02:00
text: controller.loadingMode
? L10n.of(context)!.loadingPleaseWait
: event.content.tryGet<String>('body') ?? '',
textAlign: TextAlign.center,
2023-05-23 08:22:34 +02:00
options: const LinkifyOptions(humanize: false),
onOpen: (url) =>
UrlLauncher(context, url.url).launchUrl(),
2022-04-03 10:22:19 +02:00
linkStyle: TextStyle(
fontSize: 24,
color: Colors.blue.shade50,
decoration: TextDecoration.underline,
2023-02-14 14:05:18 +01:00
decorationColor: Colors.blue.shade50,
2022-04-03 10:22:19 +02:00
shadows: event.messageType == MessageTypes.Text
? null
: textShadows,
),
style: TextStyle(
2022-04-03 10:22:19 +02:00
fontSize: 24,
color: Colors.white,
shadows: event.messageType == MessageTypes.Text
? null
: textShadows,
),
2022-02-20 10:24:23 +01:00
),
2021-12-24 14:18:09 +01:00
),
),
2022-03-10 17:28:53 +01:00
),
Positioned(
top: 4,
left: 4,
right: 4,
child: SafeArea(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var i = 0; i < events.length; i++)
Expanded(
child: i == controller.index
? LinearProgressIndicator(
color: Colors.white,
minHeight: 2,
2022-12-23 05:58:40 +01:00
backgroundColor:
Colors.white.withOpacity(0.25),
2022-03-10 17:28:53 +01:00
value: controller.loadingMode
? null
: controller.progress.inMilliseconds /
StoryPageController
.maxProgress.inMilliseconds,
)
: Container(
margin: const EdgeInsets.all(4),
height: 2,
color: i < controller.index
? Colors.white
2022-12-23 05:58:40 +01:00
: Colors.white.withOpacity(0.25),
2022-03-10 17:28:53 +01:00
),
),
],
2021-12-26 11:30:44 +01:00
),
),
2022-03-10 17:28:53 +01:00
),
if (!controller.isOwnStory && currentEvent != null)
Positioned(
2022-12-23 05:58:40 +01:00
bottom: 8,
left: 8,
right: 8,
2022-03-10 17:28:53 +01:00
child: SafeArea(
2022-12-23 05:58:40 +01:00
child: Material(
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(AppConfig.borderRadius),
bottomRight: Radius.circular(AppConfig.borderRadius),
),
shadowColor: Colors.black.withAlpha(64),
clipBehavior: Clip.hardEdge,
elevation: 4,
child: TextField(
focusNode: controller.replyFocus,
controller: controller.replyController,
onSubmitted: controller.replyAction,
textInputAction: TextInputAction.send,
readOnly: controller.replyLoading,
decoration: InputDecoration(
contentPadding:
const EdgeInsets.fromLTRB(0, 16, 0, 16),
hintText: L10n.of(context)!.reply,
prefixIcon: IconButton(
onPressed: controller.replyEmojiAction,
icon: const Icon(Icons.emoji_emotions_outlined),
),
suffixIcon: controller.replyLoading
? const SizedBox(
width: 16,
height: 16,
child: Center(
child: CircularProgressIndicator.adaptive(
strokeWidth: 2,
),
2022-12-23 05:58:40 +01:00
),
)
: IconButton(
onPressed: controller.replyAction,
icon: const Icon(Icons.send_outlined),
2022-03-10 17:28:53 +01:00
),
2022-12-23 05:58:40 +01:00
fillColor: Theme.of(context).colorScheme.background,
),
2021-12-26 11:30:44 +01:00
),
2021-12-24 14:18:09 +01:00
),
),
2022-03-10 17:28:53 +01:00
),
if (controller.isOwnStory &&
controller.currentSeenByUsers.isNotEmpty)
Positioned(
bottom: 16,
left: 16,
right: 16,
child: SafeArea(
child: Center(
child: OutlinedButton.icon(
style: OutlinedButton.styleFrom(
backgroundColor:
Theme.of(context).colorScheme.surface,
2021-12-26 09:59:34 +01:00
),
2022-03-10 17:28:53 +01:00
onPressed: controller.displaySeenByUsers,
icon: const Icon(Icons.visibility_outlined),
label: Text(controller.seenByUsersTitle),
2021-12-26 11:30:44 +01:00
),
),
),
2022-03-10 17:28:53 +01:00
),
],
2021-12-24 14:18:09 +01:00
);
},
),
);
}
}