Skip to content

Commit d32490a

Browse files
committed
Create sub-package and default interval for DebounceSaver & sort playlists in db
1 parent 6526ff1 commit d32490a

8 files changed

Lines changed: 35 additions & 17 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/playlist/PlaylistLocalItem.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public interface PlaylistLocalItem extends LocalItem {
1616
static List<PlaylistLocalItem> merge(
1717
final List<PlaylistMetadataEntry> localPlaylists,
1818
final List<PlaylistRemoteEntity> remotePlaylists) {
19+
// The playlists from the database must be in the display index order.
1920

2021
// Merge localPlaylists and remotePlaylists by display index.
2122
// If two items have the same display index, sort them in CASE_INSENSITIVE_ORDER.
@@ -25,11 +26,6 @@ static List<PlaylistLocalItem> merge(
2526
localPlaylists.size() + remotePlaylists.size());
2627
final List<PlaylistLocalItem> itemsWithSameIndex = new ArrayList<>();
2728

28-
// The data from database may not be in the display index order
29-
Collections.sort(localPlaylists,
30-
Comparator.comparingLong(PlaylistMetadataEntry::getDisplayIndex));
31-
Collections.sort(remotePlaylists,
32-
Comparator.comparingLong(PlaylistRemoteEntity::getDisplayIndex));
3329
int i = 0;
3430
int j = 0;
3531
while (i < localPlaylists.size()) {

app/src/main/java/org/schabi/newpipe/database/playlist/dao/PlaylistRemoteDAO.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import io.reactivex.rxjava3.core.Flowable;
1313

14+
import static org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity.REMOTE_PLAYLIST_DISPLAY_INDEX;
1415
import static org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity.REMOTE_PLAYLIST_ID;
1516
import static org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity.REMOTE_PLAYLIST_SERVICE_ID;
1617
import static org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity.REMOTE_PLAYLIST_TABLE;
@@ -39,6 +40,10 @@ public interface PlaylistRemoteDAO extends BasicDAO<PlaylistRemoteEntity> {
3940
+ REMOTE_PLAYLIST_URL + " = :url AND " + REMOTE_PLAYLIST_SERVICE_ID + " = :serviceId")
4041
Flowable<List<PlaylistRemoteEntity>> getPlaylist(long serviceId, String url);
4142

43+
@Query("SELECT * FROM " + REMOTE_PLAYLIST_TABLE
44+
+ " ORDER BY " + REMOTE_PLAYLIST_DISPLAY_INDEX)
45+
Flowable<List<PlaylistRemoteEntity>> getDisplayIndexOrderedPlaylists();
46+
4247
@Query("SELECT " + REMOTE_PLAYLIST_ID + " FROM " + REMOTE_PLAYLIST_TABLE
4348
+ " WHERE " + REMOTE_PLAYLIST_URL + " = :url "
4449
+ "AND " + REMOTE_PLAYLIST_SERVICE_ID + " = :serviceId")

app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
import org.schabi.newpipe.local.holder.RemoteBookmarkPlaylistItemHolder;
3636
import org.schabi.newpipe.local.playlist.LocalPlaylistManager;
3737
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
38-
import org.schabi.newpipe.util.DebounceSavable;
39-
import org.schabi.newpipe.util.DebounceSaver;
38+
import org.schabi.newpipe.util.debounce.DebounceSavable;
39+
import org.schabi.newpipe.util.debounce.DebounceSaver;
4040
import org.schabi.newpipe.util.NavigationHelper;
4141
import org.schabi.newpipe.util.OnClickGesture;
4242

@@ -89,7 +89,7 @@ public void onCreate(final Bundle savedInstanceState) {
8989
disposables = new CompositeDisposable();
9090

9191
isLoadingComplete = new AtomicBoolean();
92-
debounceSaver = new DebounceSaver(10000, this);
92+
debounceSaver = new DebounceSaver(this);
9393

9494
displayIndexInDatabase = new HashMap<>();
9595
}
@@ -185,8 +185,8 @@ public void startLoading(final boolean forceLoad) {
185185
}
186186
isLoadingComplete.set(false);
187187

188-
Flowable.combineLatest(localPlaylistManager.getPlaylists(),
189-
remotePlaylistManager.getPlaylists(), PlaylistLocalItem::merge)
188+
Flowable.combineLatest(localPlaylistManager.getDisplayIndexOrderedPlaylists(),
189+
remotePlaylistManager.getDisplayIndexOrderedPlaylists(), PlaylistLocalItem::merge)
190190
.onBackpressureLatest()
191191
.observeOn(AndroidSchedulers.mainThread())
192192
.subscribe(getPlaylistsSubscriber());

app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
import org.schabi.newpipe.player.MainPlayer.PlayerType;
4747
import org.schabi.newpipe.player.playqueue.PlayQueue;
4848
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
49-
import org.schabi.newpipe.util.DebounceSavable;
50-
import org.schabi.newpipe.util.DebounceSaver;
49+
import org.schabi.newpipe.util.debounce.DebounceSavable;
50+
import org.schabi.newpipe.util.debounce.DebounceSaver;
5151
import org.schabi.newpipe.util.Localization;
5252
import org.schabi.newpipe.util.NavigationHelper;
5353
import org.schabi.newpipe.util.OnClickGesture;
@@ -112,7 +112,7 @@ public void onCreate(final Bundle savedInstanceState) {
112112
disposables = new CompositeDisposable();
113113

114114
isLoadingComplete = new AtomicBoolean();
115-
debounceSaver = new DebounceSaver(10000, this);
115+
debounceSaver = new DebounceSaver(this);
116116
}
117117

118118
@Override

app/src/main/java/org/schabi/newpipe/local/playlist/RemotePlaylistManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public Flowable<List<PlaylistRemoteEntity>> getPlaylists() {
2626
return playlistRemoteTable.getAll().subscribeOn(Schedulers.io());
2727
}
2828

29+
public Flowable<List<PlaylistRemoteEntity>> getDisplayIndexOrderedPlaylists() {
30+
return playlistRemoteTable.getDisplayIndexOrderedPlaylists().subscribeOn(Schedulers.io());
31+
}
32+
2933
public Flowable<List<PlaylistRemoteEntity>> getPlaylist(final PlaylistInfo info) {
3034
return playlistRemoteTable.getPlaylist(info.getServiceId(), info.getUrl())
3135
.subscribeOn(Schedulers.io());

app/src/main/java/org/schabi/newpipe/settings/SelectPlaylistFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ private void loadPlaylists() {
9090
final LocalPlaylistManager localPlaylistManager = new LocalPlaylistManager(database);
9191
final RemotePlaylistManager remotePlaylistManager = new RemotePlaylistManager(database);
9292

93-
disposable = Flowable.combineLatest(localPlaylistManager.getPlaylists(),
94-
remotePlaylistManager.getPlaylists(), PlaylistLocalItem::merge)
93+
disposable = Flowable.combineLatest(localPlaylistManager.getDisplayIndexOrderedPlaylists(),
94+
remotePlaylistManager.getDisplayIndexOrderedPlaylists(), PlaylistLocalItem::merge)
9595
.observeOn(AndroidSchedulers.mainThread())
9696
.subscribe(this::displayPlaylists, this::onError);
9797
}

app/src/main/java/org/schabi/newpipe/util/DebounceSavable.java renamed to app/src/main/java/org/schabi/newpipe/util/debounce/DebounceSavable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.schabi.newpipe.util;
1+
package org.schabi.newpipe.util.debounce;
22

33
import org.schabi.newpipe.error.ErrorInfo;
44

app/src/main/java/org/schabi/newpipe/util/DebounceSaver.java renamed to app/src/main/java/org/schabi/newpipe/util/debounce/DebounceSaver.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.schabi.newpipe.util;
1+
package org.schabi.newpipe.util.debounce;
22

33
import org.schabi.newpipe.error.ErrorInfo;
44
import org.schabi.newpipe.error.UserAction;
@@ -21,6 +21,9 @@ public class DebounceSaver {
2121
// Has the object been modified
2222
private final AtomicBoolean isModified;
2323

24+
// Default 10 seconds
25+
private static final long DEFAULT_SAVE_DEBOUNCE_MILLIS = 10000;
26+
2427

2528
/**
2629
* Creates a new {@code DebounceSaver}.
@@ -36,6 +39,16 @@ public DebounceSaver(final long saveDebounceMillis, final DebounceSavable deboun
3639
this.isModified = new AtomicBoolean();
3740
}
3841

42+
/**
43+
* Creates a new {@code DebounceSaver}. Save the object 10 seconds later after the last change
44+
* occurred.
45+
*
46+
* @param debounceSavable The object containing data to be saved.
47+
*/
48+
public DebounceSaver(final DebounceSavable debounceSavable) {
49+
this(DEFAULT_SAVE_DEBOUNCE_MILLIS, debounceSavable);
50+
}
51+
3952
public boolean getIsModified() {
4053
return isModified.get();
4154
}

0 commit comments

Comments
 (0)