feat: Extended stories

This commit is contained in:
Krille Fear 2022-02-20 10:24:23 +01:00 committed by Christian Pauly
parent e6b2f7f19c
commit 2967da5546
5 changed files with 374 additions and 177 deletions

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'dart:math';
import 'package:fluffychat/utils/story_theme_data.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:file_picker_cross/file_picker_cross.dart'; import 'package:file_picker_cross/file_picker_cross.dart';
@ -41,22 +42,29 @@ class AddStoryController extends State<AddStoryPage> {
bool textFieldHasFocus = false; bool textFieldHasFocus = false;
Timer? _updateColorsCooldown; BoxFit fit = BoxFit.contain;
void updateColors() { int alignmentX = 0;
if (hasText != controller.text.isNotEmpty) { int alignmentY = 0;
void toggleBoxFit() {
if (fit == BoxFit.contain) {
setState(() { setState(() {
hasText = controller.text.isNotEmpty; fit = BoxFit.cover;
});
} else {
setState(() {
fit = BoxFit.contain;
});
}
}
void updateHasText(String text) {
if (hasText != text.isNotEmpty) {
setState(() {
hasText = text.isNotEmpty;
}); });
} }
_updateColorsCooldown?.cancel();
_updateColorsCooldown = Timer(
const Duration(seconds: 3),
() => setState(() {
backgroundColor = controller.text.color;
backgroundColorDark = controller.text.darkColor;
}),
);
} }
void importMedia() async { void importMedia() async {
@ -65,13 +73,16 @@ class AddStoryController extends State<AddStoryPage> {
); );
final fileName = picked.fileName; final fileName = picked.fileName;
if (fileName == null) return; if (fileName == null) return;
final shrinked = await MatrixImageFile.shrink( final shrinked = await showFutureLoadingDialog(
context: context,
future: () => MatrixImageFile.shrink(
bytes: picked.toUint8List(), bytes: picked.toUint8List(),
name: fileName, name: fileName,
compute: Matrix.of(context).client.runInBackground, compute: Matrix.of(context).client.runInBackground,
),
); );
setState(() { setState(() {
image = shrinked; image = shrinked.result;
}); });
} }
@ -80,14 +91,27 @@ class AddStoryController extends State<AddStoryPage> {
source: ImageSource.camera, source: ImageSource.camera,
); );
if (picked == null) return; if (picked == null) return;
final shrinked = await showFutureLoadingDialog(
context: context,
future: () async {
final bytes = await picked.readAsBytes(); final bytes = await picked.readAsBytes();
final shrinked = await MatrixImageFile.shrink( return await MatrixImageFile.shrink(
bytes: bytes, bytes: bytes,
name: picked.name, name: picked.name,
compute: Matrix.of(context).client.runInBackground, compute: Matrix.of(context).client.runInBackground,
); );
});
setState(() { setState(() {
image = shrinked; image = shrinked.result;
});
}
void updateColor() {
final rand = Random().nextInt(1000).toString();
setState(() {
backgroundColor = rand.color;
backgroundColorDark = rand.darkColor;
}); });
} }
@ -143,7 +167,14 @@ class AddStoryController extends State<AddStoryPage> {
final thumbnail = await video.getVideoThumbnail(); final thumbnail = await video.getVideoThumbnail();
await storiesRoom.sendFileEvent( await storiesRoom.sendFileEvent(
video, video,
extraContent: {'body': controller.text}, extraContent: {
'body': controller.text,
StoryThemeData.contentKey: StoryThemeData(
fit: fit,
alignmentX: alignmentX,
alignmentY: alignmentY,
).toJson(),
},
thumbnail: thumbnail, thumbnail: thumbnail,
); );
return; return;
@ -152,11 +183,28 @@ class AddStoryController extends State<AddStoryPage> {
if (image != null) { if (image != null) {
await storiesRoom.sendFileEvent( await storiesRoom.sendFileEvent(
image, image,
extraContent: {'body': controller.text}, extraContent: {
'body': controller.text,
StoryThemeData.contentKey: StoryThemeData(
fit: fit,
alignmentX: alignmentX,
alignmentY: alignmentY,
).toJson(),
},
); );
return; return;
} }
await storiesRoom.sendTextEvent(controller.text); await storiesRoom.sendEvent(<String, dynamic>{
'msgtype': MessageTypes.Text,
'body': controller.text,
StoryThemeData.contentKey: StoryThemeData(
color1: backgroundColor,
color2: backgroundColorDark,
fit: fit,
alignmentX: alignmentX,
alignmentY: alignmentY,
).toJson(),
});
}, },
); );
if (postResult.error == null) { if (postResult.error == null) {
@ -164,12 +212,40 @@ class AddStoryController extends State<AddStoryPage> {
} }
} }
void onVerticalDragUpdate(DragUpdateDetails details) {
final delta = details.primaryDelta;
if (delta == null) return;
if (delta > 0 && alignmentY < 100) {
setState(() {
alignmentY += 1;
});
} else if (delta < 0 && alignmentY > -100) {
setState(() {
alignmentY -= 1;
});
}
}
void onHorizontalDragUpdate(DragUpdateDetails details) {
final delta = details.primaryDelta;
if (delta == null) return;
if (delta > 0 && alignmentX < 100) {
setState(() {
alignmentX += 1;
});
} else if (delta < 0 && alignmentX > -100) {
setState(() {
alignmentX -= 1;
});
}
}
@override @override
void initState() { void initState() {
super.initState(); super.initState();
final text = Matrix.of(context).client.userID!; final rand = Random().nextInt(1000).toString();
backgroundColor = text.color; backgroundColor = rand.color;
backgroundColorDark = text.darkColor; backgroundColorDark = rand.darkColor;
focusNode.addListener(() { focusNode.addListener(() {
if (textFieldHasFocus != focusNode.hasFocus) { if (textFieldHasFocus != focusNode.hasFocus) {
setState(() { setState(() {

View File

@ -13,6 +13,7 @@ class AddStoryView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final video = controller.videoPlayerController; final video = controller.videoPlayerController;
return Scaffold( return Scaffold(
backgroundColor: Colors.blueGrey.shade900, backgroundColor: Colors.blueGrey.shade900,
appBar: AppBar( appBar: AppBar(
@ -35,6 +36,15 @@ class AddStoryView extends StatelessWidget {
), ),
actions: [ actions: [
if (controller.hasMedia) if (controller.hasMedia)
IconButton(
icon: const Icon(Icons.fullscreen_outlined),
onPressed: controller.toggleBoxFit,
),
if (!controller.hasMedia)
IconButton(
icon: const Icon(Icons.color_lens_outlined),
onPressed: controller.updateColor,
),
IconButton( IconButton(
icon: const Icon(Icons.delete_outlined), icon: const Icon(Icons.delete_outlined),
onPressed: controller.reset, onPressed: controller.reset,
@ -42,7 +52,10 @@ class AddStoryView extends StatelessWidget {
], ],
), ),
extendBodyBehindAppBar: true, extendBodyBehindAppBar: true,
body: Stack( body: GestureDetector(
onVerticalDragUpdate: controller.onVerticalDragUpdate,
onHorizontalDragUpdate: controller.onHorizontalDragUpdate,
child: Stack(
children: [ children: [
if (video != null) if (video != null)
Padding( Padding(
@ -53,7 +66,7 @@ class AddStoryView extends StatelessWidget {
), ),
), ),
AnimatedContainer( AnimatedContainer(
duration: const Duration(seconds: 2), duration: const Duration(seconds: 1),
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
horizontal: 8.0, horizontal: 8.0,
vertical: 80.0, vertical: 80.0,
@ -63,7 +76,7 @@ class AddStoryView extends StatelessWidget {
? null ? null
: DecorationImage( : DecorationImage(
image: MemoryImage(controller.image!.bytes), image: MemoryImage(controller.image!.bytes),
fit: BoxFit.contain, fit: controller.fit,
opacity: 0.75, opacity: 0.75,
), ),
gradient: controller.hasMedia gradient: controller.hasMedia
@ -77,7 +90,12 @@ class AddStoryView extends StatelessWidget {
end: Alignment.bottomCenter, end: Alignment.bottomCenter,
), ),
), ),
child: Center( child: Align(
alignment: Alignment(
controller.alignmentX / 100,
controller.alignmentY / 100,
),
child: IntrinsicWidth(
child: TextField( child: TextField(
controller: controller.controller, controller: controller.controller,
focusNode: controller.focusNode, focusNode: controller.focusNode,
@ -89,11 +107,32 @@ class AddStoryView extends StatelessWidget {
style: TextStyle( style: TextStyle(
fontSize: 24, fontSize: 24,
color: Colors.white, color: Colors.white,
backgroundColor: !controller.hasMedia shadows: controller.hasMedia
? null ? const [
: Colors.black.withOpacity(0.5), Shadow(
color: Colors.black,
offset: Offset(5, 5),
blurRadius: 20,
), ),
onChanged: (_) => controller.updateColors(), 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,
),
]
: null,
),
onChanged: controller.updateHasText,
decoration: InputDecoration( decoration: InputDecoration(
border: InputBorder.none, border: InputBorder.none,
hintText: controller.hasMedia hintText: controller.hasMedia
@ -110,28 +149,14 @@ class AddStoryView extends StatelessWidget {
), ),
), ),
), ),
),
], ],
), ),
floatingActionButton: Column( ),
floatingActionButton: Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
if (!controller.hasMedia && !controller.textFieldHasFocus) ...[ if (!controller.hasMedia) ...[
FloatingActionButton(
onPressed: controller.captureVideo,
backgroundColor: controller.backgroundColorDark,
foregroundColor: Colors.white,
heroTag: null,
child: const Icon(Icons.video_camera_front_outlined),
),
const SizedBox(height: 16),
FloatingActionButton(
onPressed: controller.capturePhoto,
backgroundColor: controller.backgroundColorDark,
foregroundColor: Colors.white,
heroTag: null,
child: const Icon(Icons.camera_alt_outlined),
),
const SizedBox(height: 16),
FloatingActionButton( FloatingActionButton(
onPressed: controller.importMedia, onPressed: controller.importMedia,
backgroundColor: controller.backgroundColorDark, backgroundColor: controller.backgroundColorDark,
@ -139,9 +164,25 @@ class AddStoryView extends StatelessWidget {
heroTag: null, heroTag: null,
child: const Icon(Icons.photo_outlined), child: const Icon(Icons.photo_outlined),
), ),
const SizedBox(width: 16),
FloatingActionButton(
onPressed: controller.capturePhoto,
backgroundColor: controller.backgroundColorDark,
foregroundColor: Colors.white,
heroTag: null,
child: const Icon(Icons.camera_alt_outlined),
),
const SizedBox(width: 16),
FloatingActionButton(
onPressed: controller.captureVideo,
backgroundColor: controller.backgroundColorDark,
foregroundColor: Colors.white,
heroTag: null,
child: const Icon(Icons.video_camera_front_outlined),
),
], ],
if (controller.hasMedia || controller.hasText) ...[ if (controller.hasMedia || controller.hasText) ...[
const SizedBox(height: 16), const SizedBox(width: 16),
FloatingActionButton( FloatingActionButton(
onPressed: controller.postStory, onPressed: controller.postStory,
backgroundColor: Theme.of(context).colorScheme.surface, backgroundColor: Theme.of(context).colorScheme.surface,

View File

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:fluffychat/utils/story_theme_data.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:adaptive_dialog/adaptive_dialog.dart'; import 'package:adaptive_dialog/adaptive_dialog.dart';
@ -44,6 +45,10 @@ class StoryPageController extends State<StoryPage> {
Timeline? timeline; Timeline? timeline;
Event? get currentEvent => index < events.length ? events[index] : null; Event? get currentEvent => index < events.length ? events[index] : null;
StoryThemeData get storyThemeData =>
StoryThemeData.fromJson(currentEvent?.content
.tryGetMap<String, dynamic>(StoryThemeData.contentKey) ??
{});
bool replyLoading = false; bool replyLoading = false;
bool _modalOpened = false; bool _modalOpened = false;
@ -467,6 +472,14 @@ class StoryPageController extends State<StoryPage> {
case PopupStoryAction.delete: case PopupStoryAction.delete:
_delete(); _delete();
break; break;
case PopupStoryAction.message:
final roomIdResult = await showFutureLoadingDialog(
context: context,
future: () => currentEvent!.sender.startDirectChat(),
);
if (roomIdResult.error != null) return;
VRouter.of(context).toSegments(['rooms', roomIdResult.result!]);
break;
} }
} }
@ -493,4 +506,5 @@ extension on List<Event> {
enum PopupStoryAction { enum PopupStoryAction {
report, report,
delete, delete,
message,
} }

View File

@ -19,6 +19,29 @@ class StoryView extends StatelessWidget {
final StoryPageController controller; final StoryPageController controller;
const StoryView(this.controller, {Key? key}) : super(key: key); const StoryView(this.controller, {Key? key}) : super(key: key);
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,
),
];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final currentEvent = controller.currentEvent; final currentEvent = controller.currentEvent;
@ -85,6 +108,11 @@ class StoryView extends StatelessWidget {
value: PopupStoryAction.report, value: PopupStoryAction.report,
child: Text(L10n.of(context)!.reportMessage), child: Text(L10n.of(context)!.reportMessage),
), ),
if (!controller.isOwnStory)
PopupMenuItem(
value: PopupStoryAction.message,
child: Text(L10n.of(context)!.sendAMessage),
),
], ],
), ),
], ],
@ -134,9 +162,10 @@ class StoryView extends StatelessWidget {
); );
} }
final event = events[controller.index]; final event = events[controller.index];
final backgroundColor = event.content.tryGet<String>('body')?.color ?? final backgroundColor = controller.storyThemeData.color1 ??
event.content.tryGet<String>('body')?.color ??
Theme.of(context).primaryColor; Theme.of(context).primaryColor;
final backgroundColorDark = final backgroundColorDark = controller.storyThemeData.color2 ??
event.content.tryGet<String>('body')?.darkColor ?? event.content.tryGet<String>('body')?.darkColor ??
Theme.of(context).primaryColorDark; Theme.of(context).primaryColorDark;
if (event.messageType == MessageTypes.Text) { if (event.messageType == MessageTypes.Text) {
@ -146,6 +175,11 @@ class StoryView extends StatelessWidget {
return GestureDetector( return GestureDetector(
onTapDown: controller.hold, onTapDown: controller.hold,
onTapUp: controller.unhold, onTapUp: controller.unhold,
onTapCancel: controller.unhold,
onVerticalDragStart: controller.hold,
onVerticalDragEnd: controller.unhold,
onHorizontalDragStart: controller.hold,
onHorizontalDragEnd: controller.unhold,
child: Stack( child: Stack(
children: [ children: [
if (hash is String) if (hash is String)
@ -178,12 +212,7 @@ class StoryView extends StatelessWidget {
if (event.messageType == MessageTypes.Image || if (event.messageType == MessageTypes.Image ||
(event.messageType == MessageTypes.Video && (event.messageType == MessageTypes.Video &&
!PlatformInfos.isMobile)) !PlatformInfos.isMobile))
Positioned( FutureBuilder<MatrixFile>(
top: 80,
bottom: 64,
left: 0,
right: 0,
child: FutureBuilder<MatrixFile>(
future: controller.downloadAndDecryptAttachment( future: controller.downloadAndDecryptAttachment(
event, event.messageType == MessageTypes.Video), event, event.messageType == MessageTypes.Video),
builder: (context, snapshot) { builder: (context, snapshot) {
@ -196,12 +225,11 @@ class StoryView extends StatelessWidget {
return Center( return Center(
child: Image.memory( child: Image.memory(
matrixFile.bytes, matrixFile.bytes,
fit: BoxFit.contain, fit: controller.storyThemeData.fit,
), ),
); );
}, },
), ),
),
AnimatedContainer( AnimatedContainer(
duration: const Duration(milliseconds: 200), duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
@ -220,37 +248,32 @@ class StoryView extends StatelessWidget {
) )
: null, : null,
), ),
alignment: Alignment.center, alignment: Alignment(
child: ListView( controller.storyThemeData.alignmentX.toDouble() / 100,
shrinkWrap: true, controller.storyThemeData.alignmentY.toDouble() / 100,
children: [ ),
LinkText( child: LinkText(
text: controller.loadingMode text: controller.loadingMode
? L10n.of(context)!.loadingPleaseWait ? L10n.of(context)!.loadingPleaseWait
: event.content.tryGet<String>('body') ?? '', : event.content.tryGet<String>('body') ?? '',
textAlign: TextAlign.center, textAlign: TextAlign.center,
onLinkTap: (url) => onLinkTap: (url) => UrlLauncher(context, url).launchUrl(),
UrlLauncher(context, url).launchUrl(),
linkStyle: TextStyle( linkStyle: TextStyle(
fontSize: 24, fontSize: 24,
color: Colors.blue.shade50, color: Colors.blue.shade50,
decoration: TextDecoration.underline, decoration: TextDecoration.underline,
backgroundColor: shadows: event.messageType == MessageTypes.Text
event.messageType == MessageTypes.Text
? null ? null
: Colors.black, : textShadows,
), ),
textStyle: TextStyle( textStyle: TextStyle(
fontSize: 24, fontSize: 24,
color: Colors.white, color: Colors.white,
backgroundColor: shadows: event.messageType == MessageTypes.Text
event.messageType == MessageTypes.Text
? null ? null
: Colors.black, : textShadows,
), ),
), ),
],
),
), ),
Positioned( Positioned(
top: 4, top: 4,

View File

@ -0,0 +1,43 @@
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
class StoryThemeData {
final Color? color1;
final Color? color2;
final BoxFit fit;
final int alignmentX;
final int alignmentY;
static const String contentKey = 'msc3588.stories.design';
const StoryThemeData({
this.color1,
this.color2,
this.fit = BoxFit.contain,
this.alignmentX = 0,
this.alignmentY = 0,
});
factory StoryThemeData.fromJson(Map<String, dynamic> json) {
final color1Int = json.tryGet<int>('color1');
final color2Int = json.tryGet<int>('color2');
final color1 = color1Int == null ? null : Color(color1Int);
final color2 = color2Int == null ? null : Color(color2Int);
return StoryThemeData(
color1: color1,
color2: color2,
fit:
json.tryGet<String>('fit') == 'cover' ? BoxFit.cover : BoxFit.contain,
alignmentX: json.tryGet<int>('alignment_x') ?? 0,
alignmentY: json.tryGet<int>('alignment_y') ?? 0,
);
}
Map<String, dynamic> toJson() => {
if (color1 != null) 'color1': color1?.value,
if (color2 != null) 'color2': color2?.value,
'fit': fit.name,
'alignment_x': alignmentX,
'alignment_y': alignmentY,
};
}