1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-24 00:50:32 +00:00

Update states in lists

This commit is contained in:
Vasiliy 2019-04-27 18:01:18 +03:00
parent 03b1a8bd41
commit 41fb6f5464
No known key found for this signature in database
GPG Key ID: 9F74C4D2874D7523
5 changed files with 58 additions and 0 deletions

View File

@ -4,6 +4,7 @@ package org.schabi.newpipe.database.stream.model;
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.ForeignKey;
import android.support.annotation.Nullable;
import java.util.concurrent.TimeUnit;
@ -62,4 +63,12 @@ public class StreamStateEntity {
return seconds > PLAYBACK_SAVE_THRESHOLD_START_SECONDS
&& seconds < durationInSeconds - PLAYBACK_SAVE_THRESHOLD_END_SECONDS;
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof StreamStateEntity) {
return ((StreamStateEntity) obj).streamUid == streamUid
&& ((StreamStateEntity) obj).progressTime == progressTime;
} else return false;
}
}

View File

@ -100,6 +100,8 @@ public abstract class BaseListFragment<I, N> extends BaseStateFragment<I> implem
}
updateFlags = 0;
}
infoListAdapter.updateStates();
}
/*//////////////////////////////////////////////////////////////////////////

View File

@ -31,6 +31,7 @@ import org.schabi.newpipe.util.OnClickGesture;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
@ -195,6 +196,29 @@ public class InfoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
}
}
public void updateStates() {
if (infoItemList.isEmpty()) {
return;
}
stateLoaders.add(
historyRecordManager.loadStreamStateBatch(infoItemList)
.observeOn(AndroidSchedulers.mainThread())
.subscribe((streamStateEntities) -> {
if (streamStateEntities.size() == states.size()) {
for (int i = 0; i < states.size(); i++) {
final StreamStateEntity newState = streamStateEntities.get(i);
if (!Objects.equals(states.get(i), newState)) {
states.set(i, newState);
notifyItemChanged(header == null ? i : i + 1);
}
}
} else {
//oops, something is wrong
}
})
);
}
public void clearStreamItemList() {
if (infoItemList.isEmpty()) {
return;

View File

@ -76,6 +76,7 @@ public abstract class BaseLocalListFragment<I, N> extends BaseStateFragment<I>
}
updateFlags = 0;
}
itemListAdapter.updateStates();
}
/*//////////////////////////////////////////////////////////////////////////

View File

@ -26,6 +26,7 @@ import org.schabi.newpipe.util.OnClickGesture;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
@ -136,6 +137,27 @@ public class LocalItemListAdapter extends RecyclerView.Adapter<RecyclerView.View
}
}
public void updateStates() {
if (localItems.isEmpty()) return;
stateLoaders.add(
historyRecordManager.loadLocalStreamStateBatch(localItems)
.observeOn(AndroidSchedulers.mainThread())
.subscribe((streamStateEntities) -> {
if (streamStateEntities.size() == states.size()) {
for (int i = 0; i < states.size(); i++) {
final StreamStateEntity newState = streamStateEntities.get(i);
if (!Objects.equals(states.get(i), newState)) {
states.set(i, newState);
notifyItemChanged(header == null ? i : i + 1);
}
}
} else {
//oops, something is wrong
}
})
);
}
public void removeItem(final LocalItem data) {
final int index = localItems.indexOf(data);