Android Open Source - Joetz-Android-V2 Activity Detail 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.activities;
//w  w  w .  j a v a2s. co m
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.jens.myapplication.R;
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.ActivityManager;
import com.example.jens.myapplication.domain.Person;
import com.example.jens.myapplication.domain.User;
import com.example.jens.myapplication.sam.JoetzApplication;
import com.example.jens.myapplication.util.DateTimeStringConverter;

import java.util.Iterator;

/**
 * Fragment display the details of an activity
 */
public class ActivityDetailFragment extends Fragment {
    private static final String ARG_ACTIVITY_ID = "argActivityId";

//    private OnFragmentInteractionListener mListener;

    private JoetzActivity mJActivity;

    private TextView mTxtDate;
    private TextView mTxtLocation;
    private TextView mTxtSpotsAvailable;
    private TextView mTxtSummary;

    private TextView mTxtDescription;
    private LinearLayout mLlyAcceptedMonitors;

    private Button mBtnAccept;
    private Button mBtnDecline;
    private CardView mCardChosenInfo;
    private TextView mTxtChosenInfo;

    private boolean fullyBooked;

    private CancellableTask mTask;

    /**
     *
     * @param activityId The ID of the activity whose details should be displayed
     * @return
     */
    public static ActivityDetailFragment newInstance(long activityId) {
        ActivityDetailFragment fragment = new ActivityDetailFragment();
        Bundle b = new Bundle();
        b.putLong(ARG_ACTIVITY_ID, activityId);
        fragment.setArguments(b);
        return fragment;
    }

    public ActivityDetailFragment() {
        // Required empty public constructor
    }

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

        ActivityManager manager = JoetzApplication.getContext().getActivityManager();
        long activityId = getArguments().getLong(ARG_ACTIVITY_ID);

        mJActivity = manager.findActivityById(activityId);
        getActivity().setTitle(mJActivity.getTitle());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_activity_detail, container, false);

        mTxtDate = (TextView) rootView.findViewById(R.id.txtDate);
        mTxtLocation = (TextView) rootView.findViewById(R.id.txtLocation);
        mTxtSpotsAvailable = (TextView) rootView.findViewById(R.id.txtMaxParticipants);
        mTxtSummary = (TextView) rootView.findViewById(R.id.txtSummary);

        mTxtDescription = (TextView) rootView.findViewById(R.id.txtDescription);
        mLlyAcceptedMonitors = (LinearLayout) rootView.findViewById(R.id.llyAcceptedMonitors);

        mTxtDate.setText(DateTimeStringConverter.getSimpleDate(mJActivity.getStartDate()) +
            " - " + DateTimeStringConverter.getSimpleDate(mJActivity.getEndDate()));
        mTxtLocation.setText(mJActivity.getLocation());
        mTxtSpotsAvailable.setText(getString(R.string.max_participants,
                mJActivity.getMaxParticipants()));

        setSpotsAvailableText();

        mTxtSummary.setText(mJActivity.getSummary());
        mTxtDescription.setText(mJActivity.getDescription());

        fillAcceptedMonitors();

        mBtnAccept = (Button) rootView.findViewById(R.id.btnAccept);
        mBtnDecline = (Button) rootView.findViewById(R.id.btnDecline);

        mBtnAccept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                acceptActivity();
            }
        });

        mBtnDecline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                declineActivity();
            }
        });

        mCardChosenInfo = (CardView) rootView.findViewById(R.id.cardHasChosenInfo);
        mTxtChosenInfo = (TextView) rootView.findViewById(R.id.txtHasChosenInfo);

        checkHasChosenActivity();

        return rootView;
    }

    /**
     * Create a new textview to be added in the list of monitors
     * @param text
     * @return
     */
    private TextView createTextView(String text){
        TextView newText = new TextView(getActivity());
        newText.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        newText.setPadding(0,7,0,7);
        newText.setText(text);
        return newText;
    }

    /**
     * Set the text for available spots
     */
    private void setSpotsAvailableText(){
        int maxParticipants = mJActivity.getMaxParticipants();
//        int curParticipants = mJActivity.getInvitedUsers().size()
//                + mJActivity.getAcceptedUsers().size();
        int curParticipants = mJActivity.getAcceptedUsers().size();
        int available = maxParticipants - curParticipants;
        String maxText;
        if(available > 1){
            maxText = getString(R.string.$_free_places, available);
        }else if(available == 1){
            maxText = getString(R.string.one_free_place);
        }else{
            maxText = getString(R.string.activity_is_fully_booked);
            fullyBooked = true;
        }
        mTxtSpotsAvailable.setText(maxText);
    }

    /**
     * Fill the list of accepted monitors
     */
    private void fillAcceptedMonitors(){
        mLlyAcceptedMonitors.removeAllViews();

        if(mJActivity.getAcceptedUsers().size() < 1){
            mLlyAcceptedMonitors.addView(createTextView(getString(R.string.no_registered_monitors)));
        }else{
            Iterator<Person> it = mJActivity.getAcceptedUsers().iterator();
            while(it.hasNext()){
                final Person p = it.next();
//                if(it.hasNext()){
                String name = p.getFirstName() + " " + p.getLastName();
                TextView textView = createTextView(name);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Ability to open more information about monitors
                        openMonitorInfo(p);
                    }
                });
                mLlyAcceptedMonitors.addView(textView);
