com.fullmeadalchemist.mustwatch.ui.common.DatePickerFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.fullmeadalchemist.mustwatch.ui.common.DatePickerFragment.java

Source

/*
 * Copyright (c) 2017 Full Mead Alchemist, LLC.
 *
 * 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.fullmeadalchemist.mustwatch.ui.common;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.DatePicker;

import java.util.Calendar;

import timber.log.Timber;

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    public static final String DATE_SET_EVENT = "DATE_SET_EVENT";
    public static final String YEAR = "YEAR";
    public static final String MONTH = "MONTH";
    public static final String DAY_OF_MONTH = "DAY_OF_MONTH";

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of TimePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        Timber.d("Broadcasting %s with date: %s/%s/%s", DATE_SET_EVENT, dayOfMonth, month, year);

        Intent intent = new Intent(DATE_SET_EVENT);
        intent.putExtra(YEAR, year);
        intent.putExtra(MONTH, month);
        intent.putExtra(DAY_OF_MONTH, dayOfMonth);
        LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
    }
}