diff --git a/lib/ui/home/memories/full_screen_memory_new.dart b/lib/ui/home/memories/full_screen_memory_new.dart index 240df4bd6..7c718eb54 100644 --- a/lib/ui/home/memories/full_screen_memory_new.dart +++ b/lib/ui/home/memories/full_screen_memory_new.dart @@ -2,31 +2,107 @@ import "dart:io"; import "package:flutter/cupertino.dart"; import "package:flutter/material.dart"; +import "package:photos/core/configuration.dart"; import "package:photos/models/memory.dart"; import "package:photos/ui/actions/file/file_actions.dart"; import "package:photos/ui/viewer/file/file_widget.dart"; +class FullScreenMemoryDataUpdater extends StatefulWidget { + final List memories; + final int initialIndex; + final Widget child; + const FullScreenMemoryDataUpdater({ + required this.memories, + required this.initialIndex, + required this.child, + super.key, + }); + + @override + State createState() => + _FullScreenMemoryDataUpdaterState(); +} + +class _FullScreenMemoryDataUpdaterState + extends State { + late ValueNotifier indexNotifier; + + @override + void initState() { + super.initState(); + indexNotifier = ValueNotifier(widget.initialIndex); + } + + @override + void dispose() { + indexNotifier.dispose(); + super.dispose(); + } + + void removeCurrentMemory() { + setState(() { + widget.memories.removeAt(indexNotifier.value); + }); + } + + @override + Widget build(BuildContext context) { + return FullScreenMemoryData( + memories: widget.memories, + indexNotifier: indexNotifier, + removeCurrentMemory: removeCurrentMemory, + child: widget.child, + ); + } +} + +class FullScreenMemoryData extends InheritedWidget { + final List memories; + final ValueNotifier indexNotifier; + final VoidCallback removeCurrentMemory; + + const FullScreenMemoryData({ + required this.memories, + required this.indexNotifier, + required this.removeCurrentMemory, + required Widget child, + Key? key, + }) : super(child: child, key: key); + + static FullScreenMemoryData? of(BuildContext context) { + return context.dependOnInheritedWidgetOfExactType(); + } + + @override + bool updateShouldNotify(FullScreenMemoryData oldWidget) { + // Checking oldWidget.memories.length != memories.length here doesn't work + //because the old widget and new widget reference the same memories list. + return true; + } +} + class FullScreenMemoryNew extends StatefulWidget { final String title; - final List memories; - final int index; - const FullScreenMemoryNew(this.title, this.memories, this.index, {super.key}); + final int initialIndex; + const FullScreenMemoryNew( + this.title, + this.initialIndex, { + super.key, + }); @override State createState() => _FullScreenMemoryNewState(); } class _FullScreenMemoryNewState extends State { - late final ValueNotifier _currentIndex; - @override void initState() { super.initState(); - _currentIndex = ValueNotifier(widget.index); } @override Widget build(BuildContext context) { + final inheritedData = FullScreenMemoryData.of(context)!; return Scaffold( extendBodyBehindAppBar: true, appBar: AppBar(), @@ -36,43 +112,57 @@ class _FullScreenMemoryNewState extends State { alignment: Alignment.bottomCenter, children: [ FileWidget( - widget.memories[index].file, + inheritedData.memories[index].file, autoPlay: false, tagPrefix: "memories", backgroundDecoration: const BoxDecoration( color: Colors.transparent, ), ), - IconButton( - icon: Icon( - Platform.isAndroid - ? Icons.delete_outline - : CupertinoIcons.delete, - color: Colors.white, //same for both themes - ), - onPressed: () async { - await showSingleFileDeleteSheet( - context, - widget.memories[_currentIndex.value].file, - onFileRemoved: (file) => - {onFileDeleted(widget.memories[_currentIndex.value])}, - ); - }, - ), + Configuration.instance.getUserID() != + inheritedData.memories[index].file.ownerID + ? Padding( + padding: const EdgeInsets.all(64.0), + child: Container( + color: Colors.red, + height: 30, + width: 30, + ), + ) + : const SizedBox.shrink(), + const BottomIcons(), ], ); }, onPageChanged: (index) { - _currentIndex.value = index; + inheritedData.indexNotifier.value = index; }, - itemCount: widget.memories.length, + itemCount: inheritedData.memories.length, ), ); } +} - Future onFileDeleted(Memory removedMemory) async { - setState(() { - widget.memories.remove(removedMemory); - }); +class BottomIcons extends StatelessWidget { + const BottomIcons({super.key}); + + @override + Widget build(BuildContext context) { + final inheritedData = FullScreenMemoryData.of(context)!; + return IconButton( + icon: Icon( + Platform.isAndroid ? Icons.delete_outline : CupertinoIcons.delete, + color: Colors.white, //same for both themes + ), + onPressed: () async { + await showSingleFileDeleteSheet( + context, + inheritedData.memories[inheritedData.indexNotifier.value].file, + onFileRemoved: (file) => { + inheritedData.removeCurrentMemory.call(), + }, + ); + }, + ); } } diff --git a/lib/ui/home/memories/memory_cover_widget.dart b/lib/ui/home/memories/memory_cover_widget.dart index bad08dd20..1be67f8dd 100644 --- a/lib/ui/home/memories/memory_cover_widget.dart +++ b/lib/ui/home/memories/memory_cover_widget.dart @@ -27,7 +27,11 @@ class _MemoryCovertWidgetState extends State { onTap: () async { await routeToPage( context, - FullScreenMemoryNew(title, widget.memories, index), + FullScreenMemoryDataUpdater( + initialIndex: index, + memories: widget.memories, + child: FullScreenMemoryNew(title, index), + ), forceCustomPageRoute: true, ); setState(() {});