//                }
            }
        }
    }

    /**
     * Accept this activity (user(monitor) wants to go to this activity)
     */
    private void acceptActivity(){
        if(mTask != null){
            return;
        }

        setButtonsEnabled(false);

        final ProgressDialog pgdProgress = new ProgressDialog(getActivity());
        pgdProgress.setMessage(getString(R.string.accepting_activity));
        pgdProgress.setCancelable(false);
        pgdProgress.show();

        SimpleRequestTask afterTask = new SimpleRequestTask() {
            @Override
            public void doTask(int statusCode) {
                mTask = null;
                setButtonsEnabled(true);
                pgdProgress.dismiss();
                //int msgStringId;
                if(ApiConnection.isSuccessCode(statusCode)){
                    setSpotsAvailableText();
                    fillAcceptedMonitors();
                   // msgStringId = R.string.activity_register_success;
                }else{
                    //msgStringId = R.string.activity_register_failed;
                }
                checkHasChosenActivity();
                /*new AlertDialog.Builder(getActivity())
                        .setMessage(msgStringId)
                        .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                checkHasChosenActivity();
                            }
                        })
                        .create().show();*/
            }
        };

        mTask = ActivityManager.registerForActivity(mJActivity.getId(), afterTask);
    }

    /**
     * User(monitor) does not want to go to this activity
     */
    private void declineActivity(){
        if(mTask != null){
            return;
        }

        setButtonsEnabled(false);

        final ProgressDialog pgdProgress = new ProgressDialog(getActivity());
        pgdProgress.setMessage(getString(R.string.declining_activity));
        pgdProgress.setCancelable(false);
        pgdProgress.show();

        SimpleRequestTask afterTask = new SimpleRequestTask() {
            @Override
            public void doTask(int statusCode) {
                pgdProgress.dismiss();
                mTask = null;
                setButtonsEnabled(true);
                //int msgStringId;
                if(ApiConnection.isSuccessCode(statusCode)){
                    //msgStringId = R.string.activity_decline_success;
                }else{
                    //msgStringId = R.string.activity_decline_failed;
                }
                checkHasChosenActivity();
                /*new AlertDialog.Builder(getActivity())
                        .setMessage(msgStringId)
                        .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                checkHasChosenActivity();
                            }
                        })
                        .create().show();*/
            }
        };

        mTask = ActivityManager.declineActivity(mJActivity.getId(), afterTask);
    }

    private void setButtonsEnabled(boolean enabled){
        mBtnAccept.setEnabled(enabled);
        mBtnDecline.setEnabled(enabled);
    }

    /**
     * Check what the user currently wants to do with this activity
     * If he has not decided yet, the buttons are shown
     * If he has made a decision before, a textview will show him what he chose
     */
    private void checkHasChosenActivity(){
        User u = JoetzApplication.getContext().getLoginManager().getUser();
        if(u.getInvitedActivities().contains(mJActivity.getId())){
            if(fullyBooked){
                mBtnAccept.setVisibility(View.GONE);
                mBtnDecline.setVisibility(View.GONE);
                mTxtChosenInfo.setBackgroundColor(getResources().getColor(R.color.state_red));
                mTxtChosenInfo.setText(R.string.activity_is_fully_booked);
                mCardChosenInfo.setVisibility(View.VISIBLE);
                return;
            }
            //mTxtChosenInfo.setBackgroundColor(getResources().getColor(R.color.state_yellow));
            //mCardChosenInfo.setVisibility(View.VISIBLE);
            return;
        }

        boolean accepted = u.getAcceptedActivities().contains(mJActivity.getId());

        mBtnAccept.setVisibility(View.GONE);
        mBtnDecline.setVisibility(View.GONE);

        int stringId = accepted ? R.string.activity_accepted : R.string.activity_declined;
        mTxtChosenInfo.setText(stringId);

        if(accepted){
            mTxtChosenInfo.setBackgroundColor(getResources().getColor(R.color.state_green));
        }else{
            mTxtChosenInfo.setBackgroundColor(getResources().getColor(R.color.state_red));
        }
        mCardChosenInfo.setVisibility(View.VISIBLE);
    }

    /**
     * Show monitor info dialog
     * @param person Person object of the monitor to be shown
     */
    private void openMonitorInfo(Person person){
        String separator = System.getProperty("line.separator");

        String message = getString(R.string.name)
                + separator + "\t" + person.getFirstName() + " " + person.getLastName()
                + separator + separator + getString(R.string.email_address)
                + separator + "\t" + person.getEmail()
                + separator + separator + getString(R.string.telephone_number)
                + separator + "\t" + person.getPhone();

        new AlertDialog.Builder(getActivity())
                .setMessage(message)
                .setTitle(R.string.monitor_information)
                .setNeutralButton(R.string.ok, null)
                .create().show();
    }

   /* @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();
        mListener = null;
    }*/

    public interface OnFragmentInteractionListener {

    }

}




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