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.camps; /*w w w .ja v a 2s. c om*/ import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.example.jens.myapplication.R; import com.example.jens.myapplication.apimanager.ApiConnection; import com.example.jens.myapplication.apimanager.SimpleRequestTask; import com.example.jens.myapplication.apimanager.manager.CampManager; import com.example.jens.myapplication.apimanager.manager.LoginManager; import com.example.jens.myapplication.domain.Camp; import com.example.jens.myapplication.domain.PeriodCategory; import com.example.jens.myapplication.sam.JoetzApplication; import com.example.jens.myapplication.sam.MyFragment; import com.example.jens.myapplication.util.ActivityUtils; import org.joda.time.DateTime; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * Created by Jens on 8/11/2014. */ public class FragmentListCamps extends MyFragment { private static final String ARG_FAVO = "argfavos"; private MenuItem mMenuItemFilter; private MenuItem mMenuItemRefresh; private ProgressBar mSpinner; private View mViewError; private RelativeLayout mRlyNoCamps; private ImageView mImgTutorial; private TextView mTxtNoCamps; private Button mBtnTryAgain; private RecyclerView mRecyclerView; private CampAdapter mAdapter; private GridLayoutManager mGridLayoutManager; private CampManager mCampManager; private SimpleRequestTask mListenerLoadCamps; private Callbacks mCallbacks = sCampCallbacks; private boolean mShowFavorites; private List<Camp> mCamps; private int mFilterMinPrice = Integer.MIN_VALUE; private int mFilterMaxPrice = Integer.MAX_VALUE; private int mFilterMinAge = Integer.MIN_VALUE; private int mFilterMaxAge = Integer.MAX_VALUE; private PeriodCategory mFilterPeriod = null; public static FragmentListCamps newInstance(){ return newInstance(false); } /** * * @param favorites true if only the bookmarked camps should be shown * @return */ public static FragmentListCamps newInstance(boolean favorites){ FragmentListCamps fragment = new FragmentListCamps(); Bundle b = new Bundle(); b.putBoolean(ARG_FAVO, favorites); fragment.setArguments(b); return fragment; } public FragmentListCamps() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); Bundle args = getArguments(); if(args != null){ if(args.containsKey(ARG_FAVO)){ mShowFavorites = args.getBoolean(ARG_FAVO); } } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_camplist, container, false); mSpinner = (ProgressBar) view.findViewById(R.id.progressBar1); mSpinner.setVisibility(View.VISIBLE); mViewError = view.findViewById(R.id.llyError); // mTxtNoCamps = (TextView) view.findViewById(R.id.txtNoCamps); mRlyNoCamps = (RelativeLayout) view.findViewById(R.id.rlyNoCamps); mImgTutorial = (ImageView) view.findViewById(R.id.imgTutorialBookmark); mTxtNoCamps = (TextView) view.findViewById(R.id.txtNoCamps); mBtnTryAgain = (Button) view.findViewById(R.id.btnTryAgain); mBtnTryAgain.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { retrieveCamps(); } }); mRecyclerView = (RecyclerView) view.findViewById(R.id.list); mGridLayoutManager = new GridLayoutManager(getActivity(), ActivityUtils.getSpanCount(getActivity())); mRecyclerView.setLayoutManager(mGridLayoutManager); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); mAdapter = new CampAdapter(mCamps, R.layout.item_camp, getActivity(), mGridLayoutManager); mRecyclerView.setAdapter(mAdapter); mRecyclerView.addOnItemTouchListener( new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Camp c = mAdapter.getItem(position); // Intent i = new Intent(getActivity(),MainActivity.class); // i.putExtra(FragmentCampDetail.EXTRA_CAMP_ID, c.getId()); // startActivity(i); mCallbacks.onCampSelected(c.getId()); } }) ); retrieveCamps(); return view; } @Override public void onAttach(Activity activity) { super.onAttach(activity); // Activities containing this fragment must implement its callbacks. if (!(activity instanceof Callbacks)) { throw new IllegalStateException("Activity must implement fragment's callbacks."); } mCallbacks = (Callbacks) activity; } @Override public void onDetach() { super.onDetach(); // Reset the active callbacks interface to the dummy implementation. mCallbacks = sCampCallbacks; } /** * Turns on activate-on-click mode. When this mode is on, list items will be * given the 'activated' state when touched. */ public interface Callbacks { /** * Callback for when an item has been selected. */ public void onCampSelected(Long id); } private static Callbacks sCampCallbacks = new Callbacks() { public void onCampSelected(Long id) { } }; // SAMYBOY /** * Retrieve the camps from the server */ private void retrieveCamps() { // showInfoText(getString(R.string.loading_camps)); mViewError.setVisibility(View.GONE); mSpinner.setVisibility(View.VISIBLE); mRlyNoCamps.setVisibility(View.GONE); mCampManager = ((JoetzApplication) getActivity().getApplication()).getCampManager(); if (mCampManager.isCampsLoaded()) { loadCamps(); return; } mListenerLoadCamps = new SimpleRequestTask() { @Override public void doTask(int statusCode) { mListenerLoadCamps = null; mSpinner.setVisibility(View.GONE); if (statusCode < 0) { mViewError.setVisibility(View.VISIBLE); } else if (statusCode != ApiConnection.STATUS_OK) { mViewError.setVisibility(View.VISIBLE); Toast.makeText(getActivity(), R.string.error_unknown, Toast.LENGTH_SHORT).show(); } else { loadCamps(); } } }; mCampManager.retrieveCamps(mListenerLoadCamps); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.action_filter) { openFilters(); return true; } if (item.getItemId() == R.id.action_refresh){ refreshCamps(); return true; } return false; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { mMenuItemFilter = menu.findItem(R.id.action_filter); mMenuItemRefresh = menu.findItem(R.id.action_refresh); if(mMenuItemFilter != null && mCamps != null){ mMenuItemFilter.setVisible(true); mMenuItemRefresh.setVisible(true); } super.onCreateOptionsMenu(menu, inflater); } /** * Open the filter dialog */ private void openFilters() { FragmentManager fm = getActivity().getSupportFragmentManager(); CampManager mgr = JoetzApplication.getContext().getCampManager(); FilterFragment filterFragment = FilterFragment.newInstance( mgr.getMinPrice().intValue(), mgr.getMaxPrice().intValue(), mFilterMinPrice, mFilterMaxPrice, mgr.getMinAge(), mgr.getMaxAge(), mFilterMinAge, mFilterMaxAge, mFilterPeriod); filterFragment.setTargetFragment(this, FilterFragment.FRAGMENT_CODE); filterFragment.show(fm, "filterdialog"); } /** * Clear the camps and retrieve them again */ private void refreshCamps(){ mMenuItemRefresh.setVisible(false); mMenuItemFilter.setVisible(false); mCampManager.clearCamps(); mRecyclerView.setVisibility(View.GONE); retrieveCamps(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { Bundle b = data.getExtras(); mFilterMinPrice = b.getInt(CampFilterActivity.RES_PRICE_MIN); mFilterMaxPrice = b.getInt(CampFilterActivity.RES_PRICE_MAX); mFilterMinAge = b.getInt(CampFilterActivity.RES_AGE_MIN); mFilterMaxAge = b.getInt(CampFilterActivity.RES_AGE_MAX); } if(requestCode == FilterFragment.FRAGMENT_CODE){ Bundle b = data.getExtras(); mFilterMinPrice = b.getInt(FilterFragment.RES_PRICE_MIN); mFilterMaxPrice = b.getInt(FilterFragment.RES_PRICE_MAX); mFilterMinAge = b.getInt(FilterFragment.RES_AGE_MIN); mFilterMaxAge = b.getInt(FilterFragment.RES_AGE_MAX); mFilterPeriod = (PeriodCategory) b.getSerializable(FilterFragment.RES_PERIOD_CATEGORY); loadCamps(); } } @Override public void onResume() { super.onResume(); LoginManager mgr = JoetzApplication.getContext().getLoginManager(); //If favorites changed, reload and re-filter the list of camps if(mCamps != null && mShowFavorites && mgr.isLoggedIn() && mCamps.size() != mgr.getUser().getBookmarkedCamps().size()){ loadCamps(); } } /** * Get the camps, filter the camps, sort the camps and put them in the list */ private void loadCamps() { mRlyNoCamps.setVisibility(View.GONE); if(mAdapter != null){ mAdapter.clearImagesMap(); } List<Camp> allCamps; LoginManager loginManager = JoetzApplication.getContext().getLoginManager(); if(mShowFavorites && loginManager.isLoggedIn()){ List<Long> listBookmarks = loginManager.getUser().getBookmarkedCamps(); allCamps = new ArrayList<Camp>(listBookmarks.size()); for(long id : listBookmarks){ allCamps.add(mCampManager.findCampById(id)); } }else{ allCamps = mCampManager.getCamps(); } if(allCamps == null){ return; } mRecyclerView.setVisibility(View.VISIBLE); mCamps = new ArrayList<Camp>(); for(Camp c : allCamps){ //Filter only future camps if(c.getStartDate().compareTo(DateTime.now()) < 0){ continue; } //Filter price if(c.getPrice().compareTo(new BigDecimal(mFilterMaxPrice)) > 0 || c.getPrice().compareTo(new BigDecimal(mFilterMinPrice)) < 0){ continue; } //Filter age if(c.getMaximumAge() < mFilterMinAge || c.getMinimumAge()-1 > mFilterMaxAge){ continue; } //Filter period if(mFilterPeriod != null && c.getPeriodCategory() != mFilterPeriod){ continue; } mCamps.add(c); } Collections.sort(mCamps, new Comparator<Camp>() { @Override public int compare(Camp lhs, Camp rhs) { int i = lhs.getStartDate().compareTo(rhs.getStartDate()); if(i == 0){ return lhs.getEndDate().compareTo(rhs.getEndDate()); }else{ return i; } } }); if(mMenuItemFilter != null && mCamps.size() > 0){ mMenuItemFilter.setVisible(true); } if(mMenuItemRefresh != null){ mMenuItemRefresh.setVisible(true); } mAdapter = new CampAdapter(mCamps, R.layout.item_camp, getActivity(), mGridLayoutManager); mRecyclerView.setAdapter(mAdapter); mSpinner.setVisibility(View.GONE); if(mCamps.size() == 0){ mRecyclerView.setVisibility(View.GONE); mRlyNoCamps.setVisibility(View.VISIBLE); if(mShowFavorites){ mImgTutorial.setVisibility(View.VISIBLE); }else{ mTxtNoCamps.setText(R.string.no_camps_found); } } } @Override public void onPause() { super.onPause(); mAdapter.clearImagesMap(); } @Override public void onDestroy() { super.onDestroy(); if(mListenerLoadCamps != null){ mCampManager.unsubscribeLoadCamps(mListenerLoadCamps); } } }