Skip to content

Commit

Permalink
fix: cache segments casting error
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Aug 4, 2023
1 parent 0b7affd commit dfd60bd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
6 changes: 3 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ Future<void> main(List<String> rawArgs) async {
MediaKit.ensureInitialized();

// force High Refresh Rate on some Android devices (like One Plus)
if (DesktopTools.platform.isAndroid) {
if (DesktopTools.platform.isAndroid) {
await FlutterDisplayMode.setHighRefreshRate();
}
}

await DesktopTools.ensureInitialized(
DesktopWindowOptions(
Expand Down Expand Up @@ -122,7 +122,7 @@ if (DesktopTools.platform.isAndroid) {
MatchedTrack.boxName,
path: hiveCacheDir,
);
await Hive.openLazyBox<List<SkipSegment>>(
await Hive.openLazyBox(
SkipSegment.boxName,
path: hiveCacheDir,
);
Expand Down
3 changes: 1 addition & 2 deletions lib/models/skip_segment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class SkipSegment {

static String version = 'v1';
static final boxName = "oss.krtirtho.spotube.skip_segments.$version";
static LazyBox<List<SkipSegment>> get box =>
Hive.lazyBox<List<SkipSegment>>(boxName);
static LazyBox get box => Hive.lazyBox(boxName);

SkipSegment.fromJson(Map<String, dynamic> json)
: start = json['start'],
Expand Down
9 changes: 5 additions & 4 deletions lib/provider/proxy_playlist/next_fetcher_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ mixin NextFetcher on StateNotifier<ProxyPlaylist> {
}

List<Track> mapSourcesToTracks(List<String> sources) {
final tracks = state.tracks;

return sources
.map((source) {
final track = tracks.firstWhereOrNull(
(track) => makeAppropriateSource(track) == source,
final track = state.tracks.firstWhereOrNull(
(track) {
final newSource = makeAppropriateSource(track);
return newSource == source;
},
);
return track;
})
Expand Down
71 changes: 36 additions & 35 deletions lib/provider/proxy_playlist/proxy_playlist_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
notificationService = await AudioServices.create(ref, this);

({String source, List<SkipSegment> segments})? currentSegments;
bool isFetchingSegments = false;

audioPlayer.activeSourceChangedStream.listen((newActiveSource) async {
final newActiveTrack =
mapSourcesToTracks([newActiveSource]).firstOrNull;
Expand All @@ -87,10 +87,6 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
.indexWhere((element) => element.id == newActiveTrack.id),
);

isFetchingSegments = true;

isFetchingSegments = false;

updatePalette();
});

Expand All @@ -114,7 +110,8 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
listenTo2Percent(int percent) async {
if (isPreSearching ||
audioPlayer.currentSource == null ||
audioPlayer.nextSource == null) return;
audioPlayer.nextSource == null ||
isPlayable(audioPlayer.nextSource!)) return;

try {
isPreSearching = true;
Expand All @@ -125,19 +122,6 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>

if (track != null) {
state = state.copyWith(tracks: mergeTracks([track], state.tracks));
if (currentSegments == null ||
(oldTrack?.id != null &&
currentSegments!.source != oldTrack!.id!) &&
!isFetchingSegments) {
isFetchingSegments = true;
currentSegments = (
source: audioPlayer.currentSource!,
segments: await getAndCacheSkipSegments(
track.ytTrack.id,
),
);
isFetchingSegments = false;
}
}

if (oldTrack != null && track != null) {
Expand All @@ -148,29 +132,34 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
}
} finally {
isPreSearching = false;
if (percent > 98 && !audioPlayer.isPlaying) {
await audioPlayer.resume();
}
}
}

audioPlayer.percentCompletedStream(2).listen(listenTo2Percent);

bool isFetchingSegments = false;

audioPlayer.positionStream.listen((position) async {
if (preferences.searchMode == SearchMode.youtubeMusic ||
if ((preferences.youtubeApiType == YoutubeApiType.piped &&
preferences.searchMode == SearchMode.youtubeMusic) ||
!preferences.skipNonMusic) return;

final notSameSegmentId =
currentSegments?.source != audioPlayer.currentSource;

if (currentSegments == null ||
currentSegments!.source != state.activeTrack!.id! &&
!isFetchingSegments) {
(notSameSegmentId && !isFetchingSegments)) {
isFetchingSegments = true;
currentSegments = (
source: audioPlayer.currentSource!,
segments: await getAndCacheSkipSegments(
(state.activeTrack as SpotubeTrack).ytTrack.id,
),
);
isFetchingSegments = false;
try {
currentSegments = (
source: audioPlayer.currentSource!,
segments: await getAndCacheSkipSegments(
(state.activeTrack as SpotubeTrack).ytTrack.id,
),
);
} finally {
isFetchingSegments = false;
}
}

final (source: _, :segments) = currentSegments!;
Expand Down Expand Up @@ -354,6 +343,10 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
}

Future<void> addTracksAtFirst(Iterable<Track> tracks) async {
if (state.tracks.length == 1) {
return addTracks(tracks);
}

tracks = blacklist.filter(tracks).toList() as List<Track>;
final destIndex = state.active != null ? state.active! + 1 : 0;
final newTracks = state.tracks.toList()..insertAll(destIndex, tracks);
Expand Down Expand Up @@ -492,12 +485,15 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>

Future<List<SkipSegment>> getAndCacheSkipSegments(String id) async {
if (!preferences.skipNonMusic ||
preferences.searchMode != SearchMode.youtube) return [];
(preferences.youtubeApiType == YoutubeApiType.piped &&
preferences.searchMode == SearchMode.youtubeMusic)) return [];

try {
final cached = await SkipSegment.box.get(id);
if (cached != null && cached.isNotEmpty) {
return List.castFrom<dynamic, SkipSegment>(cached);
return List.castFrom<dynamic, SkipSegment>(
(cached as List).map((json) => SkipSegment.fromJson(json)).toList(),
);
}

final res = await get(Uri(
Expand All @@ -519,6 +515,11 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
));

if (res.body == "Not Found") {
Catcher.reportCheckedError(
"[SponsorBlock] no skip segments found for $id\n"
"${res.request?.url}",
StackTrace.current,
);
return List.castFrom<dynamic, SkipSegment>([]);
}

Expand All @@ -537,7 +538,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>

await SkipSegment.box.put(
id,
segments,
segments.map((e) => e.toJson()).toList(),
);
return List.castFrom<dynamic, SkipSegment>(segments);
} catch (e, stack) {
Expand Down
3 changes: 2 additions & 1 deletion lib/services/audio_player/mk_state_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ class MkPlayerWithState extends Player {
FutureOr<void> insert(int index, Media media) {
if (_playlist == null ||
index < 0 ||
index > _playlist!.medias.length - 1) {
(_playlist!.medias.length > 1 &&
index > _playlist!.medias.length - 1)) {
return null;
}

Expand Down

0 comments on commit dfd60bd

Please sign in to comment.