Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 31b872af authored by 2bllw8's avatar 2bllw8 Committed by Joey
Browse files

Eleven: convert playlist songs list to RecyclerView

Change-Id: Ib6ff536822a26f237849858fd39efd570157778f
parent 2fcdb0c1
Loading
Loading
Loading
Loading
+25 −7
Original line number Diff line number Diff line
@@ -14,15 +14,33 @@
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        layout="@layout/list_base_padding" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/playlist_header"
            layout="@layout/playlist_detail_header" />

</RelativeLayout>
        <FrameLayout
            android:id="@+id/list_base_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/list_base"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fadingEdge="vertical"
                android:scrollbarStyle="outsideOverlay"
                android:scrollbars="vertical" />

            <include layout="@layout/loading_empty_container" />
        </FrameLayout>
    </LinearLayout>
</androidx.core.widget.NestedScrollView>
+22 −35
Original line number Diff line number Diff line
@@ -26,45 +26,32 @@
        android:contentDescription="@null"
        android:scaleType="centerCrop" />

    <FrameLayout
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="@dimen/playlist_detail_header_bottom_height"
        android:layout_alignParentBottom="true"
        android:background="@color/header_shadow_color">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        android:layout_gravity="center"
            android:gravity="center_vertical"
        android:background="@color/header_shadow_color"
        android:gravity="center"
        android:orientation="horizontal">

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:scaleType="centerInside"
                android:src="@drawable/playlist_icon" />

        <TextView
            android:id="@+id/number_of_songs_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
                android:paddingEnd="16dp"
            android:layout_marginEnd="16dp"
            android:textColor="@color/number_of_songs_text_color"
                android:textSize="@dimen/text_size_micro" />

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:scaleType="centerInside"
                android:src="@drawable/stopwatch_icon_white" />
            android:textSize="@dimen/text_size_micro"
            android:drawableStart="@drawable/playlist_icon"
            android:drawablePadding="4dp" />

        <TextView
            android:id="@+id/duration_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/duration_text_color"
                android:textSize="@dimen/text_size_micro" />
            android:textSize="@dimen/text_size_micro"
            android:drawableStart="@drawable/stopwatch_icon_white"
            android:drawablePadding="4dp"/>
    </LinearLayout>
    </FrameLayout>
</RelativeLayout>
+177 −58
Original line number Diff line number Diff line
@@ -17,18 +17,28 @@
 */
package org.lineageos.eleven.adapters;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import org.lineageos.eleven.Config;
import org.lineageos.eleven.cache.ImageFetcher;
import org.lineageos.eleven.model.Song;
import org.lineageos.eleven.service.MusicPlaybackTrack;
import org.lineageos.eleven.ui.MusicHolder;
import org.lineageos.eleven.utils.ElevenUtils;
import org.lineageos.eleven.utils.MusicUtils;
import org.lineageos.eleven.widgets.IPopupMenuCallback;

import java.util.Collection;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * This {@link ArrayAdapter} is used to display the songs for a particular playlist
@@ -36,96 +46,205 @@ import java.util.Collection;
 *
 * @author Andrew Neal (andrewdneal@gmail.com)
 */
