ente/lib/ui/home/memories/full_screen_memory.dart

365 lines
11 KiB
Dart
Raw Normal View History

2023-05-18 17:55:03 +00:00
import "dart:async";
import "dart:io";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
2023-05-10 06:27:11 +00:00
import "package:intl/intl.dart";
2023-05-19 07:20:01 +00:00
import "package:photos/core/configuration.dart";
2023-05-18 17:43:38 +00:00
import 'package:photos/models/file.dart';
import "package:photos/models/memory.dart";
import "package:photos/services/memories_service.dart";
import "package:photos/theme/text_style.dart";
2023-02-22 09:53:50 +00:00
import "package:photos/ui/actions/file/file_actions.dart";
2023-02-25 12:13:37 +00:00
import "package:photos/ui/extents_page_view.dart";
import "package:photos/ui/viewer/file/file_widget.dart";
2023-05-15 12:01:34 +00:00
import "package:photos/ui/viewer/file_details/favorite_widget.dart";
import "package:photos/utils/file_util.dart";
import "package:photos/utils/share_util.dart";
import "package:step_progress_indicator/step_progress_indicator.dart";
2020-07-21 10:25:19 +00:00
class FullScreenMemory extends StatefulWidget {
2020-07-21 12:11:33 +00:00
final String title;
2020-07-21 22:01:44 +00:00
final List<Memory> memories;
final int index;
2020-07-21 10:25:19 +00:00
const FullScreenMemory(this.title, this.memories, this.index, {Key? key})
2020-07-21 22:01:44 +00:00
: super(key: key);
2020-07-21 10:25:19 +00:00
@override
2022-07-03 09:45:00 +00:00
State<FullScreenMemory> createState() => _FullScreenMemoryState();
2020-07-21 10:25:19 +00:00
}
class _FullScreenMemoryState extends State<FullScreenMemory> {
2020-07-21 22:01:44 +00:00
int _index = 0;
double _opacity = 1;
// shows memory counter as index+1/totalFiles for large number of memories
// when the top step indicator isn't visible.
bool _showCounter = false;
bool _showStepIndicator = true;
PageController? _pageController;
bool _shouldDisableScroll = false;
2023-05-19 07:20:01 +00:00
late int currentUserID;
final GlobalKey shareButtonKey = GlobalKey();
2020-07-21 12:11:33 +00:00
@override
void initState() {
super.initState();
2020-07-21 22:01:44 +00:00
_index = widget.index;
2023-05-19 07:20:01 +00:00
currentUserID = Configuration.instance.getUserID() ?? 0;
_showStepIndicator = widget.memories.length <= 60;
2022-07-04 06:02:17 +00:00
Future.delayed(const Duration(seconds: 3), () {
2020-07-21 22:01:44 +00:00
if (mounted) {
setState(() {
_opacity = 0;
_showCounter = !_showStepIndicator;
2020-07-21 22:01:44 +00:00
});
}
2020-07-21 12:11:33 +00:00
});
2020-07-21 22:01:44 +00:00
MemoriesService.instance.markMemoryAsSeen(widget.memories[_index]);
2020-07-21 12:11:33 +00:00
}
2020-07-21 10:25:19 +00:00
@override
Widget build(BuildContext context) {
_pageController ??= PageController(initialPage: _index);
2020-07-29 19:07:23 +00:00
final file = widget.memories[_index].file;
return Scaffold(
2020-07-21 22:54:36 +00:00
appBar: AppBar(
2022-04-25 14:27:10 +00:00
toolbarHeight: 84,
automaticallyImplyLeading: false,
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_showStepIndicator
? StepProgressIndicator(
totalSteps: widget.memories.length,
currentStep: _index + 1,
size: 2,
selectedColor: Colors.white, //same for both themes
unselectedColor: Colors.white.withOpacity(0.4),
)
: const SizedBox.shrink(),
2022-07-04 06:02:17 +00:00
const SizedBox(
2022-04-25 14:27:10 +00:00
height: 18,
),
Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 16),
child: InkWell(
onTap: () {
Navigator.pop(context);
},
2022-07-04 06:02:17 +00:00
child: const Icon(
2022-04-25 14:27:10 +00:00
Icons.close,
color: Colors.white, //same for both themes
),
),
),
Text(
2023-05-10 06:27:11 +00:00
DateFormat.yMMMd(Localizations.localeOf(context).languageCode)
.format(
DateTime.fromMicrosecondsSinceEpoch(
file.creationTime!,
),
2022-06-11 08:23:52 +00:00
),
2023-06-13 06:41:31 +00:00
style: Theme.of(context).textTheme.titleMedium!.copyWith(
2022-06-11 08:23:52 +00:00
fontSize: 14,
color: Colors.white,
), //same for both themes
2022-04-25 14:27:10 +00:00
),
],
),
],
),
flexibleSpace: Container(
decoration: BoxDecoration(
2022-06-11 08:23:52 +00:00
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.black.withOpacity(0.6),
Colors.black.withOpacity(0.5),
Colors.transparent,
],
stops: const [0, 0.6, 1],
),
),
2022-04-25 14:27:10 +00:00
),
2022-07-04 06:02:17 +00:00
backgroundColor: const Color(0x00000000),
2020-07-21 22:54:36 +00:00
elevation: 0,
),
extendBodyBehindAppBar: true,
2020-07-21 22:31:18 +00:00
body: Container(
2023-05-18 17:43:38 +00:00
key: ValueKey(widget.memories.length),
2020-07-21 22:31:18 +00:00
color: Colors.black,
2022-06-11 08:23:52 +00:00
child: Stack(
alignment: Alignment.bottomCenter,
children: [
_buildSwiper(),
bottomGradient(),
_buildInfoText(),
2022-06-11 08:23:52 +00:00
_buildBottomIcons(),
],
),
2020-07-29 19:07:23 +00:00
),
);
}
@override
void dispose() {
debugPrint("FullScreenMemoryDisposed");
// _pageController?.dispose();
_pageController = null;
super.dispose();
}
2023-05-18 17:43:38 +00:00
Future<void> onFileDeleted(Memory removedMemory) async {
if (!mounted) {
return;
}
final totalFiles = widget.memories.length;
if (totalFiles == 1) {
// Deleted the only file
Navigator.of(context).pop(); // Close pageview
return;
}
if (_index == totalFiles - 1) {
// Deleted the last file
widget.memories.remove(removedMemory);
2023-05-18 17:43:38 +00:00
await _pageController!.previousPage(
duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut,
);
setState(() {});
} else {
widget.memories.remove(removedMemory);
2023-05-18 17:43:38 +00:00
await _pageController!.nextPage(
duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut,
);
setState(() {});
}
}
Hero _buildInfoText() {
2020-07-29 19:07:23 +00:00
return Hero(
tag: widget.title,
child: SafeArea(
child: Container(
alignment: Alignment.bottomCenter,
padding: const EdgeInsets.fromLTRB(0, 0, 0, 72),
child: _showCounter
? Text(
'${_index + 1}/${widget.memories.length}',
style: darkTextTheme.bodyMuted,
)
: AnimatedOpacity(
opacity: _opacity,
duration: const Duration(milliseconds: 500),
child: Text(
widget.title,
style: darkTextTheme.h2,
),
),
),
2020-07-21 22:31:18 +00:00
),
2020-07-21 10:25:19 +00:00
);
}
2020-07-21 22:01:44 +00:00
2022-04-25 14:27:10 +00:00
Widget _buildBottomIcons() {
2023-05-18 17:43:38 +00:00
final File currentFile = widget.memories[_index].file;
2023-05-19 07:20:01 +00:00
final List<Widget> rowChildren = [
IconButton(
icon: Icon(
Platform.isAndroid ? Icons.info_outline : CupertinoIcons.info,
color: Colors.white, //same for both themes
),
onPressed: () {
showDetailsSheet(context, currentFile);
},
),
];
if (currentFile.ownerID == null || currentUserID == currentFile.ownerID) {
rowChildren.addAll(
[
IconButton(
icon: Icon(
Platform.isAndroid ? Icons.delete_outline : CupertinoIcons.delete,
color: Colors.white, //same for both themes
),
onPressed: () async {
await showSingleFileDeleteSheet(
context,
currentFile,
onFileRemoved: (file) =>
{onFileDeleted(widget.memories[_index])},
);
},
),
SizedBox(
height: 32,
child: FavoriteWidget(currentFile),
),
],
);
}
rowChildren.add(
IconButton(
icon: Icon(
Icons.adaptive.share,
color: Colors.white, //same for both themes
),
onPressed: () {
share(context, [currentFile]);
},
),
);
return SafeArea(
child: Container(
2023-05-15 12:01:34 +00:00
alignment: Alignment.bottomCenter,
2023-05-19 07:20:01 +00:00
padding: const EdgeInsets.fromLTRB(12, 0, 12, 20),
child: Row(
2023-05-19 07:20:01 +00:00
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: rowChildren,
),
2022-06-11 08:23:52 +00:00
),
);
2022-04-25 14:27:10 +00:00
}
Widget bottomGradient() {
return Container(
height: 124,
width: double.infinity,
decoration: BoxDecoration(
2022-06-11 08:23:52 +00:00
gradient: LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
colors: [
Colors.black.withOpacity(0.5), //same for both themes
Colors.transparent,
],
stops: const [0, 0.8],
),
),
2020-07-29 19:07:23 +00:00
);
}
Widget _buildSwiper() {
debugPrint(
"FullScreenbuildSwiper: $_index and total ${widget.memories.length}",
);
_pageController = PageController(initialPage: _index);
2023-05-18 18:56:21 +00:00
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTapDown: (TapDownDetails details) {
2023-05-18 18:57:52 +00:00
if (_shouldDisableScroll) {
return;
}
2023-05-18 18:56:21 +00:00
final screenWidth = MediaQuery.of(context).size.width;
2023-05-18 19:03:47 +00:00
final edgeWidth = screenWidth * 0.20; // 20% of screen width
2023-05-18 18:56:21 +00:00
if (details.localPosition.dx < edgeWidth) {
if (_index > 0) {
_pageController!.previousPage(
duration: const Duration(milliseconds: 250),
curve: Curves.ease,
);
}
} else if (details.localPosition.dx > screenWidth - edgeWidth) {
if (_index < (widget.memories.length - 1)) {
_pageController!.nextPage(
duration: const Duration(milliseconds: 250),
curve: Curves.ease,
);
}
2020-07-29 19:07:23 +00:00
}
2023-05-18 18:56:21 +00:00
},
child: ExtentsPageView.extents(
itemBuilder: (BuildContext context, int index) {
if (index < widget.memories.length - 1) {
final nextFile = widget.memories[index + 1].file;
preloadThumbnail(nextFile);
preloadFile(nextFile);
}
final file = widget.memories[index].file;
return FileWidget(
file,
autoPlay: false,
tagPrefix: "memories",
shouldDisableScroll: (value) {
if (value == _shouldDisableScroll) {
return;
}
2023-05-18 18:56:21 +00:00
setState(() {
_shouldDisableScroll = value;
});
},
backgroundDecoration: const BoxDecoration(
color: Colors.transparent,
),
);
},
itemCount: widget.memories.length,
controller: _pageController,
onPageChanged: (index) async {
unawaited(
MemoriesService.instance.markMemoryAsSeen(widget.memories[index]),
);
if (mounted) {
debugPrint(
"FullScreenonPageChanged: $index and total ${widget.memories.length}",
);
setState(() {
2023-05-18 18:56:21 +00:00
_index = index;
});
2023-05-18 18:56:21 +00:00
}
},
physics: _shouldDisableScroll
? const NeverScrollableScrollPhysics()
: const PageScrollPhysics(),
),
2020-07-21 22:01:44 +00:00
);
}
2020-07-21 10:25:19 +00:00
}