ente/lib/ui/gallery_app_bar_widget.dart

329 lines
10 KiB
Dart
Raw Normal View History

2020-05-05 12:56:24 +00:00
import 'dart:async';
2021-01-13 10:34:23 +00:00
import 'dart:io';
2020-04-18 18:46:38 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
2020-10-28 12:03:28 +00:00
import 'package:page_transition/page_transition.dart';
import 'package:photos/core/configuration.dart';
2020-11-15 07:19:44 +00:00
import 'package:photos/core/event_bus.dart';
2021-02-02 16:35:38 +00:00
import 'package:photos/events/subscription_purchased_event.dart';
2020-10-29 12:56:30 +00:00
import 'package:photos/models/collection.dart';
import 'package:photos/models/selected_files.dart';
import 'package:photos/services/billing_service.dart';
2020-10-13 05:22:20 +00:00
import 'package:photos/services/collections_service.dart';
2020-10-28 12:03:28 +00:00
import 'package:photos/ui/create_collection_page.dart';
import 'package:photos/ui/share_collection_widget.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/file_util.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/utils/share_util.dart';
import 'package:photos/utils/toast_util.dart';
2020-04-18 18:46:38 +00:00
enum GalleryAppBarType {
homepage,
local_folder,
2020-10-13 21:46:46 +00:00
shared_collection,
collection,
search_results,
}
2020-05-17 14:23:37 +00:00
2020-04-18 18:46:38 +00:00
class GalleryAppBarWidget extends StatefulWidget
implements PreferredSizeWidget {
2020-05-17 14:23:37 +00:00
final GalleryAppBarType type;
2020-04-18 18:46:38 +00:00
final String title;
final SelectedFiles selectedFiles;
2020-05-17 12:39:38 +00:00
final String path;
2020-10-29 12:56:30 +00:00
final Collection collection;
2020-04-18 18:46:38 +00:00
GalleryAppBarWidget(
this.type,
this.title,
2020-10-29 12:56:30 +00:00
this.selectedFiles, {
this.path,
2020-10-29 12:56:30 +00:00
this.collection,
});
2020-04-18 18:46:38 +00:00
@override
_GalleryAppBarWidgetState createState() => _GalleryAppBarWidgetState();
@override
Size get preferredSize => Size.fromHeight(60.0);
}
class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
final _logger = Logger("GalleryAppBar");
2020-11-15 07:19:44 +00:00
StreamSubscription _userAuthEventSubscription;
2021-01-08 16:40:03 +00:00
Function() _selectedFilesListener;
2020-04-30 15:09:41 +00:00
@override
void initState() {
2021-01-08 16:40:03 +00:00
_selectedFilesListener = () {
setState(() {});
2021-01-08 16:40:03 +00:00
};
widget.selectedFiles.addListener(_selectedFilesListener);
2020-11-15 07:19:44 +00:00
_userAuthEventSubscription =
2021-02-02 16:35:38 +00:00
Bus.instance.on<SubscriptionPurchasedEvent>().listen((event) {
2020-11-15 07:19:44 +00:00
setState(() {});
});
2020-04-30 15:09:41 +00:00
super.initState();
}
2020-11-15 07:19:44 +00:00
@override
void dispose() {
_userAuthEventSubscription.cancel();
2021-01-08 16:40:03 +00:00
widget.selectedFiles.removeListener(_selectedFilesListener);
2020-11-15 07:19:44 +00:00
super.dispose();
}
2020-04-18 18:46:38 +00:00
@override
Widget build(BuildContext context) {
if (widget.selectedFiles.files.isEmpty) {
2020-04-30 15:09:41 +00:00
return AppBar(
2020-12-12 00:31:06 +00:00
backgroundColor: Color(0x00000000),
elevation: 0,
title: widget.type == GalleryAppBarType.homepage
? Container()
: Text(
widget.title,
style: TextStyle(
color: Colors.white.withOpacity(0.60),
),
),
2020-04-30 15:09:41 +00:00
actions: _getDefaultActions(context),
);
2020-04-18 18:46:38 +00:00
}
return AppBar(
leading: IconButton(
2021-01-13 10:34:23 +00:00
icon: Icon(Platform.isAndroid ? Icons.clear : CupertinoIcons.clear),
2020-04-18 18:46:38 +00:00
onPressed: () {
2020-06-19 23:03:26 +00:00
_clearSelectedFiles();
2020-04-18 18:46:38 +00:00
},
),
title: Text(widget.selectedFiles.files.length.toString()),
2020-06-19 23:03:26 +00:00
actions: _getActions(context),
2020-04-18 18:46:38 +00:00
);
}
2020-04-30 15:09:41 +00:00
List<Widget> _getDefaultActions(BuildContext context) {
List<Widget> actions = List<Widget>();
if (Configuration.instance.hasConfiguredAccount() &&
(widget.type == GalleryAppBarType.local_folder ||
widget.type == GalleryAppBarType.collection)) {
actions.add(IconButton(
icon: Icon(Icons.person_add),
onPressed: () {
_showShareCollectionDialog();
},
));
2020-04-30 15:09:41 +00:00
}
return actions;
}
2020-05-17 14:23:37 +00:00
Future<void> _showShareCollectionDialog() async {
2020-10-29 12:56:30 +00:00
var collection = widget.collection;
if (collection == null) {
if (widget.type == GalleryAppBarType.local_folder) {
collection =
CollectionsService.instance.getCollectionForPath(widget.path);
if (collection == null) {
2021-01-08 17:02:41 +00:00
final dialog = createProgressDialog(context, "please wait...");
2020-10-29 12:56:30 +00:00
await dialog.show();
try {
collection = await CollectionsService.instance
.getOrCreateForPath(widget.path);
await dialog.hide();
} catch (e, s) {
_logger.severe(e, s);
await dialog.hide();
showGenericErrorDialog(context);
}
2020-10-29 12:56:30 +00:00
}
} else {
throw Exception(
"Cannot create a collection of type" + widget.type.toString());
}
}
2020-12-03 22:41:01 +00:00
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return SharingDialog(collection);
},
);
2020-05-17 12:39:38 +00:00
}
2020-10-28 12:03:28 +00:00
Future<void> _createAlbum() async {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.bottomToTop,
child: CreateCollectionPage(
widget.selectedFiles,
2020-10-28 12:03:28 +00:00
)));
}
2020-06-19 23:03:26 +00:00
List<Widget> _getActions(BuildContext context) {
2020-04-18 18:46:38 +00:00
List<Widget> actions = List<Widget>();
if (BillingService.instance.hasActiveSubscription()) {
actions.add(IconButton(
icon:
Icon(Platform.isAndroid ? Icons.add_outlined : CupertinoIcons.add),
onPressed: () {
_createAlbum();
},
));
}
actions.add(IconButton(
2021-01-13 10:34:23 +00:00
icon: Icon(
Platform.isAndroid ? Icons.share_outlined : CupertinoIcons.share),
onPressed: () {
_shareSelected(context);
},
));
if (widget.type == GalleryAppBarType.homepage ||
widget.type == GalleryAppBarType.local_folder) {
2020-10-28 12:03:28 +00:00
actions.add(IconButton(
2021-01-13 10:34:23 +00:00
icon: Icon(
Platform.isAndroid ? Icons.delete_outline : CupertinoIcons.delete),
2020-10-28 12:03:28 +00:00
onPressed: () {
_showDeleteSheet(context);
2020-10-28 12:03:28 +00:00
},
));
} else if (widget.type == GalleryAppBarType.collection ||
(widget.type == GalleryAppBarType.shared_collection &&
widget.collection.owner.id == Configuration.instance.getUserID())) {
2021-03-01 12:06:17 +00:00
if (widget.collection.type == CollectionType.folder) {
actions.add(IconButton(
icon: Icon(Platform.isAndroid
? Icons.delete_outline
: CupertinoIcons.delete),
onPressed: () {
_showDeleteSheet(context);
2021-03-01 12:06:17 +00:00
},
));
} else {
actions.add(IconButton(
icon: Icon(Icons.remove_circle_outline_rounded),
onPressed: () {
_showRemoveFromCollectionSheet(context);
},
));
}
2020-04-18 18:46:38 +00:00
}
return actions;
}
2020-06-19 23:03:26 +00:00
void _shareSelected(BuildContext context) {
2021-01-13 10:16:31 +00:00
share(context, widget.selectedFiles.files.toList());
2020-04-18 18:46:38 +00:00
}
void _showRemoveFromCollectionSheet(BuildContext context) {
2020-11-01 05:33:53 +00:00
final count = widget.selectedFiles.files.length;
final action = CupertinoActionSheet(
2021-03-01 12:06:17 +00:00
title: Text("remove " +
2020-11-01 05:33:53 +00:00
count.toString() +
" file" +
(count == 1 ? "" : "s") +
" from " +
widget.collection.name +
"?"),
actions: <Widget>[
CupertinoActionSheetAction(
2021-01-08 17:11:32 +00:00
child: Text("remove"),
isDestructiveAction: true,
onPressed: () async {
2021-03-21 11:21:45 +00:00
Navigator.of(context, rootNavigator: true).pop();
2021-01-08 17:02:41 +00:00
final dialog = createProgressDialog(context, "removing files...");
await dialog.show();
try {
CollectionsService.instance.removeFromCollection(
widget.collection.id, widget.selectedFiles.files.toList());
await dialog.hide();
widget.selectedFiles.clearAll();
} catch (e, s) {
_logger.severe(e, s);
await dialog.hide();
showGenericErrorDialog(context);
}
},
),
],
cancelButton: CupertinoActionSheetAction(
2021-01-08 17:11:32 +00:00
child: Text("cancel"),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
),
);
showCupertinoModalPopup(context: context, builder: (_) => action);
}
2020-06-19 23:03:26 +00:00
void _showDeleteSheet(BuildContext context) {
2020-11-01 05:33:53 +00:00
final count = widget.selectedFiles.files.length;
bool containsUploadedFile = false, containsLocalFile = false;
for (final file in widget.selectedFiles.files) {
if (file.uploadedFileID != null) {
containsUploadedFile = true;
}
if (file.localID != null) {
containsLocalFile = true;
}
}
final actions = List<Widget>();
if (containsUploadedFile && containsLocalFile) {
actions.add(CupertinoActionSheetAction(
2021-02-09 12:49:38 +00:00
child: Text("this device"),
isDestructiveAction: true,
onPressed: () async {
await deleteFilesOnDeviceOnly(
context, widget.selectedFiles.files.toList());
_clearSelectedFiles();
2021-01-08 17:11:32 +00:00
showToast("files deleted from device");
Navigator.of(context, rootNavigator: true).pop();
},
));
actions.add(CupertinoActionSheetAction(
2021-01-08 17:11:32 +00:00
child: Text("everywhere"),
isDestructiveAction: true,
onPressed: () async {
await deleteFilesFromEverywhere(
context, widget.selectedFiles.files.toList());
_clearSelectedFiles();
2021-01-08 17:11:32 +00:00
showToast("files deleted from everywhere");
Navigator.of(context, rootNavigator: true).pop();
},
));
} else {
actions.add(CupertinoActionSheetAction(
2021-01-08 17:11:32 +00:00
child: Text("delete forever"),
isDestructiveAction: true,
onPressed: () async {
await deleteFilesFromEverywhere(
context, widget.selectedFiles.files.toList());
_clearSelectedFiles();
2021-01-08 17:11:32 +00:00
showToast("files deleted from everywhere");
Navigator.of(context, rootNavigator: true).pop();
},
));
}
2020-04-18 18:46:38 +00:00
final action = CupertinoActionSheet(
2021-01-08 17:11:32 +00:00
title: Text("delete " +
2020-11-01 05:33:53 +00:00
count.toString() +
" file" +
(count == 1 ? "" : "s") +
2021-03-08 09:29:59 +00:00
(containsUploadedFile && containsLocalFile ? " from" : "?")),
actions: actions,
2020-04-18 18:46:38 +00:00
cancelButton: CupertinoActionSheetAction(
2021-01-08 17:11:32 +00:00
child: Text("cancel"),
2020-04-18 18:46:38 +00:00
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
),
);
showCupertinoModalPopup(context: context, builder: (_) => action);
}
2020-06-19 23:03:26 +00:00
void _clearSelectedFiles() {
widget.selectedFiles.clearAll();
2020-04-18 18:46:38 +00:00
}
}