Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

11.6 KB
Raw Blame
import "dart:async";

import "package:ente_strings/ente_strings.dart";
import "package:ente_ui/utils/toast_util.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:flutter_svg/flutter_svg.dart";
import "package:hugeicons/hugeicons.dart";
import "package:photo_manager/photo_manager.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/events/force_reload_trash_page_event.dart";
import "package:photos/events/guest_view_event.dart";
import "package:photos/models/collection/collection.dart";
import "package:photos/models/file/extensions/file_props.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/selected_files.dart";
import 'package:photos/module/metadata/panorama.dart';
import "package:photos/service_locator.dart";
import "package:photos/services/collections_service.dart";
import "package:photos/states/detail_page_state.dart";
import "package:photos/ui/actions/collection/collection_sharing_actions.dart";
import "package:photos/ui/actions/file/file_actions.dart";
import "package:photos/ui/collections/collection_action_sheet.dart";
import "package:photos/ui/viewer/actions/suggest_delete_sheet.dart";
import "package:photos/ui/viewer/file/file_share_button.dart";
import "package:photos/utils/delete_file_util.dart";
import "package:photos/utils/dialog_util.dart";
import "package:photos/utils/share_util.dart";

class FileBottomBar extends StatefulWidget {
  final EnteFile file;
  final Function(EnteFile) onFileRemoved;
  final int? userID;
  final ValueNotifier<bool> enableFullScreenNotifier;
  final bool isLocalOnlyContext;

  const FileBottomBar(
    this.file, {
    required this.onFileRemoved,
    required this.enableFullScreenNotifier,
    this.userID,
    this.isLocalOnlyContext = false,
    super.key,
  });

  @override
  FileBottomBarState createState() => FileBottomBarState();
}

class FileBottomBarState extends State<FileBottomBar> {
  final GlobalKey shareButtonKey = GlobalKey();
  bool isGuestView = false;
  late final StreamSubscription<GuestViewEvent> _guestViewEventSubscription;
  int? lastFileGenID;

  @override
  void initState() {
    super.initState();
    _guestViewEventSubscription = Bus.instance.on<GuestViewEvent>().listen((
      event,
    ) {
      setState(() {
        isGuestView = event.isGuestView;
      });
    });
  }

  @override
  void dispose() {
    _guestViewEventSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.file.canEditMetaInfo &&
        widget.file.canBePanorama() &&
        widget.file.isPanorama() == null) {
      if (lastFileGenID != widget.file.generatedID) {
        lastFileGenID = widget.file.generatedID;
        guardedCheckPanorama(widget.file).ignore();
      }
    }

    final sharedCollectionNotifier = InheritedDetailPageState.maybeOf(
      context,
    )?.isInSharedCollectionNotifier;

    if (sharedCollectionNotifier == null) {
      return _getBottomBar();
    }

