2017-08-29 00:38:37 +00:00
|
|
|
package org.schabi.newpipe.playlist;
|
|
|
|
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.R;
|
2017-09-03 02:30:34 +00:00
|
|
|
import org.schabi.newpipe.playlist.events.PlayQueueMessage;
|
2017-08-29 00:38:37 +00:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-09-03 02:30:34 +00:00
|
|
|
import io.reactivex.Observer;
|
|
|
|
import io.reactivex.annotations.NonNull;
|
2017-08-31 17:07:18 +00:00
|
|
|
import io.reactivex.disposables.Disposable;
|
|
|
|
|
2017-08-29 00:38:37 +00:00
|
|
|
/**
|
|
|
|
* Created by Christian Schabesberger on 01.08.16.
|
|
|
|
*
|
|
|
|
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
|
|
|
|
* InfoListAdapter.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-08-29 15:00:11 +00:00
|
|
|
public class PlayQueueAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
|
|
private static final String TAG = PlayQueueAdapter.class.toString();
|
2017-08-29 00:38:37 +00:00
|
|
|
|
2017-08-31 17:07:18 +00:00
|
|
|
private final PlayQueueItemBuilder playQueueItemBuilder;
|
2017-08-29 15:00:11 +00:00
|
|
|
private final PlayQueue playQueue;
|
2017-08-29 00:38:37 +00:00
|
|
|
private boolean showFooter = false;
|
|
|
|
private View header = null;
|
|
|
|
private View footer = null;
|
|
|
|
|
2017-08-31 17:07:18 +00:00
|
|
|
private Disposable playQueueReactor;
|
|
|
|
|
2017-08-29 00:38:37 +00:00
|
|
|
public class HFHolder extends RecyclerView.ViewHolder {
|
|
|
|
public HFHolder(View v) {
|
|
|
|
super(v);
|
|
|
|
view = v;
|
|
|
|
}
|
|
|
|
public View view;
|
|
|
|
}
|
|
|
|
|
2017-08-31 17:07:18 +00:00
|
|
|
public void showFooter(final boolean show) {
|
2017-08-29 00:38:37 +00:00
|
|
|
showFooter = show;
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2017-08-31 17:07:18 +00:00
|
|
|
public PlayQueueAdapter(final PlayQueue playQueue) {
|
|
|
|
this.playQueueItemBuilder = new PlayQueueItemBuilder();
|
2017-08-29 15:00:11 +00:00
|
|
|
this.playQueue = playQueue;
|
2017-08-29 00:38:37 +00:00
|
|
|
|
2017-09-03 02:30:34 +00:00
|
|
|
startReactor();
|
2017-08-29 00:38:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 17:07:18 +00:00
|
|
|
public void setSelectedListener(final PlayQueueItemBuilder.OnSelectedListener listener) {
|
|
|
|
playQueueItemBuilder.setOnSelectedListener(listener);
|
2017-08-29 00:38:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 17:07:18 +00:00
|
|
|
public void add(final List<PlayQueueItem> data) {
|
|
|
|
playQueue.append(data);
|
2017-08-29 00:38:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 17:07:18 +00:00
|
|
|
public void add(final PlayQueueItem data) {
|
|
|
|
playQueue.append(data);
|
2017-08-29 15:00:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 17:07:18 +00:00
|
|
|
public void remove(final int index) {
|
|
|
|
playQueue.remove(index);
|
|
|
|
}
|
2017-08-29 15:00:11 +00:00
|
|
|
|
2017-09-03 02:30:34 +00:00
|
|
|
private void startReactor() {
|
|
|
|
final Observer<PlayQueueMessage> observer = new Observer<PlayQueueMessage>() {
|
2017-08-31 17:07:18 +00:00
|
|
|
@Override
|
2017-09-03 02:30:34 +00:00
|
|
|
public void onSubscribe(@NonNull Disposable d) {
|
|
|
|
if (playQueueReactor != null) playQueueReactor.dispose();
|
|
|
|
playQueueReactor = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNext(@NonNull PlayQueueMessage playQueueMessage) {
|
2017-08-31 17:07:18 +00:00
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
2017-09-03 02:30:34 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(@NonNull Throwable e) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onComplete() {
|
|
|
|
dispose();
|
|
|
|
}
|
2017-08-31 17:07:18 +00:00
|
|
|
};
|
|
|
|
|
2017-09-03 02:30:34 +00:00
|
|
|
playQueue.getBroadcastReceiver()
|
2017-08-31 17:07:18 +00:00
|
|
|
.toObservable()
|
2017-09-03 02:30:34 +00:00
|
|
|
.subscribe(observer);
|
2017-08-29 15:00:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 17:07:18 +00:00
|
|
|
public void dispose() {
|
|
|
|
if (playQueueReactor != null) playQueueReactor.dispose();
|
2017-09-03 02:30:34 +00:00
|
|
|
playQueueReactor = null;
|
2017-08-29 00:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setHeader(View header) {
|
|
|
|
this.header = header;
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFooter(View footer) {
|
|
|
|
this.footer = footer;
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2017-08-29 15:00:11 +00:00
|
|
|
public List<PlayQueueItem> getItems() {
|
|
|
|
return playQueue.getStreams();
|
2017-08-29 00:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
2017-08-29 15:00:11 +00:00
|
|
|
int count = playQueue.getStreams().size();
|
2017-08-29 00:38:37 +00:00
|
|
|
if(header != null) count++;
|
|
|
|
if(footer != null && showFooter) count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't ask why we have to do that this way... it's android accept it -.-
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
if(header != null && position == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if(header != null) {
|
|
|
|
position--;
|
|
|
|
}
|
2017-08-29 15:00:11 +00:00
|
|
|
if(footer != null && position == playQueue.getStreams().size() && showFooter) {
|
2017-08-29 00:38:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
|
|
|
|
switch(type) {
|
|
|
|
case 0:
|
|
|
|
return new HFHolder(header);
|
|
|
|
case 1:
|
|
|
|
return new HFHolder(footer);
|
|
|
|
case 2:
|
2017-09-04 17:23:56 +00:00
|
|
|
return new PlayQueueItemHolder(LayoutInflater.from(parent.getContext())
|
2017-08-31 17:07:18 +00:00
|
|
|
.inflate(R.layout.play_queue_item, parent, false));
|
2017-08-29 00:38:37 +00:00
|
|
|
default:
|
|
|
|
Log.e(TAG, "Trollolo");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {
|
2017-08-29 15:00:11 +00:00
|
|
|
if(holder instanceof PlayQueueItemHolder) {
|
2017-08-29 00:38:37 +00:00
|
|
|
if(header != null) {
|
|
|
|
i--;
|
|
|
|
}
|
2017-08-31 17:07:18 +00:00
|
|
|
playQueueItemBuilder.buildStreamInfoItem((PlayQueueItemHolder) holder, playQueue.getStreams().get(i));
|
2017-08-29 00:38:37 +00:00
|
|
|
} else if(holder instanceof HFHolder && i == 0 && header != null) {
|
|
|
|
((HFHolder) holder).view = header;
|
2017-08-29 15:00:11 +00:00
|
|
|
} else if(holder instanceof HFHolder && i == playQueue.getStreams().size() && footer != null && showFooter) {
|
2017-08-29 00:38:37 +00:00
|
|
|
((HFHolder) holder).view = footer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|