Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

Commit

Permalink
Fixed an issue where playlists weren't updated, leading to crashes.
Browse files Browse the repository at this point in the history
The playlist observable doesn't seem to update when a change occurs (perhaps SqlBrite isn't being notified of change by the underlying content provider). Use our own ContentObserver to trigger updates. This ensures playlists are up-to-date, so the option to add to a previously deleted playlist no longer exists.
  • Loading branch information
timusus committed Apr 6, 2020
1 parent 5fbb005 commit b32d145
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.simplecity.amp_library.utils;

import android.annotation.SuppressLint;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
import com.annimon.stream.Optional;
import com.annimon.stream.Stream;
import com.annimon.stream.function.Predicate;
Expand All @@ -17,6 +23,8 @@
import com.squareup.sqlbrite2.SqlBrite;
import io.reactivex.Observable;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.Nullable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
Expand Down Expand Up @@ -67,6 +75,26 @@ public static DataManager getInstance() {

private DataManager() {

ShuttleApplication.getInstance()
.getContentResolver()
.registerContentObserver(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, true, new ContentObserver(new Handler(Looper.getMainLooper())) {
@Override
public void onChange(boolean selfChange) {
onChange(selfChange, null);
}

@SuppressLint("CheckResult")
@Override
public void onChange(boolean selfChange, @Nullable Uri uri) {
SqlBriteUtils.createObservableList(ShuttleApplication.getInstance(), Playlist::new, Playlist.getQuery())
.first(Collections.emptyList())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(
playlists -> playlistsRelay.accept(playlists),
throwable -> LogUtils.logException(TAG, "Failed to update playlist relay", throwable)
);
}
});
}

public Observable<List<Song>> getAllSongsRelay() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,14 @@ private static Completable createPlaylistMenu(SubMenu subMenu, boolean autoUpdat
.getPlaylistsRelay()
.take(autoUpdate ? Long.MAX_VALUE : 1)
.doOnNext(playlists -> {
subMenu.clear();
subMenu.add(0, MediaManager.Defs.NEW_PLAYLIST, 0, R.string.new_playlist);
for (Playlist playlist : playlists) {
final Intent intent = new Intent();
intent.putExtra(ARG_PLAYLIST, playlist);
subMenu.add(0, MediaManager.Defs.PLAYLIST_SELECTED, 0, playlist.name).setIntent(intent);
if (subMenu != null) {
subMenu.clear();
subMenu.add(0, MediaManager.Defs.NEW_PLAYLIST, 0, R.string.new_playlist);
for (Playlist playlist : playlists) {
final Intent intent = new Intent();
intent.putExtra(ARG_PLAYLIST, playlist);
subMenu.add(0, MediaManager.Defs.PLAYLIST_SELECTED, 0, playlist.name).setIntent(intent);
}
}
})
.ignoreElements()
Expand Down

0 comments on commit b32d145

Please sign in to comment.