Android Open Source - Joetz-Android-V2 Booking Bond Fragment






From Project

Back to project page Joetz-Android-V2.

License

The source code is released under:

GNU General Public License

If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.jens.myapplication.booking;
//from w w  w . ja  v a 2  s .  com
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.example.jens.myapplication.R;
import com.example.jens.myapplication.domain.Person;
import com.example.jens.myapplication.domain.binding.PersonValidatorBinding;
import com.example.jens.myapplication.domain.binding.ValidatorBinding;
import com.example.jens.myapplication.domain.validator.PersonValidator;
import com.example.jens.myapplication.sam.MyFragment;
import com.example.jens.myapplication.sam.MySaveableFragment;
import com.example.jens.myapplication.util.BindingUtils;
import com.example.jens.myapplication.view.MyLinearLayout;

import java.util.LinkedList;
import java.util.List;

public class BookingBondFragment extends MyFragment implements MySaveableFragment {
    public static final String ARG_PERSON = "person";
    public static final String ARG_USER_INFO = "userinfo";

    private OnFragmentInteractionListener mListener;

    private MyLinearLayout mLlyRoot;

    private Button mBtnNext;
    private Button mBtnBack;

    private CheckBox mChkAangesloten;
    private boolean mInitChecked = false;
    private boolean mDisableCheckbox = false;

    private EditText mTxtAansluitingsnr;
    private EditText mTxtCodeGerechtigde;

    private Person mPerson;
    private PersonValidator mPersonValidator;
    private List<PersonValidatorBinding> mBindings;

    public static BookingBondFragment newInstance(){
        return newInstance(null);
    }

    /**
     *
     * @param bundle The starting bundle (received from the fragment before), null if u dont have any
     * @return
     */
    public static BookingBondFragment newInstance(Bundle bundle) {
        BookingBondFragment fragment = new BookingBondFragment();
        if(bundle != null){
            fragment.setArguments(bundle);
        }
        return fragment;
    }
    public BookingBondFragment() {
        // Required empty public constructor
    }

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

        if(getArguments() != null){
            Bundle bundle = getArguments();
            if(bundle.containsKey(ARG_USER_INFO)){
                mPerson = bundle.getParcelable(ARG_USER_INFO);
                //Automatically check if the person currently has Bond information.
                mInitChecked = mPerson.hasBondInformation();
                if(mInitChecked){
                    mDisableCheckbox = true;
                }
            }
            else{
                mPerson = bundle.getParcelable(ARG_PERSON);
                if(mPerson != null){
                    mInitChecked = true;
                }
            }
        }

        if(mPerson == null){
            mPerson = new Person();
        }

