com.akhbulatov.wordkeeper.ui.dialog.CategoryDeleteDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.akhbulatov.wordkeeper.ui.dialog.CategoryDeleteDialog.java

Source

/*
 * Copyright 2018 Alidibir Akhbulatov
 *
 * 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.akhbulatov.wordkeeper.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

import com.akhbulatov.wordkeeper.R;

/**
 * @author Alidibir Akhbulatov
 * @since 24.09.2016
 */

/**
 * Shows a dialog to confirm deletion of category
 */
public class CategoryDeleteDialog extends BaseDialogFragment {

    private CategoryDeleteListener mListener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            mListener = (CategoryDeleteListener) getTargetFragment();
        } catch (ClassCastException e) {
            throw new ClassCastException(
                    getTargetFragment().toString() + " must implement " + CategoryDeleteListener.class.getName());
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        return builder.setTitle(R.string.category_delete_title).setMessage(R.string.category_delete_message)
                .setPositiveButton(R.string.category_action_delete,
                        (dialog, which) -> mListener.onFinishCategoryDeleteDialog(CategoryDeleteDialog.this))
                .setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss()).create();
    }

    public interface CategoryDeleteListener {
        /**
         * Applies the changes when the category is deleted
         *
         * @param dialog The current open dialog
         */
        void onFinishCategoryDeleteDialog(DialogFragment dialog);
    }
}