Android Open Source - Joetz-Android-V2 Booking Activity






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.j  a va 2 s .  co m*/
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;

import com.example.jens.myapplication.R;
import com.example.jens.myapplication.account.FragmentAccount;
import com.example.jens.myapplication.apimanager.ApiConnection;
import com.example.jens.myapplication.apimanager.CancellableTask;
import com.example.jens.myapplication.apimanager.SimpleRequestTask;
import com.example.jens.myapplication.apimanager.manager.BookingManager;
import com.example.jens.myapplication.apimanager.manager.ProfileManager;
import com.example.jens.myapplication.domain.Camp;
import com.example.jens.myapplication.domain.Person;
import com.example.jens.myapplication.main.MainActivity;
import com.example.jens.myapplication.sam.JoetzApplication;
import com.example.jens.myapplication.sam.MySaveableFragment;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class BookingActivity extends FragmentActivity
        implements BookingContactFragment.OnFragmentInteractionListener,
        BookingBondFragment.OnFragmentInteractionListener,
        BookingParticipantsFragment.OnFragmentInteractionListener,
        BookingExtraFragment.OnFragmentInteractionListener{
    public static final String ARG_CAMPID = "mCampId";
    private static final String ARG_LAST_FRAGMENT = "lastFragment";

    private static final String ARG_BUNDLE_CONTACT = "bundleContact";
    private static final String ARG_BUNDLE_BOND = "bundleBond";
    private static final String ARG_BUNDLE_PARTICIPANTS = "bundleParticipants";
    private static final String ARG_BUNDLE_EXTRA = "bundleExtra";

    private static final String TAG_FRAGMENT = "fragmentTag";

    private long mCampId;
    /**
     * in chronological order last fragments
     * 0: contact, 1: bond, 2: participants, 3: extra
     */
    private int lastFragment=0;

    //private Person mContactPerson = null;
    //private Person mBondPerson = null;

    private Bundle mBundleContact;
    private Bundle mBundleBond;
    private Bundle mBundleParticipants;
    private Bundle mBundleExtra;

    private CancellableTask mBookingTask;

    private ProgressDialog pgdBooking;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //pass null to prevent restoring fragments
        //we create new fragments
        super.onCreate(savedInstanceState);

        JoetzApplication.getContext().restartIfLoggedOut();

        //getActionBar().setHomeButtonEnabled(true);

        mCampId = getIntent().getExtras().getLong(ARG_CAMPID);
        if(savedInstanceState != null){
            lastFragment = savedInstanceState.getInt(ARG_LAST_FRAGMENT);
            mBundleContact = savedInstanceState.getBundle(ARG_BUNDLE_CONTACT);
            mBundleBond = savedInstanceState.getBundle(ARG_BUNDLE_BOND);
            mBundleParticipants = savedInstanceState.getBundle(ARG_BUNDLE_PARTICIPANTS);
            mBundleExtra = savedInstanceState.getBundle(ARG_BUNDLE_EXTRA);
        }

        setContentView(R.layout.activity_booking);

        switch(lastFragment){
            case 0: toContactFragment(); break;
            case 1: toBondFragment(); break;
            case 2: toParticipantsFragment(); break;
            case 3: toExtraFragment(); break;
        }

    }

    /*@Override
    protected FragmentPagerAdapter createFragmentPagerAdapter() {
        return new SectionsPagerAdapter(getFragmentManager());
    }*/

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //Get bundle from current fragment
        ((MySaveableFragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT)).passBundleToParent();

        //Save bundles
        outState.putInt(ARG_LAST_FRAGMENT, lastFragment);
        outState.putBundle(ARG_BUNDLE_CONTACT, mBundleContact);
        outState.putBundle(ARG_BUNDLE_BOND, mBundleBond);
        outState.putBundle(ARG_BUNDLE_PARTICIPANTS, mBundleParticipants);
        outState.putBundle(ARG_BUNDLE_EXTRA, mBundleExtra);
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        cancelBooking();
    }

    @Override
    public void onFinishContactPerson() {
        toBondFragment();
    }

    @Override
    public void onFinishBond() {
        toParticipantsFragment();
    }

    @Override
    public void onParticipantsFinished(List<Person> participants) {
        toExtraFragment();
        lastFragment=3;
    }

    @Override
    public void toContactFragment(){
        saveBundleCurrentFragment();
        changeFragment(BookingContactFragment.newInstance(mBundleContact));
        lastFragment=0;
    }

    @Override
    public void toBondFragment(){
        saveBundleCurrentFragment();

        Person curPerson = mBundleContact.getParcelable(BookingContactFragment.ARG_PERSON);
        Person uPerson = JoetzApplication.getContext().getLoginManager().getUser().getPerson();
        if(curPerson.getStreet().equals(uPerson.getStreet())
                && curPerson.getHouseNumber() == uPerson.getHouseNumber()
                && curPerson.getBus().equals(uPerson.getBus())
                && curPerson.getCity().equals(uPerson.getCity())
                && curPerson.getPostalCode() == uPerson.getPostalCode()){
            _toBondFragment();
            return;
        }

        final ProgressDialog pgb = new ProgressDialog(this);
        pgb.setMessage("Even geduld aub");
        pgb.show();

        Person person = mBundleContact.getParcelable(BookingContactFragment.ARG_PERSON);

        SimpleRequestTask afterTask = new SimpleRequestTask() {
            @Override
            public void doTask(int statusCode) {
                pgb.dismiss();
                if(!ApiConnection.isSuccessCode(statusCode)){
                    int stringId = R.string.error_unknown;
                    if(statusCode < 0){
                        stringId = R.string.error_connection_failed;
                    }
                    new AlertDialog.Builder(BookingActivity.this)
                            .setMessage(stringId)
                            .setNeutralButton(R.string.ok, null)
                            .create().show();
                    return;
                }

            _toBondFragment();
            }
        };

        ProfileManager.changeAddress(person, afterTask);
    }

    private void _toBondFragment(){
        Bundle bundle = mBundleBond;
        if(bundle == null || JoetzApplication.getContext().getLoginManager().getUser().getPerson().hasBondInformation()){
            bundle = new Bundle();
            bundle.putParcelable(
                    BookingBondFragment.ARG_USER_INFO,
                    JoetzApplication.getContext().getLoginManager().getUser().getPerson());
        }
        changeFragment(BookingBondFragment.newInstance(bundle));
        lastFragment = 1;
    }


 /* @Override
    public void toBondFragment(){
        saveBundleCurrentFragment();
        Bundle bundle = mBundleBond;
        if(bundle == null || JoetzApplication.getContext().getLoginManager().getUser().getPerson().hasBondInformation()){
            bundle = new Bundle();
            bundle.putParcelable(
                    BookingBondFragment.ARG_USER_INFO,
                    JoetzApplication.getContext().getLoginManager().getUser().getPerson());
        }
        changeFragment(BookingBondFragment.newInstance(bundle));
        lastFragment = 1;
    }*/

    @Override
    public void toParticipantsFragment(){
        saveBundleCurrentFragment();
        changeFragment(BookingParticipantsFragment.newInstance(mBundleParticipants));
        lastFragment = 2;
    }

    public void toExtraFragment(){
        saveBundleCurrentFragment();
        Bundle b;
        if(mBundleExtra != null){
            b = mBundleExtra;
        }else{
            b = new Bundle();
        }
        b.putLong(BookingExtraFragment.ARG_CAMP_ID, mCampId);
//        b.putInt(BookingExtraFragment.ARG_COUNT_PARTICIPANTS, getParticipantCount());
        int anonCount = mBundleParticipants.getInt(BookingParticipantsFragment.ARG_COUNT);
        List<Person> participants = new ArrayList<>();
        for(int i=0; i<anonCount; i++){
            participants.add((Person) mBundleParticipants.getParcelable(BookingParticipantsFragment.ARG_PERSON + i));
        }
        b.putParcelableArray(BookingExtraFragment.ARG_PARTICIPANTS,
                participants.toArray(new Parcelable[participants.size()]));
        b.putSerializable(BookingExtraFragment.ARG_CAMP_PRICE, getCalculatedPrice());
        changeFragment(BookingExtraFragment.newInstance(b));
        lastFragment = 3;
    }

    private boolean isContactUser(){
        return mBundleContact.getBoolean(BookingContactFragment.ARG_CHECKED);
    }

    @Override
    public void doBooking() {
        /**
         * Series of events
         * If one event is finished, it starts the next step, they call eachother because of async
         * requests.
         *
         * - if contactperson == user
         *      - 1) PUT request update standard information
         *      - 2) PUT request update address information
         * - 3) TODO if answer from buysse about bond : PUT request bond moyson
         * - 4) POST to finish the booking
         */

        pgdBooking = new ProgressDialog(this);
        pgdBooking.setIndeterminate(false);
        pgdBooking.setCancelable(false);
        pgdBooking.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pgdBooking.setMessage(getString(R.string.booking_is_being_processed));
        pgdBooking.setMax(4);
        pgdBooking.show();

        if(isContactUser()){
            updateStandardInformation();
        }else{
            //Go to step 3
            updateBondInformation();
        }
    }

    //Step 1
    private void updateStandardInformation(){
        Person person = mBundleContact.getParcelable(BookingContactFragment.ARG_PERSON);

        SimpleRequestTask afterTask = new SimpleRequestTask() {
            @Override
            public void doTask(int statusCode) {
                mBookingTask = null;
                if(!ApiConnection.isSuccessCode(statusCode)){
                    bookingFailed();
                    return;
                }
                pgdBooking.setProgress(1);
                updateAddressInformation();
            }
        };

        mBookingTask = ProfileManager.changeStandardInformation(person, afterTask);
    }

    //Step 2
    private void updateAddressInformation(){
        Person person = mBundleContact.getParcelable(BookingContactFragment.ARG_PERSON);

        SimpleRequestTask afterTask = new SimpleRequestTask() {
            @Override
            public void doTask(int statusCode) {
                mBookingTask = null;
                if(!ApiConnection.isSuccessCode(statusCode)){
                    bookingFailed();
                    return;
                }
                pgdBooking.setProgress(2);
                updateBondInformation();
            }
        };

        mBookingTask = ProfileManager.changeAddress(person, afterTask);
    }

    //Step 3
    private void updateBondInformation(){
        //TODO
        pgdBooking.setProgress(3);
        registerBooking();
    }

    //Step 4
    private void registerBooking(){
        Person contactPerson = mBundleContact.getParcelable(BookingContactFragment.ARG_PERSON);
        List<Person> anonPersons = new LinkedList<Person>();
        int anonCount = mBundleParticipants.getInt(BookingParticipantsFragment.ARG_COUNT);
        for(int i=0; i<anonCount; i++){
            anonPersons.add((Person) mBundleParticipants.getParcelable(BookingParticipantsFragment.ARG_PERSON + i));
        }
        Person bondPerson = mBundleBond.getParcelable(BookingBondFragment.ARG_PERSON);

        SimpleRequestTask postTask = new SimpleRequestTask() {
            @Override
            public void doTask(int statusCode) {
                mBookingTask = null;
                if(ApiConnection.isSuccessCode(statusCode)){
                    pgdBooking.setProgress(4);
                    bookingSuccess();
                }
                else{
                    bookingFailed();
                }
            }
        };

        List<Person> correctAnons = new LinkedList<Person>();
        for(Person p : anonPersons){
            correctAnons.add(p);
        }

        List<Long> personIds = new LinkedList<Long>();
        Iterator<Person> itAnons = correctAnons.iterator();
        while(itAnons.hasNext()){
            Person p = itAnons.next();
            if(p.getId() >= 0){
                personIds.add(p.getId());
                itAnons.remove();
            }
        }

        mBookingTask = BookingManager.bookPeople(mCampId,
                contactPerson, personIds, correctAnons, new Person[]{bondPerson}, postTask);
    }

    private void bookingSuccess(){
        pgdBooking.dismiss();

        AlertDialog.OnCancelListener cancelListener = new AlertDialog.OnCancelListener(){
            @Override
            public void onCancel(DialogInterface dialog) {
                onwardToProfileBookings();
            }
        };
        AlertDialog.OnClickListener okListener = new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                onwardToProfileBookings();
            }
        };

        new AlertDialog.Builder(this)
                .setMessage(R.string.booking_success)
                .setNeutralButton(R.string.ok, okListener)
                .setOnCancelListener(cancelListener).create().show();
    }

    private void onwardToProfileBookings(){
        Intent i = new Intent(BookingActivity.this, MainActivity.class);
        i.putExtra(MainActivity.EXTRA_START_PAGE, MainActivity.VALUE_PAGE_ACCOUNT);
        i.putExtra(MainActivity.EXTRA_ACCOUNT_START_PAGE, FragmentAccount.PAGE_BOOKING_HISTORY);
        startActivity(i);
        finish();
    }

    private void bookingFailed(){
        bookingFailed(getString(R.string.booking_failed));
    }

    private void bookingFailed(String message){
        pgdBooking.dismiss();
        pgdBooking = null;
        new AlertDialog.Builder(this)
                .setMessage(message)
                .setNeutralButton(R.string.ok, null).create().show();
    }

    private void changeFragment(Fragment fragment){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.flyMainFragment, fragment, TAG_FRAGMENT);
        ft.commit();
    }

    private void saveBundleCurrentFragment(){
        Fragment curFragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
        if(curFragment != null){
            ((MySaveableFragment) curFragment).passBundleToParent();
        }
    }

