Android Open Source - Joetz-Android-V2 Booking Participants 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;
//  ww w.  j a  va 2  s .  c  o m
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jens.myapplication.R;
import com.example.jens.myapplication.domain.Person;
import com.example.jens.myapplication.sam.JoetzApplication;
import com.example.jens.myapplication.sam.MyFragment;
import com.example.jens.myapplication.sam.MySaveableFragment;

import java.util.ArrayList;
import java.util.List;

public class BookingParticipantsFragment extends MyFragment implements MySaveableFragment {
    public static final String ARG_COUNT = "amount";
    public static final String ARG_PERSON = "person";

    //private static final String ARG_BUNDLE = "bundle";

    private OnFragmentInteractionListener mListener;

    private boolean mSelfAdded = false;

    private List<Person> mParticipants;
    private LinearLayout mLlyParticipants;

    private TextView mTxtNoParticipants;

    private Button mBtnAdd;
    private Button mBtnBack;
    private Button mBtnNext;

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

    /**
     *
     * @param bundle Bundle containing previous information of this fragment
     * @return
     */
    public static BookingParticipantsFragment newInstance(Bundle bundle) {
        BookingParticipantsFragment fragment = new BookingParticipantsFragment();
        if(bundle != null){
            fragment.setArguments(bundle);
        }
        return fragment;
    }
    public BookingParticipantsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mParticipants = new ArrayList<Person>();

        Bundle bundle = null;
        if(getArguments() != null){
            bundle = getArguments();
        }

