com.pyamsoft.dontsuckmp.album.AlbumDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.pyamsoft.dontsuckmp.album.AlbumDialog.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.album;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.bumptech.glide.Glide;
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter;
import com.pyamsoft.dontsuckmp.DontSuckMp;
import com.pyamsoft.dontsuckmp.databinding.DialogAlbumBinding;
import com.pyamsoft.dontsuckmp.model.Album;
import com.pyamsoft.dontsuckmp.model.Track;
import com.pyamsoft.dontsuckmp.uicore.MediaBrowserDelegate;
import com.pyamsoft.dontsuckmp.uicore.NowPlayingDelegate;
import java.util.Locale;
import timber.log.Timber;

public class AlbumDialog extends DialogFragment {

    @NonNull
    public static final String TAG = "AlbumTrackListPagerFragment";
    @NonNull
    public static final String EXTRA_ALBUM = "album";
    @NonNull
    final NowPlayingDelegate nowPlayingDelegate = new NowPlayingDelegate();
    @NonNull
    private final MediaBrowserDelegate mediaBrowserDelegate = new MediaBrowserDelegate();
    Album album;
    FastItemAdapter<TrackListItem> fastAdapter;
    private DialogAlbumBinding binding;
    private RecyclerView.ItemDecoration decoration;

    @CheckResult
    @NonNull
    public static AlbumDialog newInstance(@NonNull Album album) {
        final Bundle args = new Bundle();
        final AlbumDialog fragment = new AlbumDialog();
        args.putParcelable(EXTRA_ALBUM, album);
        fragment.setArguments(args);
        return fragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
        album = getArguments().getParcelable(EXTRA_ALBUM);

        if (album == null) {
            throw new NullPointerException("Album is NULL");
        }
    }

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

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mediaBrowserDelegate.onDestroyView();
        binding.trackList.removeItemDecoration(decoration);
        binding.trackList.setAdapter(null);
        binding.unbind();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupToolbar();
        setupRecyclerView();
        setupAlbumInformation();
    }

    private void setupToolbar() {
        mediaBrowserDelegate.onViewCreated(fastAdapter);
        binding.dialogAlbumToolbar.setNavigationOnClickListener(v -> dismiss());

        nowPlayingDelegate.onCreateOptionsMenu(binding.dialogAlbumToolbar);

        mediaBrowserDelegate.onCreateOptionsMenu(binding.dialogAlbumToolbar);
        mediaBrowserDelegate.onPrepareOptionsMenu(binding.dialogAlbumToolbar.getMenu(), fastAdapter);

        binding.dialogAlbumToolbar.setOnMenuItemClickListener(item -> {
            final boolean handled = nowPlayingDelegate.onOptionsItemSelected(getActivity(), item);
            if (handled) {
                dismiss();
            }
            return handled;
        });
    }

    private void setupAlbumInformation() {
        final String albumName = album.name();
        final int trackCount = album.tracks().size();
        final String albumTrackCount = String.format(Locale.getDefault(), "Track count: %d %s", trackCount,
                trackCount > 1 ? "tracks" : "track");
        binding.albumArtist.setText(album.artist());
        binding.albumTrackCount.setText(albumTrackCount);
        binding.albumName.setText(albumName);

        final String albumArtworkPath = album.artwork();
        if (!albumArtworkPath.isEmpty()) {
            Timber.d("Load album art: %s", albumArtworkPath);
            Glide.with(getContext()).load(albumArtworkPath).into(binding.albumArt);
        }
    }

    private void setupRecyclerView() {
        if (fastAdapter == null) {
            throw new IllegalStateException("FastAdapter is NULL");
        }

        fastAdapter.withSelectable(true);
        fastAdapter.withOnClickListener((v, adapter, item, position) -> {
            // TODO
            //PlayerService.queueNext(v.getContext(), album.tracks(), position);
            return true;
        });

        fastAdapter.withOnLongClickListener((v, adapter, item, position) -> {
            // TODO
            //PlayerService.addToQueue(v.getContext(), album.tracks().get(position));
            return true;
        });

        Timber.d("Populate adapter");
        for (final Track track : album.tracks()) {
            fastAdapter.add(new TrackListItem(track));
        }

        decoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        binding.trackList.setAdapter(fastAdapter);
        binding.trackList.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.trackList.addItemDecoration(decoration);
    }

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

    @Override
    public void onResume() {
        super.onResume();
        // The dialog is super small for some reason. We have to set the size manually, in onResume
        final Window window = getDialog().getWindow();
        if (window != null) {
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        }
    }
}