123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- import 'dart:io';
- import 'package:easy_localization/easy_localization.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_hooks/flutter_hooks.dart';
- import 'package:hooks_riverpod/hooks_riverpod.dart';
- import 'package:immich_mobile/extensions/build_context_extensions.dart';
- import 'package:immich_mobile/modules/album/providers/album.provider.dart';
- import 'package:immich_mobile/modules/backup/providers/error_backup_list.provider.dart';
- import 'package:immich_mobile/modules/backup/providers/ios_background_settings.provider.dart';
- import 'package:immich_mobile/modules/backup/providers/manual_upload.provider.dart';
- import 'package:immich_mobile/modules/backup/ui/current_backup_asset_info_box.dart';
- import 'package:immich_mobile/modules/backup/models/backup_state.model.dart';
- import 'package:immich_mobile/modules/backup/providers/backup.provider.dart';
- import 'package:immich_mobile/routing/router.dart';
- import 'package:immich_mobile/shared/providers/websocket.provider.dart';
- import 'package:immich_mobile/modules/backup/ui/backup_info_card.dart';
- class BackupControllerPage extends HookConsumerWidget {
- const BackupControllerPage({Key? key}) : super(key: key);
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- BackUpState backupState = ref.watch(backupProvider);
- final hasAnyAlbum = backupState.selectedBackupAlbums.isNotEmpty;
- bool hasExclusiveAccess =
- backupState.backupProgress != BackUpProgressEnum.inBackground;
- bool shouldBackup = backupState.allUniqueAssets.length -
- backupState.selectedAlbumsBackupAssetsIds.length ==
- 0 ||
- !hasExclusiveAccess
- ? false
- : true;
- useEffect(
- () {
- if (backupState.backupProgress != BackUpProgressEnum.inProgress &&
- backupState.backupProgress != BackUpProgressEnum.manualInProgress) {
- ref.watch(backupProvider.notifier).getBackupInfo();
- }
- // Update the background settings information just to make sure we
- // have the latest, since the platform channel will not update
- // automatically
- if (Platform.isIOS) {
- ref.watch(iOSBackgroundSettingsProvider.notifier).refresh();
- }
- ref
- .watch(websocketProvider.notifier)
- .stopListenToEvent('on_upload_success');
- return null;
- },
- [],
- );
- Widget buildSelectedAlbumName() {
- var text = "backup_controller_page_backup_selected".tr();
- var albums = ref.watch(backupProvider).selectedBackupAlbums;
- if (albums.isNotEmpty) {
- for (var album in albums) {
- if (album.name == "Recent" || album.name == "Recents") {
- text += "${album.name} (${'backup_all'.tr()}), ";
- } else {
- text += "${album.name}, ";
- }
- }
- return Padding(
- padding: const EdgeInsets.only(top: 8.0),
- child: Text(
- text.trim().substring(0, text.length - 2),
- style: context.textTheme.labelLarge?.copyWith(
- color: context.primaryColor,
- ),
- ),
- );
- } else {
- return Padding(
- padding: const EdgeInsets.only(top: 8.0),
- child: Text(
- "backup_controller_page_none_selected".tr(),
- style: context.textTheme.labelLarge?.copyWith(
- color: context.primaryColor,
- ),
- ),
- );
- }
- }
- Widget buildExcludedAlbumName() {
- var text = "backup_controller_page_excluded".tr();
- var albums = ref.watch(backupProvider).excludedBackupAlbums;
- if (albums.isNotEmpty) {
- for (var album in albums) {
- text += "${album.name}, ";
- }
- return Padding(
- padding: const EdgeInsets.only(top: 8.0),
- child: Text(
- text.trim().substring(0, text.length - 2),
- style: context.textTheme.labelLarge?.copyWith(
- color: Colors.red[300],
- ),
- ),
- );
- } else {
- return const SizedBox();
- }
- }
- buildFolderSelectionTile() {
- return Padding(
- padding: const EdgeInsets.only(top: 8.0),
- child: Card(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(20),
- side: BorderSide(
- color: context.isDarkTheme
- ? const Color.fromARGB(255, 56, 56, 56)
- : Colors.black12,
- width: 1,
- ),
- ),
- elevation: 0,
- borderOnForeground: false,
- child: ListTile(
- minVerticalPadding: 18,
- title: Text(
- "backup_controller_page_albums",
- style: context.textTheme.titleMedium,
- ).tr(),
- subtitle: Padding(
- padding: const EdgeInsets.only(top: 8.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- "backup_controller_page_to_backup",
- style: context.textTheme.bodyMedium,
- ).tr(),
- buildSelectedAlbumName(),
- buildExcludedAlbumName(),
- ],
- ),
- ),
- trailing: ElevatedButton(
- onPressed: () async {
- await context.autoPush(const BackupAlbumSelectionRoute());
- // waited until returning from selection
- await ref
- .read(backupProvider.notifier)
- .backupAlbumSelectionDone();
- // waited until backup albums are stored in DB
- ref.read(albumProvider.notifier).getDeviceAlbums();
- },
- child: const Text(
- "backup_controller_page_select",
- style: TextStyle(
- fontWeight: FontWeight.bold,
- ),
- ).tr(),
- ),
- ),
- ),
- );
- }
- void startBackup() {
- ref.watch(errorBackupListProvider.notifier).empty();
- if (ref.watch(backupProvider).backupProgress !=
- BackUpProgressEnum.inBackground) {
- ref.watch(backupProvider.notifier).startBackupProcess();
- }
- }
- Widget buildBackupButton() {
- return Padding(
- padding: const EdgeInsets.only(
- top: 24,
- ),
- child: Container(
- child: backupState.backupProgress == BackUpProgressEnum.inProgress ||
- backupState.backupProgress ==
- BackUpProgressEnum.manualInProgress
- ? ElevatedButton(
- style: ElevatedButton.styleFrom(
- foregroundColor: Colors.grey[50],
- backgroundColor: Colors.red[300],
- // padding: const EdgeInsets.all(14),
- ),
- onPressed: () {
- if (backupState.backupProgress ==
- BackUpProgressEnum.manualInProgress) {
- ref.read(manualUploadProvider.notifier).cancelBackup();
- } else {
- ref.read(backupProvider.notifier).cancelBackup();
- }
- },
- child: const Text(
- "backup_controller_page_cancel",
- style: TextStyle(
- fontSize: 14,
- fontWeight: FontWeight.bold,
- ),
- ).tr(),
- )
- : ElevatedButton(
- onPressed: shouldBackup ? startBackup : null,
- child: const Text(
- "backup_controller_page_start_backup",
- style: TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold,
- ),
- ).tr(),
- ),
- ),
- );
- }
- buildBackgroundBackupInfo() {
- return const ListTile(
- leading: Icon(Icons.info_outline_rounded),
- title: Text(
- "Background backup is currently running, cannot start manual backup",
- ),
- );
- }
- return Scaffold(
- appBar: AppBar(
- elevation: 0,
- title: const Text(
- "backup_controller_page_backup",
- ).tr(),
- leading: IconButton(
- onPressed: () {
- ref.watch(websocketProvider.notifier).listenUploadEvent();
- context.autoPop(true);
- },
- splashRadius: 24,
- icon: const Icon(
- Icons.arrow_back_ios_rounded,
- ),
- ),
- actions: [
- Padding(
- padding: const EdgeInsets.only(right: 8.0),
- child: IconButton(
- onPressed: () => context.autoPush(const BackupOptionsRoute()),
- splashRadius: 24,
- icon: const Icon(
- Icons.settings_outlined,
- ),
- ),
- ),
- ],
- ),
- body: Padding(
- padding: const EdgeInsets.only(left: 16.0, right: 16, bottom: 32),
- child: ListView(
- // crossAxisAlignment: CrossAxisAlignment.start,
- children: hasAnyAlbum
- ? [
- buildFolderSelectionTile(),
- BackupInfoCard(
- title: "backup_controller_page_total".tr(),
- subtitle: "backup_controller_page_total_sub".tr(),
- info: ref.watch(backupProvider).availableAlbums.isEmpty
- ? "..."
- : "${backupState.allUniqueAssets.length}",
- ),
- BackupInfoCard(
- title: "backup_controller_page_backup".tr(),
- subtitle: "backup_controller_page_backup_sub".tr(),
- info: ref.watch(backupProvider).availableAlbums.isEmpty
- ? "..."
- : "${backupState.selectedAlbumsBackupAssetsIds.length}",
- ),
- BackupInfoCard(
- title: "backup_controller_page_remainder".tr(),
- subtitle: "backup_controller_page_remainder_sub".tr(),
- info: ref.watch(backupProvider).availableAlbums.isEmpty
- ? "..."
- : "${backupState.allUniqueAssets.length - backupState.selectedAlbumsBackupAssetsIds.length}",
- ),
- const Divider(),
- const CurrentUploadingAssetInfoBox(),
- if (!hasExclusiveAccess) buildBackgroundBackupInfo(),
- buildBackupButton(),
- ]
- : [buildFolderSelectionTile()],
- ),
- ),
- );
- }
- }
|