summaryrefslogtreecommitdiffstats
path: root/src/org/lineageos/eleven/adapters/ArtistDetailAlbumAdapter.java
blob: 4670e0da97286780e22cd47b2ee4380e04c6f9c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (C) 2014 The CyanogenMod 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 com.cyanogenmod.eleven.adapters;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cyanogenmod.eleven.Config;
import com.cyanogenmod.eleven.R;
import com.cyanogenmod.eleven.cache.ImageFetcher;
import com.cyanogenmod.eleven.loaders.AlbumLoader;
import com.cyanogenmod.eleven.model.Album;
import com.cyanogenmod.eleven.utils.ApolloUtils;
import com.cyanogenmod.eleven.utils.NavUtils;
import com.cyanogenmod.eleven.widgets.IPopupMenuCallback;
import com.cyanogenmod.eleven.widgets.PopupMenuButton;

import java.util.Collections;
import java.util.List;

public class ArtistDetailAlbumAdapter
extends RecyclerView.Adapter<ArtistDetailAlbumAdapter.ViewHolder>
implements LoaderCallbacks<List<Album>>, IPopupMenuCallback {
    private static final int TYPE_FIRST = 1;
    private static final int TYPE_MIDDLE = 2;
    private static final int TYPE_LAST = 3;

    private final Activity mActivity;
    private final ImageFetcher mImageFetcher;
    private final LayoutInflater mInflater;
    private List<Album> mAlbums = Collections.emptyList();
    private IListener mListener;
    private int mListMargin;

    public ArtistDetailAlbumAdapter(final Activity activity) {
        mActivity = activity;
        mImageFetcher = ApolloUtils.getImageFetcher(activity);
        mInflater = LayoutInflater.from(activity);
        mListMargin = activity.getResources().
            getDimensionPixelSize(R.dimen.list_item_general_margin);
    }

    @Override
    public int getItemViewType(int position) {
        // use view types to distinguish first and last elements
        // so they can be given special treatment for layout
        if(position == 0) { return TYPE_FIRST; }
        else if(position == getItemCount()-1) { return TYPE_LAST; }
        else return TYPE_MIDDLE;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mInflater.inflate(R.layout.artist_detail_album, parent, false);
        // add extra margin to the first and last elements
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)v.getLayoutParams();
        if     (viewType == TYPE_FIRST) { params.leftMargin = mListMargin; }
        else if(viewType == TYPE_LAST)  { params.rightMargin = mListMargin; }
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Album a = mAlbums.get(position);
        holder.title.setText(a.mAlbumName);
        holder.year.setText(a.mYear);
        mImageFetcher.loadAlbumImage(
            a.mArtistName, a.mAlbumName, a.mAlbumId, holder.art);
        holder.popupbutton.setPopupMenuClickedListener(mListener);
        holder.popupbutton.setPosition(position);
        addAction(holder.itemView, a);
    }

    private void addAction(View view, final Album album) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavUtils.openAlbumProfile(
                    mActivity, album.mAlbumName, album.mArtistName, album.mAlbumId);
            }
        });
    }

    @Override
    public int getItemCount() { return mAlbums.size(); }

    public Album getItem(int position) {
        return mAlbums.get(position);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView art;
        public TextView title;
        public TextView year;
        public PopupMenuButton popupbutton;
        public ViewHolder(View root) {
            super(root);
            art = (ImageView)root.findViewById(R.id.album_art);
            title = (TextView)root.findViewById(R.id.title);
            year = (TextView)root.findViewById(R.id.year);
            popupbutton = (PopupMenuButton)root.findViewById(R.id.overflow);
        }
    }

    @Override // LoaderCallbacks
    public Loader<List<Album>> onCreateLoader(int id, Bundle args) {
        return new AlbumLoader(mActivity, args.getLong(Config.ID));
    }

    @Override // LoaderCallbacks
    public void onLoadFinished(Loader<List<Album>> loader, List<Album> albums) {
        if (albums.isEmpty()) { return; }
        mAlbums = albums;
        notifyDataSetChanged();
    }

    @Override // LoaderCallbacks
    public void onLoaderReset(Loader<List<Album>> loader) {
        mAlbums = Collections.emptyList();
        notifyDataSetChanged();
        mImageFetcher.flush();
    }
}