ente/lib/ui/device_folders_gallery_widget.dart

143 lines
4.4 KiB
Dart
Raw Normal View History

2020-06-06 20:08:57 +00:00
import 'dart:async';
2020-04-18 18:46:38 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2020-06-06 20:08:57 +00:00
import 'package:photos/core/event_bus.dart';
2020-07-20 11:03:09 +00:00
import 'package:photos/db/files_db.dart';
2020-06-06 20:08:57 +00:00
import 'package:photos/events/local_photos_updated_event.dart';
2020-06-19 23:06:07 +00:00
import 'package:photos/favorite_files_repository.dart';
2020-05-17 14:35:31 +00:00
import 'package:photos/models/device_folder.dart';
import 'package:photos/models/filters/favorite_items_filter.dart';
import 'package:photos/models/filters/folder_name_filter.dart';
2020-06-21 00:05:15 +00:00
import 'package:photos/models/filters/video_file_filter.dart';
2020-08-09 14:58:41 +00:00
import 'package:photos/ui/common_elements.dart';
2020-05-17 14:35:31 +00:00
import 'package:photos/ui/device_folder_page.dart';
2020-05-06 18:13:24 +00:00
import 'package:photos/ui/loading_widget.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/ui/thumbnail_widget.dart';
2020-05-06 18:13:24 +00:00
import 'package:path/path.dart' as p;
2020-04-18 18:46:38 +00:00
2020-05-17 14:35:31 +00:00
class DeviceFolderGalleryWidget extends StatefulWidget {
const DeviceFolderGalleryWidget({Key key}) : super(key: key);
2020-04-18 18:46:38 +00:00
@override
2020-05-17 14:35:31 +00:00
_DeviceFolderGalleryWidgetState createState() =>
_DeviceFolderGalleryWidgetState();
2020-04-18 18:46:38 +00:00
}
2020-05-17 14:35:31 +00:00
class _DeviceFolderGalleryWidgetState extends State<DeviceFolderGalleryWidget> {
2020-06-06 20:08:57 +00:00
StreamSubscription<LocalPhotosUpdatedEvent> _subscription;
@override
void initState() {
_subscription = Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
setState(() {});
});
super.initState();
}
2020-04-18 18:46:38 +00:00
@override
Widget build(BuildContext context) {
2020-08-09 14:58:41 +00:00
return FutureBuilder<List<DeviceFolder>>(
2020-05-17 14:35:31 +00:00
future: _getDeviceFolders(),
2020-05-06 18:13:24 +00:00
builder: (context, snapshot) {
if (snapshot.hasData) {
2020-08-09 14:58:41 +00:00
if (snapshot.data.length == 0) {
return nothingToSeeHere;
}
2020-05-17 14:35:31 +00:00
return _getDeviceFolderGalleryWidget(snapshot.data);
2020-05-06 18:13:24 +00:00
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return loadWidget;
}
},
);
}
2020-04-18 18:46:38 +00:00
2020-05-17 14:35:31 +00:00
Widget _getDeviceFolderGalleryWidget(List<DeviceFolder> folders) {
2020-04-18 18:46:38 +00:00
return Container(
margin: EdgeInsets.only(top: 24),
child: GridView.builder(
shrinkWrap: true,
padding: EdgeInsets.only(bottom: 12),
physics: ScrollPhysics(), // to disable GridView's scrolling
itemBuilder: (context, index) {
2020-05-17 14:35:31 +00:00
return _buildFolder(context, folders[index]);
2020-04-18 18:46:38 +00:00
},
2020-05-17 14:35:31 +00:00
itemCount: folders.length,
2020-04-18 18:46:38 +00:00
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
),
);
}
2020-05-17 14:35:31 +00:00
Future<List<DeviceFolder>> _getDeviceFolders() async {
2020-07-20 11:03:09 +00:00
final paths = await FilesDB.instance.getLocalPaths();
2020-05-17 14:35:31 +00:00
final folders = List<DeviceFolder>();
2020-05-06 18:13:24 +00:00
for (final path in paths) {
2020-07-20 11:03:09 +00:00
final file = await FilesDB.instance.getLatestFileInPath(path);
2020-05-17 14:35:31 +00:00
final folderName = p.basename(path);
folders.add(
DeviceFolder(folderName, path, file, FolderNameFilter(folderName)));
2020-05-06 16:43:03 +00:00
}
2020-05-17 14:35:31 +00:00
folders.sort((first, second) {
return second.thumbnail.creationTime
.compareTo(first.thumbnail.creationTime);
2020-05-06 18:13:24 +00:00
});
2020-06-19 23:03:26 +00:00
if (FavoriteFilesRepository.instance.hasFavorites()) {
2020-08-09 22:34:59 +00:00
final file = await FilesDB.instance.getLatestFileAmongGeneratedIDs(
2020-06-19 23:03:26 +00:00
FavoriteFilesRepository.instance.getLiked().toList());
folders.insert(0,
DeviceFolder("Favorites", "/Favorites", file, FavoriteItemsFilter()));
2020-04-18 18:46:38 +00:00
}
2020-07-20 11:03:09 +00:00
final videos = await FilesDB.instance.getAllVideos();
2020-06-21 00:05:15 +00:00
if (videos.length > 0) {
folders.insert(
0, DeviceFolder("Videos", "/Videos", videos[0], VideoFileFilter()));
2020-06-21 00:05:15 +00:00
}
2020-05-17 14:35:31 +00:00
return folders;
2020-04-18 18:46:38 +00:00
}
2020-05-17 14:35:31 +00:00
Widget _buildFolder(BuildContext context, DeviceFolder folder) {
2020-04-18 18:46:38 +00:00
return GestureDetector(
child: Column(
children: <Widget>[
2020-04-25 10:42:27 +00:00
Container(
child: Hero(
tag: "device_folder:" + folder.path + folder.thumbnail.tag(),
child: ThumbnailWidget(folder.thumbnail)),
2020-08-09 14:51:46 +00:00
height: 140,
width: 140,
2020-04-25 10:42:27 +00:00
),
Padding(
2020-06-20 22:55:39 +00:00
padding: const EdgeInsets.all(8.0),
2020-04-18 18:46:38 +00:00
child: Text(
2020-05-17 14:35:31 +00:00
folder.name,
2020-04-18 18:46:38 +00:00
style: TextStyle(
2020-06-20 22:55:39 +00:00
fontSize: 14,
2020-04-18 18:46:38 +00:00
),
overflow: TextOverflow.ellipsis,
2020-04-18 18:46:38 +00:00
),
),
],
),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return DeviceFolderPage(folder);
},
),
);
2020-04-18 18:46:38 +00:00
},
);
}
2020-06-06 20:08:57 +00:00
@override
void dispose() {
_subscription.cancel();
super.dispose();
}
2020-04-18 18:46:38 +00:00
}