Skip to content

Commit

Permalink
fixed bug of get_application_document_directory which always changes …
Browse files Browse the repository at this point in the history
…whenever app restarts in ios -> added feature for checking paths in ios
  • Loading branch information
sb-dor committed Aug 31, 2024
1 parent ad7b399 commit 36b1fe8
Showing 9 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:youtube/core/db/base_downloaded_file_model/base_downloaded_file_model.dart';
import 'package:youtube/core/db/db_floor.dart';
import 'package:youtube/features/library_downloads/data/sources/get_downloaded_files_source/get_downloaded_files_source.dart';
import 'package:youtube/utils/mixins/storage_helper.dart';
import 'package:youtube/utils/regex_helper/regex_helper.dart';
import 'package:youtube/x_injection_containers/injection_container.dart';
import 'package:collection/collection.dart';

class GetDownloadedFilesSourceImpl
with StorageHelper, RegexHelper
implements GetDownloadedFilesSource {

class GetDownloadedFilesSourceImpl with StorageHelper implements GetDownloadedFilesSource {
// Override the loadDownloadFiles method from the GetDownloadedFilesSource interface
@override
Future<List<BaseDownloadedFileModel>> loadDownloadFiles() async {

// Get the directory for external storage (could be null if not available)
final Directory? externalStorage = await getStorage();

// List all files in the external storage directory and convert them to a list
var dataFromStorage = await externalStorage?.list().toList();

// Retrieve the list of downloaded files from the database using the locator pattern
var dataFromDb = await locator<DbFloor>().downloadedFiles.getDownloadedFiles();

// Loop through each downloaded file in the database
for (int i = 0; i < dataFromDb.length; i++) {
if (!(dataFromStorage ?? []).any((el) => el.path == dataFromDb[i].downloadedPath)) {
dataFromDb.removeAt(i);
i--;

// Check if the current platform is iOS
if (defaultTargetPlatform == TargetPlatform.iOS) {

// Find the corresponding file in external storage by matching the videoId in the path
final findPath = (dataFromStorage ?? <FileSystemEntity>[]).firstWhereOrNull(
(e) => e.path.contains(
"videoId_${videoIdFromStorageSavedData(dataFromDb[i].downloadedPath ?? '')}"),
);

// If a matching file is found, update the path in the database model
if (findPath != null) {
dataFromDb[i].downloadedPath = findPath.path;
}
} else {

// If not on iOS, check if the file exists in the external storage
if (!(dataFromStorage ?? []).any((el) => el.path == dataFromDb[i].downloadedPath)) {

// If the file doesn't exist, remove it from the database list
dataFromDb.removeAt(i);

// Decrement the index to adjust for the removed item
i--;
}
}
}

// Reverse the order of the list (newest files first)
dataFromDb = dataFromDb.reversed.toList();

// Return the list of downloaded files
return dataFromDb;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:io';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:path_provider/path_provider.dart';
import 'package:youtube/features/library_screen/domain/repository/library_screen_repository.dart';
import 'package:youtube/features/library_screen/domain/usecases/get_history.dart';
import 'package:youtube/features/library_screen/domain/usecases/save_in_history.dart';
3 changes: 0 additions & 3 deletions lib/features/search_screen/bloc/main_search_screen_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import 'dart:async';
import 'dart:convert';
import 'dart:isolate';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:speech_to_text/speech_recognition_result.dart';
import 'package:youtube/core/api/api_get_data/rest_api_get_video_data.dart';
import 'package:youtube/features/search_screen/data/source/rest_api_get_suggestion_text.dart';
import 'package:youtube/utils/enums.dart';
import 'package:youtube/utils/regex_helper/regex_helper.dart';
import 'package:youtube/x_injection_containers/injection_container.dart';
import 'package:youtube/youtube_data_api/models/thumbnail.dart';
import 'package:youtube/youtube_data_api/models/video.dart' as ytv;
import 'package:youtube/youtube_data_api/models/video_data.dart' as ytvdata;
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:youtube/core/db/db_floor.dart';
import 'package:youtube/core/db/downloaded_file_db/file_downloaded_model/file_downloaded_model.dart';
import 'package:youtube/features/youtube_video_player_screen/cubit/domain/repository/downloading_audio_repository/downloading_audio_repository.dart';
import 'package:youtube/features/youtube_video_player_screen/cubit/state_model/youtube_video_state_model.dart';
import 'package:youtube/utils/mixins/storage_helper.dart';
import 'package:youtube/utils/reusable_global_functions.dart';
import 'package:youtube/x_injection_containers/injection_container.dart';

class DownloadAudioInAppStorage implements DownloadingAudioRepository {
class DownloadAudioInAppStorage with StorageHelper implements DownloadingAudioRepository {
final ReusableGlobalFunctions _reusableGlobalFunctions = locator<ReusableGlobalFunctions>();

@override
Future<void> download(List<int>? downloadData, YoutubeVideoStateModel stateModel) async {
var getExternalStoragePath = await getExternalStorageDirectory();
var getExternalStoragePath = await getStorage();

var dateTimeForAudioName = DateTime.now();

var pathOfAudio = '${getExternalStoragePath?.path}/'
'${_reusableGlobalFunctions.removeSpaceFromStringForDownloadingVideo("${stateModel.videoData?.video?.title ?? '-'}"
"_${dateTimeForAudioName.toString()}")}.mp3';
"_${dateTimeForAudioName.toString()}")}'
'_videoId_${_reusableGlobalFunctions.removeSpaceFromStringForDownloadingVideo(stateModel.tempVideoId ?? '')}.mp3';

File fileForSaving = File(pathOfAudio);

Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:youtube/core/db/db_floor.dart';
import 'package:youtube/core/db/downloaded_file_db/file_downloaded_model/file_downloaded_model.dart';
import 'package:youtube/features/youtube_video_player_screen/cubit/domain/repository/downloading_video_repository/downloading_video_repository.dart';
@@ -19,7 +17,9 @@ class DownloadVideoInAppStorage with StorageHelper implements DownloadingVideoRe
var dateTimeForVideoName = DateTime.now();

var pathOfVideo = '${getExternalStoragePath?.path}/'
'${reusableGlobalFunctions.removeSpaceFromStringForDownloadingVideo("${stateModel.videoData?.video?.title ?? "-"}_${dateTimeForVideoName.toString()}")}.mp4';
'${reusableGlobalFunctions.removeSpaceFromStringForDownloadingVideo("${stateModel.videoData?.video?.title ?? "-"}"
"_${dateTimeForVideoName.toString()}")}'
'_videoId_${reusableGlobalFunctions.removeSpaceFromStringForDownloadingVideo(stateModel.tempVideoId ?? '')}.mp4';

File fileForSaving = File(pathOfVideo);

Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:isolate';

import 'package:audio_service/audio_service.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_overlay_window/flutter_overlay_window.dart';
import 'package:youtube/app_routes.dart';
import 'package:youtube/features/widgets/videos_widgets/videos_error_widget.dart';
import 'package:youtube/features/widgets/videos_widgets/videos_loaded_widget.dart';
import 'package:youtube/features/widgets/videos_widgets/videos_loading_widget.dart';
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:just_audio/just_audio.dart';
6 changes: 6 additions & 0 deletions lib/utils/regex_helper/regex_helper.dart
Original file line number Diff line number Diff line change
@@ -20,4 +20,10 @@ mixin class RegexHelper {

return foundGroup?.group(1) ?? '';
}

String videoIdFromStorageSavedData(String text) {
final regex = RegExp(r'_videoId_(.{1,}).mp4');
final data = regex.firstMatch(text);
return data?.group(1) ?? '';
}
}
1 change: 0 additions & 1 deletion lib/x_injection_containers/injection_container.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:audio_service/audio_service.dart';
import 'package:get_it/get_it.dart';
import 'package:youtube/features/youtube_video_player_screen/cubit/youtube_video_cubit.dart';
import 'package:youtube/features/youtube_video_player_screen/services/music_background_service.dart';

0 comments on commit 36b1fe8

Please sign in to comment.