com.jsw.MngProductDatabase.Fragments.AddCategory_Fragment.java Source code

Java tutorial

Introduction

Here is the source code for com.jsw.MngProductDatabase.Fragments.AddCategory_Fragment.java

Source

package com.jsw.MngProductDatabase.Fragments;

/*
 * Copyright (c) 2017 Jos Luis del Pino Gallardo.
 *
 *  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.
 *
 *  jose.gallardo994@gmail.com
 */

/**
 * Created by usuario on 2/02/17.
 */

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.Toast;

import com.jsw.MngProductDatabase.Model.Category;
import com.jsw.MngProductDatabase.Presenter.CategoryPresenter;
import com.jsw.MngProductDatabase.R;
import com.jsw.MngProductDatabase.database.DatabaseManager;
import com.jsw.MngProductDatabase.interfaces.ICategoryPresenter;

public class AddCategory_Fragment extends DialogFragment {

    EditText mName;
    ICategoryPresenter.View view;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return create();
    }

    public Dialog create() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Add New Category");
        builder.setIcon(R.drawable.ic_add_white_24dp);
        builder.setMessage("Type the category you wanna add.");

        //mName = (EditText)getActivity().getLayoutInflater().inflate(R.layout.layout_add_category, ).findViewById(R.id.et_addCategory);
        LayoutInflater infl = LayoutInflater.from(getContext());
        mName = (EditText) infl.inflate(R.layout.layout_add_category, null).findViewById(R.id.et_addCategory);

        builder.setView(mName);

        builder.setPositiveButton(getContext().getText(android.R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                Category cat = new Category(1, mName.getText().toString());

                if (DatabaseManager.getInstance().checkCategory(cat)) {
                    Toast.makeText(getContext(), "Category already exists", Toast.LENGTH_LONG).show();
                    dismiss();
                } else {
                    new CategoryPresenter(view).addCategory(cat);
                }
            }
        });

        builder.setNegativeButton(getContext().getText(android.R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dismiss();
                    }
                });

        return builder.create();
    }

    public void setView(ICategoryPresenter.View view) {
        this.view = view;
    }
}