Android Open Source - Joetz-Android-V2 Fragment List Camps






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.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);
        }
    }
}




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