Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add participant role controls

AmanRajSinghMourya943b8ca3000d
diff --git a/mobile/apps/photos/lib/ui/sharing/widgets/confirm_sheet.dart b/mobile/apps/photos/lib/ui/sharing/widgets/confirm_sheet.dart
new file mode 100644
index 0000000000..a5bec188d0
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/sharing/widgets/confirm_sheet.dart
@@ -0,0 +1,50 @@
+import "dart:async";
+
+import "package:ente_components/ente_components.dart";
+import "package:flutter/material.dart";
+import "package:photos/utils/dialog_util.dart";
+
+Future<bool> showDestructiveConfirmSheet(
+ BuildContext context, {
+ required String title,
+ required String body,
+ required String actionLabel,
+ required FutureOr<void> Function() onConfirm,
+}) async {
+ final result = await showBottomSheetComponent<bool>(
+ context: context,
+ builder: (sheetContext) => BottomSheetComponent(
+ title: title,
+ content: Text(
+ body,
+ style: TextStyles.body.copyWith(
+ color: sheetContext.componentColors.textLight,
+ ),
+ ),
+ actions: [
+ ButtonComponent(
+ label: actionLabel,
+ variant: ButtonComponentVariant.critical,
+ size: ButtonComponentSize.large,
+ onTap: () async {
+ try {
+ await onConfirm();
+ } catch (error) {
+ if (sheetContext.mounted) {
+ await showGenericErrorDialog(
+ context: sheetContext,
+ error: error,
+ );
+ }
+ rethrow;
+ }
+ if (sheetContext.mounted) {
+ Navigator.of(sheetContext).pop(true);
+ }
+ },
+ ),
+ ],
+ ),
+ );
+ return result ?? false;
+}
diff --git a/mobile/apps/photos/lib/ui/sharing/widgets/participant_role_row.dart b/mobile/apps/photos/lib/ui/sharing/widgets/participant_role_row.dart
new file mode 100644
index 0000000000..4cef95f3e8
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/sharing/widgets/participant_role_row.dart
@@ -0,0 +1,169 @@
+import "package:ente_components/ente_components.dart";
+import "package:ente_strings/ente_strings.dart";
+import "package:flutter/material.dart";
+import "package:hugeicons/hugeicons.dart";
+import "package:photos/db/files_db.dart";
+import "package:photos/models/api/collection/user.dart";
+import "package:photos/models/collection/collection.dart";
+import "package:photos/services/collections_service.dart";
+import "package:photos/services/contacts/contact_identity_resolver.dart";
+import "package:photos/ui/actions/collection/collection_sharing_actions.dart";
+import "package:photos/ui/sharing/widgets/confirm_sheet.dart";
+import "package:photos/ui/sharing/widgets/participant_row.dart";
+import "package:photos/ui/sharing/widgets/sharing_role.dart";
+
+enum _ParticipantRoleAction {
+ viewer(CollectionParticipantRole.viewer),
+ collaborator(CollectionParticipantRole.collaborator),
+ admin(CollectionParticipantRole.admin),
+ remove(null);
+
+ const _ParticipantRoleAction(this.role);
+
+ final CollectionParticipantRole? role;
+}
+
+class ParticipantRoleRow extends StatefulWidget {
+ const ParticipantRoleRow({
+ super.key,
+ required this.collection,
+ required this.user,
+ required this.currentUserID,
+ required this.onCollectionChanged,
+ });
+
+ final Collection collection;
+ final User user;
+ final int currentUserID;
+ final VoidCallback onCollectionChanged;
+
+ @override
+ State<ParticipantRoleRow> createState() => _ParticipantRoleRowState();
+}
+
+class _ParticipantRoleRowState extends State<ParticipantRoleRow> {
+ bool _isChangingRole = false;
+
+ @override
+ Widget build(BuildContext context) {
+ final role = widget.collection.getRole(widget.user.id);
+ return ParticipantRow(
+ user: widget.user,
+ role: role,
+ currentUserID: widget.currentUserID,
+ trailing: _isChangingRole
+ ? SizedBox.square(
+ dimension: IconSizes.small,
+ child: CircularProgressIndicator(
+ color: context.componentColors.textLight,
+ strokeWidth: 2,
+ ),
+ )
+ : EntePopupMenuButton<_ParticipantRoleAction>(
+ optionsBuilder: () => _options(context),
+ onSelected: (action) => action == _ParticipantRoleAction.remove
+ ? _removeParticipant()
+ : _changeRole(action.role!),
+ child: HugeIcon(
+ icon: sharingRoleIcon(role),
+ color: context.componentColors.textBase,
+ size: IconSizes.small,
+ strokeWidth: 1.6,
+ ),
+ ),
+ );
+ }
+
+ List<EntePopupMenuOption<_ParticipantRoleAction>> _options(
+ BuildContext context,
+ ) {
+ final colors = context.componentColors;
+ return [
+ EntePopupMenuOption(
+ value: _ParticipantRoleAction.viewer,
+ label: context.strings.viewer,
+ leadingWidget: HugeIcon(
+ icon: sharingRoleIcon(CollectionParticipantRole.viewer),
+ size: IconSizes.small,
+ ),
+ ),
+ EntePopupMenuOption(
+ value: _ParticipantRoleAction.collaborator,
+ label: context.strings.collaborator,
+ leadingWidget: HugeIcon(
+ icon: sharingRoleIcon(CollectionParticipantRole.collaborator),
+ size: IconSizes.small,
+ ),
+ ),
+ EntePopupMenuOption(
+ value: _ParticipantRoleAction.admin,
+ label: context.strings.admin,
+ leadingWidget: HugeIcon(
+ icon: sharingRoleIcon(CollectionParticipantRole.admin),
+ size: IconSizes.small,
+ ),
+ ),
+ EntePopupMenuOption(
+ value: _ParticipantRoleAction.remove,
+ label: context.strings.remove,
+ labelColor: colors.warning,
+ leadingWidget: HugeIcon(
+ icon: HugeIcons.strokeRoundedDelete02,
+ color: colors.warning,
+ size: IconSizes.small,
+ ),
+ showDivider: false,
+ ),
+ ];
+ }
+
+ Future<void> _changeRole(CollectionParticipantRole role) async {
+ if (role == widget.collection.getRole(widget.user.id)) {
+ return;
+ }
+ setState(() => _isChangingRole = true);
+ final actions = CollectionActions(CollectionsService.instance);
+ final result = await actions.addEmailToCollection(
+ widget.collection,
+ widget.user.email,
+ role,
+ );
+ if (!mounted) {
+ return;
+ }
+ setState(() => _isChangingRole = false);
+ if (!result.succeeded) {
+ await actions.showAddEmailToCollectionFailure(context, result);
+ return;
+ }
+ widget.onCollectionChanged();
+ }
+
+ Future<void> _removeParticipant() async {
+ final count = await FilesDB.instance.collectionFileCountForOwner(
+ widget.collection.id,
+ widget.user.id,
+ );
+ if (!mounted) {
+ return;
+ }
+ final removed = await showDestructiveConfirmSheet(
+ context,
+ title: context.strings.removePersonTitle(
+ name: resolveDisplayName(widget.user),
+ ),
+ body: context.strings.removePersonBody(count: count),
+ actionLabel: context.strings.removeParticipant,
+ onConfirm: () async {
+ final sharees = await CollectionsService.instance.unshare(
+ widget.collection.id,
+ widget.user.email,
+ );
+ widget.collection.updateSharees(sharees);
+ },
+ );
+ if (removed && mounted) {
+ widget.onCollectionChanged();
+ }
+ }
+}
diff --git a/mobile/apps/photos/lib/ui/sharing/widgets/participant_row.dart b/mobile/apps/photos/lib/ui/sharing/widgets/participant_row.dart
new file mode 100644
index 0000000000..d0191a2155
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/sharing/widgets/participant_row.dart
@@ -0,0 +1,110 @@
+import "dart:math" as math;
+
+import "package:ente_components/ente_components.dart";
+import "package:ente_strings/ente_strings.dart";
+import "package:flutter/material.dart";
+import "package:hugeicons/hugeicons.dart";
+import "package:photos/models/api/collection/user.dart";
+import "package:photos/models/collection/collection.dart";
+import "package:photos/services/contacts/contact_identity_resolver.dart";
+import "package:photos/ui/sharing/share_components.dart";
+import "package:photos/ui/sharing/user_avator_widget.dart";
+import "package:photos/ui/sharing/widgets/sharing_role.dart";
+
+class ScrollableParticipantRoster extends StatelessWidget {
+ const ScrollableParticipantRoster({
+ super.key,
+ required this.rows,
+ required this.scrollController,
+ });
+
+ final List<Widget> rows;
+ final ScrollController scrollController;
+
+ @override
+ Widget build(BuildContext context) {
+ const maxVisibleRows = 3;
+ final visibleRows = math.min(rows.length, maxVisibleRows);
+ final viewportHeight = _groupHeight(context, visibleRows);
+ final showScrollbar = rows.length > maxVisibleRows;
+ final roster = ClipRRect(
+ key: const ValueKey("participant-roster-clip"),
+ borderRadius: BorderRadius.circular(Radii.button),
+ child: SingleChildScrollView(
+ key: const ValueKey("participant-roster-scroll"),
+ controller: scrollController,
+ primary: false,
+ child: ShareMenuGroup(items: rows),
+ ),
+ );
+ return SizedBox(
+ height: viewportHeight,
+ child: showScrollbar
+ ? RawScrollbar(
+ key: const ValueKey("participant-roster-scrollbar"),
+ controller: scrollController,
+ thumbVisibility: true,
+ trackVisibility: true,
+ interactive: true,
+ thickness: 5,
+ radius: const Radius.circular(3),
+ trackRadius: const Radius.circular(3),
+ thumbColor: context.componentColors.fillDarkest,
+ trackColor: context.componentColors.fillDark,
+ padding: EdgeInsets.zero,
+ child: Padding(
+ padding: const EdgeInsetsDirectional.only(end: Spacing.sm + 5),
+ child: roster,
+ ),
+ )
+ : roster,
+ );
+ }
+
+ double _groupHeight(BuildContext context, int itemCount) {
+ const textStyle = TextStyles.body;
+ final textExtent =
+ MediaQuery.textScalerOf(context).scale(textStyle.fontSize!) *
+ textStyle.height!;
+ final rowExtent = math.max(60.0, textExtent + 20.0);
+ return itemCount * rowExtent +
+ (itemCount > 1 ? Spacing.sm * 2 : 0) +
+ math.max(0, itemCount - 1) * Spacing.xs;
+ }
+}
+
+class ParticipantRow extends StatelessWidget {
+ const ParticipantRow({
+ super.key,
+ required this.user,
+ required this.role,
+ required this.currentUserID,
+ this.trailing,
+ });
+
+ final User user;
+ final CollectionParticipantRole role;
+ final int currentUserID;
+ final Widget? trailing;
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ return ShareMenuItem(
+ title: user.id == currentUserID
+ ? context.strings.you
+ : resolveDisplayName(user),
+ titleMaxLines: 1,
+ leading: UserAvatarWidget(user, type: AvatarType.medium),
+ trailing:
+ trailing ??
+ HugeIcon(
+ icon: sharingRoleIcon(role),
+ color: colors.textLightest,
+ size: IconSizes.small,
+ strokeWidth: 1.6,
+ ),
+ isDisabled: true,
+ );
+ }
+}