ente/lib/ui/memories_widget.dart

295 lines
8.2 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2020-07-21 10:25:19 +00:00
import 'package:flutter_swiper/flutter_swiper.dart';
2020-07-21 12:19:55 +00:00
import 'package:photos/memories_service.dart';
2020-07-21 10:25:19 +00:00
import 'package:photos/models/file_type.dart';
import 'package:photos/models/memory.dart';
import 'package:photos/ui/thumbnail_widget.dart';
2020-07-21 10:25:19 +00:00
import 'package:photos/ui/video_widget.dart';
import 'package:photos/ui/zoomable_image.dart';
import 'package:photos/utils/date_time_util.dart';
2020-07-29 15:48:13 +00:00
import 'package:photos/utils/file_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 {
@override
_MemoriesWidgetState createState() => _MemoriesWidgetState();
}
class _MemoriesWidgetState extends State<MemoriesWidget> {
@override
void initState() {
MemoriesService.instance.addListener(() {
setState(() {});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<List<Memory>>(
2020-07-21 12:19:55 +00:00
future: MemoriesService.instance.getMemories(),
builder: (context, snapshot) {
if (snapshot.hasError ||
!snapshot.hasData ||
snapshot.data.length == 0) {
return Container();
} else {
return _buildMemories(snapshot.data);
}
},
);
}
Widget _buildMemories(List<Memory> memories) {
final collatedMemories = _collateMemories(memories);
final memoryWidgets = List<Widget>();
for (final memories in collatedMemories) {
memoryWidgets.add(MemoryWidget(memories: memories));
}
return Row(children: memoryWidgets);
}
List<List<Memory>> _collateMemories(List<Memory> memories) {
final yearlyMemories = List<Memory>();
final collatedMemories = List<List<Memory>>();
for (int index = 0; index < memories.length; index++) {
if (index > 0 &&
!_areMemoriesFromSameYear(memories[index - 1], memories[index])) {
final collatedYearlyMemories = List<Memory>();
collatedYearlyMemories.addAll(yearlyMemories);
collatedMemories.add(collatedYearlyMemories);
yearlyMemories.clear();
}
yearlyMemories.add(memories[index]);
}
if (yearlyMemories.isNotEmpty) {
collatedMemories.add(yearlyMemories);
}
return collatedMemories;
}
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: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
2020-07-21 22:01:44 +00:00
return FullScreenMemory(title, memories, index);
2020-07-21 10:25:19 +00:00
},
),
);
},
child: Container(
width: 100,
height: 120,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
2020-07-21 22:21:40 +00:00
_buildMemoryItem(index),
2020-07-21 10:25:19 +00:00
Padding(padding: EdgeInsets.all(2)),
Hero(
tag: title,
child: Material(
type: MaterialType.transparency,
child: Text(title),
),
),
2020-07-21 10:25:19 +00:00
],
),
),
),
);
}
2020-07-21 22:21:40 +00:00
Container _buildMemoryItem(int index) {
final isSeen = memories[index].isSeen();
return Container(
decoration: BoxDecoration(
border: isSeen
? Border()
: Border.all(
color: Colors.amber,
width: isSeen ? 0 : 2,
),
borderRadius: BorderRadius.circular(40),
),
child: ClipOval(
child: Container(
width: isSeen ? 76 : 72,
height: isSeen ? 76 : 72,
child: Hero(
tag: "memories" + memories[index].file.tag(),
child: ThumbnailWidget(memories[index].file),
),
),
),
);
}
2020-07-29 13:20:44 +00:00
int _getNextMemoryIndex() {
2020-07-21 22:01:44 +00:00
for (var index = 0; index < memories.length; index++) {
if (!memories[index].isSeen()) {
return index;
}
if (index > 0 &&
memories[index - 1].seenTime() > memories[index].seenTime()) {
return index;
}
2020-07-21 22:01:44 +00:00
}
return 0;
}
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-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) {
return Scaffold(
2020-07-21 22:54:36 +00:00
appBar: AppBar(
title: Text(getFormattedDate(DateTime.fromMicrosecondsSinceEpoch(
widget.memories[_index].file.creationTime))),
backgroundColor: Color(0x00000000),
elevation: 0,
actions: [
IconButton(
icon: Icon(Icons.share),
onPressed: () {
share(context, widget.memories[_index].file);
},
),
],
),
extendBodyBehindAppBar: true,
2020-07-21 22:31:18 +00:00
body: Container(
color: Colors.black,
child: Stack(children: [
_buildSwiper(),
Hero(
tag: widget.title,
child: Container(
alignment: Alignment.bottomCenter,
2020-07-21 22:40:31 +00:00
padding: EdgeInsets.fromLTRB(0, 0, 0, 160),
2020-07-21 22:31:18 +00:00
child: AnimatedOpacity(
opacity: _opacity,
duration: Duration(milliseconds: 500),
child: Material(
type: MaterialType.transparency,
child: Text(
widget.title,
style: TextStyle(
2020-07-21 22:41:17 +00:00
fontSize: 40,
2020-07-21 22:31:18 +00:00
fontWeight: FontWeight.bold,
decoration: TextDecoration.none),
),
2020-07-21 10:25:19 +00:00
),
2020-07-21 12:11:33 +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
Swiper _buildSwiper() {
return Swiper(
itemBuilder: (BuildContext context, int index) {
final file = widget.memories[index].file;
2020-07-21 22:54:36 +00:00
return file.fileType == FileType.image
? ZoomableImage(
file,
tagPrefix: "memories",
)
: VideoWidget(
file,
tagPrefix: "memories",
autoPlay: true,
);
2020-07-21 22:01:44 +00:00
},
itemCount: widget.memories.length,
pagination: SwiperPagination(
2020-07-21 22:40:31 +00:00
alignment: Alignment.bottomCenter,
2020-07-21 22:31:18 +00:00
margin: EdgeInsets.all(36),
2020-07-21 22:40:31 +00:00
builder: FractionPaginationBuilder(
activeColor: Colors.white,
color: Colors.grey,
)),
2020-07-21 22:01:44 +00:00
loop: false,
2020-07-21 22:54:36 +00:00
control: SwiperControl(
color: _opacity == 1 ? Colors.white54 : Colors.transparent,
),
index: _index,
2020-07-21 22:01:44 +00:00
onIndexChanged: (index) async {
await MemoriesService.instance.markMemoryAsSeen(widget.memories[index]);
2020-07-21 22:54:36 +00:00
setState(() {
_index = index;
});
2020-07-29 15:48:13 +00:00
if (index < widget.memories.length - 1) {
preloadFile(widget.memories[index + 1].file);
}
2020-07-29 13:20:44 +00:00
return index;
2020-07-21 22:01:44 +00:00
},
);
}
2020-07-21 10:25:19 +00:00
}