public class ProfileSongAdapter extends SongAdapter {
public class ProfileSongAdapter extends RecyclerView.Adapter<MusicHolder> implements
        IPopupMenuCallback {

    private static final int NOTHING_PLAYING = -1;

    /**
     * The resource Id of the layout to inflate
     */
    private final int mLayoutId;

    /**
     * Image cache and image fetcher
     */
    private final ImageFetcher mImageFetcher;

    /**
     * Used to cache the song info
     */
    private List<Song> mSongs;

    /**
     * Instead of having random +1 and -1 sprinkled around, this variable will show what is really
     * related to the header
     * Used to listen to the pop up menu callbacks
     */
    public static final int NUM_HEADERS = 1;
    private IPopupMenuCallback.IListener mListener;

    /**
     * Fake header layout Id
     * Current music track
     */
    private final int mHeaderId;
    private MusicPlaybackTrack mCurrentlyPlayingTrack;

    /**
     * Source id and type
     */
    private final long mSourceId;
    private final Config.IdType mSourceType = Config.IdType.Playlist;

    private final Context mContext;
    private final Consumer<Integer> mOnItemClickListener;

    /**
     * Constructor of <code>ProfileSongAdapter</code>
     *
     * @param activity The {@link Activity} to use
     * @param context The {@link FragmentActivity} to use
     * @param layoutId The resource Id of the view to inflate.
     */
    public ProfileSongAdapter(final long playlistId, final FragmentActivity activity,
                              final int layoutId, final int headerId) {
        super(activity, layoutId, playlistId, Config.IdType.Playlist);
        // Cache the header
        mHeaderId = headerId;
    public ProfileSongAdapter(final long playlistId, final FragmentActivity context,
                              final int layoutId, final Consumer<Integer> onItemClickListener) {
        mContext = context;
        // Get the layout Id
        mLayoutId = layoutId;
        // Initialize the cache & image fetcher
        mImageFetcher = ElevenUtils.getImageFetcher(context);
        // set the source id and type
        mSourceId = playlistId;
        mOnItemClickListener = onItemClickListener;
        mSongs = new ArrayList<>(0);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        // Return a faux header at position 0
        if (position == 0) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(mHeaderId, parent, false);
    /**
     * Determines whether the song at the position should show the currently playing indicator
     *
     * @param song     the song in question
     * @return true if we want to show the indicator
     */
    protected boolean showNowPlayingIndicator(final Song song) {
        return mCurrentlyPlayingTrack != null
                && mCurrentlyPlayingTrack.mSourceId == mSourceId
                && mCurrentlyPlayingTrack.mSourceType == mSourceType
                && mCurrentlyPlayingTrack.mId == song.mSongId;
    }

            return convertView;
    @NonNull
    @Override
    public MusicHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MusicHolder(LayoutInflater.from(parent.getContext())
                .inflate(mLayoutId, parent, false));
    }

        return super.getView(position, convertView, parent);
    @Override
    public void onBindViewHolder(@NonNull MusicHolder holder, int position) {
        // Retrieve the data holder
        Song item = getItem(position);

        holder.itemView.setOnClickListener(v -> mOnItemClickListener.accept(position));

        holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
        // Sets the position each time because of recycling
        holder.mPopupMenuButton.get().setPosition(position);
        // Set each song name (line one)
        holder.mLineOne.get().setText(item.mSongName);
        // Set the album name (line two)
        holder.mLineTwo.get().setText(MusicUtils.makeCombinedString(mContext, item.mArtistName,
                item.mAlbumName));

        // Asynchronously load the artist image into the adapter
        if (item.mAlbumId >= 0) {
            mImageFetcher.loadAlbumImage(item.mArtistName, item.mAlbumName, item.mAlbumId,
                    holder.mImage.get());
        }

    protected boolean showNowPlayingIndicator(final Song song, final int position) {
        return super.showNowPlayingIndicator(song, position)
                && mCurrentlyPlayingTrack.mSourcePosition == position - NUM_HEADERS;
        View nowPlayingIndicator = holder.mNowPlayingIndicator.get();
        if (nowPlayingIndicator != null) {
            if (showNowPlayingIndicator(item)) {
                nowPlayingIndicator.setVisibility(View.VISIBLE);
            } else {
                nowPlayingIndicator.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public boolean isEnabled(int position) {
        if (position == 0) {
            return false;
    public int getItemCount() {
        return mSongs.size();
    }

        return super.isEnabled(position);
    /**
     * Method that unloads and clears the items in the adapter
     */
    public void unload() {
        int size = mSongs.size();
        mSongs.clear();
        notifyItemRangeRemoved(0, size);
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount() + NUM_HEADERS;
    public void setPopupMenuClickedListener(IPopupMenuCallback.IListener listener) {
        mListener = listener;
    }

    @Override
    public int getItemViewType(final int position) {
        if (position == 0) {
            // since our view type count adds 1 to the super class, we can return viewtypecount - 1
            return getViewTypeCount() - 1;
    /**
     * Sets the currently playing track for the adapter to know when to show indicators
     *
     * @param currentTrack the currently playing track
     */
    public void setCurrentlyPlayingTrack(MusicPlaybackTrack currentTrack) {
        if (mCurrentlyPlayingTrack != null && mCurrentlyPlayingTrack.equals(currentTrack)) {
            return;
        }

        long previousPlayingId = mCurrentlyPlayingTrack == null
                ? NOTHING_PLAYING : mCurrentlyPlayingTrack.mId;
        mCurrentlyPlayingTrack = currentTrack;

        int toBeUpdated = (currentTrack == null || currentTrack.mId == NOTHING_PLAYING)
                ? 1 : 2;
        int updated = 0;

        for (int i = 0; i < mSongs.size() && updated < toBeUpdated; i++) {
            long id = mSongs.get(i).mSongId;
            if ((currentTrack != null && id == currentTrack.mId) || id == previousPlayingId) {
                notifyItemChanged(i);
                updated++;
            }
        }
        return super.getItemViewType(position);
    }

    @Override
    public void addAll(Collection<? extends Song> collection) {
        // insert a header if one is needed
        insertHeader();
        super.addAll(collection);
    public Song getItem(int position) {
        return mSongs.get(position);
    }

    @Override
    public void addAll(Song... items) {
        // insert a header if one is needed
        insertHeader();
        super.addAll(items);
    public void setData(List<Song> songs) {
        int oldSize = mSongs == null ? 0 : mSongs.size();
        int newSize = songs.size();

        mSongs = songs;

        if (oldSize == 0) {
            notifyItemRangeInserted(0, newSize);
        } else {
            int diff = oldSize - newSize;
            if (diff > 0) {
                // Items were removed
                notifyItemRangeChanged(0, newSize);
                notifyItemRangeRemoved(newSize, diff);
            } else if (diff < 0) {
                // Items were added
                notifyItemRangeChanged(0, oldSize);
                notifyItemRangeInserted(oldSize, diff * -1);
            } else {
                notifyItemChanged(0, oldSize);
            }
        }
    }

    /**
     * Make sure we insert our header when we add items
     */
    private void insertHeader() {
        if (getCount() == 0) {
            // add a dummy entry to the underlying adapter.  This is needed otherwise the
            // underlying adapter could crash because getCount() doesn't match up
            add(new Song(-1, null, null, null, -1, -1, -1));
    public void remove(Song song) {
        final int index = mSongs.indexOf(song);
        if (index >= 0) {
            mSongs.remove(index);
            notifyItemRemoved(index);
        }
    }

    public void move(int startPosition, int endPosition) {
        if (startPosition == endPosition) {
            return;
        }

        Song moving = mSongs.remove(startPosition);
        mSongs.add(endPosition, moving);
        notifyItemMoved(startPosition, endPosition);
    }
}
+0 −314
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 Andrew Neal
 * Copyright (C) 2014 The CyanogenMod Project
 * Copyright (C) 2019-2021 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.lineageos.eleven.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.fragment.app.FragmentActivity;

import org.lineageos.eleven.Config;
import org.lineageos.eleven.cache.ImageFetcher;
import org.lineageos.eleven.model.Song;
import org.lineageos.eleven.sectionadapter.SectionAdapter;
import org.lineageos.eleven.service.MusicPlaybackTrack;
import org.lineageos.eleven.ui.MusicHolder;
import org.lineageos.eleven.ui.MusicHolder.DataHolder;
import org.lineageos.eleven.ui.fragments.QueueFragment;
import org.lineageos.eleven.ui.fragments.SongFragment;
import org.lineageos.eleven.utils.ElevenUtils;
import org.lineageos.eleven.utils.MusicUtils;
import org.lineageos.eleven.widgets.IPopupMenuCallback;
import org.lineageos.eleven.widgets.PlayPauseButtonContainer;

/**
 * This {@link ArrayAdapter} is used to display all of the songs on a user's
 * device for {@link SongFragment}. It is also used to show the queue in
 * {@link QueueFragment}.
 *
 * @author Andrew Neal (andrewdneal@gmail.com)
 */
public class SongAdapter extends ArrayAdapter<Song>
        implements SectionAdapter.BasicAdapter, IPopupMenuCallback {

    public static final int NOTHING_PLAYING = -1;

    /**
     * Number of views (TextView)
     */
    private static final int VIEW_TYPE_COUNT = 1;

    /**
     * The resource Id of the layout to inflate
     */
    private final int mLayoutId;

    /**
     * Image cache and image fetcher
     */
    private final ImageFetcher mImageFetcher;

    /**
     * The index of the item that is currently playing
     */
    private long mCurrentQueuePosition = NOTHING_PLAYING;

    /**
     * Used to cache the song info
     */
    private DataHolder[] mData;

    /**
     * Used to listen to the pop up menu callbacks
     */
    private IPopupMenuCallback.IListener mListener;

    /**
     * Current music track
     */
    protected MusicPlaybackTrack mCurrentlyPlayingTrack;

    /**
     * Source id and type
     */
    protected long mSourceId;
    protected Config.IdType mSourceType;

    /**
     * Constructor of <code>SongAdapter</code>
     *
     * @param context    The {@link Context} to use.
     * @param layoutId   The resource Id of the view to inflate.
     * @param sourceId   The source id that the adapter is created from
     * @param sourceType The source type that the adapter is created from
     */
    public SongAdapter(final FragmentActivity context, final int layoutId, final long sourceId,
                       final Config.IdType sourceType) {
        super(context, 0);
        // Get the layout Id
        mLayoutId = layoutId;
        // Initialize the cache & image fetcher
        mImageFetcher = ElevenUtils.getImageFetcher(context);
        // set the source id and type
        mSourceId = sourceId;
        mSourceType = sourceType;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        // Recycle ViewHolder's items
        MusicHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(mLayoutId, parent, false);
            holder = new MusicHolder(convertView);
            convertView.setTag(holder);

            holder.mPopupMenuButton.get().setPopupMenuClickedListener(mListener);
        } else {
            holder = (MusicHolder) convertView.getTag();
        }

        // Retrieve the data holder
        final DataHolder dataHolder = mData[position];

        // Sets the position each time because of recycling
        holder.mPopupMenuButton.get().setPosition(position);
        // Set each song name (line one)
        holder.mLineOne.get().setText(dataHolder.lineOne);
        // Set the album name (line two)
        holder.mLineTwo.get().setText(dataHolder.lineTwo);

        // Asynchronously load the artist image into the adapter
        Song item = getItem(position);
        if (item.mAlbumId >= 0) {
            mImageFetcher.loadAlbumImage(item.mArtistName, item.mAlbumName, item.mAlbumId,
                    holder.mImage.get());
        }

        // padding doesn't apply to included layouts, so we need
        // to wrap it in a container and show/hide with the container
        PlayPauseButtonContainer playPauseButtonContainer = holder.mPlayPauseProgressButton.get();
        if (playPauseButtonContainer != null) {
            View playPauseContainer = holder.mPlayPauseProgressContainer.get();

            if (mCurrentQueuePosition == position) {
                // make it visible
                playPauseButtonContainer.enableAndShow();
                playPauseContainer.setVisibility(View.VISIBLE);
            } else {
                // hide it
                playPauseButtonContainer.disableAndHide();
                playPauseContainer.setVisibility(View.GONE);
            }
        }

        View nowPlayingIndicator = holder.mNowPlayingIndicator.get();
        if (nowPlayingIndicator != null) {
            if (showNowPlayingIndicator(item, position)) {
                nowPlayingIndicator.setVisibility(View.VISIBLE);
            } else {
                nowPlayingIndicator.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

    /**
     * Determines whether the song at the position should show the currently playing indicator
     *
     * @param song     the song in question
     * @param position the position of the song
     * @return true if we want to show the indicator
     */
    protected boolean showNowPlayingIndicator(final Song song, final int position) {
        return mCurrentlyPlayingTrack != null
                && mCurrentlyPlayingTrack.mSourceId == mSourceId
                && mCurrentlyPlayingTrack.mSourceType == mSourceType
                && mCurrentlyPlayingTrack.mId == song.mSongId;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }

    /**
     * Method used to cache the data used to populate the list or grid. The idea
     * is to cache everything before {@code #getView(int, View, ViewGroup)} is
     * called.
     */
    @Override
    public void buildCache() {
        mData = new DataHolder[getCount()];
        for (int i = 0; i < getCount(); i++) {
            // Build the song
            final Song song = getItem(i);

            // skip special placeholders
            if (song.mSongId == -1) {
                continue;
            }

            // Build the data holder
            mData[i] = new DataHolder();
            // Song Id
            mData[i].itemId = song.mSongId;
            // Song names (line one)
            mData[i].lineOne = song.mSongName;
            // Song duration (line one, right)
            mData[i].lineOneRight = MusicUtils.makeShortTimeString(getContext(), song.mDuration);

            // Artist Name | Album Name (line two)
            mData[i].lineTwo = MusicUtils.makeCombinedString(getContext(), song.mArtistName,
                    song.mAlbumName);
        }
    }

    /**
     * @param pause True to temporarily pause the disk cache, false otherwise.
     */
    public void setPauseDiskCache(final boolean pause) {
        if (mImageFetcher != null) {
            mImageFetcher.setPauseDiskCache(pause);
        }
    }

    /**
     * Method that unloads and clears the items in the adapter
     */
    @Override
    public void unload() {
        clear();
        mData = null;
    }

    /**
     * Do nothing.
     */
    @Override
    public void flush() {
    }

    /**
     * Gets the item position for a given id
     *
     * @param id identifies the object
     * @return the position if found, -1 otherwise
     */
    @Override
    public int getItemPosition(long id) {
        for (int i = 0; i < getCount(); i++) {
            if (getItem(i).mSongId == id) {
                return i;
            }
        }

        return -1;
    }

    public void setCurrentQueuePosition(long queuePosition) {
        if (mCurrentQueuePosition != queuePosition) {
            mCurrentQueuePosition = queuePosition;

            notifyDataSetChanged();
        }
    }

    @Override
    public void setPopupMenuClickedListener(IListener listener) {
        mListener = listener;
    }

    /**
     * Sets the currently playing track for the adapter to know when to show indicators
     *
     * @param currentTrack the currently playing track
     * @return true if the current track is different
     */
    public boolean setCurrentlyPlayingTrack(MusicPlaybackTrack currentTrack) {
        if (mCurrentlyPlayingTrack == null || !mCurrentlyPlayingTrack.equals(currentTrack)) {
            mCurrentlyPlayingTrack = currentTrack;

            notifyDataSetChanged();
            return true;
        }

        return false;
    }

    /**
     * @return Gets the list of song ids from the adapter
     */
    public long[] getSongIds() {
        long[] ret = new long[getCount()];
        for (int i = 0; i < getCount(); i++) {
            ret[i] = getItem(i).mSongId;
        }

        return ret;
    }
}
+71 −122

File changed.

Preview size limit exceeded, changes collapsed.