OSDN Git Service

Migrate to AndroidX
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / adapters / ArtistDetailAlbumAdapter.java
1 /*
2 * Copyright (C) 2014 The CyanogenMod Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.lineageos.eleven.adapters;
17
18 import android.app.Activity;
19 import android.os.Bundle;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ImageView;
24 import android.widget.TextView;
25
26 import androidx.loader.app.LoaderManager;
27 import androidx.loader.content.Loader;
28 import androidx.recyclerview.widget.RecyclerView;
29
30 import org.lineageos.eleven.Config;
31 import org.lineageos.eleven.R;
32 import org.lineageos.eleven.cache.ImageFetcher;
33 import org.lineageos.eleven.loaders.AlbumLoader;
34 import org.lineageos.eleven.model.Album;
35 import org.lineageos.eleven.utils.ElevenUtils;
36 import org.lineageos.eleven.utils.NavUtils;
37 import org.lineageos.eleven.widgets.IPopupMenuCallback;
38 import org.lineageos.eleven.widgets.PopupMenuButton;
39
40 import java.util.Collections;
41 import java.util.List;
42
43 public class ArtistDetailAlbumAdapter
44 extends RecyclerView.Adapter<ArtistDetailAlbumAdapter.ViewHolder>
45 implements LoaderManager.LoaderCallbacks<List<Album>>, IPopupMenuCallback {
46     private static final int TYPE_FIRST = 1;
47     private static final int TYPE_MIDDLE = 2;
48     private static final int TYPE_LAST = 3;
49
50     private final Activity mActivity;
51     private final ImageFetcher mImageFetcher;
52     private final LayoutInflater mInflater;
53     private List<Album> mAlbums = Collections.emptyList();
54     private IListener mListener;
55     private int mListMargin;
56
57     public ArtistDetailAlbumAdapter(final Activity activity) {
58         mActivity = activity;
59         mImageFetcher = ElevenUtils.getImageFetcher(activity);
60         mInflater = LayoutInflater.from(activity);
61         mListMargin = activity.getResources().
62             getDimensionPixelSize(R.dimen.list_item_general_margin);
63     }
64
65     @Override
66     public int getItemViewType(int position) {
67         // use view types to distinguish first and last elements
68         // so they can be given special treatment for layout
69         if(position == 0) { return TYPE_FIRST; }
70         else if(position == getItemCount()-1) { return TYPE_LAST; }
71         else return TYPE_MIDDLE;
72     }
73
74     @Override
75     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
76         View v = mInflater.inflate(R.layout.artist_detail_album, parent, false);
77         // add extra margin to the first and last elements
78         ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)v.getLayoutParams();
79         if     (viewType == TYPE_FIRST) { params.leftMargin = mListMargin; }
80         else if(viewType == TYPE_LAST)  { params.rightMargin = mListMargin; }
81         return new ViewHolder(v);
82     }
83
84     @Override
85     public void onBindViewHolder(ViewHolder holder, int position) {
86         Album a = mAlbums.get(position);
87         holder.title.setText(a.mAlbumName);
88         holder.year.setText(a.mYear);
89         mImageFetcher.loadAlbumImage(
90             a.mArtistName, a.mAlbumName, a.mAlbumId, holder.art);
91         holder.popupbutton.setPopupMenuClickedListener(mListener);
92         holder.popupbutton.setPosition(position);
93         addAction(holder.itemView, a);
94     }
95
96     private void addAction(View view, final Album album) {
97         view.setOnClickListener(new View.OnClickListener() {
98             @Override
99             public void onClick(View v) {
100                 NavUtils.openAlbumProfile(
101                     mActivity, album.mAlbumName, album.mArtistName, album.mAlbumId);
102             }
103         });
104     }
105
106     @Override
107     public int getItemCount() { return mAlbums.size(); }
108
109     public Album getItem(int position) {
110         return mAlbums.get(position);
111     }
112
113     @Override
114     public void setPopupMenuClickedListener(IListener listener) {
115         mListener = listener;
116     }
117
118     public static class ViewHolder extends RecyclerView.ViewHolder {
119         public ImageView art;
120         public TextView title;
121         public TextView year;
122         public PopupMenuButton popupbutton;
123         public ViewHolder(View root) {
124             super(root);
125             art = (ImageView)root.findViewById(R.id.album_art);
126             title = (TextView)root.findViewById(R.id.title);
127             year = (TextView)root.findViewById(R.id.year);
128             popupbutton = (PopupMenuButton)root.findViewById(R.id.overflow);
129         }
130     }
131
132     @Override // LoaderCallbacks
133     public Loader<List<Album>> onCreateLoader(int id, Bundle args) {
134         return new AlbumLoader(mActivity, args.getLong(Config.ID));
135     }
136
137     @Override // LoaderCallbacks
138     public void onLoadFinished(Loader<List<Album>> loader, List<Album> albums) {
139         if (albums.isEmpty()) { return; }
140         mAlbums = albums;
141         notifyDataSetChanged();
142     }
143
144     @Override // LoaderCallbacks
145     public void onLoaderReset(Loader<List<Album>> loader) {
146         mAlbums = Collections.emptyList();
147         notifyDataSetChanged();
148         mImageFetcher.flush();
149     }
150 }