Commit ee1d43e9 authored by Geoffrey Métais's avatar Geoffrey Métais

Fix playlist current track higlight

parent 569595c3
......@@ -306,8 +306,8 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, PlaybackSe
}
override fun playItem(position: Int, item: MediaWrapper) {
hideSearchField()
service?.playIndex(playlistModel.getItemPosition(position, item))
clearSearch()
service?.playIndex(playlistModel.getPlaylistPosition(position, item))
}
fun onTimeLabelClick(view: View) {
......@@ -471,19 +471,22 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, PlaybackSe
})
}
private val playlistObserver = Observer<MutableList<MediaWrapper>> {
playlistAdapter.update(it!!)
updateActor.offer(Unit)
}
override fun onConnected(service: PlaybackService) {
this.service = service
playlistModel = PlaylistModel.get(this, service).apply { setup() }
playlistModel.progress.observe(this, Observer { it?.let { updateProgress(it) } })
playlistModel.dataset.observe(this, Observer {
playlistAdapter.update(it!!)
updateActor.offer(Unit)
})
playlistAdapter.setService(service)
playlistModel.dataset.observe(this, playlistObserver)
playlistAdapter.setModel(playlistModel)
service.playlistManager.abRepeat.observe(this, abRepeatObserver)
}
override fun onDisconnected() {
playlistModel.dataset.removeObserver(playlistObserver)
playlistModel.onCleared()
service?.playlistManager?.abRepeat?.removeObserver(abRepeatObserver)
service = null
......@@ -574,7 +577,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, PlaybackSe
binding.header.setBackgroundResource(0)
setHeaderVisibilities(true, true, false, false, false, true, true)
showPlaylistTips()
service?.apply { playlistAdapter.currentIndex = currentMediaPosition }
service?.apply { playlistAdapter.currentIndex = playlistModel.selection }
}
// else -> binding.header.setBackgroundResource(0)
}
......
......@@ -39,7 +39,6 @@ import android.widget.Toast;
import org.jetbrains.annotations.NotNull;
import org.videolan.libvlc.util.AndroidUtil;
import org.videolan.medialibrary.media.MediaWrapper;
import org.videolan.vlc.PlaybackService;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.databinding.PlaylistItemBinding;
......@@ -49,6 +48,7 @@ import org.videolan.vlc.interfaces.SwipeDragHelperAdapter;
import org.videolan.vlc.media.MediaUtils;
import org.videolan.vlc.util.MediaItemDiffCallback;
import org.videolan.vlc.util.WeakHandler;
import org.videolan.vlc.viewmodels.PlaylistModel;
import java.util.Collections;
......@@ -56,10 +56,10 @@ public class PlaylistAdapter extends DiffUtilAdapter<MediaWrapper, PlaylistAdapt
private static final String TAG = "VLC/PlaylistAdapter";
private PlaybackService mService = null;
private PlaylistModel mModel = null;
private IPlayer mPlayer;
private int mCurrentIndex = 0;
private int mCurrentSelection = 0;
public interface IPlayer {
void onPopupMenu(View view, int position, MediaWrapper item);
......@@ -73,7 +73,7 @@ public class PlaylistAdapter extends DiffUtilAdapter<MediaWrapper, PlaylistAdapt
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
final View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.playlist_item, parent, false);
return new ViewHolder(v);
}
......@@ -84,7 +84,7 @@ public class PlaylistAdapter extends DiffUtilAdapter<MediaWrapper, PlaylistAdapt
final MediaWrapper media = getItem(position);
holder.binding.setMedia(media);
holder.binding.setSubTitle(MediaUtils.INSTANCE.getMediaSubtitle(media));
holder.binding.setTitleColor(mCurrentIndex == position
holder.binding.setTitleColor(mCurrentSelection == position
? UiTools.getColorFromAttribute(ctx, R.attr.list_title_last)
: UiTools.getColorFromAttribute(ctx, R.attr.list_title));
holder.binding.executePendingBindings();
......@@ -110,29 +110,28 @@ public class PlaylistAdapter extends DiffUtilAdapter<MediaWrapper, PlaylistAdapt
@Override
protected void onUpdateFinished() {
if (mService != null)
setCurrentIndex(mService.getCurrentMediaPosition());
if (mModel != null) setCurrentIndex(mModel.getSelection());
}
@MainThread
public void remove(int position) {
if (mService == null) return;
if (position == mCurrentIndex) mCurrentIndex = -1;
mService.remove(position);
if (mModel == null) return;
mModel.remove(position);
}
public int getCurrentIndex() {
return mCurrentIndex;
return mCurrentSelection;
}
public void setCurrentIndex(int position) {
if (position == mCurrentIndex || position < 0 || position >= getItemCount())
return;
int former = mCurrentIndex;
mCurrentIndex = position;
notifyItemChanged(former);
notifyItemChanged(position);
mPlayer.onSelectionSet(position);
if (position == mCurrentSelection || position >= getItemCount()) return;
int former = mCurrentSelection;
mCurrentSelection = position;
if (former >= 0) notifyItemChanged(former);
if (position >= 0) {
notifyItemChanged(position);
mPlayer.onSelectionSet(position);
}
}
@Override
......@@ -147,11 +146,11 @@ public class PlaylistAdapter extends DiffUtilAdapter<MediaWrapper, PlaylistAdapt
final MediaWrapper media = getItem(position);
String message = String.format(VLCApplication.getAppResources().getString(R.string.remove_playlist_item), media.getTitle());
if (mPlayer instanceof Fragment){
View v = ((Fragment) mPlayer).getView();
Runnable cancelAction = new Runnable() {
final View v = ((Fragment) mPlayer).getView();
final Runnable cancelAction = new Runnable() {
@Override
public void run() {
mService.insertItem(position, media);
mModel.insertMedia(position, media);
}
};
UiTools.snackerWithCancel(v, message, null, cancelAction);
......@@ -161,8 +160,8 @@ public class PlaylistAdapter extends DiffUtilAdapter<MediaWrapper, PlaylistAdapt
remove(position);
}
public void setService(PlaybackService service) {
mService = service;
public void setModel(PlaylistModel model) {
mModel = model;
}
public class ViewHolder extends RecyclerView.ViewHolder {
......@@ -210,18 +209,15 @@ public class PlaylistAdapter extends DiffUtilAdapter<MediaWrapper, PlaylistAdapt
switch (msg.what){
case ACTION_MOVE:
removeMessages(ACTION_MOVED);
if (from == -1)
from = msg.arg1;
if (from == -1) from = msg.arg1;
to = msg.arg2;
sendEmptyMessageDelayed(ACTION_MOVED, 1000);
break;
case ACTION_MOVED:
final PlaybackService service = getOwner().mService;
if (from != -1 && to != -1 && service == null)
return;
if (to > from)
++to;
service.moveItem(from, to);
final PlaylistModel model = getOwner().mModel;
if (from != -1 && to != -1 && model == null) return;
if (to > from) ++to;
model.move(from, to);
from = to = -1;
break;
}
......
......@@ -26,6 +26,7 @@ import android.app.Activity;
import android.app.KeyguardManager;
import android.app.PictureInPictureParams;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothHeadset;
import android.content.BroadcastReceiver;
......@@ -133,6 +134,7 @@ import org.videolan.vlc.util.Settings;
import org.videolan.vlc.util.Strings;
import org.videolan.vlc.util.SubtitlesDownloader;
import org.videolan.vlc.util.Util;
import org.videolan.vlc.viewmodels.PlaylistModel;
import java.lang.reflect.Method;
import java.util.ArrayList;
......@@ -170,6 +172,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
private ImageView mPlaylistToggle;
private RecyclerView mPlaylist;
private PlaylistAdapter mPlaylistAdapter;
private PlaylistModel mPlaylistModel;
private static final int SURFACE_BEST_FIT = 0;
static final int SURFACE_FIT_SCREEN = 1;
......@@ -710,7 +713,9 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
if (mService.hasPlaylist()) {
mHasPlaylist = true;
mPlaylistAdapter = new PlaylistAdapter(this);
mPlaylistAdapter.setService(mService);
mPlaylistModel = ViewModelProviders.of(this, new PlaylistModel.Factory(mService)).get(PlaylistModel.class);
mPlaylistModel.setup();
mPlaylistAdapter.setModel(mPlaylistModel);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mPlaylist.setLayoutManager(layoutManager);
......
......@@ -36,6 +36,10 @@ import org.videolan.vlc.util.PlaylistFilterDelegate
class PlaylistModel(private val service: PlaybackService) : ScopedModel(), PlaybackService.Callback by EmptyPBSCallback {
val dataset = LiveDataset<MediaWrapper>()
var originalDataset : MutableList<MediaWrapper>? = null
val selection : Int
get() = if (filtering) -1 else service.playlistManager.currentIndex
var filtering = false
val progress by lazy(LazyThreadSafetyMode.NONE) {
MediatorLiveData<PlaybackProgress>().apply {
addSource(service.playlistManager.player.progress) {
......@@ -55,16 +59,33 @@ class PlaylistModel(private val service: PlaybackService) : ScopedModel(), Playb
dataset.value = service.medias.toMutableList()
}
fun filter(query: CharSequence?) = launch { filter.filter(query) }
fun insertMedia(position: Int, media: MediaWrapper) = service.insertItem(position, media)
fun remove(position: Int) = service.remove(position)
fun move(from: Int, to: Int) = service.moveItem(from, to)
fun filter(query: CharSequence?) {
val filtering = query != null
if (this.filtering != filtering) {
this.filtering = filtering
originalDataset = if (filtering) dataset.value.toMutableList() else null
}
launch { filter.filter(query) }
}
public override fun onCleared() {
service.removeCallback(this)
}
fun getItemPosition(position: Int, media: MediaWrapper): Int {
val list = dataset.value
fun getPlaylistPosition(position: Int, media: MediaWrapper): Int {
val list = originalDataset ?: dataset.value
if (list[position] == media) return position
else for ((index, item) in list.withIndex()) if (item == media) return index
else {
for ((index, item) in list.withIndex()) if (item == media) {
return index
}
}
return -1
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment