Show streams states for local lists

This commit is contained in:
Vasiliy 2019-04-15 22:18:24 +03:00
parent 73be8cf074
commit a48cbc6971
No known key found for this signature in database
GPG Key ID: 9F74C4D2874D7523
15 changed files with 132 additions and 17 deletions

View File

@ -166,7 +166,7 @@ public class InfoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
historyRecordManager.loadStreamState(data)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(streamStateEntity -> {
addInfoItem(data, streamStateEntity);
addInfoItem(data, streamStateEntity[0]);
})
);
}

View File

@ -150,6 +150,7 @@ public abstract class BaseLocalListFragment<I, N> extends BaseStateFragment<I>
public void onDestroyView() {
super.onDestroyView();
itemsList = null;
itemListAdapter.dispose();
itemListAdapter = null;
}

View File

@ -8,6 +8,8 @@ import android.view.View;
import android.view.ViewGroup;
import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.stream.model.StreamStateEntity;
import org.schabi.newpipe.local.history.HistoryRecordManager;
import org.schabi.newpipe.local.holder.LocalItemHolder;
import org.schabi.newpipe.local.holder.LocalPlaylistGridItemHolder;
import org.schabi.newpipe.local.holder.LocalPlaylistItemHolder;
@ -25,6 +27,9 @@ import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
/*
* Created by Christian Schabesberger on 01.08.16.
*
@ -63,7 +68,10 @@ public class LocalItemListAdapter extends RecyclerView.Adapter<RecyclerView.View
private static final int REMOTE_PLAYLIST_GRID_HOLDER_TYPE = 0x2004;
private final LocalItemBuilder localItemBuilder;
private final HistoryRecordManager historyRecordManager;
private final ArrayList<LocalItem> localItems;
private final ArrayList<StreamStateEntity> states;
private final CompositeDisposable stateLoaders;
private final DateFormat dateFormat;
private boolean showFooter = false;
@ -73,9 +81,12 @@ public class LocalItemListAdapter extends RecyclerView.Adapter<RecyclerView.View
public LocalItemListAdapter(Activity activity) {
localItemBuilder = new LocalItemBuilder(activity);
historyRecordManager = new HistoryRecordManager(activity);
localItems = new ArrayList<>();
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,
Localization.getPreferredLocale(activity));
states = new ArrayList<>();
stateLoaders = new CompositeDisposable();
}
public void setSelectedListener(OnClickGesture<LocalItem> listener) {
@ -87,6 +98,15 @@ public class LocalItemListAdapter extends RecyclerView.Adapter<RecyclerView.View
}
public void addItems(List<? extends LocalItem> data) {
stateLoaders.add(
historyRecordManager.loadLocalStreamStateBatch(data)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(streamStateEntities ->
addItems(data, streamStateEntities))
);
}
private void addItems(List<? extends LocalItem> data, List<StreamStateEntity> streamStates) {
if (data != null) {
if (DEBUG) {
Log.d(TAG, "addItems() before > localItems.size() = " +
@ -95,6 +115,7 @@ public class LocalItemListAdapter extends RecyclerView.Adapter<RecyclerView.View
int offsetStart = sizeConsideringHeader();
localItems.addAll(data);
states.addAll(streamStates);
if (DEBUG) {
Log.d(TAG, "addItems() after > offsetStart = " + offsetStart +
@ -130,6 +151,7 @@ public class LocalItemListAdapter extends RecyclerView.Adapter<RecyclerView.View
if (actualFrom >= localItems.size() || actualTo >= localItems.size()) return false;
localItems.add(actualTo, localItems.remove(actualFrom));
states.add(actualTo, states.remove(actualFrom));
notifyItemMoved(fromAdapterPosition, toAdapterPosition);
return true;
}
@ -259,7 +281,7 @@ public class LocalItemListAdapter extends RecyclerView.Adapter<RecyclerView.View
// If header isn't null, offset the items by -1
if (header != null) position--;
((LocalItemHolder) holder).updateFromItem(localItems.get(position), dateFormat);
((LocalItemHolder) holder).updateFromItem(localItems.get(position), states.get(position), dateFormat);
} else if (holder instanceof HeaderFooterHolder && position == 0 && header != null) {
((HeaderFooterHolder) holder).view = header;
} else if (holder instanceof HeaderFooterHolder && position == sizeConsideringHeader()
@ -277,4 +299,8 @@ public class LocalItemListAdapter extends RecyclerView.Adapter<RecyclerView.View
}
};
}
public void dispose() {
stateLoaders.clear();
}
}

View File

@ -112,7 +112,10 @@ public final class PlaylistAppendDialog extends PlaylistDialog {
public void onDestroyView() {
super.onDestroyView();
if (playlistReactor != null) playlistReactor.dispose();
if (playlistAdapter != null) playlistAdapter.unsetSelectedListener();
if (playlistAdapter != null) {
playlistAdapter.dispose();
playlistAdapter.unsetSelectedListener();
}
playlistReactor = null;
playlistRecyclerView = null;

View File

@ -26,11 +26,14 @@ import android.support.annotation.NonNull;
import org.schabi.newpipe.NewPipeDatabase;
import org.schabi.newpipe.R;
import org.schabi.newpipe.database.AppDatabase;
import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.history.dao.SearchHistoryDAO;
import org.schabi.newpipe.database.history.dao.StreamHistoryDAO;
import org.schabi.newpipe.database.history.model.SearchHistoryEntry;
import org.schabi.newpipe.database.history.model.StreamHistoryEntity;
import org.schabi.newpipe.database.history.model.StreamHistoryEntry;
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
import org.schabi.newpipe.database.playlist.model.PlaylistStreamEntity;
import org.schabi.newpipe.database.stream.StreamStatisticsEntry;
import org.schabi.newpipe.database.stream.dao.StreamDAO;
import org.schabi.newpipe.database.stream.dao.StreamStateDAO;
@ -221,17 +224,17 @@ public class HistoryRecordManager {
})).subscribeOn(Schedulers.io());
}
public Single<StreamStateEntity> loadStreamState(final InfoItem info) {
public Single<StreamStateEntity[]> loadStreamState(final InfoItem info) {
return Single.fromCallable(() -> {
final List<StreamEntity> entities = streamTable.getStream(info.getServiceId(), info.getUrl()).blockingFirst();
if (entities.isEmpty()) {
return null;
return new StreamStateEntity[]{null};
}
final List<StreamStateEntity> states = streamStateTable.getState(entities.get(0).getUid()).blockingFirst();
if (states.isEmpty()) {
return null;
return new StreamStateEntity[]{null};
}
return states.get(0);
return new StreamStateEntity[]{states.get(0)};
}).subscribeOn(Schedulers.io());
}
@ -255,6 +258,32 @@ public class HistoryRecordManager {
}).subscribeOn(Schedulers.io());
}
public Single<List<StreamStateEntity>> loadLocalStreamStateBatch(final List<? extends LocalItem> items) {
return Single.fromCallable(() -> {
final List<StreamStateEntity> result = new ArrayList<>(items.size());
for (LocalItem item : items) {
long streamId;
if (item instanceof StreamStatisticsEntry) {
streamId = ((StreamStatisticsEntry) item).streamId;
} else if (item instanceof PlaylistStreamEntity) {
streamId = ((PlaylistStreamEntity) item).getStreamUid();
} else if (item instanceof PlaylistStreamEntry) {
streamId = ((PlaylistStreamEntry) item).streamId;
} else {
result.add(null);
continue;
}
final List<StreamStateEntity> states = streamStateTable.getState(streamId).blockingFirst();
if (states.isEmpty()) {
result.add(null);
continue;
}
result.add(states.get(0));
}
return result;
}).subscribeOn(Schedulers.io());
}
///////////////////////////////////////////////////////
// Utility
///////////////////////////////////////////////////////

View File

@ -1,10 +1,12 @@
package org.schabi.newpipe.local.holder;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.stream.model.StreamStateEntity;
import org.schabi.newpipe.local.LocalItemBuilder;
import java.text.DateFormat;
@ -38,5 +40,5 @@ public abstract class LocalItemHolder extends RecyclerView.ViewHolder {
this.itemBuilder = itemBuilder;
}
public abstract void updateFromItem(final LocalItem item, final DateFormat dateFormat);
public abstract void updateFromItem(final LocalItem item, @Nullable final StreamStateEntity state, final DateFormat dateFormat);
}

View File

@ -1,10 +1,12 @@
package org.schabi.newpipe.local.holder;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
import org.schabi.newpipe.database.stream.model.StreamStateEntity;
import org.schabi.newpipe.local.LocalItemBuilder;
import org.schabi.newpipe.util.ImageDisplayConstants;
@ -21,7 +23,7 @@ public class LocalPlaylistItemHolder extends PlaylistItemHolder {
}
@Override
public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) {
public void updateFromItem(final LocalItem localItem, @Nullable final StreamStateEntity state, final DateFormat dateFormat) {
if (!(localItem instanceof PlaylistMetadataEntry)) return;
final PlaylistMetadataEntry item = (PlaylistMetadataEntry) localItem;
@ -32,6 +34,6 @@ public class LocalPlaylistItemHolder extends PlaylistItemHolder {
itemBuilder.displayImage(item.thumbnailUrl, itemThumbnailView,
ImageDisplayConstants.DISPLAY_PLAYLIST_OPTIONS);
super.updateFromItem(localItem, dateFormat);
super.updateFromItem(localItem, state, dateFormat);
}
}

View File

@ -1,21 +1,25 @@
package org.schabi.newpipe.local.holder;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.schabi.newpipe.R;
import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
import org.schabi.newpipe.database.stream.model.StreamStateEntity;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.local.LocalItemBuilder;
import org.schabi.newpipe.util.ImageDisplayConstants;
import org.schabi.newpipe.util.Localization;
import java.text.DateFormat;
import java.util.concurrent.TimeUnit;
public class LocalPlaylistStreamItemHolder extends LocalItemHolder {
@ -24,6 +28,7 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder {
public final TextView itemAdditionalDetailsView;
public final TextView itemDurationView;
public final View itemHandleView;
public final ProgressBar itemProgressView;
LocalPlaylistStreamItemHolder(LocalItemBuilder infoItemBuilder, int layoutId, ViewGroup parent) {
super(infoItemBuilder, layoutId, parent);
@ -33,6 +38,7 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder {
itemAdditionalDetailsView = itemView.findViewById(R.id.itemAdditionalDetails);
itemDurationView = itemView.findViewById(R.id.itemDurationView);
itemHandleView = itemView.findViewById(R.id.itemHandle);
itemProgressView = itemView.findViewById(R.id.itemProgressView);
}
public LocalPlaylistStreamItemHolder(LocalItemBuilder infoItemBuilder, ViewGroup parent) {
@ -40,7 +46,7 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder {
}
@Override
public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) {
public void updateFromItem(final LocalItem localItem, @Nullable final StreamStateEntity state, final DateFormat dateFormat) {
if (!(localItem instanceof PlaylistStreamEntry)) return;
final PlaylistStreamEntry item = (PlaylistStreamEntry) localItem;
@ -53,6 +59,13 @@ public class LocalPlaylistStreamItemHolder extends LocalItemHolder {
itemDurationView.setBackgroundColor(ContextCompat.getColor(itemBuilder.getContext(),
R.color.duration_background_color));
itemDurationView.setVisibility(View.VISIBLE);
if (state != null) {
itemProgressView.setVisibility(View.VISIBLE);
itemProgressView.setMax((int) item.duration);
itemProgressView.setProgress((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime()));
} else {
itemProgressView.setVisibility(View.GONE);
}
} else {
itemDurationView.setVisibility(View.GONE);
}

View File

@ -5,17 +5,20 @@ import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.schabi.newpipe.R;
import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.stream.StreamStatisticsEntry;
import org.schabi.newpipe.database.stream.model.StreamStateEntity;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.local.LocalItemBuilder;
import org.schabi.newpipe.util.ImageDisplayConstants;
import org.schabi.newpipe.util.Localization;
import java.text.DateFormat;
import java.util.concurrent.TimeUnit;
/*
* Created by Christian Schabesberger on 01.08.16.
@ -45,6 +48,7 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder {
public final TextView itemDurationView;
@Nullable
public final TextView itemAdditionalDetails;
public final ProgressBar itemProgressView;
public LocalStatisticStreamItemHolder(LocalItemBuilder itemBuilder, ViewGroup parent) {
this(itemBuilder, R.layout.list_stream_item, parent);
@ -58,6 +62,7 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder {
itemUploaderView = itemView.findViewById(R.id.itemUploaderView);
itemDurationView = itemView.findViewById(R.id.itemDurationView);
itemAdditionalDetails = itemView.findViewById(R.id.itemAdditionalDetails);
itemProgressView = itemView.findViewById(R.id.itemProgressView);
}
private String getStreamInfoDetailLine(final StreamStatisticsEntry entry,
@ -70,7 +75,7 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder {
}
@Override
public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) {
public void updateFromItem(final LocalItem localItem, @Nullable final StreamStateEntity state, final DateFormat dateFormat) {
if (!(localItem instanceof StreamStatisticsEntry)) return;
final StreamStatisticsEntry item = (StreamStatisticsEntry) localItem;
@ -82,8 +87,16 @@ public class LocalStatisticStreamItemHolder extends LocalItemHolder {
itemDurationView.setBackgroundColor(ContextCompat.getColor(itemBuilder.getContext(),
R.color.duration_background_color));
itemDurationView.setVisibility(View.VISIBLE);
if (state != null) {
itemProgressView.setVisibility(View.VISIBLE);
itemProgressView.setMax((int) item.duration);
itemProgressView.setProgress((int) TimeUnit.MILLISECONDS.toSeconds(state.getProgressTime()));
} else {
itemProgressView.setVisibility(View.GONE);
}
} else {
itemDurationView.setVisibility(View.GONE);
itemProgressView.setVisibility(View.GONE);
}
if (itemAdditionalDetails != null) {

View File

@ -1,11 +1,13 @@
package org.schabi.newpipe.local.holder;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.schabi.newpipe.R;
import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.stream.model.StreamStateEntity;
import org.schabi.newpipe.local.LocalItemBuilder;
import java.text.DateFormat;
@ -31,7 +33,7 @@ public abstract class PlaylistItemHolder extends LocalItemHolder {
}
@Override
public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) {
public void updateFromItem(final LocalItem localItem, @Nullable final StreamStateEntity state, final DateFormat dateFormat) {
itemView.setOnClickListener(view -> {
if (itemBuilder.getOnItemSelectedListener() != null) {
itemBuilder.getOnItemSelectedListener().selected(localItem);

View File

@ -1,9 +1,11 @@
package org.schabi.newpipe.local.holder;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import org.schabi.newpipe.database.LocalItem;
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity;
import org.schabi.newpipe.database.stream.model.StreamStateEntity;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.local.LocalItemBuilder;
import org.schabi.newpipe.util.ImageDisplayConstants;
@ -21,7 +23,7 @@ public class RemotePlaylistItemHolder extends PlaylistItemHolder {
}
@Override
public void updateFromItem(final LocalItem localItem, final DateFormat dateFormat) {
public void updateFromItem(final LocalItem localItem, @Nullable final StreamStateEntity state, final DateFormat dateFormat) {
if (!(localItem instanceof PlaylistRemoteEntity)) return;
final PlaylistRemoteEntity item = (PlaylistRemoteEntity) localItem;
@ -33,6 +35,6 @@ public class RemotePlaylistItemHolder extends PlaylistItemHolder {
itemBuilder.displayImage(item.getThumbnailUrl(), itemThumbnailView,
ImageDisplayConstants.DISPLAY_PLAYLIST_OPTIONS);
super.updateFromItem(localItem, dateFormat);
super.updateFromItem(localItem, state, dateFormat);
}
}

View File

@ -81,4 +81,15 @@
android:textSize="@dimen/video_item_search_uploader_text_size"
tools:text="Uploader" />
<ProgressBar
android:id="@+id/itemProgressView"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:progressDrawable="?progress_horizontal_drawable"
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_marginTop="-2dp"
android:layout_alignStart="@id/itemThumbnailView"
android:layout_alignEnd="@id/itemThumbnailView"
android:layout_below="@id/itemThumbnailView"/>
</RelativeLayout>

View File

@ -83,4 +83,15 @@
android:textSize="@dimen/video_item_search_uploader_text_size"
tools:text="Uploader" />
<ProgressBar
android:id="@+id/itemProgressView"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:progressDrawable="?progress_horizontal_drawable"
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_marginTop="-2dp"
android:layout_alignStart="@id/itemThumbnailView"
android:layout_alignEnd="@id/itemThumbnailView"
android:layout_below="@id/itemThumbnailView"/>
</RelativeLayout>

View File

@ -14,7 +14,7 @@
<dimen name="video_item_search_thumbnail_image_width">142dp</dimen>
<dimen name="video_item_search_thumbnail_image_height">80dp</dimen>
<!-- Calculated: 2*video_item_search_padding + video_item_search_thumbnail_image_height -->
<dimen name="video_item_search_height">104dp</dimen>
<dimen name="video_item_search_height">106dp</dimen>
<!-- Paddings & Margins -->
<dimen name="video_item_search_image_right_margin">10dp</dimen>
<dimen name="video_item_search_duration_vertical_padding">1sp</dimen>

View File

@ -15,7 +15,7 @@
<dimen name="video_item_grid_thumbnail_image_width">164dp</dimen>
<dimen name="video_item_grid_thumbnail_image_height">92dp</dimen>
<!-- Calculated: 2*video_item_search_padding + video_item_search_thumbnail_image_height -->
<dimen name="video_item_search_height">94dp</dimen>
<dimen name="video_item_search_height">96dp</dimen>
<!-- Paddings & Margins -->
<dimen name="video_item_search_padding">12dp</dimen>
<dimen name="video_item_search_image_right_margin">6dp</dimen>