diff --git a/lib/screens/main_screen.dart b/lib/screens/main_screen.dart index 76040dd..c8c3856 100644 --- a/lib/screens/main_screen.dart +++ b/lib/screens/main_screen.dart @@ -35,7 +35,6 @@ class _MainScreenState extends AuthRequiredState with Loadable { bool isRecording = false; bool lockCamera = false; bool isTorchEnabled = false; - List? lastPhoto; Uint8List? uploadingPhotoAnimation; List? zoomLevels; @@ -62,7 +61,6 @@ class _MainScreenState extends AuthRequiredState with Loadable { void initState() { super.initState(); - getLastPhoto(); onNewCameraSelected(GlobalValuesManager.cameras[0]); } @@ -101,14 +99,6 @@ class _MainScreenState extends AuthRequiredState with Loadable { } } - Future getLastPhoto() async { - final data = await FileManager.getLastFile(_user); - - setState(() { - lastPhoto = data; - }); - } - void onNewCameraSelected(final CameraDescription cameraDescription) async { final previousCameraController = controller; // Instantiating the camera controller @@ -198,10 +188,6 @@ class _MainScreenState extends AuthRequiredState with Loadable { uploadingPhotoAnimation = null; }); } - - if (mounted) { - await getLastPhoto(); - } } Future takeVideo() async { @@ -240,10 +226,6 @@ class _MainScreenState extends AuthRequiredState with Loadable { lockCamera = false; }); } - - if (mounted) { - await getLastPhoto(); - } } @override @@ -362,8 +344,6 @@ class _MainScreenState extends AuthRequiredState with Loadable { opacityDuration: DEFAULT_OPACITY_DURATION * SECONDARY_BUTTONS_DURATION_MULTIPLIER, child: TodayPhotoButton( - data: lastPhoto == null ? null : lastPhoto![0], - type: lastPhoto == null ? null : lastPhoto![1], onLeave: () { controller!.setFlashMode(FlashMode.off); }, @@ -436,7 +416,7 @@ class _MainScreenState extends AuthRequiredState with Loadable { }); }, child: zoomLevels == null - ? Text('1x') + ? const Text('1x') : Text( formatZoomLevel(currentZoomLevel), ), diff --git a/lib/widgets/raw_memory_display.dart b/lib/widgets/raw_memory_display.dart index aa4c1ee..a4d40e9 100644 --- a/lib/widgets/raw_memory_display.dart +++ b/lib/widgets/raw_memory_display.dart @@ -62,6 +62,10 @@ class _RawMemoryDisplayState extends State { videoController = VideoPlayerController.file(file); videoController!.initialize().then((value) { + if (!mounted) { + return; + } + setState(() {}); videoController!.setLooping(widget.loopVideo); videoController!.play(); diff --git a/lib/widgets/today_photo_button.dart b/lib/widgets/today_photo_button.dart index 440d1b7..f9bdb1c 100644 --- a/lib/widgets/today_photo_button.dart +++ b/lib/widgets/today_photo_button.dart @@ -1,16 +1,16 @@ import 'dart:typed_data'; import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; import 'package:quid_faciam_hodie/constants/spacing.dart'; import 'package:quid_faciam_hodie/enums.dart'; +import 'package:quid_faciam_hodie/models/memories.dart'; import 'package:quid_faciam_hodie/screens/server_loading_screen.dart'; import 'package:quid_faciam_hodie/screens/timeline_screen.dart'; import 'raw_memory_display.dart'; -class TodayPhotoButton extends StatelessWidget { - final Uint8List? data; - final MemoryType? type; +class TodayPhotoButton extends StatefulWidget { final VoidCallback onLeave; final VoidCallback onComeBack; @@ -18,26 +18,70 @@ class TodayPhotoButton extends StatelessWidget { Key? key, required this.onLeave, required this.onComeBack, - this.data, - this.type, }) : super(key: key); + @override + State createState() => _TodayPhotoButtonState(); +} + +class _TodayPhotoButtonState extends State { + Uint8List? data; + MemoryType? type; + + @override + void initState() { + super.initState(); + + final memories = context.read(); + + memories.addListener(loadMemory, ['memories']); + + loadMemory(); + } + + @override + void dispose() { + final memories = context.read(); + + memories.removeListener(loadMemory); + + super.dispose(); + } + + Future loadMemory() async { + if (!mounted) { + return; + } + + final memories = context.read(); + + final lastMemory = memories.memories.first; + + final file = await lastMemory.downloadToFile(); + final memoryData = await file.readAsBytes(); + + setState(() { + data = memoryData; + type = lastMemory.type; + }); + } + @override Widget build(BuildContext context) { return InkWell( onTap: () async { - onLeave(); + widget.onLeave(); await Navigator.push( context, MaterialPageRoute( - builder: (context) => ServerLoadingScreen( + builder: (context) => const ServerLoadingScreen( nextScreen: TimelineScreen.ID, ), ), ); - onComeBack(); + widget.onComeBack(); }, child: Container( width: 45, @@ -53,7 +97,7 @@ class TodayPhotoButton extends StatelessWidget { child: ClipRRect( borderRadius: BorderRadius.circular(SMALL_SPACE), child: (data == null || type == null) - ? const SizedBox() + ? const SizedBox.shrink() : RawMemoryDisplay( data: data!, type: type!,