        if(bundle != null){
            //Extract every person from the bundle
            int amount = bundle.getInt(ARG_COUNT);
            for(int i=0; i<amount; i++){
                String key = ARG_PERSON + i;
                Person p = bundle.getParcelable(key);
                mParticipants.add(p);
            }
        }
    }

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

        mLlyParticipants = (LinearLayout) rootView.findViewById(R.id.llyParticipants);

        mTxtNoParticipants = (TextView) rootView.findViewById(R.id.txtNoParticipants);
        if(mParticipants.size() > 0){
            mTxtNoParticipants.setVisibility(View.GONE);
        }

        mBtnAdd = (Button) rootView.findViewById(R.id.btnAdd);
        mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addParticipant();
            }
        });

        mBtnBack = (Button) rootView.findViewById(R.id.btnBack);
        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });

        mBtnNext = (Button) rootView.findViewById(R.id.btnNext);
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goNext();
            }
        });

        //mLineBottom = rootView.findViewById(R.id.lineBottomParticipants);
        //mLineTop = rootView.findViewById(R.id.lineTopParticipants);

        for(Person p : mParticipants){
            addPersonView(p);
        }

        return rootView;
    }

    /**
     * Adds a new person to the list
     * @param person The person to be added
     */
    private void addPerson(Person person){
        if(person.getId() == JoetzApplication.getContext().getLoginManager().getUser().getPerson().getId()){
            mSelfAdded = true;
        }
        mParticipants.add(person);
        addPersonView(person);
    }

    /**
     * Adds the view for a new person to the list
     * @param person The person whose info should be displayed in the view
     */
    private void addPersonView(final Person person){
        final View newView = getActivity().getLayoutInflater()
                .inflate(R.layout.view_participant_list_item, null);

        initDataOnListItem(newView, person);
        mLlyParticipants.addView(newView);

        if(!(mTxtNoParticipants.getVisibility() == View.GONE)){
            mTxtNoParticipants.setVisibility(View.GONE);
        }
    }

    /**
     * Fill the view with the info of the person and attach listeners
     * @param container The container of the view
     * @param person The person 'attached' to this view
     */
    private void initDataOnListItem(final View container, final Person person){
        TextView txtName = (TextView) container.findViewById(R.id.txtName);
        Button btnDelete = (Button) container.findViewById(R.id.btnDelete);

        txtName.setText(person.getFirstName() + " " + person.getLastName());

        container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editParticipant(person);
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLlyParticipants.removeView(container);
                mParticipants.remove(person);
                if(person.getId() == JoetzApplication.getContext().getLoginManager().getUser().getPerson().getId()){
                    mSelfAdded = false;
                }
                if(mLlyParticipants.getChildCount() < 1) {
                    mTxtNoParticipants.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    /**
     * Go back to the previous screen (Bond)
     */
    private void goBack(){
        mListener.toBondFragment();
    }

    /**
     * Go to the next screen if the validation allows it
     */
    private void goNext(){
        if(mParticipants.size() < 1){
            Toast.makeText(getActivity(), R.string.error_at_least_one_participant,
                    Toast.LENGTH_SHORT).show();
            return;
        }

        mListener.onParticipantsFinished(mParticipants);
    }

    /**
     * Add a new person to the list of participants
     */
    private void addParticipant(){
        addExistingPerson();
    }

    /**
     * Start an activity to create a new participant
     */
    private void createNewParticipant(){
        Intent i = new Intent(getActivity(), AddPersonActivity.class);
        i.putExtra(AddPersonActivity.EXTRA_REQUEST_CODE, AddPersonActivity.REQUEST_NEW);
        startActivityForResult(i, AddPersonActivity.REQUEST_NEW);
    }

    /**
     * Show a dialog with the list of 'my persons' attached to the user, also has an option
     * to create a new participant
     */
    private void addExistingPerson(){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.my_persons);
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.select_dialog_item);
        final List<Person> mypersons = JoetzApplication.getContext().getLoginManager().getUser().getPersons();
        if(!mSelfAdded){
            adapter.add(" + " + getString(R.string.add_me));
        }
        adapter.add(" + " + getString(R.string.new_person));
        final List<Person> realAdded = new ArrayList<Person>();
        aLoop: for(Person p : mypersons){
            for(Person partyCipant : mParticipants){
                if(partyCipant.getId() == p.getId()){
                    continue aLoop;
                }
            }
            realAdded.add(p);
            adapter.add(p.getFirstName() + " " + p.getLastName());
        }
        builder.setNegativeButton(R.string.cancel, null);
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(which == 0 && !mSelfAdded){
                    addMyself();
                    return;
                } else if(which == 1 && !mSelfAdded
                        || which == 0 && mSelfAdded){
                    //If "New person" is chosen at the top of the list
                    createNewParticipant();
                    return;
                }
                int subtract = mSelfAdded ? 1 : 2;
                addPerson(realAdded.get(which - subtract).clone());
            }
        });

        builder.create().show();
    }

    /**
     * Start the activity to edit a person
     * @param person the person to be edited
     */
    private void editParticipant(Person person){
        int index = mParticipants.indexOf(person);

        Intent i = new Intent(getActivity(), AddPersonActivity.class);
        i.putExtra(AddPersonActivity.EXTRA_PERSON, person);
        i.putExtra(AddPersonActivity.EXTRA_INDEX, index);
        i.putExtra(AddPersonActivity.EXTRA_REQUEST_CODE, AddPersonActivity.REQUEST_EDIT);
        startActivityForResult(i, AddPersonActivity.REQUEST_EDIT);
    }

    /**
     * Called when the activity to edit a person has finished successfully. <br />
     * Updates the user information in the list
     * @param person The new person information
     * @param index The index in the list of the person that was edited
     */
    public void onEditedPerson(Person person, int index){
        mParticipants.set(index, person);
        View view = mLlyParticipants.getChildAt(index);
        initDataOnListItem(view, person);
    }

    /**
     * Add the user himself to the list of participants
     */
    private void addMyself(){
        addPerson(JoetzApplication.getContext().getLoginManager().getUser().getPerson());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != AddPersonActivity.RESULT_OK) {
            return;
        }

        Person p = data.getParcelableExtra(AddPersonActivity.EXTRA_PERSON);
        /*switch(requestCode){
            case REQUEST_NEW: addPerson(p); break;
            case REQUEST_EDIT:{
                onEditedPerson()
            }
        }*/
        if(requestCode == AddPersonActivity.REQUEST_NEW){
            addPerson(p);
        }else if(requestCode == AddPersonActivity.REQUEST_EDIT){
            int index = data.getExtras().getInt(AddPersonActivity.EXTRA_INDEX);
            onEditedPerson(p, index);
        }
    }

    @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 onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //outState.putBundle(ARG_BUNDLE, getBundle());
    }

    @Override
    public void passBundleToParent(){
        int size = mParticipants.size();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_COUNT, size);
        if(size > 0){
            for(int i=0; i<size; i++){
                String key = ARG_PERSON + i; // example: "Person1" or "Person5"
                bundle.putParcelable(key, mParticipants.get(i));
            }
        }
        mListener.passParticipantsBundle(bundle);
    }

    public interface OnFragmentInteractionListener {
        /**
         * Return to bond fragment
         */
        public void toBondFragment();

        /**
         * Called when the "next" button has been clicked
         * @param participants The list of all the participants that were added
         */
        public void onParticipantsFinished(List<Person> participants);

        /**
         * Saves the bundle with the information about the participants fragment
         * @param bundle
         */
        public void passParticipantsBundle(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