com.pyamsoft.dontsuckmp.playback.ShowQueueDialog.java Source code

Java tutorial

Introduction

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

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.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter;
import com.pyamsoft.dontsuckmp.DontSuckMp;
import com.pyamsoft.dontsuckmp.databinding.DialogQueueBinding;
import com.pyamsoft.dontsuckmp.model.Track;
import java.util.ArrayList;
import java.util.List;

public class ShowQueueDialog extends DialogFragment {

    @NonNull
    private static final String KEY_QUEUE = "key_queue";
    @NonNull
    private final List<Track> queue = new ArrayList<>();
    private DialogQueueBinding binding;

    @CheckResult
    @NonNull
    public static ShowQueueDialog newInstance(@NonNull List<Track> queue) {
        final Bundle args = new Bundle();
        final ShowQueueDialog fragment = new ShowQueueDialog();
        args.putParcelableArrayList(KEY_QUEUE, new ArrayList<>(queue));
        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);
        final List<Track> checkMe = getArguments().getParcelableArrayList(KEY_QUEUE);
        queue.clear();

        if (checkMe != null) {
            queue.addAll(checkMe);
        }
    }

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        binding = DialogQueueBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

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

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

    @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.WRAP_CONTENT);
        }
    }

    private void setupToolbar() {
        binding.queueToolbar.setNavigationOnClickListener(v -> dismiss());
        binding.queueToolbar.setTitle("Playback Queue");
    }

    private void setupList() {
        final FastItemAdapter<ShowQueueItem> fastItemAdapter = new FastItemAdapter<>();
        for (final Track track : queue) {
            fastItemAdapter.add(new ShowQueueItem(track));
        }

        fastItemAdapter.withSelectable(true);
        fastItemAdapter.withOnClickListener((v, adapter, item, position) -> {
            final List<Track> trackList = new ArrayList<>();
            //noinspection Convert2streamapi
            for (final ShowQueueItem queueItem : adapter.getAdapterItems()) {
                trackList.add(queueItem.getModel());
            }
            // TODO
            //PlayerService.queueNext(v.getContext(), trackList, position);
            return true;
        });

        binding.queueRecyclerList.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.queueRecyclerList.setAdapter(fastItemAdapter);
    }
}