        mPersonValidator = new PersonValidator(mPerson);
        mBindings = new LinkedList<PersonValidatorBinding>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_booking_bond, container, false);

        mLlyRoot = (MyLinearLayout) rootView.findViewById(R.id.llyRootLinearLayout);
        mLlyRoot.setOnDispatchTouchEventListener(new MyLinearLayout.OnDispatchTouchEventListener() {
            @Override
            public void onDispatchTouchEvent(MotionEvent e) {
                clearFocus();
            }
        });

        mTxtAansluitingsnr = (EditText) rootView.findViewById(R.id.txtBondAansluitingsnr);
        mTxtCodeGerechtigde = (EditText) rootView.findViewById(R.id.txtBondCodeGerechtigde);

        mChkAangesloten = (CheckBox) rootView.findViewById(R.id.chkIsBijBond);
        mChkAangesloten.setChecked(mInitChecked);

        if(mDisableCheckbox){
            mChkAangesloten.setEnabled(false);
        }
        mChkAangesloten.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                changeChecked(isChecked);
            }
        });

        mBtnBack = (Button) rootView.findViewById(R.id.btnBack);
        mBtnNext = (Button) rootView.findViewById(R.id.btnNext);

        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goNext();
            }
        });

        initValidators();
        changeChecked(isAangesloten());
        validateAll();

        return rootView;
    }

    /**
     * Clear the focus of the fields
     */
    private void clearFocus(){
        View view = getActivity().getCurrentFocus();
        if(view == null || view == mLlyRoot){
            return;
        }

        mLlyRoot.requestFocus();
    }

    /**
     * Called when the checked value of the checkbox is checked
     * @param checked
     */
    private void changeChecked(boolean checked){
        mTxtAansluitingsnr.setText(mPerson.getBondAansluitingsNr());
        mTxtCodeGerechtigde.setText(mPerson.getBondCodeGerechtigde());

//        boolean required = checked  && !mDisableCheckbox;
        for(ValidatorBinding binding : mBindings){
//            binding.setRequired(required);
            binding.setRequired(checked);
//            binding.getView().setEnabled(required);
            binding.getView().setEnabled(checked);
            if(!checked){
                binding.getView().setText("");
            }
        }

        validateAll();
    }

    /**
     * validate all the text fields
     */
    private void validateAll(){
        /*if(!isAangesloten()){
            return;
        }*/

        for(ValidatorBinding binding : mBindings){
            binding.validate();
        }
    }

    /**
     *
     * @return true if the checkbox is checked, false if unchecked
     */
    private boolean isAangesloten(){
        return mChkAangesloten.isChecked();
    }

    /**
     * Initialize the bindings for the textfields
     */
    private void initValidators(){
        mBindings.add(new PersonValidatorBinding(mPersonValidator,
                mTxtAansluitingsnr, PersonValidatorBinding.FIELD_BOND_AANSLUITINGSNUMMER,
                mTxtAansluitingsnr.getHint().toString()));

        mBindings.add(new PersonValidatorBinding(mPersonValidator,
                mTxtCodeGerechtigde, PersonValidatorBinding.FIELD_BOND_CODE_GERECHTIGDE,
                mTxtCodeGerechtigde.getHint().toString()));
    }

    /**
     * Tell the listener to go to the previous screen
     */
    private void goBack(){
        mListener.toContactFragment();
    }

    /**
     * Tell the listener to go to the next screen if validation allows it
     */
    private void goNext(){
        validateAll();
        for(ValidatorBinding binding : mBindings){
            if(binding.getView().getError() != null){
                Toast.makeText(getActivity(), R.string.error_errors_exist, Toast.LENGTH_SHORT).show();
                return;
            }
        }
        mListener.onFinishBond();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

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

    @Override
    public void passBundleToParent() {
        if(isAangesloten()){
            Bundle bundle = new Bundle();
            bundle.putParcelable(ARG_PERSON, mPerson);
            mListener.passBondBundle(bundle);
        }
        else{
            mListener.passBondBundle(new Bundle());
        }
    }

    public interface OnFragmentInteractionListener {
        /**
         * Bond info is finished, go to the next screen
         */
        public void onFinishBond();

        /**
         * Go to the contact page
         */
        public void toContactFragment();

        /**
         * Save the bundle with bond information
         * @param bundle
         */
        public void passBondBundle(Bundle bundle);
    }

}




Java Source Code List

