ente/lib/ui/gallery_app_bar_widget.dart

199 lines
5.3 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/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:photos/core/configuration.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/core/event_bus.dart';
import 'package:photos/events/remote_sync_event.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/file_repository.dart';
import 'package:photos/models/selected_files.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/ui/setup_page.dart';
2020-05-17 14:35:31 +00:00
import 'package:photos/ui/share_folder_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';
2020-04-18 18:46:38 +00:00
enum GalleryAppBarType {
homepage,
local_folder,
remote_folder,
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-04-18 18:46:38 +00:00
GalleryAppBarWidget(
this.type,
this.title,
this.selectedFiles, [
this.path,
]);
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> {
2020-04-30 15:09:41 +00:00
bool _hasSyncErrors = false;
2020-05-05 12:56:24 +00:00
StreamSubscription<RemoteSyncEvent> _subscription;
2020-04-30 15:09:41 +00:00
@override
void initState() {
2020-05-05 12:56:24 +00:00
_subscription = Bus.instance.on<RemoteSyncEvent>().listen((event) {
2020-04-30 15:09:41 +00:00
setState(() {
_hasSyncErrors = !event.success;
});
});
widget.selectedFiles.addListener(() {
setState(() {});
});
2020-04-30 15:09:41 +00:00
super.initState();
}
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(
title: Text(widget.title),
actions: _getDefaultActions(context),
);
2020-04-18 18:46:38 +00:00
}
return AppBar(
leading: IconButton(
icon: Icon(Icons.close),
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 (_hasSyncErrors || !Configuration.instance.hasConfiguredAccount()) {
2020-04-30 15:09:41 +00:00
actions.add(IconButton(
icon: Icon(Configuration.instance.hasConfiguredAccount()
? Icons.sync_problem
: Icons.sync_disabled),
2020-04-30 15:09:41 +00:00
onPressed: () {
_openSyncConfiguration(context);
},
));
2020-06-13 19:00:59 +00:00
} else if (widget.type == GalleryAppBarType.local_folder &&
widget.title != "Favorites") {
2020-05-17 12:39:38 +00:00
actions.add(IconButton(
icon: Icon(Icons.person_add),
onPressed: () {
2020-05-17 14:23:37 +00:00
_showShareCollectionDialog();
2020-05-17 12:39:38 +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-05-17 12:39:38 +00:00
return showDialog<void>(
context: context,
builder: (BuildContext context) {
2020-05-17 14:35:31 +00:00
return ShareFolderWidget(widget.title, widget.path);
2020-05-17 12:39:38 +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 (widget.selectedFiles.files.isNotEmpty) {
if (widget.type != GalleryAppBarType.remote_folder &&
widget.type != GalleryAppBarType.search_results) {
actions.add(IconButton(
icon: Icon(Icons.delete),
onPressed: () {
2020-06-19 23:03:26 +00:00
_showDeleteSheet(context);
},
));
}
2020-04-18 18:46:38 +00:00
actions.add(IconButton(
icon: Icon(Icons.share),
onPressed: () {
2020-06-19 23:03:26 +00:00
_shareSelected(context);
2020-04-18 18:46:38 +00:00
},
));
}
return actions;
}
2020-06-19 23:03:26 +00:00
void _shareSelected(BuildContext context) {
shareMultiple(context, widget.selectedFiles.files.toList());
2020-04-18 18:46:38 +00:00
}
2020-06-19 23:03:26 +00:00
void _showDeleteSheet(BuildContext context) {
2020-04-18 18:46:38 +00:00
final action = CupertinoActionSheet(
actions: <Widget>[
CupertinoActionSheetAction(
child: Text("Delete on device"),
isDestructiveAction: true,
onPressed: () {
_deleteSelected(context, false);
2020-04-18 18:46:38 +00:00
},
),
CupertinoActionSheetAction(
child: Text("Delete everywhere [WiP]"),
isDestructiveAction: true,
onPressed: () {
_deleteSelected(context, true);
2020-04-18 18:46:38 +00:00
},
)
],
cancelButton: CupertinoActionSheetAction(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
),
);
showCupertinoModalPopup(context: context, builder: (_) => action);
}
_deleteSelected(BuildContext context, bool deleteEveryWhere) async {
2020-07-15 20:48:46 +00:00
Navigator.of(context, rootNavigator: true).pop();
final dialog = createProgressDialog(context, "Deleting...");
await dialog.show();
await deleteWithIds(widget.selectedFiles.files.toList(),
deleteEveryWhere: deleteEveryWhere);
2020-07-15 20:48:46 +00:00
await FileRepository.instance.reloadFiles();
2020-06-19 23:03:26 +00:00
_clearSelectedFiles();
await dialog.hide();
2020-04-18 18:46:38 +00:00
}
2020-06-19 23:03:26 +00:00
void _clearSelectedFiles() {
widget.selectedFiles.clearAll();
2020-04-18 18:46:38 +00:00
}
2020-04-30 15:09:41 +00:00
void _openSyncConfiguration(BuildContext context) {
final page = SetupPage();
Navigator.of(context).push(
MaterialPageRoute(
settings: RouteSettings(name: "/setup"),
builder: (BuildContext context) {
return page;
},
),
);
}
2020-05-05 12:56:24 +00:00
void dispose() {
_subscription.cancel();
super.dispose();
}
2020-04-18 18:46:38 +00:00
}