From b9f53802b16c11bfe6be5c21d9482e6200ebcf5c Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Tue, 16 Aug 2022 19:32:37 +0200 Subject: [PATCH] improvements --- lib/foreign_types/memory.dart | 8 ++++ lib/managers/file_manager.dart | 19 +++++--- lib/widgets/memory.dart | 79 ++++++---------------------------- lib/widgets/memory_sheet.dart | 3 +- lib/widgets/memory_slide.dart | 4 +- 5 files changed, 35 insertions(+), 78 deletions(-) diff --git a/lib/foreign_types/memory.dart b/lib/foreign_types/memory.dart index fc677a7..f2b473e 100644 --- a/lib/foreign_types/memory.dart +++ b/lib/foreign_types/memory.dart @@ -1,5 +1,8 @@ +import 'dart:io'; + import 'package:path/path.dart'; import 'package:share_location/enums.dart'; +import 'package:share_location/managers/file_manager.dart'; class Memory { final String id; @@ -30,4 +33,9 @@ class Memory { MemoryType get type => filename.split('.').last == 'jpg' ? MemoryType.photo : MemoryType.video; + + Future downloadToFile() => FileManager.downloadFile( + 'memories', + location, + ); } diff --git a/lib/managers/file_manager.dart b/lib/managers/file_manager.dart index 1f15f2d..bbb4ff2 100644 --- a/lib/managers/file_manager.dart +++ b/lib/managers/file_manager.dart @@ -3,6 +3,7 @@ import 'dart:typed_data'; import 'package:path_provider/path_provider.dart'; import 'package:share_location/enums.dart'; +import 'package:share_location/foreign_types/memory.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:uuid/uuid.dart'; @@ -13,15 +14,19 @@ final supabase = Supabase.instance.client; class FileManager { static Map fileCache = {}; - static Future getUser(final String userID) async { + static Future getMemoryMetadata(final String id) async { final response = await supabase - .from('users') + .from('memories') .select() - .eq('id', userID) + .eq('id', id) .single() .execute(); - return response.data; + if (response.error != null) { + throw Exception(response.error); + } + + return Memory.parse(response.data); } static uploadFile(final User user, final File file) async { @@ -66,7 +71,7 @@ class FileManager { location.split('.').last == 'jpg' ? MemoryType.photo : MemoryType.video; try { - final file = await getFileData('memories', location); + final file = await _getFileData('memories', location); return [file, memoryType]; } catch (error) { @@ -74,7 +79,7 @@ class FileManager { } } - static Future getFileData(final String table, final String path, + static Future _getFileData(final String table, final String path, {final bool disableCache = false}) async { final key = '$table:$path'; @@ -110,7 +115,7 @@ class FileManager { } final data = - await getFileData(table, path, disableCache: disableDownloadCache); + await _getFileData(table, path, disableCache: disableDownloadCache); // Create file await file.create(recursive: true); diff --git a/lib/widgets/memory.dart b/lib/widgets/memory.dart index cd5c95e..aacd264 100644 --- a/lib/widgets/memory.dart +++ b/lib/widgets/memory.dart @@ -4,33 +4,25 @@ import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:share_location/constants/spacing.dart'; import 'package:share_location/enums.dart'; -import 'package:share_location/managers/file_manager.dart'; -import 'package:share_location/utils/auth_required.dart'; +import 'package:share_location/foreign_types/memory.dart'; import 'package:share_location/widgets/raw_memory_display.dart'; -import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:video_player/video_player.dart'; enum MemoryFetchStatus { - preparing, - loadingMetadata, downloading, error, done, } class MemoryView extends StatefulWidget { - final String location; - final DateTime creationDate; - final String filename; + final Memory memory; final bool loopVideo; final void Function(VideoPlayerController)? onVideoControllerInitialized; final VoidCallback? onFileDownloaded; const MemoryView({ Key? key, - required this.location, - required this.creationDate, - required this.filename, + required this.memory, this.loopVideo = false, this.onVideoControllerInitialized, this.onFileDownloaded, @@ -40,11 +32,9 @@ class MemoryView extends StatefulWidget { State createState() => _MemoryViewState(); } -class _MemoryViewState extends AuthRequiredState { - late final User _user; - MemoryFetchStatus status = MemoryFetchStatus.preparing; +class _MemoryViewState extends State { + MemoryFetchStatus status = MemoryFetchStatus.downloading; Uint8List? data; - MemoryType? type; @override void initState() { @@ -53,61 +43,23 @@ class _MemoryViewState extends AuthRequiredState { loadMemoryFile(); } - @override - void onAuthenticated(Session session) { - final user = session.user; - - if (user != null) { - _user = user; - } - } - Future loadMemoryFile() async { - final filename = widget.location.split('/').last; - - setState(() { - status = MemoryFetchStatus.loadingMetadata; - }); - - final response = await supabase - .from('memories') - .select() - .eq('location', '${_user.id}/$filename') - .limit(1) - .single() - .execute(); - - if (!mounted) { - return; - } - - if (response.data == null) { - setState(() { - status = MemoryFetchStatus.error; - }); - return; - } - setState(() { status = MemoryFetchStatus.downloading; }); - final memory = response.data; - final location = memory['location']; - final memoryType = - location.split('.').last == 'jpg' ? MemoryType.photo : MemoryType.video; - try { - final fileData = await FileManager.getFileData('memories', location); + final file = await widget.memory.downloadToFile(); if (!mounted) { return; } + final fileData = await file.readAsBytes(); + setState(() { status = MemoryFetchStatus.done; data = fileData; - type = memoryType; }); if (widget.onFileDownloaded != null) { @@ -138,21 +90,21 @@ class _MemoryViewState extends AuthRequiredState { fit: StackFit.expand, alignment: Alignment.center, children: [ - if (type == MemoryType.photo) + if (widget.memory.type == MemoryType.photo) ImageFiltered( imageFilter: ImageFilter.blur(sigmaX: 10, sigmaY: 10), child: RawMemoryDisplay( - filename: widget.filename, + filename: widget.memory.filename, data: data!, - type: type!, + type: widget.memory.type, loopVideo: widget.loopVideo, fit: BoxFit.cover, ), ), RawMemoryDisplay( - filename: widget.filename, + filename: widget.memory.filename, data: data!, - type: type!, + type: widget.memory.type, fit: BoxFit.contain, loopVideo: widget.loopVideo, onVideoControllerInitialized: widget.onVideoControllerInitialized, @@ -169,11 +121,6 @@ class _MemoryViewState extends AuthRequiredState { const SizedBox(height: SMALL_SPACE), () { switch (status) { - // ADD dot loading text - case MemoryFetchStatus.preparing: - return const Text('Preparing to download memory'); - case MemoryFetchStatus.loadingMetadata: - return const Text('Loading memory metadata'); case MemoryFetchStatus.downloading: return const Text('Downloading memory'); default: diff --git a/lib/widgets/memory_sheet.dart b/lib/widgets/memory_sheet.dart index 6aeadb7..88c671b 100644 --- a/lib/widgets/memory_sheet.dart +++ b/lib/widgets/memory_sheet.dart @@ -36,8 +36,7 @@ class _MemorySheetState extends State with Loadable { Future downloadFile() async { try { - final file = - await FileManager.downloadFile('memories', widget.memory.location); + final file = await widget.memory.downloadToFile(); if (!mounted) { return; diff --git a/lib/widgets/memory_slide.dart b/lib/widgets/memory_slide.dart index ee09da2..74e572b 100644 --- a/lib/widgets/memory_slide.dart +++ b/lib/widgets/memory_slide.dart @@ -92,9 +92,7 @@ class _MemorySlideState extends State paused: timeline.paused, hideProgressBar: !timelineOverlay.showOverlay, child: MemoryView( - creationDate: widget.memory.creationDate, - location: widget.memory.location, - filename: widget.memory.filename, + memory: widget.memory, loopVideo: false, onFileDownloaded: () { if (widget.memory.type == MemoryType.photo) {