spotube/lib/provider/skip_segments/skip_segments.dart
Alessio fece073def This pull request primarily involves the removal of several configuration files and assets, as well as minor updates to documentation. The most significant changes are the deletion of various .vscode configuration files and the removal of unused assets from the project.
Configuration File Removals:

    .vscode/c_cpp_properties.json: Removed the entire configuration for C/C++ properties.
    .vscode/launch.json: Removed the Dart launch configurations for different environments and modes.
    .vscode/settings.json: Removed settings related to CMake, spell checking, file nesting, and Dart Flutter SDK path.
    .vscode/snippets.code-snippets: Removed code snippets for Dart, including PaginatedState and PaginatedNotifier templates.
    .vscode/tasks.json: Removed the tasks configuration file.

Documentation Updates:

    CONTRIBUTION.md: Removed heart emoji from the introductory text.
    README.md: Updated the logo image and made minor text adjustments, including removing emojis and updating section titles. [1] [2] [3] [4] [5]

Asset Removals:

    lib/collections/assets.gen.dart: Removed multiple unused asset references, including images related to Spotube logos and banners. [1] [2] [3]

Minor Code Cleanups:

    cli/commands/build/linux.dart, cli/commands/build/windows.dart, cli/commands/translated.dart, cli/commands/untranslated.dart: Adjusted import statements for consistency. [1] [2] [3] [4]
    integration_test/app_test.dart: Removed an unnecessary blank line.
    lib/collections/routes.dart: Commented out the TrackRoute configuration.
2025-04-13 18:40:37 +02:00

112 lines
3.0 KiB
Dart

import 'package:dio/dio.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/models/database/database.dart';
import 'package:spotube/provider/database/database.dart';
import 'package:spotube/provider/server/active_sourced_track.dart';
import 'package:spotube/provider/user_preferences/user_preferences_provider.dart';
import 'package:spotube/services/dio/dio.dart';
import 'package:spotube/services/logger/logger.dart';
class SourcedSegments {
final String source;
final List<SkipSegmentTableData> segments;
SourcedSegments({required this.source, required this.segments});
}
Future<List<SkipSegmentTableData>> getAndCacheSkipSegments(
String id, Ref ref) async {
final database = ref.read(databaseProvider);
try {
final cached = await (database.select(database.skipSegmentTable)
..where((s) => s.trackId.equals(id)))
.get();
if (cached.isNotEmpty) {
return cached;
}
final res = await globalDio.getUri(
Uri(
scheme: "https",
host: "sponsor.ajay.app",
path: "/api/skipSegments",
queryParameters: {
"videoID": id,
"category": [
'sponsor',
'selfpromo',
'interaction',
'intro',
'outro',
'music_offtopic'
],
"actionType": 'skip'
},
),
options: Options(
responseType: ResponseType.json,
validateStatus: (status) => (status ?? 0) < 500,
),
);
if (res.data == "Not Found") {
return List.castFrom<dynamic, SkipSegmentTableData>([]);
}
final data = res.data as List;
final segments = data.map((obj) {
final start = obj["segment"].first.toInt();
final end = obj["segment"].last.toInt();
return SkipSegmentTableCompanion.insert(
trackId: id,
start: start,
end: end,
);
}).toList();
await database.batch((b) {
b.insertAll(database.skipSegmentTable, segments);
});
return await (database.select(database.skipSegmentTable)
..where((s) => s.trackId.equals(id)))
.get();
} catch (e, stack) {
AppLogger.reportError(e, stack);
return List.castFrom<dynamic, SkipSegmentTableData>([]);
}
}
final segmentProvider = FutureProvider<SourcedSegments?>(
(ref) async {
final track = ref.watch(activeSourcedTrackProvider);
if (track == null) return null;
final skipNonMusic = ref.watch(
userPreferencesProvider.select(
(s) {
final isPipedYTMusicMode = s.audioSource == AudioSource.piped &&
s.searchMode == SearchMode.youtubeMusic;
return s.skipNonMusic && !isPipedYTMusicMode;
},
),
);
if (!skipNonMusic) {
return SourcedSegments(
segments: [],
source: track.sourceInfo.id,
);
}
final segments = await getAndCacheSkipSegments(track.sourceInfo.id, ref);
return SourcedSegments(
source: track.sourceInfo.id,
segments: segments,
);
},
);