ente/lib/ui/device_folders_gallery_widget.dart

131 lines
3.9 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-05-18 15:38:15 +00:00
import 'package:photos/db/photo_db.dart';
2020-06-06 20:08:57 +00:00
import 'package:photos/events/local_photos_updated_event.dart';
2020-05-06 16:43:03 +00:00
import 'package:photos/favorite_photos_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-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-05-06 18:13:24 +00:00
return FutureBuilder(
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-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 {
final paths = await PhotoDB.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-05-18 15:38:15 +00:00
final photo = await PhotoDB.instance.getLatestPhotoInPath(path);
2020-05-17 14:35:31 +00:00
final folderName = p.basename(path);
folders
.add(DeviceFolder(folderName, photo, FolderNameFilter(folderName)));
2020-05-06 16:43:03 +00:00
}
2020-05-17 14:35:31 +00:00
folders.sort((first, second) {
return second.thumbnailPhoto.createTimestamp
.compareTo(first.thumbnailPhoto.createTimestamp);
2020-05-06 18:13:24 +00:00
});
if (FavoritePhotosRepository.instance.hasFavorites()) {
2020-05-18 15:38:15 +00:00
final photo = await PhotoDB.instance.getLatestPhotoAmongGeneratedIds(
FavoritePhotosRepository.instance.getLiked().toList());
2020-05-17 14:35:31 +00:00
folders.insert(
0, DeviceFolder("Favorites", photo, FavoriteItemsFilter()));
2020-04-18 18:46:38 +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(
2020-05-17 14:35:31 +00:00
child: ThumbnailWidget(folder.thumbnailPhoto),
2020-04-25 10:42:27 +00:00
height: 150,
width: 150,
),
2020-04-18 18:46:38 +00:00
Padding(padding: EdgeInsets.all(2)),
Expanded(
child: Text(
2020-05-17 14:35:31 +00:00
folder.name,
2020-04-18 18:46:38 +00:00
style: TextStyle(
fontSize: 16,
),
),
),
],
),
onTap: () {
2020-05-17 14:35:31 +00:00
final page = DeviceFolderPage(folder);
2020-04-18 18:46:38 +00:00
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return page;
},
),
);
},
);
}
2020-06-06 20:08:57 +00:00
@override
void dispose() {
_subscription.cancel();
super.dispose();
}
2020-04-18 18:46:38 +00:00
}