ente/lib/ui/memories_widget.dart

348 lines
9.5 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.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 16:35:31 +00:00
import 'package:photos/ui/blurred_file_backdrop.dart';
2020-07-29 19:07:23 +00:00
import 'package:photos/ui/extents_page_view.dart';
import 'package:photos/ui/file_widget.dart';
import 'package:photos/ui/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';
2020-07-21 22:01:44 +00:00
class MemoriesWidget extends StatefulWidget {
const MemoriesWidget({Key key}) : super(key: key);
2020-07-21 22:01:44 +00:00
@override
_MemoriesWidgetState createState() => _MemoriesWidgetState();
}
2020-08-07 16:15:56 +00:00
class _MemoriesWidgetState extends State<MemoriesWidget>
with AutomaticKeepAliveClientMixin {
final _logger = Logger("MemoriesWidget");
2021-04-27 16:00:29 +00:00
Function _listener;
2020-07-21 22:01:44 +00:00
@override
void initState() {
_listener = () {
if (mounted) {
setState(() {
_logger.info("Building because memories listener fired");
});
}
};
MemoriesService.instance.addListener(_listener);
2020-07-21 22:01:44 +00:00
super.initState();
}
@override
void dispose() {
MemoriesService.instance.removeListener(_listener);
super.dispose();
}
2020-08-07 16:15:56 +00:00
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
_logger.info("Building memories");
2020-08-07 16:15:56 +00:00
super.build(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 Container();
} else {
2020-12-12 00:31:06 +00:00
return Padding(
padding: const EdgeInsets.all(8.0),
child: _buildMemories(snapshot.data),
);
}
},
);
}
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) {
var firstDate =
DateTime.fromMicrosecondsSinceEpoch(first.file.creationTime);
var secondDate =
DateTime.fromMicrosecondsSinceEpoch(second.file.creationTime);
return firstDate.year == secondDate.year;
}
}
class MemoryWidget extends StatelessWidget {
const MemoryWidget({
Key key,
@required this.memories,
}) : super(key: key);
final List<Memory> memories;
@override
Widget build(BuildContext context) {
2020-07-29 13:20:44 +00:00
final index = _getNextMemoryIndex();
2020-07-21 22:01:44 +00:00
final title = _getTitle(memories[index]);
2020-07-21 10:25:19 +00:00
return GestureDetector(
onTap: () {
routeToPage(context, FullScreenMemory(title, memories, index));
2020-07-21 10:25:19 +00:00
},
2021-08-05 19:35:09 +00:00
child: SizedBox(
2020-07-21 10:25:19 +00:00
width: 100,
height: 120,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
2020-08-25 00:54:32 +00:00
_buildMemoryItem(context, index),
2020-12-12 00:31:06 +00:00
Padding(padding: EdgeInsets.all(4)),
Hero(
tag: title,
child: Material(
type: MaterialType.transparency,
child: Text(
title,
style: TextStyle(
fontSize: 12,
2020-12-12 00:31:06 +00:00
color: Colors.white60,
),
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) {
2020-07-21 22:21:40 +00:00
final isSeen = memories[index].isSeen();
return Container(
decoration: BoxDecoration(
border: isSeen
? Border()
: Border.all(
2021-05-12 15:59:07 +00:00
color: Theme.of(context).buttonColor,
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(
2020-07-21 22:21:40 +00:00
width: isSeen ? 76 : 72,
height: isSeen ? 76 : 72,
child: Hero(
tag: "memories" + memories[index].file.tag(),
child: ThumbnailWidget(
memories[index].file,
shouldShowSyncStatus: false,
2021-05-08 18:05:51 +00:00
key: Key("memories" + memories[index].file.tag()),
),
2020-07-21 22:21:40 +00:00
),
),
),
);
}
2020-07-29 13:20:44 +00:00
int _getNextMemoryIndex() {
int lastSeenIndex = 0;
2021-05-08 18:05:51 +00:00
for (var index = memories.length - 1; index >= 0; index--) {
2020-07-21 22:01:44 +00:00
if (!memories[index].isSeen()) {
lastSeenIndex = index;
} else {
break;
}
2020-07-21 22:01:44 +00:00
}
return lastSeenIndex;
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
2020-07-21 22:01:44 +00:00
FullScreenMemory(this.title, this.memories, this.index, {Key key})
: super(key: key);
2020-07-21 10:25:19 +00:00
@override
_FullScreenMemoryState createState() => _FullScreenMemoryState();
}
class _FullScreenMemoryState extends State<FullScreenMemory> {
2020-07-21 22:01:44 +00:00
int _index = 0;
double _opacity = 1;
2020-07-29 19:07:23 +00:00
PageController _pageController;
bool _shouldDisableScroll = false;
2020-07-21 12:11:33 +00:00
@override
void initState() {
super.initState();
2020-07-21 22:01:44 +00:00
_index = widget.index;
2020-07-21 12:11:33 +00:00
Future.delayed(Duration(seconds: 3), () {
2020-07-21 22:01:44 +00:00
if (mounted) {
setState(() {
_opacity = 0;
});
}
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(
2020-07-29 19:07:23 +00:00
title: Text(getFormattedDate(
DateTime.fromMicrosecondsSinceEpoch(file.creationTime))),
2020-07-21 22:54:36 +00:00
backgroundColor: Color(0x00000000),
elevation: 0,
actions: [
IconButton(
icon: Icon(Icons.share),
onPressed: () {
2021-01-13 10:16:31 +00:00
share(context, [file]);
2020-07-21 22:54:36 +00:00
},
),
],
),
extendBodyBehindAppBar: true,
2020-07-21 22:31:18 +00:00
body: Container(
color: Colors.black,
child: Stack(children: [
2020-07-29 19:07:23 +00:00
BlurredFileBackdrop(file),
2020-07-21 22:31:18 +00:00
_buildSwiper(),
2020-07-29 19:07:23 +00:00
_buildTitleText(),
_buildIndexText(),
]),
),
);
}
Hero _buildTitleText() {
return Hero(
tag: widget.title,
child: Container(
alignment: Alignment.bottomCenter,
padding: EdgeInsets.fromLTRB(0, 0, 0, 160),
child: AnimatedOpacity(
opacity: _opacity,
duration: Duration(milliseconds: 500),
child: Material(
type: MaterialType.transparency,
child: Text(
widget.title,
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.bold,
decoration: TextDecoration.none),
2020-07-21 12:11:33 +00:00
),
),
2020-07-29 19:07:23 +00:00
),
2020-07-21 22:31:18 +00:00
),
2020-07-21 10:25:19 +00:00
);
}
2020-07-21 22:01:44 +00:00
2020-07-29 19:07:23 +00:00
Widget _buildIndexText() {
return Container(
alignment: Alignment.bottomCenter,
padding: EdgeInsets.fromLTRB(0, 0, 0, 20),
child: Text(
(_index + 1).toString() + " / " + widget.memories.length.toString(),
style: TextStyle(
fontSize: 24,
decoration: TextDecoration.none,
color: Colors.white60,
),
),
);
}
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;
});
},
backgroundDecoration: 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
? NeverScrollableScrollPhysics()
: PageScrollPhysics(),
2020-07-21 22:01:44 +00:00
);
}
2020-07-21 10:25:19 +00:00
}