com.simplaapliko.trips.presentation.fragment.SelectDateDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.simplaapliko.trips.presentation.fragment.SelectDateDialog.java

Source

/*
 * Copyright (C) 2016 Oleg Kan
 *
 * 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.simplaapliko.trips.presentation.fragment;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.KeyEvent;
import android.widget.DatePicker;

import java.util.Calendar;
import java.util.Date;

public class SelectDateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    public interface OnDateChangedListener {
        void onDateChanged(Date date);
    }

    private static final String DATE_KEY = "com.simplaapliko.trips.DATE_KEY";

    private Calendar mCalendar;
    private OnDateChangedListener mOnDateChangedListener;

    public static SelectDateDialog newInstance() {
        return newInstance(new Date());
    }

    public static SelectDateDialog newInstance(Date date) {
        SelectDateDialog fragment = new SelectDateDialog();
        Bundle args = new Bundle();
        args.putSerializable(DATE_KEY, date);
        fragment.setArguments(args);
        return fragment;
    }

    public SelectDateDialog() {
        // Required empty public constructor
    }

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

        initializeState();

        setCancelable(false);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this,
                mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DATE));

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            pickerDialog.getDatePicker().setCalendarViewShown(false);
        }

        pickerDialog.setOnKeyListener((dialog, keyCode, event) -> {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                dialog.cancel();
                dialog.dismiss();
                return true;
            default:
                return false;
            }
        });

        return pickerDialog;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mOnDateChangedListener = null;
    }

    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int date) {
        mCalendar.set(year, month, date, 0, 0, 0);
        mCalendar.set(Calendar.MILLISECOND, 0);

        sendResult();
    }

    public void setOnDateChangedListener(OnDateChangedListener onDateChangedListener) {
        mOnDateChangedListener = onDateChangedListener;
    }

    private void initializeState() {
        mCalendar = Calendar.getInstance();

        Bundle args = getArguments();
        if (args != null) {
            Date date = (Date) args.getSerializable(DATE_KEY);
            initializeCalendar(date);
        }
    }

    private void initializeCalendar(Date date) {
        if (date != null) {
            mCalendar.setTime(date);
        }
    }

    private void sendResult() {
        if (mOnDateChangedListener != null) {
            mOnDateChangedListener.onDateChanged(mCalendar.getTime());
        }
    }
}