Skip to content

Commit

Permalink
perf: video files fetching at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Oct 21, 2023
1 parent 16c2260 commit 0e32f1e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 68 deletions.
49 changes: 5 additions & 44 deletions lib/controller/indexer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import 'package:namida/controller/settings_controller.dart';
import 'package:namida/core/constants.dart';
import 'package:namida/core/enums.dart';
import 'package:namida/core/extensions.dart';
import 'package:namida/core/functions.dart';
import 'package:namida/core/namida_converter_ext.dart';
import 'package:namida/core/translations/language.dart';

Expand Down Expand Up @@ -1159,12 +1160,13 @@ class Indexer {
final parameters = {
'allAvailableDirectories': allAvailableDirectories,
'directoriesToExclude': settings.directoriesToExclude.toList(),
'extensions': kAudioFileExtensions,
};

final mapResult = await _getAudioFilesIsolate.thready(parameters);
final mapResult = await getFilesTypeIsolate.thready(parameters);

final allPaths = mapResult['allPaths']!;
final excludedByNoMedia = mapResult['pathsExcludedByNoMedia']!;
final allPaths = mapResult['allPaths'] as Set<String>;
final excludedByNoMedia = mapResult['pathsExcludedByNoMedia'] as Set<String>;

tracksExcludedByNoMedia.value += excludedByNoMedia.length;

Expand All @@ -1176,47 +1178,6 @@ class Indexer {
return allPaths;
}

/// ```
/// {
/// 'allPaths': <String>{},
/// 'pathsExcludedByNoMedia': <String>{},
/// }
/// ```
static Map<String, Set<String>> _getAudioFilesIsolate(Map parameters) {
final allAvailableDirectories = parameters['allAvailableDirectories'] as Map<Directory, bool>;
final directoriesToExclude = parameters['directoriesToExclude'] as List<String>;

final allPaths = <String>{};
final excludedByNoMedia = <String>{};

allAvailableDirectories.keys.toList().loop((d, index) {
final hasNoMedia = allAvailableDirectories[d] ?? false;

for (final systemEntity in d.listSync()) {
if (systemEntity is File) {
final path = systemEntity.path;
if (!kAudioFileExtensions.any((ext) => path.endsWith(ext))) {
continue;
}
if (hasNoMedia) {
excludedByNoMedia.add(path);
continue;
}

// Skips if the file is included in one of the excluded folders.
if (directoriesToExclude.any((exc) => path.startsWith(exc))) {
continue;
}
allPaths.add(path);
}
}
});
return {
'allPaths': allPaths,
'pathsExcludedByNoMedia': excludedByNoMedia,
};
}

bool? _latestRespectNoMedia;
Completer<Map<Directory, bool>>? _availableDirs;
Future<Map<Directory, bool>> getAvailableDirectories({bool strictNoMedia = true, bool forceReCheck = false}) async {
Expand Down
39 changes: 15 additions & 24 deletions lib/controller/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart' hide Response;
import 'package:namida/core/functions.dart';
import 'package:newpipeextractor_dart/models/streams.dart';
import 'package:picture_in_picture/picture_in_picture.dart';
import 'package:video_player/video_player.dart';
Expand Down Expand Up @@ -1034,29 +1035,19 @@ class VideoController {
);
}

Future<List<String>> _fetchVideoPathsFromStorage({bool strictNoMedia = true, bool forceReCheckDir = false}) async {
final allAvailableDirectories = await Indexer.inst.getAvailableDirectories(forceReCheck: forceReCheckDir);
final allVideoPaths = <String>[];
Future<Set<String>> _fetchVideoPathsFromStorage({bool strictNoMedia = true, bool forceReCheckDir = false}) async {
final allAvailableDirectories = await Indexer.inst.getAvailableDirectories(forceReCheck: forceReCheckDir, strictNoMedia: strictNoMedia);

final dirToExclude = settings.directoriesToExclude;
final dirToLoop = allAvailableDirectories.keys.toList();
dirToLoop.removeWhere((element) => allAvailableDirectories[element] ?? false);
final parameters = {
'allAvailableDirectories': allAvailableDirectories,
'directoriesToExclude': settings.directoriesToExclude.toList(),
'extensions': kVideoFilesExtensions,
};

await dirToLoop.loopFuture((d, index) async {
await for (final systemEntity in d.list()) {
if (systemEntity is File) {
final path = systemEntity.path;
if (!kVideoFilesExtensions.any((ext) => path.endsWith(ext))) {
continue;
}
if (dirToExclude.any((excludedDir) => path.startsWith(excludedDir))) {
continue;
}
final mapResult = await getFilesTypeIsolate.thready(parameters);

allVideoPaths.add(path);
}
}
});
final allVideoPaths = mapResult['allPaths'] as Set<String>;
// final excludedByNoMedia = mapResult['pathsExcludedByNoMedia'] as Set<String>;
return allVideoPaths;
}
}
Expand Down Expand Up @@ -1200,8 +1191,8 @@ class _NamidaVideoPlayer {
if (updateRatioOnly) {
return await PictureInPicture.setAspectRatio(width: w, height: h);
} else {
// final videoCtx = VideoController.inst.normalControlskey.currentContext ?? VideoController.inst.fullScreenControlskey.currentContext;
// final rect = videoCtx?.globalPaintBounds;
// final videoCtx = VideoController.inst.normalControlskey.currentContext ?? VideoController.inst.fullScreenControlskey.currentContext;
// final rect = videoCtx?.globalPaintBounds;
return await PictureInPicture.enterPip(width: w, height: h /*, rectHint: rect */);
}
}
Expand All @@ -1221,7 +1212,7 @@ class _NamidaVideoPlayer {
_isBuffering.value = _videoController?.value.isBuffering ?? false;

if (_isBuffering.value) {
_bufferingCompleter?.completeIfWasnt();
_bufferingCompleter?.completeIfWasnt(false);
_bufferingCompleter = null;
_bufferingCompleter = Completer<bool>();
if (!VideoController.inst.isCurrentlyInBackground && Player.inst.isPlaying && Player.inst.shouldCareAboutAVSync) {
Expand All @@ -1243,7 +1234,7 @@ class _NamidaVideoPlayer {
_aspectRatio.value = null;
_isBuffering.value = false;
_buffered.value = null;
_bufferingCompleter?.completeIfWasnt();
_bufferingCompleter?.completeIfWasnt(false);
_isCurrentVideoFromCache.value = false;
await _videoController?.dispose();

Expand Down
58 changes: 58 additions & 0 deletions lib/core/functions.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:calendar_date_picker2/calendar_date_picker2.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
Expand Down Expand Up @@ -371,3 +373,59 @@ bool checkIfQueueSameAsCurrent(List<Selectable> queue) {
bool checkIfQueueSameAsAllTracks(List<Selectable> queue) {
return checkIfListsSimilar(queue, allTracksInLibrary) == 1.0;
}

/// **takes:**
/// ```
/// {
/// 'allAvailableDirectories': <Directory, bool>{},
/// 'directoriesToExclude': <String>[],
/// 'extensions': <String>{},
/// 'respectNoMedia': bool ?? true,
/// }
/// ```
///
/// **returns:**
/// ```
/// {
/// 'allPaths': <String>{},
/// 'pathsExcludedByNoMedia': <String>{},
/// }
/// ```
Map<String, Set<String>> getFilesTypeIsolate(Map parameters) {
final allAvailableDirectories = parameters['allAvailableDirectories'] as Map<Directory, bool>;
final directoriesToExclude = parameters['directoriesToExclude'] as List<String>? ?? [];
final extensions = parameters['extensions'] as Set<String>;
final respectNoMedia = parameters['respectNoMedia'] as bool? ?? true;

final allPaths = <String>{};
final excludedByNoMedia = <String>{};

allAvailableDirectories.keys.toList().loop((d, index) {
final hasNoMedia = allAvailableDirectories[d] ?? false;

for (final systemEntity in d.listSync()) {
if (systemEntity is File) {
final path = systemEntity.path;
// -- skip if not in extensions
if (!extensions.any((ext) => path.endsWith(ext))) {
continue;
}
// -- skip if in nomedia folder & specified to exclude
if (respectNoMedia && hasNoMedia) {
excludedByNoMedia.add(path);
continue;
}

// -- skips if the file is included in one of the excluded folders.
if (directoriesToExclude.any((exc) => path.startsWith(exc))) {
continue;
}
allPaths.add(path);
}
}
});
return {
'allPaths': allPaths,
'pathsExcludedByNoMedia': excludedByNoMedia,
};
}

0 comments on commit 0e32f1e

Please sign in to comment.