mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
handle more interactions like uploading and single asset links
This commit is contained in:
parent
c8c6f86518
commit
cc9ec29c83
10 changed files with 335 additions and 181 deletions
218
mobile/lib/presentation/pages/remote_shared_link.dart
Normal file
218
mobile/lib/presentation/pages/remote_shared_link.dart
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:immich_mobile/domain/models/album/shared_album.model.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/domain/models/timeline.model.dart';
|
||||
import 'package:immich_mobile/domain/services/remote_shared_album.service.dart';
|
||||
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
||||
import 'package:immich_mobile/extensions/translate_extensions.dart';
|
||||
import 'package:immich_mobile/models/shared_link/shared_link.model.dart';
|
||||
import 'package:immich_mobile/presentation/widgets/timeline/timeline.widget.dart';
|
||||
import 'package:immich_mobile/providers/api.provider.dart';
|
||||
import 'package:immich_mobile/providers/infrastructure/timeline.provider.dart';
|
||||
import 'package:immich_mobile/repositories/asset_api.repository.dart';
|
||||
import 'package:immich_mobile/repositories/drift_album_api_repository.dart';
|
||||
import 'package:immich_mobile/routing/router.dart';
|
||||
import 'package:immich_mobile/services/api.service.dart';
|
||||
import 'package:immich_mobile/services/shared_link.service.dart';
|
||||
import 'package:immich_mobile/utils/image_url_builder.dart';
|
||||
import 'package:immich_mobile/widgets/common/immich_toast.dart';
|
||||
import 'package:immich_mobile/widgets/common/shared_link_password_dialog.dart';
|
||||
// ignore: import_rule_openapi
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
@RoutePage()
|
||||
class RemoteSharedLinkPage extends ConsumerStatefulWidget {
|
||||
final String shareKey;
|
||||
final String endpoint;
|
||||
|
||||
const RemoteSharedLinkPage({super.key, required this.shareKey, required this.endpoint});
|
||||
|
||||
@override
|
||||
ConsumerState<RemoteSharedLinkPage> createState() => _RemoteSharedLinkPageState();
|
||||
}
|
||||
|
||||
class _RemoteSharedLinkPageState extends ConsumerState<RemoteSharedLinkPage> {
|
||||
late final ApiService _apiService;
|
||||
|
||||
SharedLink? sharedLink;
|
||||
List<RemoteAsset>? assets;
|
||||
SharedRemoteAlbum? sharedAlbum;
|
||||
|
||||
late final RemoteSharedAlbumService sharedAlbumService;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
String endpoint = widget.endpoint;
|
||||
if (!endpoint.endsWith('/api')) {
|
||||
endpoint += '/api';
|
||||
}
|
||||
ImageUrlBuilder.setHost(endpoint);
|
||||
ImageUrlBuilder.setParameter('key', widget.shareKey);
|
||||
_apiService = ApiService.shared(endpoint, widget.shareKey);
|
||||
|
||||
final assetApiRepository = AssetApiRepository(
|
||||
_apiService.assetsApi,
|
||||
_apiService.searchApi,
|
||||
_apiService.stacksApi,
|
||||
_apiService.trashApi,
|
||||
);
|
||||
final driftApiRepository = DriftAlbumApiRepository(_apiService.albumsApi);
|
||||
sharedAlbumService = RemoteSharedAlbumService(driftApiRepository, assetApiRepository);
|
||||
|
||||
retrieveSharedLink();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
ImageUrlBuilder.clear();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> retrieveSharedLink() async {
|
||||
try {
|
||||
sharedLink = await SharedLinkService(_apiService).getMySharedLink();
|
||||
} on ApiException catch (error, _) {
|
||||
if (error.code == 401 && error.message != null && error.message!.contains("Invalid password")) {
|
||||
final password = await showDialog<String>(
|
||||
context: context,
|
||||
builder: (context) => const SharedLinkPasswordDialog(),
|
||||
);
|
||||
|
||||
if (password == null) {
|
||||
context.pop();
|
||||
}
|
||||
|
||||
try {
|
||||
sharedLink = await SharedLinkService(_apiService).getMySharedLink(password: password);
|
||||
} catch (e) {
|
||||
ImmichToast.show(
|
||||
context: context,
|
||||
msg: "errors.shared_link_invalid_password".t(context: context),
|
||||
toastType: ToastType.error,
|
||||
);
|
||||
|
||||
context.pop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sharedLink == null) {
|
||||
ImmichToast.show(
|
||||
context: context,
|
||||
msg: "errors.unable_to_get_shared_link".t(context: context),
|
||||
toastType: ToastType.error,
|
||||
);
|
||||
context.pop();
|
||||
return;
|
||||
}
|
||||
|
||||
_refreshAssets();
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
Future<List<RemoteAsset>> retrieveSharedAlbumAssets() async {
|
||||
try {
|
||||
sharedAlbum = await sharedAlbumService.getSharedAlbum(sharedLink!.albumId!);
|
||||
|
||||
return sharedAlbum?.assets ?? [];
|
||||
} catch (e) {
|
||||
ImmichToast.show(
|
||||
context: context,
|
||||
msg: "errors.failed_to_load_assets".t(context: context),
|
||||
toastType: ToastType.error,
|
||||
);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _refreshAssets() async {
|
||||
// Retrieve assets from the shared link
|
||||
switch (sharedLink!.type) {
|
||||
case SharedLinkSource.album:
|
||||
assets = await retrieveSharedAlbumAssets();
|
||||
break;
|
||||
case SharedLinkSource.individual:
|
||||
assets = sharedLink!.assets;
|
||||
|
||||
if (!(sharedLink!.allowUpload)) {
|
||||
context.replaceRoute(
|
||||
AssetViewerRoute(
|
||||
initialIndex: 0,
|
||||
timelineService: ref.read(timelineFactoryProvider).fromAssets(sharedLink!.assets),
|
||||
),
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Future<void> addAssets() async {
|
||||
final List<XFile> uploadAssets = await ImagePicker().pickMultipleMedia();
|
||||
|
||||
if (uploadAssets.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text("uploading".t(context: context)),
|
||||
content: const SizedBox(height: 48, child: Center(child: CircularProgressIndicator())),
|
||||
),
|
||||
);
|
||||
|
||||
await sharedAlbumService.uploadAssets(sharedAlbum!.id, uploadAssets);
|
||||
sharedAlbum = await sharedAlbumService.getSharedAlbum(sharedAlbum!.id);
|
||||
|
||||
_refreshAssets();
|
||||
|
||||
// close the dialog
|
||||
context.pop();
|
||||
}
|
||||
|
||||
if (assets == null) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return ProviderScope(
|
||||
key: ValueKey(assets?.length),
|
||||
overrides: [
|
||||
apiServiceProvider.overrideWith((ref) => _apiService),
|
||||
timelineServiceProvider.overrideWith((ref) {
|
||||
final timelineService = ref.watch(timelineFactoryProvider).fromAssets(assets!);
|
||||
ref.onDispose(timelineService.dispose);
|
||||
return timelineService;
|
||||
}),
|
||||
],
|
||||
child: Timeline(
|
||||
topSliverWidgetHeight: 0,
|
||||
topSliverWidget: const SliverToBoxAdapter(child: SizedBox.shrink()),
|
||||
groupBy: GroupAssetsBy.none,
|
||||
bottomSheet: null,
|
||||
appBar: SliverToBoxAdapter(
|
||||
child: AppBar(
|
||||
title: Text(sharedAlbum?.name ?? "shared_link".t(context: context)),
|
||||
actions: [
|
||||
if (sharedLink!.allowUpload)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.cloud_upload),
|
||||
onPressed: () => addAssets(),
|
||||
tooltip: "shared_link_upload".t(context: context),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,176 +0,0 @@
|
|||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/domain/services/remote_shared_album.service.dart';
|
||||
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
||||
import 'package:immich_mobile/extensions/translate_extensions.dart';
|
||||
import 'package:immich_mobile/models/shared_link/shared_link.model.dart';
|
||||
import 'package:immich_mobile/presentation/widgets/timeline/timeline.widget.dart';
|
||||
import 'package:immich_mobile/providers/api.provider.dart';
|
||||
import 'package:immich_mobile/providers/infrastructure/timeline.provider.dart';
|
||||
import 'package:immich_mobile/providers/user.provider.dart';
|
||||
import 'package:immich_mobile/repositories/drift_album_api_repository.dart';
|
||||
import 'package:immich_mobile/services/api.service.dart';
|
||||
import 'package:immich_mobile/services/shared_link.service.dart';
|
||||
import 'package:immich_mobile/utils/image_url_builder.dart';
|
||||
import 'package:immich_mobile/widgets/common/immich_toast.dart';
|
||||
// ignore: import_rule_openapi
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
@RoutePage()
|
||||
class RemoteSharedLinkPage extends ConsumerStatefulWidget {
|
||||
final String shareKey;
|
||||
final String endpoint;
|
||||
|
||||
const RemoteSharedLinkPage({super.key, required this.shareKey, required this.endpoint});
|
||||
|
||||
@override
|
||||
ConsumerState<RemoteSharedLinkPage> createState() => _RemoteSharedLinkPageState();
|
||||
}
|
||||
|
||||
class _RemoteSharedLinkPageState extends ConsumerState<RemoteSharedLinkPage> {
|
||||
late final ApiService _apiService;
|
||||
|
||||
SharedLink? sharedLink;
|
||||
List<RemoteAsset>? assets;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
String endpoint = widget.endpoint;
|
||||
if (!endpoint.endsWith('/api')) {
|
||||
endpoint += '/api';
|
||||
}
|
||||
ImageUrlBuilder.setHost(endpoint);
|
||||
ImageUrlBuilder.setParameter('key', widget.shareKey);
|
||||
_apiService = ApiService.shared(endpoint, widget.shareKey);
|
||||
|
||||
retrieveSharedLink();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
ImageUrlBuilder.clear();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> retrieveSharedLink() async {
|
||||
try {
|
||||
sharedLink = await SharedLinkService(_apiService).getMySharedLink();
|
||||
} on ApiException catch (error, _) {
|
||||
if (error.code == 401) {
|
||||
// We need a password from user.
|
||||
// TODO: make password input and try to auth again
|
||||
}
|
||||
}
|
||||
|
||||
if (sharedLink == null) {
|
||||
ImmichToast.show(
|
||||
context: context,
|
||||
msg: "shared_link_not_found".t(context: context),
|
||||
toastType: ToastType.error,
|
||||
);
|
||||
context.pop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Retrieve assets from the shared link
|
||||
switch (sharedLink!.type) {
|
||||
case SharedLinkSource.album:
|
||||
assets = await retrieveSharedAlbumAssets();
|
||||
break;
|
||||
case SharedLinkSource.individual:
|
||||
assets = sharedLink!.assets;
|
||||
break;
|
||||
}
|
||||
|
||||
// if (assets!.isEmpty) {
|
||||
// context.pop();
|
||||
// }
|
||||
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
Future<List<RemoteAsset>> retrieveSharedAlbumAssets() async {
|
||||
try {
|
||||
final driftApiRepository = DriftAlbumApiRepository(_apiService.albumsApi);
|
||||
final albumService = RemoteSharedAlbumService(driftApiRepository);
|
||||
final sharedAlbum = await albumService.getSharedAlbum(sharedLink!.albumId!);
|
||||
|
||||
return sharedAlbum?.assets ?? [];
|
||||
} catch (e) {
|
||||
ImmichToast.show(
|
||||
context: context,
|
||||
msg: "failed_to_retrieve_assets".t(context: context),
|
||||
toastType: ToastType.error,
|
||||
);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addAssets(BuildContext context) async {
|
||||
// TODO add upload assets to shared album
|
||||
}
|
||||
|
||||
void showOptionSheet(BuildContext context) {
|
||||
final user = ref.watch(currentUserProvider);
|
||||
|
||||
// showModalBottomSheet(
|
||||
// context: context,
|
||||
// backgroundColor: context.colorScheme.surface,
|
||||
// isScrollControlled: false,
|
||||
// builder: (context) {
|
||||
// return DriftRemoteAlbumOption(
|
||||
// onDeleteAlbum: isOwner
|
||||
// ? () async {
|
||||
// await deleteAlbum(context);
|
||||
// if (context.mounted) {
|
||||
// context.pop();
|
||||
// }
|
||||
// }
|
||||
// : null,
|
||||
// onAddUsers: isOwner
|
||||
// ? () async {
|
||||
// await addUsers(context);
|
||||
// context.pop();
|
||||
// }
|
||||
// : null,
|
||||
// onAddPhotos: () async {
|
||||
// await addAssets(context);
|
||||
// context.pop();
|
||||
// },
|
||||
// onToggleAlbumOrder: () async {
|
||||
// await toggleAlbumOrder();
|
||||
// context.pop();
|
||||
// },
|
||||
// onEditAlbum: () async {
|
||||
// context.pop();
|
||||
// await showEditTitleAndDescription(context);
|
||||
// },
|
||||
// );
|
||||
// },
|
||||
// );
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (assets == null || assets!.isEmpty) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
return ProviderScope(
|
||||
overrides: [
|
||||
apiServiceProvider.overrideWith((ref) => _apiService),
|
||||
timelineServiceProvider.overrideWith((ref) {
|
||||
print(assets!.length);
|
||||
final timelineService = ref.watch(timelineFactoryProvider).fromAssets(assets!);
|
||||
ref.onDispose(timelineService.dispose);
|
||||
return timelineService;
|
||||
}),
|
||||
],
|
||||
child: Timeline(appBar: SliverToBoxAdapter(child: AppBar())),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue