Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] add cast page to settings

Priyanshu Dangaref38ad84fcaca
diff --git a/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart b/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart
index 7a7e6dd5c9..a0fa108ec5 100644
--- a/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart
+++ b/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart
@@ -1,5 +1,28 @@
import "package:dio/dio.dart";
+class CastInfo {
+ final int collectionID;
+ final String deviceIP;
+ final String deviceCode;
+ final DateTime lastUsedAt;
+
+ CastInfo({
+ required this.collectionID,
+ required this.deviceIP,
+ required this.deviceCode,
+ required this.lastUsedAt,
+ });
+
+ factory CastInfo.fromJson(dynamic json) {
+ return CastInfo(
+ collectionID: json["collectionID"],
+ deviceIP: json["deviceIP"],
+ deviceCode: json["deviceCode"],
+ lastUsedAt: DateTime.fromMillisecondsSinceEpoch(json["lastUsedAt"]),
+ );
+ }
+}
+
class CastGateway {
final Dio _enteDio;
@@ -23,6 +46,12 @@ class CastGateway {
}
}
+ Future<List<CastInfo>> getAllCastSessions() async {
+ final response = await _enteDio.get("/cast/device-info");
+ final devices = response.data['devices'] as List<dynamic>;
+ return devices.map((session) => CastInfo.fromJson(session)).toList();
+ }
+
Future<void> publishCastPayload(
String code,
String castPayload,
@@ -47,6 +76,12 @@ class CastGateway {
// swallow error
}
}
+
+ Future<void> revokeSession(CastInfo session) async {
+ await _enteDio.delete(
+ "/cast/device-info/${session.deviceCode}?collectionID=${session.collectionID}",
+ );
+ }
}
class CastIPMismatchException implements Exception {
diff --git a/mobile/apps/photos/lib/l10n/intl_en.arb b/mobile/apps/photos/lib/l10n/intl_en.arb
index 5e027319ee..a89ba6da7a 100644
--- a/mobile/apps/photos/lib/l10n/intl_en.arb
+++ b/mobile/apps/photos/lib/l10n/intl_en.arb
@@ -8,6 +8,7 @@
"accountSetupIncompleteCreateAccount": "Account setup incomplete. Create account to finish setup.",
"email": "Email",
"cancel": "Cancel",
+ "cast": "Cast",
"verify": "Verify",
"invalidEmailAddress": "Invalid email address",
"validEmailAddress": "Valid email address",
diff --git a/mobile/apps/photos/lib/ui/settings/cast/cast_settings_page.dart b/mobile/apps/photos/lib/ui/settings/cast/cast_settings_page.dart
new file mode 100644
index 0000000000..9cfd1004a9
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/settings/cast/cast_settings_page.dart
@@ -0,0 +1,114 @@
+import "package:ente_components/ente_components.dart";
+import "package:flutter/material.dart";
+import "package:hugeicons/hugeicons.dart";
+import "package:photos/core/network/network.dart";
+import "package:photos/gateways/cast/cast_gateway.dart";
+import "package:photos/generated/l10n.dart";
+import "package:photos/services/collections_service.dart";
+import "package:photos/ui/settings/components/settings_item.dart";
+import "package:photos/ui/settings/components/settings_page_scaffold.dart";
+import "package:photos/utils/dialog_util.dart";
+import 'package:timeago/timeago.dart' as timeago;
+
+class CastSettingsPage extends StatefulWidget {
+ const CastSettingsPage({super.key});
+
+ @override
+ State<CastSettingsPage> createState() => _CastSettingsPageState();
+}
+
+class _CastSettingsPageState extends State<CastSettingsPage> {
+ Future<List<CastInfo>>? _castSessionsFuture;
+
+ @override
+ void initState() {
+ super.initState();
+ _castSessionsFuture = _load();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final l10n = AppLocalizations.of(context);
+ final gw = CastGateway(NetworkClient.instance.enteDio);
+
+ return SettingsPageScaffold(
+ title: l10n.cast,
+ children: [
+ FutureBuilder(
+ future: _castSessionsFuture,
+ builder: (context, snapshot) {
+ if (snapshot.connectionState == ConnectionState.waiting) {
+ return const Center(child: CircularProgressIndicator());
+ }
+ if (snapshot.hasError) {
+ throw snapshot.error!;
+ }
+
+ if (!snapshot.hasData || snapshot.data == null) {
+ throw Exception(
+ "There is no data returned by getAllCastSessions",
+ );
+ }
+
+ return Column(
+ children: snapshot.data!.map((session) {
+ final collection = CollectionsService.instance
+ .getCollectionByID(session.collectionID);
+ final title =
+ collection?.displayName ?? session.collectionID.toString();
+ return SettingsItem(
+ title: "$title on ${session.deviceIP}",
+ subtitle: timeago.format(session.lastUsedAt),
+ icon: HugeIcons.strokeRoundedTv01,
+ trailing: IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedCancel01,
+ color: context.componentColors.warning,
+ size: IconSizes.small,
+ strokeWidth: 1.6,
+ ),
+ onTap: () async {
+ await _revokeSession(gw, session, title);
+ },
+ shouldShowSuccessConfirmation: false,
+ ),
+ );
+ }).toList(),
+ );
+ },
+ ),
+ ],
+ );
+ }
+
+ Future<void> _revokeSession(
+ CastGateway gw,
+ CastInfo session,
+ String collectionTitle,
+ ) async {
+ final l10n = AppLocalizations.of(context);
+ final result = await showChoiceDialog(
+ context,
+ title: l10n.stopCastingTitle,
+ body: l10n.stopCastingBody,
+ firstButtonLabel: l10n.yes,
+ secondButtonLabel: l10n.no,
+ );
+ if (result?.action != .first) {
+ return;
+ }
+ await gw.revokeSession(session);
+ await _refresh();
+ }
+
+ Future<List<CastInfo>> _load() {
+ final gw = CastGateway(NetworkClient.instance.enteDio);
+ return gw.getAllCastSessions();
+ }
+
+ Future<void> _refresh() async {
+ setState(() {
+ _castSessionsFuture = _load();
+ });
+ }
+}
diff --git a/mobile/apps/photos/lib/ui/settings_page.dart b/mobile/apps/photos/lib/ui/settings_page.dart
index e6f013183c..5dee9d0203 100644
--- a/mobile/apps/photos/lib/ui/settings_page.dart
+++ b/mobile/apps/photos/lib/ui/settings_page.dart
@@ -23,6 +23,7 @@ import "package:photos/ui/settings/app_version_widget.dart";
import "package:photos/ui/settings/appearance/appearance_settings_page.dart";
import "package:photos/ui/settings/backup/backup_settings_page.dart";
import "package:photos/ui/settings/backup/free_space_options.dart";
+import "package:photos/ui/settings/cast/cast_settings_page.dart";
import "package:photos/ui/settings/components/settings_item.dart";
import "package:photos/ui/settings/components/settings_page_scaffold.dart";
import "package:photos/ui/settings/debug/debug_settings_page.dart";
@@ -121,6 +122,14 @@ class _SettingsBody extends StatelessWidget {
},
),
const SizedBox(height: 8),
+ _buildMenuItem(
+ title: AppLocalizations.of(context).cast,
+ icon: HugeIcons.strokeRoundedTv02,
+ onTap: () async {
+ await routeToPage(context, const CastSettingsPage());
+ },
+ ),
+ const SizedBox(height: 8),
],
// Privacy and personalization section
_buildMenuItem(
diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/gallery_app_bar_widget.dart b/mobile/apps/photos/lib/ui/viewer/gallery/gallery_app_bar_widget.dart
index 92ae32df2d..54637d6718 100644
--- a/mobile/apps/photos/lib/ui/viewer/gallery/gallery_app_bar_widget.dart
+++ b/mobile/apps/photos/lib/ui/viewer/gallery/gallery_app_bar_widget.dart
@@ -1144,23 +1144,7 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
Future<void> _castChoiceDialog() async {
final gw = CastGateway(NetworkClient.instance.enteDio);
- if (castService.getActiveSessions().isNotEmpty) {
- await showChoiceDialog(
- context,
- title: AppLocalizations.of(context).stopCastingTitle,
- firstButtonLabel: AppLocalizations.of(context).yes,
- secondButtonLabel: AppLocalizations.of(context).no,
- body: AppLocalizations.of(context).stopCastingBody,
- firstButtonOnTap: () async {
- gw.revokeAllTokens().ignore();
- await castService.closeActiveCasts();
- },
- );
- return;
- }
- // stop any existing cast session
- gw.revokeAllTokens().ignore();
if (!Platform.isAndroid && !kDebugMode) {
await _pairWithPin(gw, '');
} else {
diff --git a/mobile/apps/photos/pubspec.lock b/mobile/apps/photos/pubspec.lock
index 382a377782..1bfd640673 100644
--- a/mobile/apps/photos/pubspec.lock
+++ b/mobile/apps/photos/pubspec.lock
@@ -2803,6 +2803,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.11"
+ timeago:
+ dependency: "direct main"
+ description:
+ name: timeago
+ sha256: b05159406a97e1cbb2b9ee4faa9fb096fe0e2dfcd8b08fcd2a00553450d3422e
+ url: "https://pub.dev"
+ source: hosted
+ version: "3.7.1"
timezone:
dependency: "direct main"
description:
diff --git a/mobile/apps/photos/pubspec.yaml b/mobile/apps/photos/pubspec.yaml
index 6f7ea97b3e..7e17ac9e7c 100644
--- a/mobile/apps/photos/pubspec.yaml
+++ b/mobile/apps/photos/pubspec.yaml
@@ -228,6 +228,7 @@ dependencies:
synchronized: 3.3.1
system_info_plus: 0.0.6
thermal: 1.1.11
+ timeago: ^3.7.1
timezone: 0.10.1
tuple: 2.0.2
ua_client_hints: 1.4.1
diff --git a/server/ente/cast/entity.go b/server/ente/cast/entity.go
index a54d109fcc..c76c3dfcca 100644
--- a/server/ente/cast/entity.go
+++ b/server/ente/cast/entity.go
@@ -16,3 +16,10 @@ type AuthContext struct {
CollectionID int64
UserID int64
}
+
+type CastInfo struct {
+ DeviceCode string `json:"deviceCode"`
+ CollectionID int64 `json:"collectionID"`
+ DeviceIP string `json:"deviceIP"`
+ LastUsedAt int64 `json:"lastUsedAt"`
+}
diff --git a/server/pkg/controller/cast/controller.go b/server/pkg/controller/cast/controller.go
index 6d1a9fa10e..7ad59f19c8 100644
--- a/server/pkg/controller/cast/controller.go
+++ b/server/pkg/controller/cast/controller.go
@@ -31,7 +31,7 @@ func (c *Controller) RegisterDevice(ctx *gin.Context, request *cast.RegisterDevi
return c.CastRepo.AddCode(ctx, request.PublicKey, network.GetClientIP(ctx))
}
-func (c *Controller) GetAllDevices(ctx *gin.Context, userID int64) ([]cast.CastRequest, error) {
+func (c *Controller) GetAllDevices(ctx *gin.Context, userID int64) ([]cast.CastInfo, error) {
return c.CastRepo.GetAllDevices(ctx, userID)
}
diff --git a/server/pkg/repo/cast/repo.go b/server/pkg/repo/cast/repo.go
index 103789046e..cbf5cafc3e 100644
--- a/server/pkg/repo/cast/repo.go
+++ b/server/pkg/repo/cast/repo.go
@@ -30,17 +30,16 @@ func (r *Repository) AddCode(ctx context.Context, pubKey string, ip string) (str
return codeValue, nil
}
-func (r *Repository) GetAllDevices(ctx context.Context, userID int64) ([]cast.CastRequest, error) {
- rows, err := r.DB.QueryContext(ctx, "SELECT collection_id, token, encrypted_payload, code FROM casting WHERE cast_user = $1 and is_deleted=false", userID)
+func (r *Repository) GetAllDevices(ctx context.Context, userID int64) ([]cast.CastInfo, error) {
+ rows, err := r.DB.QueryContext(ctx, "SELECT code, collection_id, ip, last_used_at FROM casting WHERE cast_user = $1 and is_deleted=false", userID)
if err != nil {
return nil, stacktrace.Propagate(err, "failed to query devices")
}
defer rows.Close()
-
- var devices []cast.CastRequest
+ devices := make([]cast.CastInfo, 0)
for rows.Next() {
- var device cast.CastRequest
- if err := rows.Scan(&device.CollectionID, &device.CastToken, &device.EncPayload, &device.DeviceCode); err != nil {
+ var device cast.CastInfo
+ if err := rows.Scan(&device.DeviceCode, &device.CollectionID, &device.DeviceIP, &device.LastUsedAt); err != nil {
return nil, stacktrace.Propagate(err, "failed to scan device row")
}
devices = append(devices, device)