ente/lib/ui/gallery_app_bar_widget.dart

210 lines
5.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/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/db/file_db.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/events/remote_sync_event.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file.dart';
import 'package:photos/file_repository.dart';
import 'package:photos/ui/gallery.dart';
2020-05-01 18:20:12 +00:00
import 'package:photos/ui/setup_page.dart';
2020-04-24 12:40:24 +00:00
import 'package:photo_manager/photo_manager.dart';
2020-05-17 14:35:31 +00:00
import 'package:photos/ui/share_folder_widget.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 {
final Gallery gallery;
2020-05-17 14:23:37 +00:00
final GalleryAppBarType type;
2020-04-18 18:46:38 +00:00
final String title;
2020-05-17 12:39:38 +00:00
final String path;
2020-04-18 18:46:38 +00:00
GalleryAppBarWidget(
this.gallery,
this.type,
this.title, [
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;
Set<File> _selectedFiles = Set<File>();
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;
});
});
if (widget.gallery != null)
widget.gallery.fileSelectionChangeListeners.add((selectedFiles) {
setState(() {
_selectedFiles = selectedFiles;
});
});
2020-04-30 15:09:41 +00:00
super.initState();
}
2020-04-18 18:46:38 +00:00
@override
Widget build(BuildContext context) {
if (_selectedFiles.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(_selectedFiles.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 (_selectedFiles.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, _selectedFiles.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: () async {
2020-06-19 23:03:26 +00:00
await _deleteSelected(context, false);
2020-04-18 18:46:38 +00:00
},
),
CupertinoActionSheetAction(
child: Text("Delete everywhere [WiP]"),
isDestructiveAction: true,
onPressed: () async {
2020-06-19 23:03:26 +00:00
await _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);
}
2020-06-19 23:03:26 +00:00
Future _deleteSelected(BuildContext context, bool deleteEverywhere) async {
2020-04-24 12:40:24 +00:00
await PhotoManager.editor
.deleteWithIds(_selectedFiles.map((p) => p.localId).toList());
2020-04-24 12:40:24 +00:00
for (File file in _selectedFiles) {
2020-04-18 18:46:38 +00:00
deleteEverywhere
2020-06-19 23:03:26 +00:00
? await FileDB.instance.markForDeletion(file)
: await FileDB.instance.delete(file);
2020-04-18 18:46:38 +00:00
}
Navigator.of(context, rootNavigator: true).pop();
2020-06-19 23:03:26 +00:00
FileRepository.instance.reloadFiles();
_clearSelectedFiles();
2020-04-18 18:46:38 +00:00
}
2020-06-19 23:03:26 +00:00
void _clearSelectedFiles() {
widget.gallery.clearSelection();
2020-04-18 18:46:38 +00:00
setState(() {
_selectedFiles.clear();
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
}