2022-11-30 18:58:07 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-11-06 03:21:55 +02:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2023-01-25 18:10:04 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-11-06 03:21:55 +02:00
|
|
|
import 'package:immich_mobile/modules/album/providers/album.provider.dart';
|
2023-08-02 03:29:14 +02:00
|
|
|
import 'package:immich_mobile/modules/album/providers/album_detail.provider.dart';
|
2023-01-27 23:05:08 +02:00
|
|
|
import 'package:immich_mobile/modules/album/providers/shared_album.provider.dart';
|
2022-11-06 03:21:55 +02:00
|
|
|
import 'package:immich_mobile/modules/album/services/album.service.dart';
|
2023-08-06 04:40:50 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/providers/manual_upload.provider.dart';
|
2022-10-14 22:37:15 +02:00
|
|
|
import 'package:immich_mobile/modules/home/providers/multiselect.provider.dart';
|
2022-10-01 10:33:06 +02:00
|
|
|
import 'package:immich_mobile/modules/home/ui/asset_grid/immich_asset_grid.dart';
|
2022-02-09 20:41:02 +02:00
|
|
|
import 'package:immich_mobile/modules/home/ui/control_bottom_app_bar.dart';
|
2023-01-24 20:23:06 +02:00
|
|
|
import 'package:immich_mobile/modules/home/ui/home_page_app_bar.dart';
|
2023-06-27 23:00:20 +02:00
|
|
|
import 'package:immich_mobile/modules/memories/ui/memory_lane.dart';
|
2022-08-13 22:51:09 +02:00
|
|
|
import 'package:immich_mobile/modules/home/ui/profile_drawer/profile_drawer.dart';
|
2022-11-06 03:21:55 +02:00
|
|
|
import 'package:immich_mobile/routing/router.dart';
|
2023-02-06 09:13:32 +02:00
|
|
|
import 'package:immich_mobile/shared/models/album.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2022-04-24 04:08:45 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/asset.provider.dart';
|
2022-03-22 08:22:04 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/server_info.provider.dart';
|
2023-05-25 05:52:43 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/user.provider.dart';
|
2022-02-14 18:40:41 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/websocket.provider.dart';
|
2022-11-30 18:58:07 +02:00
|
|
|
import 'package:immich_mobile/shared/ui/immich_loading_indicator.dart';
|
2022-11-06 03:21:55 +02:00
|
|
|
import 'package:immich_mobile/shared/ui/immich_toast.dart';
|
2023-08-27 07:07:35 +02:00
|
|
|
import 'package:immich_mobile/utils/selection_handlers.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
class HomePage extends HookConsumerWidget {
|
|
|
|
const HomePage({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-10-14 22:37:15 +02:00
|
|
|
final multiselectEnabled = ref.watch(multiselectProvider.notifier);
|
2022-11-06 03:21:55 +02:00
|
|
|
final selectionEnabledHook = useState(false);
|
2023-08-06 04:40:50 +02:00
|
|
|
final selectionAssetState = useState(AssetState.remote);
|
2022-11-06 03:21:55 +02:00
|
|
|
|
2022-11-08 19:00:24 +02:00
|
|
|
final selection = useState(<Asset>{});
|
2023-03-04 00:38:30 +02:00
|
|
|
final albums = ref.watch(albumProvider).where((a) => a.isRemote).toList();
|
2023-01-27 23:05:08 +02:00
|
|
|
final sharedAlbums = ref.watch(sharedAlbumProvider);
|
2022-11-06 03:21:55 +02:00
|
|
|
final albumService = ref.watch(albumServiceProvider);
|
2023-05-25 05:52:43 +02:00
|
|
|
final currentUser = ref.watch(currentUserProvider);
|
2022-02-04 06:08:22 +02:00
|
|
|
|
2022-11-30 18:58:07 +02:00
|
|
|
final tipOneOpacity = useState(0.0);
|
2023-03-27 04:35:52 +02:00
|
|
|
final refreshCount = useState(0);
|
2023-05-17 19:36:02 +02:00
|
|
|
final processing = useState(false);
|
2022-11-30 18:58:07 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
useEffect(
|
|
|
|
() {
|
2023-06-10 20:13:59 +02:00
|
|
|
ref.read(websocketProvider.notifier).connect();
|
|
|
|
Future(() => ref.read(assetProvider.notifier).getAllAsset());
|
|
|
|
ref.read(albumProvider.notifier).getAllAlbums();
|
|
|
|
ref.read(sharedAlbumProvider.notifier).getAllSharedAlbums();
|
2023-09-28 05:26:48 +02:00
|
|
|
ref.read(serverInfoProvider.notifier).getServerInfo();
|
2022-11-06 03:21:55 +02:00
|
|
|
|
|
|
|
selectionEnabledHook.addListener(() {
|
|
|
|
multiselectEnabled.state = selectionEnabledHook.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
return () {
|
2023-01-25 18:10:04 +02:00
|
|
|
// This does not work in tests
|
|
|
|
if (kReleaseMode) {
|
|
|
|
selectionEnabledHook.dispose();
|
|
|
|
}
|
2022-11-06 03:21:55 +02:00
|
|
|
};
|
2022-07-13 14:23:48 +02:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
2022-02-04 05:01:14 +02:00
|
|
|
|
2022-04-03 19:31:45 +02:00
|
|
|
void reloadAllAsset() {
|
2023-02-21 17:25:31 +02:00
|
|
|
ref.watch(assetProvider.notifier).getAllAsset();
|
2022-04-03 19:31:45 +02:00
|
|
|
}
|
|
|
|
|
2022-10-01 10:33:06 +02:00
|
|
|
Widget buildBody() {
|
2022-10-06 22:41:56 +02:00
|
|
|
void selectionListener(
|
2022-10-14 22:37:15 +02:00
|
|
|
bool multiselect,
|
2022-11-08 19:00:24 +02:00
|
|
|
Set<Asset> selectedAssets,
|
2022-10-14 22:37:15 +02:00
|
|
|
) {
|
2022-11-06 03:21:55 +02:00
|
|
|
selectionEnabledHook.value = multiselect;
|
2022-10-06 22:41:56 +02:00
|
|
|
selection.value = selectedAssets;
|
2023-08-06 04:40:50 +02:00
|
|
|
selectionAssetState.value = selectedAssets.any((e) => e.isRemote)
|
|
|
|
? AssetState.remote
|
|
|
|
: AssetState.local;
|
2022-04-30 17:55:27 +02:00
|
|
|
}
|
|
|
|
|
2022-10-06 22:41:56 +02:00
|
|
|
void onShareAssets() {
|
2023-08-27 07:07:35 +02:00
|
|
|
handleShareAssets(ref, context, selection.value.toList());
|
2023-04-03 23:43:46 +02:00
|
|
|
|
2022-11-06 03:21:55 +02:00
|
|
|
selectionEnabledHook.value = false;
|
2022-10-06 22:41:56 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
List<Asset> remoteOnlySelection({String? localErrorMessage}) {
|
2022-11-08 19:00:24 +02:00
|
|
|
final Set<Asset> assets = selection.value;
|
|
|
|
final bool onlyRemote = assets.every((e) => e.isRemote);
|
|
|
|
if (!onlyRemote) {
|
2023-02-06 14:59:56 +02:00
|
|
|
if (localErrorMessage != null && localErrorMessage.isNotEmpty) {
|
|
|
|
ImmichToast.show(
|
|
|
|
context: context,
|
|
|
|
msg: localErrorMessage,
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
}
|
2023-05-17 19:36:02 +02:00
|
|
|
return assets.where((a) => a.isRemote).toList();
|
2022-11-08 19:00:24 +02:00
|
|
|
}
|
2023-05-17 19:36:02 +02:00
|
|
|
return assets.toList();
|
2022-11-08 19:00:24 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
void onFavoriteAssets() async {
|
|
|
|
processing.value = true;
|
|
|
|
try {
|
|
|
|
final remoteAssets = remoteOnlySelection(
|
|
|
|
localErrorMessage: 'home_page_favorite_err_local'.tr(),
|
2023-02-10 08:05:39 +02:00
|
|
|
);
|
2023-05-17 19:36:02 +02:00
|
|
|
if (remoteAssets.isNotEmpty) {
|
2023-08-27 07:07:35 +02:00
|
|
|
await handleFavoriteAssets(ref, context, remoteAssets);
|
2023-05-17 19:36:02 +02:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
processing.value = false;
|
|
|
|
selectionEnabledHook.value = false;
|
|
|
|
}
|
2023-02-06 14:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
void onArchiveAsset() async {
|
|
|
|
processing.value = true;
|
|
|
|
try {
|
|
|
|
final remoteAssets = remoteOnlySelection(
|
|
|
|
localErrorMessage: 'home_page_archive_err_local'.tr(),
|
2023-04-17 07:02:07 +02:00
|
|
|
);
|
2023-08-27 07:07:35 +02:00
|
|
|
await handleArchiveAssets(ref, context, remoteAssets);
|
2023-05-17 19:36:02 +02:00
|
|
|
} finally {
|
|
|
|
processing.value = false;
|
|
|
|
selectionEnabledHook.value = false;
|
|
|
|
}
|
2023-04-17 07:02:07 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
void onDelete() async {
|
|
|
|
processing.value = true;
|
|
|
|
try {
|
2023-06-10 20:13:59 +02:00
|
|
|
await ref.read(assetProvider.notifier).deleteAssets(selection.value);
|
2023-05-17 19:36:02 +02:00
|
|
|
selectionEnabledHook.value = false;
|
|
|
|
} finally {
|
|
|
|
processing.value = false;
|
|
|
|
}
|
2023-02-06 14:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 04:40:50 +02:00
|
|
|
void onUpload() async {
|
|
|
|
processing.value = true;
|
|
|
|
try {
|
|
|
|
final Set<Asset> assets = selection.value;
|
|
|
|
if (assets.length > 30) {
|
|
|
|
ImmichToast.show(
|
|
|
|
context: context,
|
|
|
|
msg: 'home_page_upload_err_limit'.tr(),
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
processing.value = false;
|
|
|
|
selectionEnabledHook.value = false;
|
|
|
|
await ref
|
|
|
|
.read(manualUploadProvider.notifier)
|
|
|
|
.uploadAssets(context, assets);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
processing.value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
void onAddToAlbum(Album album) async {
|
2023-05-17 19:36:02 +02:00
|
|
|
processing.value = true;
|
|
|
|
try {
|
|
|
|
final Iterable<Asset> assets = remoteOnlySelection(
|
|
|
|
localErrorMessage: "home_page_add_to_album_err_local".tr(),
|
|
|
|
);
|
|
|
|
if (assets.isEmpty) {
|
|
|
|
return;
|
2022-11-06 03:21:55 +02:00
|
|
|
}
|
2023-05-17 19:36:02 +02:00
|
|
|
final result = await albumService.addAdditionalAssetToAlbum(
|
|
|
|
assets,
|
|
|
|
album,
|
|
|
|
);
|
2022-11-06 03:21:55 +02:00
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
if (result != null) {
|
|
|
|
if (result.alreadyInAlbum.isNotEmpty) {
|
|
|
|
ImmichToast.show(
|
|
|
|
context: context,
|
|
|
|
msg: "home_page_add_to_album_conflicts".tr(
|
|
|
|
namedArgs: {
|
|
|
|
"album": album.name,
|
|
|
|
"added": result.successfullyAdded.toString(),
|
2023-08-19 00:52:40 +02:00
|
|
|
"failed": result.alreadyInAlbum.length.toString(),
|
2023-05-17 19:36:02 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
ImmichToast.show(
|
|
|
|
context: context,
|
|
|
|
msg: "home_page_add_to_album_success".tr(
|
|
|
|
namedArgs: {
|
|
|
|
"album": album.name,
|
|
|
|
"added": result.successfullyAdded.toString(),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
toastType: ToastType.success,
|
|
|
|
);
|
2023-08-02 03:29:14 +02:00
|
|
|
|
|
|
|
ref.watch(albumProvider.notifier).getAllAlbums();
|
|
|
|
ref.invalidate(albumDetailProvider(album.id));
|
2023-05-17 19:36:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
processing.value = false;
|
2022-11-06 03:21:55 +02:00
|
|
|
selectionEnabledHook.value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onCreateNewAlbum() async {
|
2023-05-17 19:36:02 +02:00
|
|
|
processing.value = true;
|
|
|
|
try {
|
|
|
|
final Iterable<Asset> assets = remoteOnlySelection(
|
|
|
|
localErrorMessage: "home_page_add_to_album_err_local".tr(),
|
|
|
|
);
|
|
|
|
if (assets.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final result =
|
|
|
|
await albumService.createAlbumWithGeneratedName(assets);
|
2022-11-06 03:21:55 +02:00
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
if (result != null) {
|
|
|
|
ref.watch(albumProvider.notifier).getAllAlbums();
|
|
|
|
ref.watch(sharedAlbumProvider.notifier).getAllSharedAlbums();
|
|
|
|
selectionEnabledHook.value = false;
|
2022-11-06 03:21:55 +02:00
|
|
|
|
2023-05-17 19:36:02 +02:00
|
|
|
AutoRouter.of(context).push(AlbumViewerRoute(albumId: result.id));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
processing.value = false;
|
2022-11-06 03:21:55 +02:00
|
|
|
}
|
2022-10-01 19:19:40 +02:00
|
|
|
}
|
|
|
|
|
2023-03-27 04:35:52 +02:00
|
|
|
Future<void> refreshAssets() async {
|
|
|
|
final fullRefresh = refreshCount.value > 0;
|
|
|
|
await ref.read(assetProvider.notifier).getAllAsset(clear: fullRefresh);
|
|
|
|
if (fullRefresh) {
|
|
|
|
// refresh was forced: user requested another refresh within 2 seconds
|
|
|
|
refreshCount.value = 0;
|
|
|
|
} else {
|
|
|
|
refreshCount.value++;
|
|
|
|
// set counter back to 0 if user does not request refresh again
|
2023-08-06 04:40:50 +02:00
|
|
|
Timer(const Duration(seconds: 4), () {
|
2023-03-27 04:35:52 +02:00
|
|
|
refreshCount.value = 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 18:58:07 +02:00
|
|
|
buildLoadingIndicator() {
|
|
|
|
Timer(const Duration(seconds: 2), () {
|
|
|
|
tipOneOpacity.value = 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const ImmichLoadingIndicator(),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 16.0),
|
|
|
|
child: Text(
|
2023-01-25 18:10:04 +02:00
|
|
|
'home_page_building_timeline',
|
2022-11-30 18:58:07 +02:00
|
|
|
style: TextStyle(
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
fontSize: 16,
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
),
|
2023-01-25 18:10:04 +02:00
|
|
|
).tr(),
|
2022-11-30 18:58:07 +02:00
|
|
|
),
|
|
|
|
AnimatedOpacity(
|
|
|
|
duration: const Duration(milliseconds: 500),
|
|
|
|
opacity: tipOneOpacity.value,
|
2023-01-25 18:10:04 +02:00
|
|
|
child: SizedBox(
|
2022-11-30 18:58:07 +02:00
|
|
|
width: 250,
|
|
|
|
child: Padding(
|
2023-01-25 18:10:04 +02:00
|
|
|
padding: const EdgeInsets.only(top: 8.0),
|
|
|
|
child: const Text(
|
|
|
|
'home_page_first_time_notice',
|
2022-11-30 18:58:07 +02:00
|
|
|
textAlign: TextAlign.justify,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 12,
|
|
|
|
),
|
2023-01-25 18:10:04 +02:00
|
|
|
).tr(),
|
2022-11-30 18:58:07 +02:00
|
|
|
),
|
|
|
|
),
|
2023-08-19 00:52:40 +02:00
|
|
|
),
|
2022-11-30 18:58:07 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:20:26 +02:00
|
|
|
return SafeArea(
|
|
|
|
top: true,
|
2023-04-03 23:43:46 +02:00
|
|
|
bottom: false,
|
2023-02-02 20:20:26 +02:00
|
|
|
child: Stack(
|
|
|
|
children: [
|
2023-05-25 05:52:43 +02:00
|
|
|
ref.watch(assetsProvider(currentUser?.isarId)).when(
|
2023-05-17 19:36:02 +02:00
|
|
|
data: (data) => data.isEmpty
|
|
|
|
? buildLoadingIndicator()
|
|
|
|
: ImmichAssetGrid(
|
|
|
|
renderList: data,
|
|
|
|
listener: selectionListener,
|
|
|
|
selectionActive: selectionEnabledHook.value,
|
|
|
|
onRefresh: refreshAssets,
|
2023-08-14 19:52:06 +02:00
|
|
|
topWidget: (currentUser != null &&
|
|
|
|
currentUser.memoryEnabled != null &&
|
|
|
|
currentUser.memoryEnabled!)
|
|
|
|
? const MemoryLane()
|
|
|
|
: const SizedBox(),
|
2023-05-17 19:36:02 +02:00
|
|
|
),
|
|
|
|
error: (error, _) => Center(child: Text(error.toString())),
|
|
|
|
loading: buildLoadingIndicator,
|
|
|
|
),
|
2023-02-02 20:20:26 +02:00
|
|
|
if (selectionEnabledHook.value)
|
2023-04-03 23:43:46 +02:00
|
|
|
ControlBottomAppBar(
|
|
|
|
onShare: onShareAssets,
|
|
|
|
onFavorite: onFavoriteAssets,
|
2023-04-17 07:02:07 +02:00
|
|
|
onArchive: onArchiveAsset,
|
2023-04-03 23:43:46 +02:00
|
|
|
onDelete: onDelete,
|
|
|
|
onAddToAlbum: onAddToAlbum,
|
|
|
|
albums: albums,
|
|
|
|
sharedAlbums: sharedAlbums,
|
|
|
|
onCreateNewAlbum: onCreateNewAlbum,
|
2023-08-06 04:40:50 +02:00
|
|
|
onUpload: onUpload,
|
2023-05-17 19:36:02 +02:00
|
|
|
enabled: !processing.value,
|
2023-08-06 04:40:50 +02:00
|
|
|
selectionAssetState: selectionAssetState.value,
|
2023-02-02 20:20:26 +02:00
|
|
|
),
|
2023-08-19 00:52:40 +02:00
|
|
|
if (processing.value) const Center(child: ImmichLoadingIndicator()),
|
2023-02-02 20:20:26 +02:00
|
|
|
],
|
2022-02-04 06:08:22 +02:00
|
|
|
),
|
2022-02-04 05:01:14 +02:00
|
|
|
);
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Scaffold(
|
2023-04-03 23:43:46 +02:00
|
|
|
appBar: !selectionEnabledHook.value
|
2023-04-17 07:02:07 +02:00
|
|
|
? HomePageAppBar(onPopBack: reloadAllAsset)
|
2023-04-03 23:43:46 +02:00
|
|
|
: null,
|
2022-02-03 18:06:44 +02:00
|
|
|
drawer: const ProfileDrawer(),
|
2022-10-01 10:33:06 +02:00
|
|
|
body: buildBody(),
|
2022-02-04 06:08:22 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|