Skip to content

Commit

Permalink
the logic related to getting specific folder data was done with isolate
Browse files Browse the repository at this point in the history
  • Loading branch information
sb-dor committed Aug 17, 2024
1 parent d5e2bbe commit 594963f
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 39 deletions.
4 changes: 4 additions & 0 deletions lib/core/global_usages/constants/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ abstract class Constants {

static const String tempDateTime = "2024-01-01 12:00:00";
static const String defaultUserImage = "assets/default_images/temp_user.jpg";


//
static const String killIsolate = "kill_isolate";
}
5 changes: 3 additions & 2 deletions lib/core/utils/image_comporessor/image_compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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';

abstract final class ImageCompressor {
static Future<TelegramFileImageWithCompressedAndOriginalPathModel?> compressedImageFile({
static Future<TelegramPathFolderFileModel?> compressedImageFile({
required File file,
required String? directoryPath,
int quality = 60,
Expand All @@ -23,9 +23,10 @@ abstract final class ImageCompressor {
if (compressedImage == null) {
throw ("Failed to compress the image");
}
final result = TelegramFileImageWithCompressedAndOriginalPathModel(
final result = TelegramPathFolderFileModel(
File(compressedImage.path),
originalPath: file.path,
isImage: true,
);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_with_compressed_and_original_path_entity.dart';

final class TelegramFileImageWithCompressedAndOriginalPathModel
extends TelegramFileImageWithCompressedAndOriginalPathEntity {
TelegramFileImageWithCompressedAndOriginalPathModel(
final class TelegramPathFolderFileModel extends TelegramPathFolderFile {
TelegramPathFolderFileModel(
super.file, {
super.originalPath,
super.isFolder,
super.isImage,
super.isVideo,
super.fileExtension,
});

TelegramFileImageWithCompressedAndOriginalPathModel? fromEntity(
TelegramFileImageWithCompressedAndOriginalPathEntity? entity,
TelegramPathFolderFileModel? fromEntity(
TelegramPathFolderFile? entity,
) {
if (entity == null) return null;
return TelegramFileImageWithCompressedAndOriginalPathModel(
file,
originalPath: originalPath,
return TelegramPathFolderFileModel(
entity.file,
originalPath: entity.originalPath,
isFolder: entity.isFolder,
isImage: entity.isImage,
isVideo: entity.isVideo,
fileExtension: entity.fileExtension,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class TelegramFilePickerRepoImpl
with RecentFileMixin, DownloadsPathFiles, AppStorageFileMixin
implements TelegramFilePickerRepo {
@override
Stream<TelegramFileImageWithCompressedAndOriginalPathModel?> getRecentImagesAndVideos() =>
Stream<TelegramPathFolderFileModel?> getRecentImagesAndVideos() =>
getAllImagesAndVideos();

@override
Stream<TelegramFileImageWithCompressedAndOriginalPathModel?> getRecentFiles() =>
Stream<TelegramPathFolderFileModel?> getRecentFiles() =>
downloadsPathFilesData();

@override
Stream<TelegramFileImageWithCompressedAndOriginalPathEntity?> getSpecificFolderData(
Stream<TelegramPathFolderFileModel?> getSpecificFolderData(
String path,
) =>
getSpecificFolderDataStream(path);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import 'dart:io';

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

TelegramFileImageWithCompressedAndOriginalPathEntity(
TelegramPathFolderFile(
this.file, {
this.originalPath,
this.isFolder = false,
this.isImage = false,
this.isVideo = false,
this.fileExtension,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import 'package:yahay/features/telegram_file_picker_feature/domain/entities/tele
// classes only can "implement"
abstract interface class TelegramFilePickerRepo {
// for getting recent files -> images and videos
Stream<TelegramFileImageWithCompressedAndOriginalPathEntity?> getRecentImagesAndVideos();
Stream<TelegramPathFolderFile?> getRecentImagesAndVideos();

// for getting all download's path files here
Stream<TelegramFileImageWithCompressedAndOriginalPathEntity?> getRecentFiles();
Stream<TelegramPathFolderFile?> getRecentFiles();

// for getting specific app folder data, does not matter what
Stream<TelegramFileImageWithCompressedAndOriginalPathEntity?> getSpecificFolderData(String path);
Stream<TelegramPathFolderFile?> getSpecificFolderData(String path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class FilePickerUseCase {
FilePickerUseCase(this._filePickerRepo);

//
Stream<TelegramFileImageWithCompressedAndOriginalPathEntity?> getRecentImagesAndVideos() async* {
Stream<TelegramPathFolderFile?> getRecentImagesAndVideos() async* {
yield* _filePickerRepo.getRecentImagesAndVideos();
}

//
Stream<TelegramFileImageWithCompressedAndOriginalPathEntity?> downloadsPathFilesData() async* {
Stream<TelegramPathFolderFile?> downloadsPathFilesData() async* {
yield* _filePickerRepo.getRecentFiles();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import 'dart:io';
import 'dart:isolate';
import 'dart:ui';

import 'package:yahay/features/telegram_file_picker_feature/domain/entities/telegram_file_image_with_compressed_and_original_path_entity.dart';
import 'package:flutter/services.dart';
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/injections/injections.dart';
import 'package:path/path.dart' as p;

mixin class AppStorageFileMixin {
//
Stream<TelegramFileImageWithCompressedAndOriginalPathEntity?> getSpecificFolderDataStream(
Stream<TelegramPathFolderFileModel?> getSpecificFolderDataStream(
String path,
) async* {
//
Expand All @@ -21,18 +26,73 @@ mixin class AppStorageFileMixin {
path,
]);

await for (final each in mainPort) {}
await for (final each in mainPort) {
if (each is TelegramPathFolderFileModel) {
yield each;
} else if (each is String && each == Constants.killIsolate) {
mainPort.close();
break;
}
}
}

//
static Future<void> _isolateHelper(List<dynamic> args) async {
RootIsolateToken mainRootIsolateToken = args[0] as RootIsolateToken;

BackgroundIsolateBinaryMessenger.ensureInitialized(mainRootIsolateToken);

SendPort sendPort = args[1] as SendPort;

String path = args[2] as String;

final directory = Directory(path);

if (!directory.existsSync()) return;
if (!directory.existsSync()) {
sendPort.send(Constants.killIsolate);
return;
}

final reusables = snoopy<ReusableGlobalFunctions>();

final tempPath = await getTemporaryDirectory();

final data = directory.listSync();

for (final each in data) {
if (each.existsSync()) {
File file = File(each.path);

if (FileSystemEntity.isDirectorySync(each.path)) {
final folder = TelegramPathFolderFileModel(
file,
isFolder: true,
);
sendPort.send(folder);
} else if (reusables.isImageFile(each.path)) {
final compressedImage = await ImageCompressor.compressedImageFile(
file: 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);
}
}
}

sendPort.send(Constants.killIsolate);
}
//
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:get_it/get_it.dart';
import 'package:lecle_downloads_path_provider/constants/downloads_directory_type.dart';
import 'package:lecle_downloads_path_provider/lecle_downloads_path_provider.dart';
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/core/utils/permissions/permissions_service.dart';
Expand All @@ -18,7 +19,7 @@ mixin class DownloadsPathFiles {

// final _reusables = snoopy<ReusableGlobalFunctions>();

Stream<TelegramFileImageWithCompressedAndOriginalPathModel?> downloadsPathFilesData({
Stream<TelegramPathFolderFileModel?> downloadsPathFilesData({
String dirType = DownloadDirectoryTypes.downloads,
}) async* {
try {
Expand Down Expand Up @@ -51,9 +52,10 @@ mixin class DownloadsPathFiles {
Isolate.spawn(_fileFinder, sendingList);

await for (final each in sendingPort) {
if (each is TelegramFileImageWithCompressedAndOriginalPathModel) {
if (each is TelegramPathFolderFileModel) {
yield each;
} else if (each == null) {
} else if (each is String && each == Constants.killIsolate) {
sendingPort.close();
break; // null indicates the end of the processing
}
}
Expand Down Expand Up @@ -103,5 +105,7 @@ mixin class DownloadsPathFiles {
}
}
}

receivingPort.send(Constants.killIsolate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import 'package:get_it/get_it.dart';
import 'package:path_provider/path_provider.dart';
import 'package:photo_manager/photo_manager.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/core/utils/permissions/permissions_service.dart';
Expand All @@ -20,7 +21,7 @@ mixin class RecentFileMixin {

// final _reusableFunctions = snoopy<ReusableGlobalFunctions>();

Stream<TelegramFileImageWithCompressedAndOriginalPathModel?> getAllImagesAndVideos() async* {
Stream<TelegramPathFolderFileModel?> getAllImagesAndVideos() async* {
try {
final externalStoragePermission = await _permissions.manageExternalStoragePermission();

Expand Down Expand Up @@ -62,8 +63,11 @@ mixin class RecentFileMixin {
]);

await for (final each in receivePort) {
if (each is TelegramFileImageWithCompressedAndOriginalPathModel) {
if (each is TelegramPathFolderFileModel) {
yield each;
} else if (each is String && each == Constants.killIsolate) {
receivePort.close();
break;
}
}
} catch (e) {
Expand Down Expand Up @@ -125,6 +129,8 @@ mixin class RecentFileMixin {
}
}
}

sendPort.send(Constants.killIsolate);
}

// Future<File?> _compressedFile(File? file) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ class TelegramFilePickerStateModel {
List<TelegramFileImageEntity?> get clonedPickedFiles =>
_pickedFiles.map((e) => TelegramFileImageModel.fromEntity(e)).toList();

StreamSubscription<TelegramFileImageWithCompressedAndOriginalPathEntity?>? _fileStreamData;
StreamSubscription<TelegramPathFolderFile?>? _fileStreamData;

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

StreamSubscription<TelegramFileImageWithCompressedAndOriginalPathEntity?>? _recentFileData;
StreamSubscription<TelegramPathFolderFile?>? _recentFileData;

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

bool _openBottomSectionButton = true;
Expand Down Expand Up @@ -135,12 +135,12 @@ class TelegramFilePickerStateModel {
_recentFilesPagination.addAll(list);

void initFileStreamData(
StreamSubscription<TelegramFileImageWithCompressedAndOriginalPathEntity?> stream,
StreamSubscription<TelegramPathFolderFile?> stream,
) =>
_fileStreamData = stream;

void initRecentFileStreamData(
StreamSubscription<TelegramFileImageWithCompressedAndOriginalPathEntity?> stream,
StreamSubscription<TelegramPathFolderFile?> stream,
) =>
_recentFileData = stream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ final class ClosePopupEvent extends TelegramFilePickerEvents {

@immutable
final class FileStreamHandlerEvent extends TelegramFilePickerEvents {
final TelegramFileImageWithCompressedAndOriginalPathEntity? file;
final TelegramPathFolderFile? file;

const FileStreamHandlerEvent(this.file);
}

@immutable
final class RecentFileStreamHandlerEvent extends TelegramFilePickerEvents {
final TelegramFileImageWithCompressedAndOriginalPathEntity? file;
final TelegramPathFolderFile? file;

const RecentFileStreamHandlerEvent(this.file);
}
Expand Down

0 comments on commit 594963f

Please sign in to comment.