2020-05-05 12:56:24 +00:00
|
|
|
import 'dart:async';
|
2021-09-15 11:22:58 +00:00
|
|
|
|
2020-04-18 18:46:38 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2020-10-30 19:57:14 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2020-05-30 21:15:21 +00:00
|
|
|
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';
|
2022-05-08 04:34:55 +00:00
|
|
|
import 'package:photos/models/galleryType.dart';
|
2021-09-20 06:41:38 +00:00
|
|
|
import 'package:photos/models/magic_metadata.dart';
|
2020-07-15 20:29:29 +00:00
|
|
|
import 'package:photos/models/selected_files.dart';
|
2020-10-13 05:22:20 +00:00
|
|
|
import 'package:photos/services/collections_service.dart';
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/common/rename_dialog.dart';
|
2022-07-01 14:39:02 +00:00
|
|
|
import 'package:photos/ui/sharing/share_collection_widget.dart';
|
2020-07-15 19:14:37 +00:00
|
|
|
import 'package:photos/utils/dialog_util.dart';
|
2021-10-30 05:43:56 +00:00
|
|
|
import 'package:photos/utils/magic_util.dart';
|
2020-04-18 18:46:38 +00:00
|
|
|
|
2021-05-02 11:19:05 +00:00
|
|
|
class GalleryAppBarWidget extends StatefulWidget {
|
2022-05-08 04:34:55 +00:00
|
|
|
final GalleryType type;
|
2020-04-18 18:46:38 +00:00
|
|
|
final String title;
|
2020-07-15 20:29:29 +00:00
|
|
|
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
|
|
|
|
2020-07-15 17:17:53 +00:00
|
|
|
GalleryAppBarWidget(
|
|
|
|
this.type,
|
2020-07-15 20:29:29 +00:00
|
|
|
this.title,
|
2020-10-29 12:56:30 +00:00
|
|
|
this.selectedFiles, {
|
2020-07-15 17:17:53 +00:00
|
|
|
this.path,
|
2020-10-29 12:56:30 +00:00
|
|
|
this.collection,
|
|
|
|
});
|
2020-04-18 18:46:38 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_GalleryAppBarWidgetState createState() => _GalleryAppBarWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
|
2020-10-30 19:57:14 +00:00
|
|
|
final _logger = Logger("GalleryAppBar");
|
2020-11-15 07:19:44 +00:00
|
|
|
StreamSubscription _userAuthEventSubscription;
|
2021-01-08 16:40:03 +00:00
|
|
|
Function() _selectedFilesListener;
|
2021-09-23 10:41:48 +00:00
|
|
|
String _appBarTitle;
|
2022-04-22 16:05:30 +00:00
|
|
|
final GlobalKey shareButtonKey = GlobalKey();
|
2020-04-30 15:09:41 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2021-01-08 16:40:03 +00:00
|
|
|
_selectedFilesListener = () {
|
2020-07-15 20:29:29 +00:00
|
|
|
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(() {});
|
|
|
|
});
|
2021-09-23 10:41:48 +00:00
|
|
|
_appBarTitle = widget.title;
|
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) {
|
2022-05-04 11:08:06 +00:00
|
|
|
// if (widget.selectedFiles.files.isEmpty) {
|
2020-04-18 18:46:38 +00:00
|
|
|
return AppBar(
|
2022-05-04 11:08:06 +00:00
|
|
|
backgroundColor:
|
2022-05-08 04:34:55 +00:00
|
|
|
widget.type == GalleryType.homepage ? Color(0x00000000) : null,
|
2022-05-04 11:08:06 +00:00
|
|
|
elevation: 0,
|
2022-05-30 11:13:43 +00:00
|
|
|
centerTitle: false,
|
2022-05-08 04:34:55 +00:00
|
|
|
title: widget.type == GalleryType.homepage
|
2022-05-04 11:08:06 +00:00
|
|
|
? const SizedBox.shrink()
|
|
|
|
: TextButton(
|
|
|
|
child: Text(
|
|
|
|
_appBarTitle,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.headline5
|
|
|
|
.copyWith(fontSize: 16),
|
|
|
|
),
|
|
|
|
onPressed: () => _renameAlbum(context),
|
|
|
|
),
|
|
|
|
actions: _getDefaultActions(context),
|
2020-04-18 18:46:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-22 14:17:45 +00:00
|
|
|
Future<dynamic> _renameAlbum(BuildContext context) async {
|
2022-05-08 04:34:55 +00:00
|
|
|
if (widget.type != GalleryType.owned_collection) {
|
2021-09-22 14:17:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-09 06:58:37 +00:00
|
|
|
final result = await showDialog<String>(
|
2021-09-22 14:27:50 +00:00
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
2022-06-09 04:51:59 +00:00
|
|
|
return RenameDialog(_appBarTitle, 'Album');
|
2021-09-22 14:27:50 +00:00
|
|
|
},
|
|
|
|
barrierColor: Colors.black.withOpacity(0.85),
|
|
|
|
);
|
2021-09-23 10:41:48 +00:00
|
|
|
// indicates user cancelled the rename request
|
2021-11-09 06:58:37 +00:00
|
|
|
if (result == null || result.trim() == _appBarTitle.trim()) {
|
2021-09-22 14:17:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-09 04:51:59 +00:00
|
|
|
final dialog = createProgressDialog(context, "Changing name...");
|
2021-09-22 14:17:45 +00:00
|
|
|
await dialog.show();
|
|
|
|
try {
|
2021-09-23 11:33:25 +00:00
|
|
|
await CollectionsService.instance.rename(widget.collection, result);
|
2021-09-22 14:17:45 +00:00
|
|
|
await dialog.hide();
|
2021-09-22 14:27:50 +00:00
|
|
|
if (mounted) {
|
2021-09-23 10:41:48 +00:00
|
|
|
_appBarTitle = result;
|
2021-09-22 14:27:50 +00:00
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-09-22 14:17:45 +00:00
|
|
|
await dialog.hide();
|
|
|
|
showGenericErrorDialog(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:09:41 +00:00
|
|
|
List<Widget> _getDefaultActions(BuildContext context) {
|
2021-09-05 09:27:07 +00:00
|
|
|
List<Widget> actions = <Widget>[];
|
2021-01-08 16:17:26 +00:00
|
|
|
if (Configuration.instance.hasConfiguredAccount() &&
|
2022-05-30 11:13:55 +00:00
|
|
|
widget.selectedFiles.files.isEmpty &&
|
2022-05-08 04:34:55 +00:00
|
|
|
(widget.type == GalleryType.local_folder ||
|
|
|
|
widget.type == GalleryType.owned_collection)) {
|
2021-09-23 09:52:59 +00:00
|
|
|
actions.add(
|
|
|
|
Tooltip(
|
2022-05-30 11:13:55 +00:00
|
|
|
message: "Share",
|
2021-09-23 09:52:59 +00:00
|
|
|
child: IconButton(
|
2022-04-30 07:00:06 +00:00
|
|
|
icon: Icon(Icons.adaptive.share),
|
2021-09-23 09:52:59 +00:00
|
|
|
onPressed: () {
|
|
|
|
_showShareCollectionDialog();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2020-04-30 15:09:41 +00:00
|
|
|
}
|
2022-05-08 04:34:55 +00:00
|
|
|
if (widget.type == GalleryType.owned_collection) {
|
2022-06-11 08:23:52 +00:00
|
|
|
actions.add(
|
|
|
|
PopupMenuButton(
|
|
|
|
itemBuilder: (context) {
|
|
|
|
final List<PopupMenuItem> items = [];
|
|
|
|
if (widget.collection.type == CollectionType.album) {
|
|
|
|
items.add(
|
|
|
|
PopupMenuItem(
|
|
|
|
value: 1,
|
|
|
|
child: Row(
|
|
|
|
children: const [
|
|
|
|
Icon(Icons.edit),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.all(8),
|
|
|
|
),
|
|
|
|
Text("Rename"),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
bool isArchived = widget.collection.isArchived();
|
2022-03-21 09:32:24 +00:00
|
|
|
items.add(
|
|
|
|
PopupMenuItem(
|
2022-06-11 08:23:52 +00:00
|
|
|
value: 2,
|
2022-03-21 09:32:24 +00:00
|
|
|
child: Row(
|
2022-06-11 08:23:52 +00:00
|
|
|
children: [
|
|
|
|
Icon(isArchived ? Icons.visibility : Icons.visibility_off),
|
2022-03-21 09:32:24 +00:00
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.all(8),
|
|
|
|
),
|
2022-06-11 08:23:52 +00:00
|
|
|
Text(isArchived ? "Unhide" : "Hide"),
|
2022-03-21 09:32:24 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2022-06-11 08:23:52 +00:00
|
|
|
return items;
|
|
|
|
},
|
|
|
|
onSelected: (value) async {
|
|
|
|
if (value == 1) {
|
|
|
|
await _renameAlbum(context);
|
|
|
|
}
|
|
|
|
if (value == 2) {
|
|
|
|
await changeCollectionVisibility(
|
2022-03-21 09:32:24 +00:00
|
|
|
context,
|
|
|
|
widget.collection,
|
|
|
|
widget.collection.isArchived()
|
|
|
|
? kVisibilityVisible
|
2022-06-11 08:23:52 +00:00
|
|
|
: kVisibilityArchive,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2021-09-23 02:34:15 +00:00
|
|
|
}
|
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;
|
2022-05-17 11:38:21 +00:00
|
|
|
final dialog = createProgressDialog(context, "Please wait...");
|
2021-08-18 08:58:16 +00:00
|
|
|
await dialog.show();
|
2022-01-07 16:04:51 +00:00
|
|
|
try {
|
|
|
|
if (collection == null) {
|
2022-05-08 04:34:55 +00:00
|
|
|
if (widget.type == GalleryType.local_folder) {
|
2022-01-07 16:04:51 +00:00
|
|
|
collection =
|
|
|
|
await CollectionsService.instance.getOrCreateForPath(widget.path);
|
|
|
|
} else {
|
|
|
|
throw Exception(
|
2022-06-11 08:23:52 +00:00
|
|
|
"Cannot create a collection of type" + widget.type.toString(),
|
|
|
|
);
|
2020-10-29 12:56:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-07 16:04:51 +00:00
|
|
|
final sharees =
|
|
|
|
await CollectionsService.instance.getSharees(collection.id);
|
|
|
|
collection = collection.copyWith(sharees: sharees);
|
2020-10-29 12:56:30 +00:00
|
|
|
}
|
2022-01-07 16:04:51 +00:00
|
|
|
await dialog.hide();
|
|
|
|
return showDialog<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return SharingDialog(collection);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} catch (e, s) {
|
|
|
|
_logger.severe(e, s);
|
|
|
|
await dialog.hide();
|
|
|
|
showGenericErrorDialog(context);
|
2020-10-29 12:56:30 +00:00
|
|
|
}
|
2020-05-17 12:39:38 +00:00
|
|
|
}
|
2020-04-18 18:46:38 +00:00
|
|
|
}
|