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

Commit b32d145

Browse files
committed
Fixed an issue where playlists weren't updated, leading to crashes.
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.
1 parent 5fbb005 commit b32d145

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

app/src/main/java/com/simplecity/amp_library/utils/DataManager.java

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.simplecity.amp_library.utils;
22

3+
import android.annotation.SuppressLint;
4+
import android.database.ContentObserver;
5+
import android.net.Uri;
6+
import android.os.Handler;
7+
import android.os.Looper;
8+
import android.provider.MediaStore;
39
import com.annimon.stream.Optional;
410
import com.annimon.stream.Stream;
511
import com.annimon.stream.function.Predicate;
@@ -17,6 +23,8 @@
1723
import com.squareup.sqlbrite2.SqlBrite;
1824
import io.reactivex.Observable;
1925
import io.reactivex.ObservableTransformer;
26+
import io.reactivex.android.schedulers.AndroidSchedulers;
27+
import io.reactivex.annotations.Nullable;
2028
import io.reactivex.disposables.Disposable;
2129
import io.reactivex.schedulers.Schedulers;
2230
import java.util.ArrayList;
@@ -67,6 +75,26 @@ public static DataManager getInstance() {
6775

6876
private DataManager() {
6977

78+
ShuttleApplication.getInstance()
79+
.getContentResolver()
80+
.registerContentObserver(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, true, new ContentObserver(new Handler(Looper.getMainLooper())) {
81+
@Override
82+
public void onChange(boolean selfChange) {
83+
onChange(selfChange, null);
84+
}
85+
86+
@SuppressLint("CheckResult")
87+
@Override
88+
public void onChange(boolean selfChange, @Nullable Uri uri) {
89+
SqlBriteUtils.createObservableList(ShuttleApplication.getInstance(), Playlist::new, Playlist.getQuery())
90+
.first(Collections.emptyList())
91+
.subscribeOn(AndroidSchedulers.mainThread())
92+
.subscribe(
93+
playlists -> playlistsRelay.accept(playlists),
94+
throwable -> LogUtils.logException(TAG, "Failed to update playlist relay", throwable)
95+
);
96+
}
97+
});
7098
}
7199

72100
public Observable<List<Song>> getAllSongsRelay() {

app/src/main/java/com/simplecity/amp_library/utils/PlaylistUtils.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,14 @@ private static Completable createPlaylistMenu(SubMenu subMenu, boolean autoUpdat
237237
.getPlaylistsRelay()
238238
.take(autoUpdate ? Long.MAX_VALUE : 1)
239239
.doOnNext(playlists -> {
240-
subMenu.clear();
241-
subMenu.add(0, MediaManager.Defs.NEW_PLAYLIST, 0, R.string.new_playlist);
242-
for (Playlist playlist : playlists) {
243-
final Intent intent = new Intent();
244-
intent.putExtra(ARG_PLAYLIST, playlist);
245-
subMenu.add(0, MediaManager.Defs.PLAYLIST_SELECTED, 0, playlist.name).setIntent(intent);
240+
if (subMenu != null) {
241+
subMenu.clear();
242+
subMenu.add(0, MediaManager.Defs.NEW_PLAYLIST, 0, R.string.new_playlist);
243+
for (Playlist playlist : playlists) {
244+
final Intent intent = new Intent();
245+
intent.putExtra(ARG_PLAYLIST, playlist);
246+
subMenu.add(0, MediaManager.Defs.PLAYLIST_SELECTED, 0, playlist.name).setIntent(intent);
247+
}
246248
}
247249
})
248250
.ignoreElements()

0 commit comments

Comments
 (0)