|
| 1 | +package ch.hsr.mge.gadgeothek; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Fragment; |
| 5 | +import android.content.Context; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.view.LayoutInflater; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewGroup; |
| 10 | +import android.widget.Button; |
| 11 | +import android.widget.ImageView; |
| 12 | +import android.widget.TextView; |
| 13 | + |
| 14 | +import ch.hsr.mge.gadgeothek.domain.Gadget; |
| 15 | +import ch.hsr.mge.gadgeothek.domain.Loan; |
| 16 | +import ch.hsr.mge.gadgeothek.domain.Reservation; |
| 17 | +import ch.hsr.mge.gadgeothek.service.IDecoratorService; |
| 18 | +import ch.hsr.mge.gadgeothek.service.LocalDecoratorService; |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * A simple {@link Fragment} subclass. |
| 23 | + * Activities that contain this fragment must implement the |
| 24 | + * {@link IHandleGadgetDetailFragment} interface |
| 25 | + * to handle interaction events. |
| 26 | + */ |
| 27 | +public class GadgetDetailFragment extends Fragment implements View.OnClickListener { |
| 28 | + |
| 29 | + private IHandleGadgetDetailFragment mListener; |
| 30 | + private IDecoratorService decorator; |
| 31 | + |
| 32 | + public enum DetailType { |
| 33 | + GADGET, RESERVATION, LOAN |
| 34 | + } |
| 35 | + |
| 36 | + TextView mInventoryNumber; |
| 37 | + TextView mNameView; |
| 38 | + TextView mManufacturerView; |
| 39 | + TextView mPrice; |
| 40 | + TextView mConditionView; |
| 41 | + ImageView mManufacturerLogoView; |
| 42 | + |
| 43 | + TextView mLoanSince; |
| 44 | + TextView mLoanSinceLabel; |
| 45 | + |
| 46 | + Button mAddReservation; |
| 47 | + Button mDeleteReservation; |
| 48 | + |
| 49 | + public GadgetDetailFragment() { |
| 50 | + // Required empty public constructor |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + @Override |
| 55 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 56 | + Bundle savedInstanceState) { |
| 57 | + decorator = LocalDecoratorService.getDecoratorService(); |
| 58 | + return inflater.inflate(R.layout.fragment_gadget_detail, container, false); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onViewCreated(View view, Bundle savedInstanceState) { |
| 64 | + super.onViewCreated(view, savedInstanceState); |
| 65 | + mInventoryNumber = (TextView) getView().findViewById(R.id.gadget_inventorynumber); |
| 66 | + mNameView = (TextView) getView().findViewById(R.id.gadget_name); |
| 67 | + mManufacturerView = (TextView) getView().findViewById(R.id.gadget_manufacturer); |
| 68 | + mPrice = (TextView) getView().findViewById(R.id.gadget_price); |
| 69 | + mConditionView = (TextView) getView().findViewById(R.id.gadget_condition); |
| 70 | + |
| 71 | + mLoanSince = (TextView) getView().findViewById(R.id.loan_since); |
| 72 | + mLoanSinceLabel = (TextView) getView().findViewById(R.id.labelLoanSince); |
| 73 | + |
| 74 | + mAddReservation = (Button) getView().findViewById(R.id.btn_add_reservation); |
| 75 | + mDeleteReservation = (Button) getView().findViewById(R.id.btn_delete_reservation); |
| 76 | + |
| 77 | + mManufacturerLogoView = (ImageView) getView().findViewById(R.id.gadget_manufacturer_logo); |
| 78 | + |
| 79 | + mAddReservation.setOnClickListener(this); |
| 80 | + mDeleteReservation.setOnClickListener(this); |
| 81 | + |
| 82 | + // |
| 83 | + Gadget gadget = null; |
| 84 | + switch (mListener.getDetailType()){ |
| 85 | + case GADGET: |
| 86 | + gadget = mListener.getDetailGadget(); |
| 87 | + |
| 88 | + mLoanSince.setVisibility(View.GONE); |
| 89 | + mLoanSinceLabel.setVisibility(View.GONE); |
| 90 | + |
| 91 | + mAddReservation.setVisibility(View.VISIBLE); |
| 92 | + mDeleteReservation.setVisibility(View.GONE); |
| 93 | + break; |
| 94 | + case RESERVATION: |
| 95 | + gadget = mListener.getDetailReservation().getGadget(); |
| 96 | + |
| 97 | + mLoanSince.setVisibility(View.GONE); |
| 98 | + mLoanSinceLabel.setVisibility(View.GONE); |
| 99 | + |
| 100 | + mAddReservation.setVisibility(View.GONE); |
| 101 | + mDeleteReservation.setVisibility(View.VISIBLE); |
| 102 | + break; |
| 103 | + case LOAN: |
| 104 | + gadget = mListener.getDetailLoan().getGadget(); |
| 105 | + |
| 106 | + mLoanSince.setText(mListener.getDetailLoan().getPickupDate().toString()); |
| 107 | + mLoanSince.setVisibility(View.VISIBLE); |
| 108 | + mLoanSinceLabel.setVisibility(View.VISIBLE); |
| 109 | + |
| 110 | + mAddReservation.setVisibility(View.GONE); |
| 111 | + mDeleteReservation.setVisibility(View.GONE); |
| 112 | + break; |
| 113 | + } |
| 114 | + |
| 115 | + mInventoryNumber.setText(gadget.getInventoryNumber()); |
| 116 | + mNameView.setText(gadget.getName()); |
| 117 | + mManufacturerView.setText(gadget.getManufacturer()); |
| 118 | + mPrice.setText(Double.toString(gadget.getPrice())); |
| 119 | + mConditionView.setText(gadget.getCondition().toString()); |
| 120 | + mManufacturerLogoView.setImageResource(decorator.getDrawableIdForManufacturerName(gadget.getManufacturer())); |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void onAttach(Context context) { |
| 126 | + super.onAttach(context); |
| 127 | + _onAttach_API_independent(context); |
| 128 | + } |
| 129 | + /** |
| 130 | + * Code duplication for API Level 22 support |
| 131 | + */ |
| 132 | + public void onAttach(Activity activity) { |
| 133 | + super.onAttach(activity); |
| 134 | + _onAttach_API_independent(activity); |
| 135 | + } |
| 136 | + |
| 137 | + private void _onAttach_API_independent(Context context){ |
| 138 | + if (context instanceof IHandleGadgetDetailFragment) { |
| 139 | + this.mListener = (IHandleGadgetDetailFragment) context; |
| 140 | + } else { |
| 141 | + throw new RuntimeException(context.toString() |
| 142 | + + " must implement IHandleGadgetDetailFragment"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void onDetach() { |
| 148 | + super.onDetach(); |
| 149 | + mListener = null; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void onClick(View view) { |
| 154 | + switch (view.getId()){ |
| 155 | + case R.id.btn_add_reservation: { |
| 156 | + mListener.onReserveGadget(); |
| 157 | + break; |
| 158 | + } |
| 159 | + case R.id.btn_delete_reservation: { |
| 160 | + mListener.onDeleteReservation(); |
| 161 | + break; |
| 162 | + } |
| 163 | + |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * This interface must be implemented by activities that contain this |
| 169 | + * fragment to allow an interaction in this fragment to be communicated |
| 170 | + * to the activity and potentially other fragments contained in that |
| 171 | + * activity. |
| 172 | + * <p> |
| 173 | + * See the Android Training lesson <a href= |
| 174 | + * "http://developer.android.com/training/basics/fragments/communicating.html" |
| 175 | + * >Communicating with Other Fragments</a> for more information. |
| 176 | + */ |
| 177 | + public interface IHandleGadgetDetailFragment { |
| 178 | + Gadget getDetailGadget(); |
| 179 | + Reservation getDetailReservation(); |
| 180 | + Loan getDetailLoan(); |
| 181 | + DetailType getDetailType(); |
| 182 | + void onReserveGadget(); |
| 183 | + void onDeleteReservation(); |
| 184 | + } |
| 185 | +} |
0 commit comments