ente/lib/ui/memories_widget.dart

421 lines
12 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2022-07-12 06:30:02 +00:00
import 'package:photos/ente_theme_data.dart';
import 'package:photos/models/memory.dart';
2021-07-10 07:44:21 +00:00
import 'package:photos/services/memories_service.dart';
2020-07-29 19:07:23 +00:00
import 'package:photos/ui/extents_page_view.dart';
import 'package:photos/ui/viewer/file/file_widget.dart';
import 'package:photos/ui/viewer/file/thumbnail_widget.dart';
2020-07-21 10:25:19 +00:00
import 'package:photos/utils/date_time_util.dart';
2020-07-29 15:48:13 +00:00
import 'package:photos/utils/file_util.dart';
import 'package:photos/utils/navigation_util.dart';
2020-07-21 11:51:09 +00:00
import 'package:photos/utils/share_util.dart';
2022-04-25 14:27:10 +00:00
import 'package:step_progress_indicator/step_progress_indicator.dart';
class MemoriesWidget extends StatelessWidget {
const MemoriesWidget({Key key}) : super(key: key);
2020-08-07 16:15:56 +00:00
@override
Widget build(BuildContext context) {
return FutureBuilder<List<Memory>>(
2020-07-21 12:19:55 +00:00
future: MemoriesService.instance.getMemories(),
builder: (context, snapshot) {
2021-08-05 19:35:09 +00:00
if (snapshot.hasError || !snapshot.hasData || snapshot.data.isEmpty) {
return const SizedBox.shrink();
} else {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildMemories(snapshot.data),
2022-07-04 06:02:17 +00:00
const Divider(),
],
2020-12-12 00:31:06 +00:00
);
}
},
);
}
Widget _buildMemories(List<Memory> memories) {
final collatedMemories = _collateMemories(memories);
2021-07-10 07:45:21 +00:00
final List<Widget> memoryWidgets = [];
for (final memories in collatedMemories) {
memoryWidgets.add(MemoryWidget(memories: memories));
}
2021-04-19 10:52:52 +00:00
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(children: memoryWidgets),
);
}
List<List<Memory>> _collateMemories(List<Memory> memories) {
2021-07-10 07:45:21 +00:00
final List<Memory> yearlyMemories = [];
final List<List<Memory>> collatedMemories = [];
for (int index = 0; index < memories.length; index++) {
if (index > 0 &&
!_areMemoriesFromSameYear(memories[index - 1], memories[index])) {
2021-07-10 07:45:21 +00:00
final List<Memory> collatedYearlyMemories = [];
collatedYearlyMemories.addAll(yearlyMemories);
collatedMemories.add(collatedYearlyMemories);
yearlyMemories.clear();
}
yearlyMemories.add(memories[index]);
}
if (yearlyMemories.isNotEmpty) {
collatedMemories.add(yearlyMemories);
}
2021-04-27 16:00:29 +00:00
return collatedMemories.reversed.toList();
}
bool _areMemoriesFromSameYear(Memory first, Memory second) {
2022-08-29 14:43:31 +00:00
final firstDate =
DateTime.fromMicrosecondsSinceEpoch(first.file.creationTime);
2022-08-29 14:43:31 +00:00
final secondDate =
DateTime.fromMicrosecondsSinceEpoch(second.file.creationTime);
return firstDate.year == secondDate.year;
}
}
class MemoryWidget extends StatefulWidget {
const MemoryWidget({
Key key,
@required this.memories,
}) : super(key: key);
final List<Memory> memories;
@override
State<MemoryWidget> createState() => _MemoryWidgetState();
}
class _MemoryWidgetState extends State<MemoryWidget> {
@override
Widget build(BuildContext context) {
2020-07-29 13:20:44 +00:00
final index = _getNextMemoryIndex();
final title = _getTitle(widget.memories[index]);
2020-07-21 10:25:19 +00:00
return GestureDetector(
onTap: () async {
await routeToPage(
2022-06-11 08:23:52 +00:00
context,
FullScreenMemory(title, widget.memories, index),
forceCustomPageRoute: true,
2022-06-11 08:23:52 +00:00
);
setState(() {});
2020-07-21 10:25:19 +00:00
},
2021-08-05 19:35:09 +00:00
child: SizedBox(
width: 92,
height: 100,
2020-07-21 10:25:19 +00:00
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
2020-08-25 00:54:32 +00:00
_buildMemoryItem(context, index),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(4)),
Hero(
tag: title,
child: Material(
type: MaterialType.transparency,
child: Text(
title,
style: Theme.of(context)
.textTheme
.subtitle1
.copyWith(fontSize: 12),
2021-07-10 07:44:21 +00:00
textAlign: TextAlign.center,
),
),
),
2020-07-21 10:25:19 +00:00
],
),
),
),
);
}
2020-08-25 00:54:32 +00:00
Container _buildMemoryItem(BuildContext context, int index) {
final memory = widget.memories[index];
final isSeen = memory.isSeen();
2020-07-21 22:21:40 +00:00
return Container(
decoration: BoxDecoration(
border: isSeen
2022-07-04 06:02:17 +00:00
? const Border()
2020-07-21 22:21:40 +00:00
: Border.all(
2022-07-12 06:30:02 +00:00
color: Theme.of(context).colorScheme.greenAlternative,
2020-07-21 22:21:40 +00:00
width: isSeen ? 0 : 2,
),
borderRadius: BorderRadius.circular(40),
),
child: ClipOval(
2021-08-05 19:35:09 +00:00
child: SizedBox(
width: isSeen ? 60 : 56,
height: isSeen ? 60 : 56,
2020-07-21 22:21:40 +00:00
child: Hero(
tag: "memories" + memory.file.tag(),
child: ThumbnailWidget(
memory.file,
shouldShowSyncStatus: false,
key: Key("memories" + memory.file.tag()),
),
2020-07-21 22:21:40 +00:00
),
),
),
);
}
2021-10-03 20:21:07 +00:00
// Returns either the first unseen memory or the memory that succeeds the
// last seen memory
2020-07-29 13:20:44 +00:00
int _getNextMemoryIndex() {
int lastSeenIndex = 0;
2021-10-03 20:21:07 +00:00
int lastSeenTimestamp = 0;
for (var index = 0; index < widget.memories.length; index++) {
final memory = widget.memories[index];
if (!memory.isSeen()) {
return index;
} else {
2021-10-03 20:21:07 +00:00
if (memory.seenTime() > lastSeenTimestamp) {
lastSeenIndex = index;
lastSeenTimestamp = memory.seenTime();
}
}
2020-07-21 22:01:44 +00:00
}
2021-10-03 20:21:07 +00:00
if (lastSeenIndex == widget.memories.length - 1) {
return 0;
}
return lastSeenIndex + 1;
2020-07-21 22:01:44 +00:00
}
2020-07-21 12:11:33 +00:00
String _getTitle(Memory memory) {
final present = DateTime.now();
final then = DateTime.fromMicrosecondsSinceEpoch(memory.file.creationTime);
final diffInYears = present.year - then.year;
if (diffInYears == 1) {
2020-07-21 12:11:33 +00:00
return "1 year ago";
} else {
2020-07-21 12:11:33 +00:00
return diffInYears.toString() + " years ago";
}
}
}
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;
2020-07-29 19:07:23 +00:00
PageController _pageController;
bool _shouldDisableScroll = false;
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;
_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) {
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(
getFormattedDate(
2022-06-11 08:23:52 +00:00
DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
),
2022-04-25 14:27:10 +00:00
style: Theme.of(context).textTheme.subtitle1.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(
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
),
);
}
Hero _buildInfoText() {
2020-07-29 19:07:23 +00:00
return Hero(
tag: widget.title,
child: Container(
alignment: Alignment.bottomCenter,
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.fromLTRB(0, 0, 0, 28),
child: _showCounter
? Text(
'${_index + 1}/${widget.memories.length}',
style: Theme.of(context)
.textTheme
.bodyText1
.copyWith(color: Colors.white.withOpacity(0.4)),
)
: AnimatedOpacity(
opacity: _opacity,
2022-07-04 06:02:17 +00:00
duration: const Duration(milliseconds: 500),
child: Text(
widget.title,
style: Theme.of(context)
.textTheme
.headline4
.copyWith(color: Colors.white),
),
),
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() {
final file = widget.memories[_index].file;
2020-07-29 19:07:23 +00:00
return Container(
2022-06-11 08:23:52 +00:00
alignment: Alignment.bottomRight,
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.fromLTRB(0, 0, 26, 20),
2022-06-11 08:23:52 +00:00
child: IconButton(
icon: Icon(
Icons.adaptive.share,
color: Colors.white, //same for both themes
),
2022-06-11 08:23:52 +00:00
onPressed: () {
share(context, [file]);
},
),
);
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() {
_pageController = PageController(initialPage: _index);
return ExtentsPageView.extents(
2020-07-21 22:01:44 +00:00
itemBuilder: (BuildContext context, int index) {
2020-07-29 19:07:23 +00:00
if (index < widget.memories.length - 1) {
final nextFile = widget.memories[index + 1].file;
2021-04-27 16:29:58 +00:00
preloadThumbnail(nextFile);
2021-04-27 16:36:07 +00:00
preloadFile(nextFile);
2020-07-29 19:07:23 +00:00
}
2020-07-21 22:01:44 +00:00
final file = widget.memories[index].file;
return FileWidget(
file,
autoPlay: false,
tagPrefix: "memories",
shouldDisableScroll: (value) {
setState(() {
_shouldDisableScroll = value;
});
},
2022-07-04 06:02:17 +00:00
backgroundDecoration: const BoxDecoration(
color: Colors.transparent,
),
);
2020-07-21 22:01:44 +00:00
},
itemCount: widget.memories.length,
2020-07-29 19:07:23 +00:00
controller: _pageController,
2020-08-07 16:15:56 +00:00
extents: 1,
2020-07-29 19:07:23 +00:00
onPageChanged: (index) async {
2020-07-21 22:01:44 +00:00
await MemoriesService.instance.markMemoryAsSeen(widget.memories[index]);
2020-07-21 22:54:36 +00:00
setState(() {
_index = index;
});
2020-07-21 22:01:44 +00:00
},
physics: _shouldDisableScroll
2022-07-04 06:02:17 +00:00
? const NeverScrollableScrollPhysics()
: const PageScrollPhysics(),
2020-07-21 22:01:44 +00:00
);
}
2020-07-21 10:25:19 +00:00
}