com.example.jens.myapplication.ApplicationTest.java
com.example.jens.myapplication.account.FragmentAccount.java
com.example.jens.myapplication.account.LoginDialogActivity.java
com.example.jens.myapplication.account.LoginFragment.java
com.example.jens.myapplication.account.PersonsFragment.java
com.example.jens.myapplication.account.ProfileFragment.java
com.example.jens.myapplication.account.RegisterDialogActivity.java
com.example.jens.myapplication.account.RegisterFragment.java
com.example.jens.myapplication.account.TabsPagerAdapter.java
com.example.jens.myapplication.activities.ActivityDetailActivity.java
com.example.jens.myapplication.activities.ActivityDetailFragment.java
com.example.jens.myapplication.activities.FragmentListActivities.java
com.example.jens.myapplication.activities.JoetzActivitiesAdapter.java
com.example.jens.myapplication.activities.JoetzActivity.java
com.example.jens.myapplication.activities.StartActivity.java
com.example.jens.myapplication.apimanager.ApiConnection.java
com.example.jens.myapplication.apimanager.CancellableTask.java
com.example.jens.myapplication.apimanager.JSONParsingTask.java
com.example.jens.myapplication.apimanager.PostRequestTask.java
com.example.jens.myapplication.apimanager.RequestParams.java
com.example.jens.myapplication.apimanager.SimpleRequestTask.java
com.example.jens.myapplication.apimanager.filter.PersonFilter.java
com.example.jens.myapplication.apimanager.manager.ActivityManager.java
com.example.jens.myapplication.apimanager.manager.AuthToken.java
com.example.jens.myapplication.apimanager.manager.BookingManager.java
com.example.jens.myapplication.apimanager.manager.CampManager.java
com.example.jens.myapplication.apimanager.manager.ImageManager.java
com.example.jens.myapplication.apimanager.manager.LoginManager.java
com.example.jens.myapplication.apimanager.manager.ProfileManager.java
com.example.jens.myapplication.apimanager.manager.RegisterManager.java
com.example.jens.myapplication.booking.AddPersonActivity.java
com.example.jens.myapplication.booking.BookingActivity.java
com.example.jens.myapplication.booking.BookingBondFragment.java
com.example.jens.myapplication.booking.BookingContactFragment.java
com.example.jens.myapplication.booking.BookingExtraFragment.java
com.example.jens.myapplication.booking.BookingParticipantsFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailActivity.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryOverviewFragment.java
com.example.jens.myapplication.bookingHistory.BookingsAdapter.java
com.example.jens.myapplication.calendar.FragmentCalendar.java
com.example.jens.myapplication.calendar.KalenderAdapter.java
com.example.jens.myapplication.calendar.KalenderItem.java
com.example.jens.myapplication.camps.CampAdapter.java
com.example.jens.myapplication.camps.CampDetailActivity.java
com.example.jens.myapplication.camps.CampFilterActivity.java
com.example.jens.myapplication.camps.FilterFragment.java
com.example.jens.myapplication.camps.FragmentListCamps.java
com.example.jens.myapplication.camps.ImageActivity.java
com.example.jens.myapplication.camps.ImagePagerFragment.java
com.example.jens.myapplication.camps.InfoFragment.java
com.example.jens.myapplication.camps.PhotoFragment.java
com.example.jens.myapplication.camps.RecyclerItemClickListener.java
com.example.jens.myapplication.camps.SquareImageView.java
com.example.jens.myapplication.camps.TabsPagerAdapter.java
com.example.jens.myapplication.domain.BookingStatus.java
com.example.jens.myapplication.domain.Booking.java
com.example.jens.myapplication.domain.Camp.java
com.example.jens.myapplication.domain.PeriodCategory.java
com.example.jens.myapplication.domain.Person.java
com.example.jens.myapplication.domain.UserRole.java
com.example.jens.myapplication.domain.User.java
com.example.jens.myapplication.domain.binding.PersonValidatorBinding.java
com.example.jens.myapplication.domain.binding.UserValidatorBinding.java
com.example.jens.myapplication.domain.binding.ValidatorBinding.java
com.example.jens.myapplication.domain.converter.ErrorConverter.java
com.example.jens.myapplication.domain.converter.MyStringParser.java
com.example.jens.myapplication.domain.converter.UnixDateTimeConverter.java
com.example.jens.myapplication.domain.validator.PersonValidator.java
com.example.jens.myapplication.domain.validator.UserValidator.java
com.example.jens.myapplication.domain.validator.Validator.java
com.example.jens.myapplication.home.AanbevolenAdapter.java
com.example.jens.myapplication.home.HomeFragment.java
com.example.jens.myapplication.home.ImageSlideAdapter.java
com.example.jens.myapplication.home.Reclame.java
com.example.jens.myapplication.lib.RangeSeekBar.java
com.example.jens.myapplication.main.MainActivity.java
com.example.jens.myapplication.main.NavDrawerAdapter.java
com.example.jens.myapplication.main.NavDrawerItem.java
com.example.jens.myapplication.main.NavMenuItem.java
com.example.jens.myapplication.main.NavMenuSection.java
com.example.jens.myapplication.sam.JoetzApplication.java
com.example.jens.myapplication.sam.MyFragment.java
com.example.jens.myapplication.sam.MySaveableFragment.java
com.example.jens.myapplication.util.AESEncryption.java
com.example.jens.myapplication.util.ActivityUtils.java
com.example.jens.myapplication.util.BindingUtils.java
com.example.jens.myapplication.util.DateTimeStringConverter.java
com.example.jens.myapplication.util.JsonUtils.java
com.example.jens.myapplication.util.MyMonths.java
com.example.jens.myapplication.util.StringUtils.java
com.example.jens.myapplication.view.MyLinearLayout.java