improvements

This commit is contained in:
Myzel394 2022-08-16 19:32:37 +02:00
parent bb1d65addb
commit b9f53802b1
5 changed files with 35 additions and 78 deletions

View File

@ -1,5 +1,8 @@
import 'dart:io';
import 'package:path/path.dart'; import 'package:path/path.dart';
import 'package:share_location/enums.dart'; import 'package:share_location/enums.dart';
import 'package:share_location/managers/file_manager.dart';
class Memory { class Memory {
final String id; final String id;
@ -30,4 +33,9 @@ class Memory {
MemoryType get type => MemoryType get type =>
filename.split('.').last == 'jpg' ? MemoryType.photo : MemoryType.video; filename.split('.').last == 'jpg' ? MemoryType.photo : MemoryType.video;
Future<File> downloadToFile() => FileManager.downloadFile(
'memories',
location,
);
} }

View File

@ -3,6 +3,7 @@ import 'dart:typed_data';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:share_location/enums.dart'; import 'package:share_location/enums.dart';
import 'package:share_location/foreign_types/memory.dart';
import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
@ -13,15 +14,19 @@ final supabase = Supabase.instance.client;
class FileManager { class FileManager {
static Map<String, Uint8List> fileCache = {}; static Map<String, Uint8List> fileCache = {};
static Future<User> getUser(final String userID) async { static Future<Memory> getMemoryMetadata(final String id) async {
final response = await supabase final response = await supabase
.from('users') .from('memories')
.select() .select()
.eq('id', userID) .eq('id', id)
.single() .single()
.execute(); .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 { static uploadFile(final User user, final File file) async {
@ -66,7 +71,7 @@ class FileManager {
location.split('.').last == 'jpg' ? MemoryType.photo : MemoryType.video; location.split('.').last == 'jpg' ? MemoryType.photo : MemoryType.video;
try { try {
final file = await getFileData('memories', location); final file = await _getFileData('memories', location);
return [file, memoryType]; return [file, memoryType];
} catch (error) { } catch (error) {
@ -74,7 +79,7 @@ class FileManager {
} }
} }
static Future<Uint8List> getFileData(final String table, final String path, static Future<Uint8List> _getFileData(final String table, final String path,
{final bool disableCache = false}) async { {final bool disableCache = false}) async {
final key = '$table:$path'; final key = '$table:$path';
@ -110,7 +115,7 @@ class FileManager {
} }
final data = final data =
await getFileData(table, path, disableCache: disableDownloadCache); await _getFileData(table, path, disableCache: disableDownloadCache);
// Create file // Create file
await file.create(recursive: true); await file.create(recursive: true);

View File

@ -4,33 +4,25 @@ import 'dart:ui';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:share_location/constants/spacing.dart'; import 'package:share_location/constants/spacing.dart';
import 'package:share_location/enums.dart'; import 'package:share_location/enums.dart';
import 'package:share_location/managers/file_manager.dart'; import 'package:share_location/foreign_types/memory.dart';
import 'package:share_location/utils/auth_required.dart';
import 'package:share_location/widgets/raw_memory_display.dart'; import 'package:share_location/widgets/raw_memory_display.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:video_player/video_player.dart'; import 'package:video_player/video_player.dart';
enum MemoryFetchStatus { enum MemoryFetchStatus {
preparing,
loadingMetadata,
downloading, downloading,
error, error,
done, done,
} }
class MemoryView extends StatefulWidget { class MemoryView extends StatefulWidget {
final String location; final Memory memory;
final DateTime creationDate;
final String filename;
final bool loopVideo; final bool loopVideo;
final void Function(VideoPlayerController)? onVideoControllerInitialized; final void Function(VideoPlayerController)? onVideoControllerInitialized;
final VoidCallback? onFileDownloaded; final VoidCallback? onFileDownloaded;
const MemoryView({ const MemoryView({
Key? key, Key? key,
required this.location, required this.memory,
required this.creationDate,
required this.filename,
this.loopVideo = false, this.loopVideo = false,
this.onVideoControllerInitialized, this.onVideoControllerInitialized,
this.onFileDownloaded, this.onFileDownloaded,
@ -40,11 +32,9 @@ class MemoryView extends StatefulWidget {
State<MemoryView> createState() => _MemoryViewState(); State<MemoryView> createState() => _MemoryViewState();
} }
class _MemoryViewState extends AuthRequiredState<MemoryView> { class _MemoryViewState extends State<MemoryView> {
late final User _user; MemoryFetchStatus status = MemoryFetchStatus.downloading;
MemoryFetchStatus status = MemoryFetchStatus.preparing;
Uint8List? data; Uint8List? data;
MemoryType? type;
@override @override
void initState() { void initState() {
@ -53,61 +43,23 @@ class _MemoryViewState extends AuthRequiredState<MemoryView> {
loadMemoryFile(); loadMemoryFile();
} }
@override
void onAuthenticated(Session session) {
final user = session.user;
if (user != null) {
_user = user;
}
}
Future<void> loadMemoryFile() async { Future<void> 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(() { setState(() {
status = MemoryFetchStatus.downloading; status = MemoryFetchStatus.downloading;
}); });
final memory = response.data;
final location = memory['location'];
final memoryType =
location.split('.').last == 'jpg' ? MemoryType.photo : MemoryType.video;
try { try {
final fileData = await FileManager.getFileData('memories', location); final file = await widget.memory.downloadToFile();
if (!mounted) { if (!mounted) {
return; return;
} }
final fileData = await file.readAsBytes();
setState(() { setState(() {
status = MemoryFetchStatus.done; status = MemoryFetchStatus.done;
data = fileData; data = fileData;
type = memoryType;
}); });
if (widget.onFileDownloaded != null) { if (widget.onFileDownloaded != null) {
@ -138,21 +90,21 @@ class _MemoryViewState extends AuthRequiredState<MemoryView> {
fit: StackFit.expand, fit: StackFit.expand,
alignment: Alignment.center, alignment: Alignment.center,
children: <Widget>[ children: <Widget>[
if (type == MemoryType.photo) if (widget.memory.type == MemoryType.photo)
ImageFiltered( ImageFiltered(
imageFilter: ImageFilter.blur(sigmaX: 10, sigmaY: 10), imageFilter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
child: RawMemoryDisplay( child: RawMemoryDisplay(
filename: widget.filename, filename: widget.memory.filename,
data: data!, data: data!,
type: type!, type: widget.memory.type,
loopVideo: widget.loopVideo, loopVideo: widget.loopVideo,
fit: BoxFit.cover, fit: BoxFit.cover,
), ),
), ),
RawMemoryDisplay( RawMemoryDisplay(
filename: widget.filename, filename: widget.memory.filename,
data: data!, data: data!,
type: type!, type: widget.memory.type,
fit: BoxFit.contain, fit: BoxFit.contain,
loopVideo: widget.loopVideo, loopVideo: widget.loopVideo,
onVideoControllerInitialized: widget.onVideoControllerInitialized, onVideoControllerInitialized: widget.onVideoControllerInitialized,
@ -169,11 +121,6 @@ class _MemoryViewState extends AuthRequiredState<MemoryView> {
const SizedBox(height: SMALL_SPACE), const SizedBox(height: SMALL_SPACE),
() { () {
switch (status) { 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: case MemoryFetchStatus.downloading:
return const Text('Downloading memory'); return const Text('Downloading memory');
default: default:

View File

@ -36,8 +36,7 @@ class _MemorySheetState extends State<MemorySheet> with Loadable {
Future<void> downloadFile() async { Future<void> downloadFile() async {
try { try {
final file = final file = await widget.memory.downloadToFile();
await FileManager.downloadFile('memories', widget.memory.location);
if (!mounted) { if (!mounted) {
return; return;

View File

@ -92,9 +92,7 @@ class _MemorySlideState extends State<MemorySlide>
paused: timeline.paused, paused: timeline.paused,
hideProgressBar: !timelineOverlay.showOverlay, hideProgressBar: !timelineOverlay.showOverlay,
child: MemoryView( child: MemoryView(
creationDate: widget.memory.creationDate, memory: widget.memory,
location: widget.memory.location,
filename: widget.memory.filename,
loopVideo: false, loopVideo: false,
onFileDownloaded: () { onFileDownloaded: () {
if (widget.memory.type == MemoryType.photo) { if (widget.memory.type == MemoryType.photo) {