2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.cyngn.eleven.ui.fragments;
14 import android.content.Context;
15 import android.os.Bundle;
16 import android.os.SystemClock;
17 import android.support.v4.app.Fragment;
18 import android.support.v4.app.LoaderManager.LoaderCallbacks;
19 import android.support.v4.content.Loader;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.AbsListView;
24 import android.widget.AbsListView.OnScrollListener;
25 import android.widget.AdapterView;
26 import android.widget.AdapterView.OnItemClickListener;
27 import android.widget.ListView;
29 import com.cyngn.eleven.MusicStateListener;
30 import com.cyngn.eleven.R;
31 import com.cyngn.eleven.adapters.ArtistAdapter;
32 import com.cyngn.eleven.adapters.PagerAdapter;
33 import com.cyngn.eleven.loaders.ArtistLoader;
34 import com.cyngn.eleven.model.Artist;
35 import com.cyngn.eleven.recycler.RecycleHolder;
36 import com.cyngn.eleven.sectionadapter.SectionAdapter;
37 import com.cyngn.eleven.sectionadapter.SectionCreator;
38 import com.cyngn.eleven.sectionadapter.SectionListContainer;
39 import com.cyngn.eleven.ui.activities.BaseActivity;
40 import com.cyngn.eleven.ui.fragments.phone.MusicBrowserFragment;
41 import com.cyngn.eleven.utils.ArtistPopupMenuHelper;
42 import com.cyngn.eleven.utils.MusicUtils;
43 import com.cyngn.eleven.utils.NavUtils;
44 import com.cyngn.eleven.utils.PopupMenuHelper;
45 import com.cyngn.eleven.utils.SectionCreatorUtils;
46 import com.cyngn.eleven.utils.SectionCreatorUtils.IItemCompare;
47 import com.cyngn.eleven.widgets.IPopupMenuCallback;
48 import com.cyngn.eleven.widgets.LoadingEmptyContainer;
49 import com.viewpagerindicator.TitlePageIndicator;
52 * This class is used to display all of the artists on a user's device.
54 * @author Andrew Neal (andrewdneal@gmail.com)
56 public class ArtistFragment extends MusicBrowserFragment implements
57 LoaderCallbacks<SectionListContainer<Artist>>,
58 OnScrollListener, OnItemClickListener, MusicStateListener {
63 private ViewGroup mRootView;
66 * The adapter for the grid
68 private SectionAdapter<Artist, ArtistAdapter> mAdapter;
73 private ListView mListView;
78 private PopupMenuHelper mPopupMenuHelper;
81 * Loading container and no results container
83 private LoadingEmptyContainer mLoadingEmptyContainer;
86 * Empty constructor as per the {@link Fragment} documentation
88 public ArtistFragment() {
92 public int getLoaderId() {
93 return PagerAdapter.MusicFragments.ARTIST.ordinal();
100 public void onCreate(final Bundle savedInstanceState) {
101 super.onCreate(savedInstanceState);
103 mPopupMenuHelper = new ArtistPopupMenuHelper(getActivity(), getFragmentManager()) {
105 public Artist getArtist(int position) {
106 return mAdapter.getTItem(position);
110 // Create the adapter
111 final int layout = R.layout.list_item_normal;
112 ArtistAdapter adapter = new ArtistAdapter(getActivity(), layout);
113 mAdapter = new SectionAdapter<Artist, ArtistAdapter>(getActivity(), adapter);
114 mAdapter.setPopupMenuClickedListener(new IPopupMenuCallback.IListener() {
116 public void onPopupMenuClicked(View v, int position) {
117 mPopupMenuHelper.showPopupMenu(v, position);
126 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
127 final Bundle savedInstanceState) {
128 // The View for the fragment's UI
129 mRootView = (ViewGroup)inflater.inflate(R.layout.list_base, null);
132 // Register the music status listener
133 ((BaseActivity)getActivity()).setMusicStateListenerListener(this);
139 public void onDestroyView() {
140 super.onDestroyView();
142 ((BaseActivity)getActivity()).removeMusicStateListenerListener(this);
150 public void onActivityCreated(final Bundle savedInstanceState) {
151 super.onActivityCreated(savedInstanceState);
152 // Enable the options menu
153 setHasOptionsMenu(true);
155 initLoader(null, this);
162 public void onPause() {
171 public void onScrollStateChanged(final AbsListView view, final int scrollState) {
172 // Pause disk cache access to ensure smoother scrolling
173 if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
174 mAdapter.getUnderlyingAdapter().setPauseDiskCache(true);
176 mAdapter.getUnderlyingAdapter().setPauseDiskCache(false);
177 mAdapter.notifyDataSetChanged();
185 public void onItemClick(final AdapterView<?> parent, final View view, final int position,
187 Artist artist = mAdapter.getTItem(position);
188 NavUtils.openArtistProfile(getActivity(), artist.mArtistName);
195 public Loader<SectionListContainer<Artist>> onCreateLoader(final int id, final Bundle args) {
196 mLoadingEmptyContainer.showLoading();
197 final Context context = getActivity();
198 IItemCompare<Artist> comparator = SectionCreatorUtils.createArtistComparison(context);
199 return new SectionCreator<Artist>(getActivity(), new ArtistLoader(context), comparator);
206 public void onLoadFinished(final Loader<SectionListContainer<Artist>> loader,
207 final SectionListContainer<Artist> data) {
208 // Check for any errors
209 if (data.mListResults.isEmpty()) {
210 mLoadingEmptyContainer.showNoResults();
215 mAdapter.setData(data);
222 public void onLoaderReset(final Loader<SectionListContainer<Artist>> loader) {
223 // Clear the data in the adapter
228 * Scrolls the list to the currently playing artist when the user touches
229 * the header in the {@link TitlePageIndicator}.
231 public void scrollToCurrentArtist() {
232 final int currentArtistPosition = getItemPositionByArtist();
234 if (currentArtistPosition != 0) {
235 mListView.setSelection(currentArtistPosition);
240 * @return The position of an item in the list or grid based on the name of
241 * the currently playing artist.
243 private int getItemPositionByArtist() {
244 final long artistId = MusicUtils.getCurrentArtistId();
245 if (mAdapter == null) {
249 int position = mAdapter.getItemPosition(artistId);
251 // if for some reason we don't find the item, just jump to the top
260 * Restarts the loader.
262 public void refresh() {
263 // Wait a moment for the preference to change.
264 SystemClock.sleep(10);
272 public void onScroll(final AbsListView view, final int firstVisibleItem,
273 final int visibleItemCount, final int totalItemCount) {
281 public void restartLoader() {
282 // Update the list when the user deletes any items
283 restartLoader(null, this);
290 public void onMetaChanged() {
295 public void onPlaylistChanged() {
300 * Sets up various helpers for both the list and grid
302 * @param list The list or grid
304 private void initAbsListView(final AbsListView list) {
305 // Release any references to the recycled Views
306 list.setRecyclerListener(new RecycleHolder());
307 // Show the albums and songs from the selected artist
308 list.setOnItemClickListener(this);
309 // To help make scrolling smooth
310 list.setOnScrollListener(this);
314 * Sets up the list view
316 private void initListView() {
317 // Initialize the grid
318 mListView = (ListView)mRootView.findViewById(R.id.list_base);
319 // Set the data behind the list
320 mListView.setAdapter(mAdapter);
321 // set the loading and empty view container
322 mLoadingEmptyContainer = (LoadingEmptyContainer)mRootView.findViewById(R.id.loading_empty_container);
323 mListView.setEmptyView(mLoadingEmptyContainer);
324 // Set up the helpers
325 initAbsListView(mListView);