|
| 1 | +import 'dart:io'; |
| 2 | +import 'package:path/path.dart' as p; |
| 3 | +import 'package:distributeapp/core/preferences/settings_cubit.dart'; |
| 4 | +import 'package:distributeapp/repositories/storage_repository.dart'; |
| 5 | +import 'package:distributeapp/core/artwork/artwork_repository.dart'; |
| 6 | +import 'package:distributeapp/repositories/audio/music_player_controller.dart'; |
| 7 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 8 | +import 'package:freezed_annotation/freezed_annotation.dart'; |
| 9 | + |
| 10 | +part 'storage_state.dart'; |
| 11 | +part 'storage_cubit.freezed.dart'; |
| 12 | + |
| 13 | +class StorageCubit extends Cubit<StorageState> { |
| 14 | + final StorageRepository _storageRepository; |
| 15 | + final SettingsCubit _settingsCubit; |
| 16 | + final ArtworkRepository _artworkRepository; |
| 17 | + final MusicPlayerController _musicPlayerController; |
| 18 | + |
| 19 | + StorageCubit({ |
| 20 | + required StorageRepository storageRepository, |
| 21 | + required SettingsCubit settingsCubit, |
| 22 | + required ArtworkRepository artworkRepository, |
| 23 | + required MusicPlayerController musicPlayerController, |
| 24 | + }) : _storageRepository = storageRepository, |
| 25 | + _settingsCubit = settingsCubit, |
| 26 | + _artworkRepository = artworkRepository, |
| 27 | + _musicPlayerController = musicPlayerController, |
| 28 | + super(const StorageState.initial()); |
| 29 | + |
| 30 | + Future<void> requestMove(String selectedPath) async { |
| 31 | + String newPath = selectedPath; |
| 32 | + |
| 33 | + if (newPath.endsWith(Platform.pathSeparator)) { |
| 34 | + newPath = newPath.substring(0, newPath.length - 1); |
| 35 | + } |
| 36 | + |
| 37 | + final String paramName = newPath.split(Platform.pathSeparator).last; |
| 38 | + if (paramName != 'Distribute') { |
| 39 | + newPath = '$newPath${Platform.pathSeparator}Distribute'; |
| 40 | + } |
| 41 | + |
| 42 | + emit(const StorageState.checking()); |
| 43 | + |
| 44 | + try { |
| 45 | + final oldRootPath = _settingsCubit.state.rootPath; |
| 46 | + |
| 47 | + if (p.equals(oldRootPath, newPath)) { |
| 48 | + emit( |
| 49 | + const StorageState.error( |
| 50 | + "Selected location is the same as the current one.", |
| 51 | + ), |
| 52 | + ); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (p.isWithin(oldRootPath, newPath)) { |
| 57 | + emit( |
| 58 | + const StorageState.error( |
| 59 | + "New location cannot be a subdirectory of the current one as it would cause issues during file transfer.", |
| 60 | + ), |
| 61 | + ); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + final freeSpace = await _storageRepository.getFreeSpaceInBytes(newPath); |
| 66 | + |
| 67 | + final oldDir = Directory(oldRootPath); |
| 68 | + final requiredBytes = await _storageRepository.calculateDirectorySize( |
| 69 | + oldDir, |
| 70 | + ); |
| 71 | + |
| 72 | + if (freeSpace < requiredBytes) { |
| 73 | + emit( |
| 74 | + StorageState.insufficientSpace( |
| 75 | + requiredBytes: requiredBytes, |
| 76 | + availableBytes: freeSpace, |
| 77 | + pendingPath: newPath, |
| 78 | + ), |
| 79 | + ); |
| 80 | + } else { |
| 81 | + await _startTransfer(oldRootPath, newPath); |
| 82 | + } |
| 83 | + } catch (e) { |
| 84 | + emit(StorageState.error(e.toString())); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + Future<void> confirmMoveWithoutTransfer(String newPath) async { |
| 89 | + await _settingsCubit.setCustomDownloadPath(newPath); |
| 90 | + _artworkRepository.clearCache(); |
| 91 | + _musicPlayerController.clearCache(); |
| 92 | + emit(const StorageState.success()); |
| 93 | + } |
| 94 | + |
| 95 | + Future<void> startTransfer(String newPath) async { |
| 96 | + final oldRootPath = _settingsCubit.state.rootPath; |
| 97 | + await _startTransfer(oldRootPath, newPath); |
| 98 | + } |
| 99 | + |
| 100 | + Future<void> _startTransfer(String oldPath, String newPath) async { |
| 101 | + emit(const StorageState.transferring(0.0)); |
| 102 | + |
| 103 | + try { |
| 104 | + if (oldPath == newPath) { |
| 105 | + emit(const StorageState.success()); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + await for (final progress in _storageRepository.moveFiles( |
| 110 | + oldPath: oldPath, |
| 111 | + newPath: newPath, |
| 112 | + )) { |
| 113 | + emit(StorageState.transferring(progress)); |
| 114 | + } |
| 115 | + |
| 116 | + await _settingsCubit.setCustomDownloadPath(newPath); |
| 117 | + _artworkRepository.clearCache(); |
| 118 | + _musicPlayerController.clearCache(); |
| 119 | + emit(const StorageState.success()); |
| 120 | + } catch (e) { |
| 121 | + emit(StorageState.error(e.toString())); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + void reset() { |
| 126 | + emit(const StorageState.initial()); |
| 127 | + } |
| 128 | +} |
0 commit comments