    return ValueListenableBuilder<bool>(
      valueListenable: sharedCollectionNotifier,
      builder: (context, _, _) => _getBottomBar(),
    );
  }

  Widget _getBottomBar() {
    final file = widget.file;
    final isInSharedCollection =
        InheritedDetailPageState.maybeOf(
          context,
        )?.isInSharedCollectionNotifier.value ??
        false;

    final Collection? collection = widget.file.collectionID != null
        ? CollectionsService.instance.getCollectionByID(
            widget.file.collectionID!,
          )
        : null;
    final List<Widget> children = [];
    final bool isOwnedByUser =
        widget.file.ownerID == null || widget.file.ownerID == widget.userID;
    final bool isFileHidden =
        widget.file.isOwner &&
        widget.file.isUploaded &&
        (collection?.isHidden() ?? false);
    if (widget.file.isTrash) {
      _addTrashOptions(children);
    }

    if (!widget.file.isTrash) {
      if (isOwnedByUser) {
        children.add(
          Tooltip(
            message: context.strings.delete,
            child: Padding(
              padding: const EdgeInsets.only(top: 12),
              child: IconButton(
                icon: const HugeIcon(
                  icon: HugeIcons.strokeRoundedDelete02,
                  color: Colors.white,
                ),
                onPressed: () async {
                  await _showSingleFileDeleteSheet(widget.file);
                },
              ),
            ),
          ),
        );
      }

      final bool canShowSuggestDelete =
          collection != null &&
          flagService.internalUser &&
          isInSharedCollection &&
          canSuggestDeleteForFile(file: widget.file, collection: collection);

      if (canShowSuggestDelete) {
        children.add(_buildSuggestDeleteButton(collection));
      }

      children.add(
        Padding(
          padding: const EdgeInsets.only(top: 12),
          child: FileShareButton(
            key: shareButtonKey,
            canSendLink: file.isUploaded && file.isOwner && !isFileHidden,
            onSendFile: () =>
                share(context, [file], shareButtonKey: shareButtonKey),
            onSendLink: () => _sendLink(file),
          ),
        ),
      );

      if (widget.file.isUploaded && !isFileHidden) {
        children.add(
          Tooltip(
            message: context.strings.addToAlbum,
            child: Padding(
              padding: const EdgeInsets.only(top: 12),
              child: IconButton(
                icon: const HugeIcon(
                  icon: HugeIcons.strokeRoundedAddSquare,
                  color: Colors.white,
                  size: 24,
                ),
                onPressed: () {
                  final selectedFiles = SelectedFiles();
                  selectedFiles.files.add(widget.file);
                  showCollectionActionSheet(
                    context,
                    selectedFiles: selectedFiles,
                    actionType: CollectionActionType.addFiles,
                  );
                },
              ),
            ),
          ),
        );
      }
    }
    return ValueListenableBuilder(
      valueListenable: widget.enableFullScreenNotifier,
      builder: (BuildContext context, bool isFullScreen, _) {
        return IgnorePointer(
          ignoring: isFullScreen || isGuestView,
          child: AnimatedOpacity(
            opacity: isFullScreen || isGuestView ? 0 : 1,
            duration: const Duration(milliseconds: 250),
            curve: Curves.easeInOut,
            child: Align(
              alignment: Alignment.bottomCenter,
              child: SafeArea(
                top: false,
                left: false,
                right: false,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: children,
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  Future<void> _showSingleFileDeleteSheet(EnteFile file) async {
    await showSingleFileDeleteSheet(
      context,
      file,
      onFileRemoved: widget.onFileRemoved,
      isLocalOnlyContext: widget.isLocalOnlyContext,
    );
  }

  Future<void> _sendLink(EnteFile file) async {
    if (!file.isUploaded || !file.isOwner) {
      showShortToast(
        context,
        context.strings.canOnlyCreateLinkForFilesOwnedByYou,
      );
      return;
    }
    final dialog = createProgressDialog(
      context,
      context.strings.creatingLink,
      isDismissible: true,
    );
    await dialog.show();
    if (!mounted) {
      await dialog.hide();
      return;
    }
    final Collection? sharedLinkCollection = await CollectionActions(
      CollectionsService.instance,
    ).createSharedCollectionLink(context, [file]);
    if (!mounted) {
      await dialog.hide();
      return;
    }
    if (sharedLinkCollection == null) {
      await dialog.hide();
      return;
    }
    final String url = CollectionsService.instance.getPublicUrl(
      sharedLinkCollection,
    );
    await dialog.hide();
    unawaited(Clipboard.setData(ClipboardData(text: url)));
    if (!mounted) return;
    await shareLinkWithDescription(url, context: context, key: shareButtonKey);
  }

  void _addTrashOptions(List<Widget> children) {
    children.add(
      Tooltip(
        message: context.strings.restore,
        child: Padding(
          padding: const EdgeInsets.only(top: 12),
          child: IconButton(
            icon: const Icon(CupertinoIcons.gobackward, color: Colors.white),
            onPressed: () {
              if (widget.file.isDeviceTrash) {
                _restoreFromDeviceTrash()
                    .then((didRestore) {
                      if (!didRestore || !mounted) return;
                      showToast(
                        context,
                        context.strings.restoredItems(count: 1),
                      );
                    })
                    .onError((e, s) {
                      if (!mounted) return;
                      showGenericErrorDialog(
                        context: context,
                        error: e,
                      ).ignore();
                    });
                return;
              }
              final selectedFiles = SelectedFiles();
              selectedFiles.toggleSelection(widget.file);
              showCollectionActionSheet(
                context,
                selectedFiles: selectedFiles,
                actionType: CollectionActionType.restoreFiles,
              );
            },
          ),
        ),
      ),
    );

    children.add(
      Tooltip(
        message: context.strings.delete,
        child: Padding(
          padding: const EdgeInsets.only(top: 12),
          child: IconButton(
            icon: const HugeIcon(
              icon: HugeIcons.strokeRoundedDelete02,
              color: Colors.white,
            ),
            onPressed: () async {
              if (widget.file.isDeviceTrash) {
                final deletedIDs = await permanentlyDeleteFromDeviceTrash(
                  context,
                  [widget.file.localID!],
                );
                if (deletedIDs.isNotEmpty) {
                  await widget.onFileRemoved(widget.file);
                }
                return;
              }
              if (await deleteFromEnteTrash(context, [
                    widget.file.asEnteTrashFile!,
                  ]) ==
                  true) {
                widget.onFileRemoved(widget.file);
              }
            },
          ),
        ),
      ),
    );
  }

  Widget _buildSuggestDeleteButton(Collection collection) {
    return Tooltip(
      message: context.strings.suggestDeletion,
      child: Padding(
        padding: const EdgeInsets.only(top: 12),
        child: IconButton(
          icon: SvgPicture.asset(
            "assets/icons/delete-01.svg",
            width: 24,
            height: 24,
          ),
          onPressed: () => _onSuggestDelete(collection),
        ),
      ),
    );
  }

  Future<void> _onSuggestDelete(Collection collection) async {
    if (widget.file.uploadedFileID == null) {
      return;
    }
    await showSuggestDeleteSheet(
      context: context,
      onConfirm: () async {
        await CollectionsService.instance.suggestDeleteFromCollection(
          collection.id,
          [widget.file],
        );
        widget.onFileRemoved(widget.file);
      },
    );
  }

  Future<bool> _restoreFromDeviceTrash() async {
    final restoredIDs = await PhotoManager.editor.android.restoreFromTrash([
      widget.file.asDeviceTrashFile!.toAssetEntity(),
    ]);
    if (restoredIDs.isEmpty) return false;
    Bus.instance.fire(ForceReloadTrashPageEvent());
    await widget.onFileRemoved(widget.file);
    return true;
  }
}