fluffychat/lib/pages/story/story_view.dart

360 lines
14 KiB
Dart
Raw Normal View History

2021-12-24 14:18:09 +01:00
//@dart=2.12
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:matrix/matrix.dart';
2021-12-26 12:46:18 +01:00
import 'package:matrix_link_text/link_text.dart';
2021-12-24 14:18:09 +01:00
import 'package:video_player/video_player.dart';
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';
2021-12-24 14:18:09 +01:00
class StoryView extends StatelessWidget {
final StoryPageController controller;
const StoryView(this.controller, {Key? key}) : super(key: key);
@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(
icon: const Icon(Icons.close),
onPressed: Navigator.of(context).pop,
),
title: AnimatedOpacity(
duration: const Duration(seconds: 1),
opacity: controller.isHold ? 0 : 1,
child: 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-25 14:07:48 +01:00
),
2021-12-26 11:30:44 +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,
leading: Avatar(
mxContent: controller.avatar,
name: controller.title,
),
2021-12-25 08:56:35 +01:00
),
),
2021-12-26 12:46:18 +01:00
actions: [
2021-12-28 23:08:27 +01:00
if (!controller.isOwnStory && currentEvent != null)
2021-12-28 19:30:19 +01:00
AnimatedOpacity(
duration: const Duration(seconds: 1),
opacity: controller.isHold ? 0 : 1,
child: IconButton(
icon: Icon(Icons.adaptive.share_outlined),
onPressed: controller.share,
),
),
2021-12-26 12:46:18 +01:00
AnimatedOpacity(
duration: const Duration(seconds: 1),
opacity: controller.isHold ? 0 : 1,
2021-12-28 19:30:19 +01:00
child: PopupMenuButton<PopupStoryAction>(
onSelected: controller.onPopupStoryAction,
2021-12-26 12:46:18 +01:00
itemBuilder: (context) => [
2021-12-30 09:53:55 +01:00
if (controller.currentEvent?.canRedact ?? false)
PopupMenuItem(
value: PopupStoryAction.delete,
child: Text(L10n.of(context)!.delete),
),
2021-12-28 19:30:19 +01:00
PopupMenuItem(
value: PopupStoryAction.report,
2021-12-26 12:46:18 +01:00
child: Text(L10n.of(context)!.reportMessage),
),
],
)),
],
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,
));
}
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];
final backgroundColor = event.content.tryGet<String>('body')?.color ??
Theme.of(context).primaryColor;
final backgroundColorDark =
event.content.tryGet<String>('body')?.darkColor ??
Theme.of(context).primaryColorDark;
if (event.messageType == MessageTypes.Text) {
controller.loadingModeOff();
}
return GestureDetector(
onTapDown: controller.hold,
onTapUp: controller.unhold,
child: Stack(
children: [
if (event.messageType == MessageTypes.Video &&
PlatformInfos.isMobile)
2021-12-28 23:08:27 +01:00
FutureBuilder<VideoPlayerController?>(
2021-12-28 20:04:26 +01:00
future: controller.loadVideoControllerFuture ??=
controller.loadVideoController(event),
2021-12-24 14:18:09 +01:00
builder: (context, snapshot) {
final videoPlayerController = snapshot.data;
if (videoPlayerController == null) {
controller.loadingModeOn();
return Container();
}
controller.loadingModeOff();
return Center(child: VideoPlayer(videoPlayerController));
},
),
if (event.messageType == MessageTypes.Image ||
(event.messageType == MessageTypes.Video &&
!PlatformInfos.isMobile))
Positioned(
top: 0,
bottom: 0,
left: 0,
right: 0,
child: FutureBuilder<MatrixFile>(
future: controller.downloadAndDecryptAttachment(
event, event.messageType == MessageTypes.Video),
builder: (context, snapshot) {
final matrixFile = snapshot.data;
if (matrixFile == null) {
controller.loadingModeOn();
final hash = event.infoMap['xyz.amorgan.blurhash'];
return hash is String
? BlurHash(
hash: hash,
imageFit: BoxFit.cover,
)
: Container();
}
controller.loadingModeOff();
2021-12-27 16:43:26 +01:00
return Center(
child: Image.memory(
matrixFile.bytes,
fit: BoxFit.contain,
),
2021-12-24 14:18:09 +01:00
);
},
),
),
AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
gradient: event.messageType == MessageTypes.Text
? LinearGradient(
colors: [
backgroundColorDark,
backgroundColor,
],
2021-12-25 08:56:35 +01:00
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
2021-12-24 14:18:09 +01:00
)
: null,
),
alignment: Alignment.center,
2021-12-26 12:46:18 +01:00
child: ListView(
shrinkWrap: true,
children: [
LinkText(
text: controller.loadingMode
? L10n.of(context)!.loadingPleaseWait
: event.content.tryGet<String>('body') ?? '',
textAlign: TextAlign.center,
onLinkTap: (url) =>
UrlLauncher(context, url).launchUrl(),
linkStyle: TextStyle(
fontSize: 24,
color: Colors.blue.shade50,
decoration: TextDecoration.underline,
backgroundColor:
event.messageType == MessageTypes.Text
? null
: Colors.black,
),
textStyle: TextStyle(
fontSize: 24,
color: Colors.white,
backgroundColor:
event.messageType == MessageTypes.Text
? null
: Colors.black,
),
),
],
2021-12-24 14:18:09 +01:00
),
),
Positioned(
2021-12-25 08:56:35 +01:00
top: 4,
left: 4,
right: 4,
2021-12-26 11:30:44 +01:00
child: AnimatedOpacity(
duration: const Duration(seconds: 1),
opacity: controller.isHold ? 0 : 1,
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,
backgroundColor: Colors.grey.shade600,
value: controller.loadingMode
? null
: controller.progress.inMilliseconds /
StoryPageController
.maxProgress.inMilliseconds,
)
: Container(
margin: const EdgeInsets.all(4),
height: 2,
color: i < controller.index
? Colors.white
: Colors.grey.shade600,
),
),
],
),
),
),
),
if (!controller.isOwnStory && currentEvent != null)
Positioned(
bottom: 16,
left: 16,
right: 16,
child: AnimatedOpacity(
duration: const Duration(seconds: 1),
opacity: controller.isHold ? 0 : 1,
child: SafeArea(
child: TextField(
focusNode: controller.replyFocus,
controller: controller.replyController,
minLines: 1,
maxLines: 7,
onSubmitted: controller.replyAction,
textInputAction: TextInputAction.newline,
readOnly: controller.replyLoading,
decoration: InputDecoration(
hintText: L10n.of(context)!.reply,
prefixIcon: IconButton(
onPressed: controller.replyEmojiAction,
icon: const Icon(Icons.emoji_emotions_outlined),
),
suffixIcon: controller.replyLoading
? const CircularProgressIndicator.adaptive(
strokeWidth: 2)
: IconButton(
onPressed: controller.replyAction,
icon: const Icon(Icons.send_outlined),
2021-12-25 08:56:35 +01:00
),
),
2021-12-26 11:30:44 +01:00
),
),
2021-12-24 14:18:09 +01:00
),
),
2021-12-26 09:59:34 +01:00
if (controller.isOwnStory &&
controller.currentSeenByUsers.isNotEmpty)
Positioned(
2021-12-26 11:30:44 +01:00
bottom: 16,
left: 16,
right: 16,
2021-12-26 11:38:16 +01:00
child: AnimatedOpacity(
duration: const Duration(seconds: 1),
opacity: controller.isHold ? 0 : 1,
child: SafeArea(
child: Center(
child: OutlinedButton.icon(
2021-12-30 09:53:55 +01:00
style: OutlinedButton.styleFrom(
backgroundColor:
Theme.of(context).colorScheme.surface,
),
2021-12-26 11:38:16 +01:00
onPressed: controller.displaySeenByUsers,
2021-12-31 10:17:15 +01:00
icon: const Icon(Icons.visibility_outlined),
label: Text(controller.seenByUsersTitle),
2021-12-26 09:59:34 +01:00
),
),
2021-12-26 11:30:44 +01:00
),
),
),
2021-12-24 14:18:09 +01:00
],
),
);
},
),
);
}
}