ente/lib/ui/gallery_app_bar_widget.dart

224 lines
6.5 KiB
Dart
Raw Normal View History

2020-05-05 12:56:24 +00:00
import 'dart:async';
2020-04-18 18:46:38 +00:00
import 'package:flutter/material.dart';
import 'package:logging/logging.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/galleryType.dart';
2021-09-20 06:41:38 +00:00
import 'package:photos/models/magic_metadata.dart';
import 'package:photos/models/selected_files.dart';
2020-10-13 05:22:20 +00:00
import 'package:photos/services/collections_service.dart';
import 'package:photos/ui/rename_dialog.dart';
import 'package:photos/ui/share_collection_widget.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/magic_util.dart';
2020-04-18 18:46:38 +00:00
class GalleryAppBarWidget extends StatefulWidget {
final GalleryType 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();
}
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;
2021-09-23 10:41:48 +00:00
String _appBarTitle;
final GlobalKey shareButtonKey = GlobalKey();
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(() {});
});
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) {
// if (widget.selectedFiles.files.isEmpty) {
2020-04-18 18:46:38 +00:00
return AppBar(
backgroundColor:
widget.type == GalleryType.homepage ? Color(0x00000000) : null,
elevation: 0,
2022-05-30 11:13:43 +00:00
centerTitle: false,
title: widget.type == GalleryType.homepage
? 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
);
}
Future<dynamic> _renameAlbum(BuildContext context) async {
if (widget.type != GalleryType.owned_collection) {
return;
}
final result = await showDialog<String>(
context: context,
builder: (BuildContext context) {
return RenameDialog(_appBarTitle, 'album');
},
barrierColor: Colors.black.withOpacity(0.85),
);
2021-09-23 10:41:48 +00:00
// indicates user cancelled the rename request
if (result == null || result.trim() == _appBarTitle.trim()) {
return;
}
final dialog = createProgressDialog(context, "changing name...");
await dialog.show();
try {
2021-09-23 11:33:25 +00:00
await CollectionsService.instance.rename(widget.collection, result);
await dialog.hide();
if (mounted) {
2021-09-23 10:41:48 +00:00
_appBarTitle = result;
setState(() {});
}
} catch (e) {
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>[];
if (Configuration.instance.hasConfiguredAccount() &&
widget.selectedFiles.files.isEmpty &&
(widget.type == GalleryType.local_folder ||
widget.type == GalleryType.owned_collection)) {
2021-09-23 09:52:59 +00:00
actions.add(
Tooltip(
message: "Share",
2021-09-23 09:52:59 +00:00
child: IconButton(
icon: Icon(Icons.adaptive.share),
2021-09-23 09:52:59 +00:00
onPressed: () {
_showShareCollectionDialog();
},
),
),
);
2020-04-30 15:09:41 +00:00
}
if (widget.type == GalleryType.owned_collection) {
2021-09-23 02:34:15 +00:00
actions.add(PopupMenuButton(
itemBuilder: (context) {
final List<PopupMenuItem> items = [];
2022-03-21 09:32:24 +00:00
if (widget.collection.type == CollectionType.album) {
items.add(
PopupMenuItem(
value: 1,
child: Row(
children: const [
Icon(Icons.edit),
Padding(
padding: EdgeInsets.all(8),
),
2022-05-16 21:15:40 +00:00
Text("Rename"),
2022-03-21 09:32:24 +00:00
],
),
),
);
}
bool isArchived = widget.collection.isArchived();
2021-09-23 02:34:15 +00:00
items.add(
PopupMenuItem(
2022-03-21 09:32:24 +00:00
value: 2,
2021-09-23 02:34:15 +00:00
child: Row(
2022-03-21 09:32:24 +00:00
children: [
Icon(isArchived ? Icons.visibility : Icons.visibility_off),
2021-09-23 02:34:15 +00:00
Padding(
padding: EdgeInsets.all(8),
),
Text(isArchived ? "Unhide" : "Hide"),
2021-09-23 02:34:15 +00:00
],
),
),
);
return items;
},
onSelected: (value) async {
if (value == 1) {
await _renameAlbum(context);
}
2022-03-21 09:32:24 +00:00
if (value == 2) {
2022-03-21 10:07:02 +00:00
await changeCollectionVisibility(
2022-03-21 09:32:24 +00:00
context,
widget.collection,
widget.collection.isArchived()
? kVisibilityVisible
: 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...");
await dialog.show();
try {
if (collection == null) {
if (widget.type == GalleryType.local_folder) {
collection =
await CollectionsService.instance.getOrCreateForPath(widget.path);
} else {
throw Exception(
"Cannot create a collection of type" + widget.type.toString());
2020-10-29 12:56:30 +00:00
}
} else {
final sharees =
await CollectionsService.instance.getSharees(collection.id);
collection = collection.copyWith(sharees: sharees);
2020-10-29 12:56:30 +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
}