//    private void detachFragment(){
//        Fragment f = getFragmentManager().findFragmentByTag(TAG_FRAGMENT);
//        if(f == null){
//            return;
//        }
//        FragmentTransaction ft = getFragmentManager().beginTransaction();
//        ft.remove(f);
//        ft.commit();
//    }

    private int getParticipantCount(){
        if(mBundleParticipants == null
                || !mBundleParticipants.containsKey(BookingParticipantsFragment.ARG_COUNT)){
            return 0;
        }
        return mBundleParticipants.getInt(BookingParticipantsFragment.ARG_COUNT);
    }

    @Override
    public void cancelBooking() {
        AlertDialog.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(which == AlertDialog.BUTTON_POSITIVE){
                    finish();
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(getString(R.string.question_cancel_booking));
        builder.setPositiveButton(R.string.yes, listener);
        builder.setNegativeButton(R.string.no, listener);
        builder.create().show();
    }

    public BigDecimal getCalculatedPrice(){
        //Camp price * participantcount (temporary)
        int participantCount = mBundleParticipants.getInt(BookingParticipantsFragment.ARG_COUNT);
        Camp camp = JoetzApplication.getContext().getCampManager().findCampById(mCampId);
        return camp.getPrice().multiply(new BigDecimal(participantCount));
    }

    @Override
    public void passContactBundle(Bundle bundle){
        mBundleContact = bundle;
    }

    @Override
    public void passBondBundle(Bundle bundle) {
        mBundleBond = bundle;
    }

    @Override
    public void passParticipantsBundle(Bundle bundle) {
        mBundleParticipants = bundle;
    }

    @Override
    public void passExtraBundle(Bundle bundle) {
        mBundleExtra = bundle;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            // This ID represents the Home or Up button. In the case of this
            cancelBooking();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(pgdBooking != null){
            pgdBooking.dismiss();
        }
        if(mBookingTask != null){
            mBookingTask.cancelTask();
        }
    }
}




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