com.devgmail.mitroshin.totutu.controllers.StartFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.devgmail.mitroshin.totutu.controllers.StartFragment.java

Source

package com.devgmail.mitroshin.totutu.controllers;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.devgmail.mitroshin.totutu.R;
import com.devgmail.mitroshin.totutu.hosts.AboutActivity;
import com.devgmail.mitroshin.totutu.hosts.InfoActivity;
import com.devgmail.mitroshin.totutu.hosts.ListActivity;
import com.devgmail.mitroshin.totutu.model.Station;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 Copyright 2017 Mitroshin Dmitry (mitroshin.develop@gmail.com)
    
 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.
 */

// ? ?? fragment_start.xml

public class StartFragment extends Fragment implements View.OnClickListener {

    //     ? ?? ??  ?   
    private static final String SAVE_STATION_FROM = "save_station_from";
    private static final String SAVE_STATION_TO = "save_station_to";

    // ?  ?, ? ? ?
    private static TextView fromStationTextView;
    private static TextView fromCityTextView;

    // ?  ?, ? ? ?
    private TextView toStationTextView;
    private TextView toCityTextView;

    // ?    ??
    private static Button fromSetButton;
    private static Button toSetButton;

    // ?   ? ? 
    private static Button fromInfoButton;
    private Button toInfoButton;

    // ?    datepicker
    private static Button datePickerButton;
    private static Button aboutAppButton;

    //  ?  ? ??
    private static final int REQUEST_STATION_OBJECT = 0;

    //  , ?  From  To
    private Station mCurrentStationFrom = null;
    private Station mCurrentStationTo = null;

    //   ?    ? ?  
    private String mResultDirectionType = null;

    private DatePickerDialog mDatePickerDialog;
    private SimpleDateFormat mSimpleDateFormat;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {

        //          
        View view = inflater.inflate(R.layout.fragment_start, container, false);

        mSimpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

        //         ? ??    ? 
        findAllViewById(view);

        datePickerButton.setInputType(InputType.TYPE_NULL);
        setDateField();

        //         ? ?? ? 
        setAllClickListener();

        //        ? ?    ? ? ??.
        if (savedInstanceState != null) {
            if (savedInstanceState.getParcelable(SAVE_STATION_FROM) != null) {
                mCurrentStationFrom = savedInstanceState.getParcelable(SAVE_STATION_FROM);
                updateStationUI(mCurrentStationFrom, "From");
            }

            if (savedInstanceState.getParcelable(SAVE_STATION_TO) != null) {
                mCurrentStationTo = savedInstanceState.getParcelable(SAVE_STATION_TO);
                updateStationUI(mCurrentStationTo, "To");
            }
        }

        return view;
    }

    private void updateStationUI(Station currentStation, String direction) {
        switch (direction) {
        case "From":
            fromStationTextView.setText(currentStation.getStation());
            fromCityTextView.setText(currentStation.getCity());
            break;
        case "To":
            toStationTextView.setText(currentStation.getStation());
            toCityTextView.setText(currentStation.getCity());
            break;
        }
    }

    private void setDateField() {

        Calendar myCalendar = Calendar.getInstance();

        mDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Calendar myDate = Calendar.getInstance();
                myDate.set(year, month, dayOfMonth);
                datePickerButton.setText(mSimpleDateFormat.format(myDate.getTime()));
            }
        }, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.start_button_from_info:
            if (mCurrentStationFrom == null) {
                Toast.makeText(getActivity(), "Select the station", Toast.LENGTH_SHORT).show();
                break;
            }
            Intent intentInfoFrom = InfoActivity.newIntent(getActivity(), mCurrentStationFrom);
            startActivity(intentInfoFrom);
            break;
        case R.id.start_button_to_info:
            if (mCurrentStationTo == null) {
                Toast.makeText(getActivity(), "Select the station", Toast.LENGTH_SHORT).show();
                break;
            }
            Intent intentInfoTo = InfoActivity.newIntent(getActivity(), mCurrentStationTo);
            startActivity(intentInfoTo);
            break;
        case R.id.start_button_from_set:
            Intent intentFrom = ListActivity.newIntent(getActivity(), "From");
            startActivityForResult(intentFrom, REQUEST_STATION_OBJECT);
            break;
        case R.id.start_button_to_set:
            Intent intentTo = ListActivity.newIntent(getActivity(), "To");
            startActivityForResult(intentTo, REQUEST_STATION_OBJECT);
            break;
        case R.id.start_button_date:
            mDatePickerDialog.show();
            break;
        case R.id.start_button_about:
            Intent intentToAbout = new Intent(getActivity(), AboutActivity.class);
            startActivity(intentToAbout);
            break;
        }
    }

    //    ,    ? ?  
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //            ?,     Back.
        if (resultCode == Activity.RESULT_CANCELED) {
            return;
        }

        //        ??   ? ?, ?  ??,
        //         ??     ?? ?

        if (requestCode == REQUEST_STATION_OBJECT) {
            if (data == null) {
                return;
            }
            mResultDirectionType = ListFragment.resultDirectionType(data);

            //             ??      ??   ?,
            //              ??  ?  ? ?.
            updateUI(mResultDirectionType, data);
        }
    }

    //      ? ? From  To  ?? ?   .
    private void updateUI(String directionType, Intent resultStationObject) {

        switch (directionType) {
        case "From":
            mCurrentStationFrom = ListFragment.resultStationObject(resultStationObject);
            fromStationTextView.setText(mCurrentStationFrom.getStation());
            fromCityTextView.setText(mCurrentStationFrom.getCity());
            break;
        case "To":
            mCurrentStationTo = ListFragment.resultStationObject(resultStationObject);
            toStationTextView.setText(mCurrentStationTo.getStation());
            toCityTextView.setText(mCurrentStationTo.getCity());
            break;
        }
    }

    //    Bundle  .
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        if (mCurrentStationFrom != null) {
            outState.putParcelable(SAVE_STATION_FROM, mCurrentStationFrom);
        }

        if (mCurrentStationTo != null) {
            outState.putParcelable(SAVE_STATION_TO, mCurrentStationTo);
        }
    }

    private void findAllViewById(View view) {
        fromSetButton = (Button) view.findViewById(R.id.start_button_from_set);
        toSetButton = (Button) view.findViewById(R.id.start_button_to_set);
        fromInfoButton = (Button) view.findViewById(R.id.start_button_from_info);
        toInfoButton = (Button) view.findViewById(R.id.start_button_to_info);
        datePickerButton = (Button) view.findViewById(R.id.start_button_date);
        fromStationTextView = (TextView) view.findViewById(R.id.start_text_from_station);
        fromCityTextView = (TextView) view.findViewById(R.id.start_text_from_city);
        toStationTextView = (TextView) view.findViewById(R.id.start_text_to_station);
        toCityTextView = (TextView) view.findViewById(R.id.start_text_to_city);
        aboutAppButton = (Button) view.findViewById(R.id.start_button_about);
    }

    private void setAllClickListener() {
        fromSetButton.setOnClickListener(this);
        toSetButton.setOnClickListener(this);
        fromInfoButton.setOnClickListener(this);
        toInfoButton.setOnClickListener(this);
        datePickerButton.setOnClickListener(this);
        aboutAppButton.setOnClickListener(this);
    }
}