com.pyamsoft.dontsuckmp.artist.AllArtistListFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.pyamsoft.dontsuckmp.artist.AllArtistListFragment.java

Source

/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * 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.pyamsoft.dontsuckmp.artist;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter;
import com.pyamsoft.dontsuckmp.DontSuckMp;
import com.pyamsoft.dontsuckmp.Injector;
import com.pyamsoft.dontsuckmp.album.AlbumDialog;
import com.pyamsoft.dontsuckmp.databinding.FragmentAllArtistListBinding;
import com.pyamsoft.dontsuckmp.model.Album;
import com.pyamsoft.dontsuckmp.uicore.MediaBrowserDelegate;
import com.pyamsoft.dontsuckmp.uicore.NowPlayingDelegate;
import com.pyamsoft.pydroid.util.DialogUtil;
import timber.log.Timber;

public class AllArtistListFragment extends Fragment {

    @NonNull
    public static final String TAG = "ArtistListFragment";
    @NonNull
    final MediaBrowserDelegate mediaBrowserDelegate = new MediaBrowserDelegate();
    @NonNull
    private final NowPlayingDelegate nowPlayingDelegate = new NowPlayingDelegate();
    FastItemAdapter<ArtistAlbumListItem> fastAdapter;
    boolean listIsRefreshed;
    FragmentAllArtistListBinding binding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        Injector.get().provideComponent().plusAllArtistComponent().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        listIsRefreshed = false;
        fastAdapter = new FastItemAdapter<>();
        binding = FragmentAllArtistListBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        mediaBrowserDelegate.onDestroyView();
        binding.unbind();
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DontSuckMp.getRefWatcher(this).watch(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mediaBrowserDelegate.onViewCreated(fastAdapter);
        setupRecyclerView();
        setupProgressBar();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        mediaBrowserDelegate.onCreateOptionsMenu(menu, inflater);
        nowPlayingDelegate.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        mediaBrowserDelegate.onPrepareOptionsMenu(menu, fastAdapter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return nowPlayingDelegate.onOptionsItemSelected(getActivity(), item);
    }

    private void setupRecyclerView() {
        fastAdapter.withSelectable(true);
        fastAdapter.withOnClickListener((v, adapter, item, position) -> {
            Album album = item.getModel();
            Timber.d("Show album track list focus for: %s", album);
            DialogUtil.guaranteeSingleDialogFragment(getActivity(), AlbumDialog.newInstance(album),
                    AlbumDialog.TAG);
            return true;
        });

        binding.artistList.setAdapter(fastAdapter);
        final int currentOrientation = getActivity().getResources().getConfiguration().orientation;
        final int columnCount;
        switch (currentOrientation) {
        case Configuration.ORIENTATION_PORTRAIT:
            columnCount = 2;
            break;
        case Configuration.ORIENTATION_LANDSCAPE:
            columnCount = 3;
            break;
        default:
            columnCount = 2;
        }
        binding.artistList.setLayoutManager(new GridLayoutManager(getContext(), columnCount));
    }

    private void setupProgressBar() {
        binding.artistLoadingProgress.setIndeterminate(true);
    }
}