ente/lib/ui/collections_gallery_widget.dart

340 lines
11 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
2020-10-26 11:18:00 +00:00
import 'package:fluttertoast/fluttertoast.dart';
2020-10-28 15:25:32 +00:00
import 'package:logging/logging.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/events/backup_folders_updated_event.dart';
2020-10-28 12:03:28 +00:00
import 'package:photos/events/collection_updated_event.dart';
import 'package:photos/events/local_photos_updated_event.dart';
2020-10-26 11:18:00 +00:00
import 'package:photos/events/tab_changed_event.dart';
2021-03-17 21:07:17 +00:00
import 'package:photos/events/user_logged_out_event.dart';
import 'package:photos/models/collection_items.dart';
import 'package:photos/services/billing_service.dart';
2020-10-28 15:25:32 +00:00
import 'package:photos/services/collections_service.dart';
import 'package:photos/models/device_folder.dart';
import 'package:photos/ui/collection_page.dart';
2020-12-12 01:11:12 +00:00
import 'package:photos/ui/common_elements.dart';
import 'package:photos/ui/device_folder_page.dart';
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/thumbnail_widget.dart';
import 'package:path/path.dart' as p;
2020-10-26 11:18:00 +00:00
import 'package:photos/utils/toast_util.dart';
class CollectionsGalleryWidget extends StatefulWidget {
const CollectionsGalleryWidget({Key key}) : super(key: key);
@override
_CollectionsGalleryWidgetState createState() =>
_CollectionsGalleryWidgetState();
}
2020-11-10 11:36:51 +00:00
class _CollectionsGalleryWidgetState extends State<CollectionsGalleryWidget>
with AutomaticKeepAliveClientMixin {
2020-10-28 15:25:32 +00:00
final _logger = Logger("CollectionsGallery");
2020-10-28 12:03:28 +00:00
StreamSubscription<LocalPhotosUpdatedEvent> _localFilesSubscription;
StreamSubscription<CollectionUpdatedEvent> _collectionUpdatesSubscription;
StreamSubscription<BackupFoldersUpdatedEvent> _backupFoldersUpdatedEvent;
2021-03-17 21:07:17 +00:00
StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
@override
void initState() {
2020-10-28 12:03:28 +00:00
_localFilesSubscription =
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
setState(() {});
});
_collectionUpdatesSubscription =
Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
setState(() {});
});
2021-03-17 21:07:17 +00:00
_loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
setState(() {});
});
_backupFoldersUpdatedEvent =
Bus.instance.on<BackupFoldersUpdatedEvent>().listen((event) {
setState(() {});
});
super.initState();
}
@override
Widget build(BuildContext context) {
2020-11-16 08:00:31 +00:00
super.build(context);
return FutureBuilder<CollectionItems>(
future: _getCollections(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return _getCollectionsGalleryWidget(snapshot.data);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return loadWidget;
}
},
);
}
Future<CollectionItems> _getCollections() async {
2020-10-30 22:30:54 +00:00
final filesDB = FilesDB.instance;
final collectionsService = CollectionsService.instance;
final userID = Configuration.instance.getUserID();
final folders = List<DeviceFolder>();
final paths = await filesDB.getLocalPaths();
for (final path in paths) {
final folderName = p.basename(path);
folders.add(DeviceFolder(
folderName, path, await filesDB.getLastCreatedFileInPath(path)));
}
folders.sort((first, second) =>
second.thumbnail.creationTime.compareTo(first.thumbnail.creationTime));
2020-10-28 15:25:32 +00:00
final collectionsWithThumbnail = List<CollectionWithThumbnail>();
2020-10-30 22:30:54 +00:00
final collections = collectionsService.getCollections();
2020-10-28 15:25:32 +00:00
for (final c in collections) {
2020-10-31 16:11:43 +00:00
if (c.owner.id == userID) {
2020-11-30 08:57:23 +00:00
final thumbnail =
await FilesDB.instance.getLatestFileInCollection(c.id);
if (thumbnail == null) {
continue;
}
collectionsWithThumbnail
.add(CollectionWithThumbnail(c, thumbnail, null));
2020-10-28 15:25:32 +00:00
}
2020-10-30 22:30:54 +00:00
}
2020-11-30 08:57:23 +00:00
collectionsWithThumbnail.sort((first, second) {
return second.thumbnail.updationTime
.compareTo(first.thumbnail.updationTime);
2020-11-30 08:57:23 +00:00
});
2020-10-30 22:30:54 +00:00
2020-10-28 15:25:32 +00:00
return CollectionItems(folders, collectionsWithThumbnail);
}
2020-12-12 01:11:12 +00:00
Widget _getCollectionsGalleryWidget(CollectionItems items) {
return SingleChildScrollView(
child: Container(
margin: const EdgeInsets.only(bottom: 50),
child: Column(
children: [
Padding(padding: EdgeInsets.all(6)),
SectionTitle("device folders"),
Padding(padding: EdgeInsets.all(6)),
2021-03-12 08:47:57 +00:00
items.folders.isEmpty
? Padding(
padding: const EdgeInsets.all(22),
child: nothingToSeeHere,
)
: Container(
height: 160,
child: Align(
alignment: Alignment.centerLeft,
child: items.folders.isEmpty
? nothingToSeeHere
: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
physics:
ScrollPhysics(), // to disable GridView's scrolling
itemBuilder: (context, index) {
return _buildFolder(
context, items.folders[index]);
},
itemCount: items.folders.length,
),
),
),
Divider(),
2021-03-12 08:47:57 +00:00
Padding(padding: EdgeInsets.all(10)),
SectionTitle("backed-up memories"),
2021-03-08 10:48:06 +00:00
Padding(padding: EdgeInsets.all(14)),
BillingService.instance.hasActiveSubscription()
? GridView.builder(
shrinkWrap: true,
padding: EdgeInsets.fromLTRB(12, 0, 12, 0),
physics: ScrollPhysics(), // to disable GridView's scrolling
itemBuilder: (context, index) {
return _buildCollection(
context, items.collections, index);
},
itemCount:
items.collections.length + 1, // To include the + button
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 12,
),
)
: nothingToSeeHere,
],
),
2020-12-12 01:11:12 +00:00
),
);
}
Widget _buildFolder(BuildContext context, DeviceFolder folder) {
final isBackedUp =
Configuration.instance.getPathsToBackUp().contains(folder.path);
return GestureDetector(
2020-12-12 00:31:06 +00:00
child: Container(
height: 110,
2020-12-12 01:11:12 +00:00
width: 132,
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
ClipRRect(
2020-12-12 01:11:12 +00:00
borderRadius: BorderRadius.circular(18.0),
child: Container(
child: Hero(
tag: "device_folder:" + folder.path + folder.thumbnail.tag(),
child: Stack(
children: [
ThumbnailWidget(
folder.thumbnail,
shouldShowSyncStatus: false,
),
isBackedUp
? Container()
: Align(
alignment: Alignment.bottomRight,
child: Padding(
padding:
const EdgeInsets.only(right: 8, bottom: 6),
child: Icon(
Icons.cloud_off_outlined,
size: 18,
),
),
)
],
),
),
height: 110,
width: 110,
),
),
Padding(
padding: const EdgeInsets.all(6.0),
child: Text(
folder.name,
style: TextStyle(
2020-12-12 00:31:06 +00:00
fontSize: 12,
),
overflow: TextOverflow.ellipsis,
),
),
],
),
),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return DeviceFolderPage(folder);
},
),
);
},
);
}
2020-10-26 11:18:00 +00:00
Widget _buildCollection(BuildContext context,
List<CollectionWithThumbnail> collections, int index) {
2020-12-12 01:11:12 +00:00
if (index < collections.length) {
final c = collections[index];
return GestureDetector(
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(18.0),
child: Container(
child: Hero(
tag: "collection" + c.thumbnail.tag(),
child: ThumbnailWidget(c.thumbnail)),
2021-01-08 16:30:30 +00:00
height: 140,
width: 140,
2020-12-12 01:11:12 +00:00
),
),
Padding(padding: EdgeInsets.all(4)),
Expanded(
2021-01-08 16:30:30 +00:00
child: Text(
c.collection.name,
style: TextStyle(
fontSize: 16,
2020-12-12 01:11:12 +00:00
),
2021-01-08 16:30:30 +00:00
overflow: TextOverflow.ellipsis,
2020-12-12 01:11:12 +00:00
),
),
],
),
onTap: () {
final page = CollectionPage(c.collection);
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return page;
},
),
);
},
);
} else {
2020-10-26 11:18:00 +00:00
return Container(
2021-01-02 06:38:12 +00:00
padding: EdgeInsets.fromLTRB(28, 12, 28, 46),
2020-10-26 11:18:00 +00:00
child: OutlineButton(
2020-12-12 00:31:06 +00:00
shape: RoundedRectangleBorder(
2020-12-12 01:11:12 +00:00
borderRadius: BorderRadius.circular(18.0),
2020-12-12 00:31:06 +00:00
),
2020-10-26 11:18:00 +00:00
child: Icon(
Icons.add,
),
onPressed: () async {
await showToast(
2021-01-08 17:11:32 +00:00
"long press to select photos and click + to create an album",
2020-10-26 11:18:00 +00:00
toastLength: Toast.LENGTH_LONG);
2020-11-10 11:36:51 +00:00
Bus.instance.fire(
TabChangedEvent(0, TabChangedEventSource.collections_page));
2020-10-26 11:18:00 +00:00
},
),
);
}
}
@override
void dispose() {
2020-10-28 12:03:28 +00:00
_localFilesSubscription.cancel();
_collectionUpdatesSubscription.cancel();
2021-03-17 21:07:17 +00:00
_loggedOutEvent.cancel();
_backupFoldersUpdatedEvent.cancel();
super.dispose();
}
2020-11-10 11:36:51 +00:00
@override
bool get wantKeepAlive => true;
}
class SectionTitle extends StatelessWidget {
final String title;
const SectionTitle(this.title, {Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
2020-12-12 00:31:06 +00:00
margin: EdgeInsets.fromLTRB(24, 12, 0, 0),
child: Column(children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
title,
style: TextStyle(
fontWeight: FontWeight.bold,
2021-02-06 19:54:29 +00:00
color: Theme.of(context).buttonColor.withOpacity(0.8),
2020-12-12 00:31:06 +00:00
fontSize: 20,
letterSpacing: 1,
),
),
),
]));
}
}