Skip to content

Commit

Permalink
done with getting specific data from folder's path in bloc and bloc's…
Browse files Browse the repository at this point in the history
… state model
  • Loading branch information
sb-dor committed Aug 17, 2024
1 parent 594963f commit 677dcd7
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lib/core/utils/image_comporessor/image_compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:io';

import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:path/path.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_file_image_with_compressed_and_original_path_model.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_path_folder_file_model.dart';

abstract final class ImageCompressor {
static Future<TelegramPathFolderFileModel?> compressedImageFile({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_with_compressed_and_original_path_entity.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_path_folder_file.dart';

final class TelegramPathFolderFileModel extends TelegramPathFolderFile {
TelegramPathFolderFileModel(
Expand All @@ -8,6 +8,7 @@ final class TelegramPathFolderFileModel extends TelegramPathFolderFile {
super.isImage,
super.isVideo,
super.fileExtension,
super.fileName,
});

TelegramPathFolderFileModel? fromEntity(
Expand All @@ -21,6 +22,7 @@ final class TelegramPathFolderFileModel extends TelegramPathFolderFile {
isImage: entity.isImage,
isVideo: entity.isVideo,
fileExtension: entity.fileExtension,
fileName: entity.fileName,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_file_image_with_compressed_and_original_path_model.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_with_compressed_and_original_path_entity.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_path_folder_file_model.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_path_folder_file.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/repo/telegram_file_picker_repo.dart';
import 'package:yahay/features/telegram_file_picker_feature/mixins/app_storage_file_mixin/app_storage_file_mixin.dart';
import 'package:yahay/features/telegram_file_picker_feature/mixins/recent_file_mixin/downloads_path_files.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import 'dart:io';

base class TelegramPathFolderFile {
final File file;
final String? originalPath;
final bool isFolder;
final bool isImage;
final bool isVideo;
final String? fileExtension;
String? originalPath;
bool isFolder;
bool isImage;
bool isVideo;
String? fileExtension;
String? fileName;

TelegramPathFolderFile(
this.file, {
Expand All @@ -15,5 +16,6 @@ base class TelegramPathFolderFile {
this.isImage = false,
this.isVideo = false,
this.fileExtension,
this.fileName,
});
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_with_compressed_and_original_path_entity.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_path_folder_file.dart';

// means that other classes can't "extend" it
// classes only can "implement"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@

import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_with_compressed_and_original_path_entity.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_path_folder_file.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/repo/telegram_file_picker_repo.dart';

class FilePickerUseCase {
final TelegramFilePickerRepo _filePickerRepo;

FilePickerUseCase(this._filePickerRepo);

//
// for getting recent files -> images and videos
Stream<TelegramPathFolderFile?> getRecentImagesAndVideos() async* {
yield* _filePickerRepo.getRecentImagesAndVideos();
}

//
// for getting all download's path files here
Stream<TelegramPathFolderFile?> downloadsPathFilesData() async* {
yield* _filePickerRepo.getRecentFiles();
}

//
// for getting specific app folder data, does not matter what
Stream<TelegramPathFolderFile?> getSpecificFolderData(String path) =>
_filePickerRepo.getSpecificFolderData(path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:yahay/core/global_usages/constants/constants.dart';
import 'package:yahay/core/global_usages/reusables/reusable_global_functions.dart';
import 'package:yahay/core/utils/image_comporessor/image_compressor.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_file_image_with_compressed_and_original_path_model.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_path_folder_file_model.dart';
import 'package:yahay/injections/injections.dart';
import 'package:path/path.dart' as p;

Expand Down Expand Up @@ -61,34 +61,23 @@ mixin class AppStorageFileMixin {

for (final each in data) {
if (each.existsSync()) {
File file = File(each.path);
TelegramPathFolderFileModel? model = TelegramPathFolderFileModel(
File(each.path),
fileExtension: p.extension(each.path),
fileName: p.basenameWithoutExtension(each.path),
);

if (FileSystemEntity.isDirectorySync(each.path)) {
final folder = TelegramPathFolderFileModel(
file,
isFolder: true,
);
sendPort.send(folder);
model.isFolder = true;
} else if (reusables.isImageFile(each.path)) {
final compressedImage = await ImageCompressor.compressedImageFile(
file: file,
model = await ImageCompressor.compressedImageFile(
file: model.file,
directoryPath: tempPath.path,
);
sendPort.send(compressedImage);
return;
} else if (reusables.isVideoFile(each.path)) {
final video = TelegramPathFolderFileModel(
file,
isVideo: true,
);
sendPort.send(video);
} else {
final otherFile = TelegramPathFolderFileModel(
file,
fileExtension: p.extension(each.path),
);
sendPort.send(otherFile);
model.isVideo = true;
}
sendPort.send(model);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:yahay/core/global_usages/constants/constants.dart';
import 'package:yahay/core/global_usages/reusables/reusable_global_functions.dart';
import 'package:yahay/core/utils/image_comporessor/image_compressor.dart';
import 'package:yahay/core/utils/permissions/permissions_service.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_file_image_with_compressed_and_original_path_model.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_path_folder_file_model.dart';
import 'package:yahay/injections/injections.dart';

mixin class DownloadsPathFiles {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:yahay/core/global_usages/constants/constants.dart';
import 'package:yahay/core/global_usages/reusables/reusable_global_functions.dart';
import 'package:yahay/core/utils/image_comporessor/image_compressor.dart';
import 'package:yahay/core/utils/permissions/permissions_service.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_file_image_with_compressed_and_original_path_model.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_path_folder_file_model.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_asset_entity.dart';
import 'package:yahay/injections/injections.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:yahay/core/utils/global_context/global_context.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_file_image_model.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_folder_enums.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_entity.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_with_compressed_and_original_path_entity.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_path_folder_file.dart';
import 'package:yahay/features/telegram_file_picker_feature/view/bloc/telegram_file_picker_state.dart';
import 'package:yahay/injections/injections.dart';

Expand All @@ -31,6 +31,10 @@ class TelegramFilePickerStateModel {

final List<TelegramFileImageEntity> _pickedFiles = [];

final List<TelegramFileImageEntity> _specificFolderFilesAll = [];

final List<TelegramFileImageEntity> _specificFolderFilesPagination = [];

UnmodifiableListView<TelegramFileImageEntity> get galleryPathFiles => UnmodifiableListView(
_galleryPathFiles,
);
Expand All @@ -47,18 +51,25 @@ class TelegramFilePickerStateModel {
_recentFilesPagination,
);

UnmodifiableListView<TelegramFileImageEntity> get specificFolderFilesPagination =>
UnmodifiableListView(
_specificFolderFilesPagination,
);

List<TelegramFileImageEntity?> get clonedPickedFiles =>
_pickedFiles.map((e) => TelegramFileImageModel.fromEntity(e)).toList();

StreamSubscription<TelegramPathFolderFile?>? _fileStreamData;

StreamSubscription<TelegramPathFolderFile?>? get fileStreamData =>
_fileStreamData;
StreamSubscription<TelegramPathFolderFile?>? get fileStreamData => _fileStreamData;

StreamSubscription<TelegramPathFolderFile?>? _recentFileData;

StreamSubscription<TelegramPathFolderFile?>? get recentFileData =>
_recentFileData;
StreamSubscription<TelegramPathFolderFile?>? get recentFileData => _recentFileData;

StreamSubscription<TelegramPathFolderFile?>? _specificFolderData;

StreamSubscription<TelegramPathFolderFile?>? get specificFolderData => _specificFolderData;

bool _openBottomSectionButton = true;

Expand Down Expand Up @@ -113,6 +124,11 @@ class TelegramFilePickerStateModel {

void clearRecentPagFiles() => _recentFilesPagination.clear();

void clearSpecificFolderData() {
_specificFolderFilesAll.clear();
_specificFolderFilesPagination.clear();
}

Stream<TelegramFilePickerStates> addOnStreamOfValuesInPaginationList(
TelegramFileImageEntity value, {
Stream<TelegramFilePickerStates>? emitter,
Expand All @@ -129,6 +145,13 @@ class TelegramFilePickerStateModel {
}
}

void addToFolderDataList(TelegramFileImageEntity value) {
_specificFolderFilesAll.add(value);
if (_specificFolderFilesPagination.length < Constants.perPage) {
_recentFilesPagination.add(value);
}
}

void addToPagination(List<TelegramFileImageEntity> list) => _galleryPathPagination.addAll(list);

void addToRecentFilesPagination(List<TelegramFileImageEntity> list) =>
Expand All @@ -144,6 +167,15 @@ class TelegramFilePickerStateModel {
) =>
_recentFileData = stream;

void initSpecificFolderDataStream(
StreamSubscription<TelegramPathFolderFile?> stream,
) =>
_specificFolderData = stream;

Future<void> closeSpecificFolderDataStream() async {
await _specificFolderData?.cancel();
}

void closeAllStreamSubs() {
_fileStreamData?.cancel();
_recentFileData?.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:yahay/core/global_usages/reusables/reusable_global_functions.dar
import 'package:yahay/core/utils/camera_helper_service/camera_helper_service.dart';
import 'package:yahay/core/utils/list_pagination_checker/list_pagination_checker.dart';
import 'package:yahay/features/telegram_file_picker_feature/data/models/telegram_file_image_model.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_entity.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/repo/telegram_file_picker_repo.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/usecases/file_picker_usecase/file_picker_usecase.dart';
import 'package:yahay/features/telegram_file_picker_feature/view/bloc/state_model/telegram_file_picker_state_model.dart';
Expand Down Expand Up @@ -102,6 +103,10 @@ class TelegramFilePickerBloc {
yield* _browseInternalStorageAndSelectFilesEvent(event);
} else if (event is SelectScreenForFilesPickerScreenEvent) {
yield* _selectScreenForFilesPickerScreenEvent(event);
} else if (event is GetSpecificFolderDataEvent) {
yield* _getSpecificFolderDataEvent(event);
} else if (event is SpecificFolderDataStreamHandlerEvent) {
yield* _specificFolderDataStreamHandlerEvent(event);
}
}

Expand Down Expand Up @@ -349,14 +354,49 @@ class TelegramFilePickerBloc {
for (final each in convertedData) {
_currentStateModel.removeOrAddEntity(each);
}
yield* _emitter();
yield* _emitter();
}

static Stream<TelegramFilePickerStates> _selectScreenForFilesPickerScreenEvent(
SelectScreenForFilesPickerScreenEvent event,
) async* {
_currentStateModel.selectScreen(event.screen);
yield* _emitter();
yield* _emitter();
}

//
static Stream<TelegramFilePickerStates> _getSpecificFolderDataEvent(
GetSpecificFolderDataEvent event,
) async* {
if (event.path == null) return;

_currentStateModel.clearSpecificFolderData();
_currentStateModel.closeSpecificFolderDataStream();
_currentStateModel.initSpecificFolderDataStream(
_filePickerUseCase.getSpecificFolderData(event.path!).listen(
(data) {
_events.add(SpecificFolderDataStreamHandlerEvent(data));
},
),
);
}

//
static Stream<TelegramFilePickerStates> _specificFolderDataStreamHandlerEvent(
SpecificFolderDataStreamHandlerEvent event,
) async* {
if (event.file == null) return;
final value = TelegramFileImageEntity(
file: event.file!.file,
videoPlayerController:
event.file!.isVideo ? VideoPlayerController.file(event.file!.file) : null,
videoPreview: event.file!.isVideo
? await VideoThumbnail.thumbnailData(video: event.file!.file.path)
: null,
fileName: event.file!.fileName,
);
_currentStateModel.addToFolderDataList(value);
yield* _emitter();
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_folder_enums.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_entity.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_with_compressed_and_original_path_entity.dart';
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_path_folder_file.dart';

@immutable
abstract base class TelegramFilePickerEvents {
Expand Down Expand Up @@ -94,12 +94,29 @@ final class RecentFilesPaginationEvent extends TelegramFilePickerEvents {
}

//
@immutable
final class BrowseInternalStorageAndSelectFilesEvent extends TelegramFilePickerEvents {
const BrowseInternalStorageAndSelectFilesEvent();
}

@immutable
final class SelectScreenForFilesPickerScreenEvent extends TelegramFilePickerEvents {
final TelegramFileFolderEnum screen;

const SelectScreenForFilesPickerScreenEvent(this.screen);
}

//
@immutable
final class GetSpecificFolderDataEvent extends TelegramFilePickerEvents {
final String? path;

const GetSpecificFolderDataEvent(this.path);
}

@immutable
final class SpecificFolderDataStreamHandlerEvent extends TelegramFilePickerEvents {
final TelegramPathFolderFile? file;

const SpecificFolderDataStreamHandlerEvent(this.file);
}

0 comments on commit 677dcd7

Please sign in to comment.