refactor: Controllers

This commit is contained in:
Christian Pauly 2021-04-10 09:06:24 +02:00
parent 05dc57ad2a
commit f5f02c62d8
5 changed files with 57 additions and 33 deletions

View File

@ -0,0 +1,42 @@
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/views/image_viewer_view.dart';
import 'package:fluffychat/views/widgets/matrix.dart';
import 'package:flutter/material.dart';
import '../utils/event_extension.dart';
class ImageViewer extends StatefulWidget {
final Event event;
final void Function() onLoaded;
const ImageViewer(this.event, {Key key, this.onLoaded}) : super(key: key);
@override
ImageViewerController createState() => ImageViewerController();
}
class ImageViewerController extends State<ImageViewer> {
/// Forward this image to another room.
void forwardAction() {
Matrix.of(context).shareContent = widget.event.content;
AdaptivePageLayout.of(context).popUntilIsFirst();
}
/// Open this file with a system call.
void openFileAction() => widget.event.openFile(context, downloadOnly: true);
/// Go back if user swiped it away
void onInteractionEnds(ScaleEndDetails endDetails) {
if (PlatformInfos.usesTouchscreen == false) {
if (endDetails.velocity.pixelsPerSecond.dy >
MediaQuery.of(context).size.height * 1.50) {
Navigator.of(context, rootNavigator: false).pop();
}
}
}
@override
Widget build(BuildContext context) => ImageViewerView(this);
}

View File

@ -4,14 +4,14 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'matrix_file_extension.dart'; import 'matrix_file_extension.dart';
import '../views/image_view.dart'; import '../controllers/image_viewer_controller.dart';
extension LocalizedBody on Event { extension LocalizedBody on Event {
void openFile(BuildContext context, {bool downloadOnly = false}) async { void openFile(BuildContext context, {bool downloadOnly = false}) async {
if (!downloadOnly && if (!downloadOnly &&
[MessageTypes.Image, MessageTypes.Sticker].contains(messageType)) { [MessageTypes.Image, MessageTypes.Sticker].contains(messageType)) {
await Navigator.of(context, rootNavigator: false).push( await Navigator.of(context, rootNavigator: false).push(
MaterialPageRoute(builder: (_) => ImageView(this)), MaterialPageRoute(builder: (_) => ImageViewer(this)),
); );
return; return;
} }

View File

@ -1,4 +1,4 @@
import 'package:fluffychat/controllers/homeserver_picker_controller.dart'; import '../controllers/homeserver_picker_controller.dart';
import 'package:fluffychat/views/widgets/default_app_bar_search_field.dart'; import 'package:fluffychat/views/widgets/default_app_bar_search_field.dart';
import 'package:fluffychat/views/widgets/fluffy_banner.dart'; import 'package:fluffychat/views/widgets/fluffy_banner.dart';
import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/config/app_config.dart';

View File

@ -1,27 +1,15 @@
import 'package:adaptive_page_layout/adaptive_page_layout.dart'; import '../controllers/image_viewer_controller.dart';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/views/widgets/image_bubble.dart'; import 'package:fluffychat/views/widgets/image_bubble.dart';
import 'package:fluffychat/views/widgets/matrix.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../utils/event_extension.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import '../utils/platform_infos.dart';
class ImageView extends StatelessWidget { class ImageViewerView extends StatelessWidget {
final Event event; final ImageViewerController controller;
final void Function() onLoaded;
const ImageView(this.event, {Key key, this.onLoaded}) : super(key: key); const ImageViewerView(this.controller, {Key key}) : super(key: key);
void _forwardAction(BuildContext context) async {
Matrix.of(context).shareContent = event.content;
AdaptivePageLayout.of(context).popUntilIsFirst();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var calcVelocity = MediaQuery.of(context).size.height * 1.50;
return Scaffold( return Scaffold(
backgroundColor: Colors.black, backgroundColor: Colors.black,
extendBodyBehindAppBar: true, extendBodyBehindAppBar: true,
@ -29,7 +17,7 @@ class ImageView extends StatelessWidget {
elevation: 0, elevation: 0,
leading: IconButton( leading: IconButton(
icon: Icon(Icons.close), icon: Icon(Icons.close),
onPressed: () => Navigator.of(context, rootNavigator: false).pop(), onPressed: Navigator.of(context, rootNavigator: false).pop,
color: Colors.white, color: Colors.white,
tooltip: L10n.of(context).close, tooltip: L10n.of(context).close,
), ),
@ -37,13 +25,13 @@ class ImageView extends StatelessWidget {
actions: [ actions: [
IconButton( IconButton(
icon: Icon(Icons.reply_outlined), icon: Icon(Icons.reply_outlined),
onPressed: () => _forwardAction(context), onPressed: () => controller.forwardAction,
color: Colors.white, color: Colors.white,
tooltip: L10n.of(context).share, tooltip: L10n.of(context).share,
), ),
IconButton( IconButton(
icon: Icon(Icons.download_outlined), icon: Icon(Icons.download_outlined),
onPressed: () => event.openFile(context, downloadOnly: true), onPressed: () => controller.openFileAction,
color: Colors.white, color: Colors.white,
tooltip: L10n.of(context).downloadFile, tooltip: L10n.of(context).downloadFile,
), ),
@ -52,18 +40,12 @@ class ImageView extends StatelessWidget {
body: InteractiveViewer( body: InteractiveViewer(
minScale: 1.0, minScale: 1.0,
maxScale: 10.0, maxScale: 10.0,
onInteractionEnd: (ScaleEndDetails endDetails) { onInteractionEnd: controller.onInteractionEnds,
if (PlatformInfos.usesTouchscreen == false) {
if (endDetails.velocity.pixelsPerSecond.dy > calcVelocity) {
Navigator.of(context, rootNavigator: false).pop();
}
}
},
child: Center( child: Center(
child: ImageBubble( child: ImageBubble(
event, controller.widget.event,
tapToView: false, tapToView: false,
onLoaded: onLoaded, onLoaded: controller.widget.onLoaded,
fit: BoxFit.contain, fit: BoxFit.contain,
backgroundColor: Colors.black, backgroundColor: Colors.black,
maxSize: false, maxSize: false,

View File

@ -1,5 +1,5 @@
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/views/image_view.dart'; import 'package:fluffychat/controllers/image_viewer_controller.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter_blurhash/flutter_blurhash.dart'; import 'package:flutter_blurhash/flutter_blurhash.dart';
@ -235,7 +235,7 @@ class _ImageBubbleState extends State<ImageBubble> {
if (!widget.tapToView) return; if (!widget.tapToView) return;
Navigator.of(context, rootNavigator: false).push( Navigator.of(context, rootNavigator: false).push(
MaterialPageRoute( MaterialPageRoute(
builder: (_) => ImageView(widget.event, onLoaded: () { builder: (_) => ImageViewer(widget.event, onLoaded: () {
// If the original file didn't load yet, we want to do that now. // If the original file didn't load yet, we want to do that now.
// This is so that the original file displays after going on the image viewer, // This is so that the original file displays after going on the image viewer,
// waiting for it to load, and then hitting back. This ensures that we always // waiting for it to load, and then hitting back. This ensures that we always