Back to project page Joetz-Android-V2.
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.
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 { } }