Android Open Source - Joetz-Android-V2 Camp Manager






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.apimanager.manager;
//from w  w w  .  j  av  a 2  s .  c  om
import com.example.jens.myapplication.apimanager.ApiConnection;
import com.example.jens.myapplication.apimanager.CancellableTask;
import com.example.jens.myapplication.apimanager.JSONParsingTask;
import com.example.jens.myapplication.apimanager.PostRequestTask;
import com.example.jens.myapplication.apimanager.RequestParams;
import com.example.jens.myapplication.apimanager.SimpleRequestTask;
import com.example.jens.myapplication.domain.Camp;
import com.example.jens.myapplication.sam.JoetzApplication;
import com.example.jens.myapplication.util.JsonUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Sam on 23/10/2014.
 */
public class CampManager {
    private static final String URL_CAMPS = "api/camps";
    private static final String URL_BOOKMARK = "api/camps/%d/bookmark";

    private List<Camp> camps;
    private CancellableTask currentTask;
    private LinkedList<SimpleRequestTask> campListListeners;

    //for quicker finding of camps
    private Camp lastSearchedForCamp;

    private BigDecimal minPrice;
    private BigDecimal maxPrice;

    private int minAge;
    private int maxAge;

    public CampManager(){
    }

    public List<Camp> getCamps(){
        return camps;
    }

    public boolean isCampsLoaded(){
        return camps != null;
    }

    public Camp findCampById(long id){
        if(lastSearchedForCamp != null && lastSearchedForCamp.getId() == id){
            return lastSearchedForCamp;
        }
        if(camps == null){
            return null;
        }
        for(Camp c : camps){
            if(c.getId() == id){
                lastSearchedForCamp = c;
                return c;
            }
        }
        return null;
    }

    public void unsubscribeLoadCamps(SimpleRequestTask task){
        if(campListListeners == null){
            return;
        }

        campListListeners.remove(task);
    }

    public CancellableTask retrieveCamps(SimpleRequestTask postTask){
        if(currentTask != null){
            if(postTask != null){
                campListListeners.add(postTask);
            }
            return currentTask;
        }

        campListListeners = new LinkedList<>();
        if(postTask != null){
            campListListeners.add(postTask);
        }

        JSONParsingTask<List<Camp>> jsonTask = new JSONParsingTask<List<Camp>>() {
            @Override
            public List<Camp> handleJSON(String json) throws IOException {
                ObjectMapper mapper = new ObjectMapper();
                TypeFactory typeFactory = mapper.getTypeFactory();
                return mapper.readValue(json,
                        typeFactory.constructCollectionType(List.class, Camp.class));
            }
        };

        PostRequestTask<List<Camp>> campsTask = new PostRequestTask<List<Camp>>(){
            @Override
            public void doTask(List<Camp> obj, int statusCode) {
                if(statusCode == ApiConnection.STATUS_OK){
                    camps = obj;
                    if(camps.size() > 0){
                        Camp firstCamp = camps.get(0);
                        minPrice = firstCamp.getPrice();
                        maxPrice = firstCamp.getPrice();
                        minAge = firstCamp.getMinimumAge();
                        maxAge = firstCamp.getMaximumAge();
                        for(Camp c : camps){
                            //Price
                            if(minPrice.compareTo(c.getPrice()) > 0){
                                minPrice = c.getPrice();
                            }
                            if(maxPrice.compareTo(c.getPrice()) < 0){
                                maxPrice = c.getPrice();
                            }

                            if(c.getMinimumAge() < minAge){
                                minAge = c.getMinimumAge();
                            }
                            if(c.getMaximumAge() > maxAge){
                                maxAge = c.getMaximumAge();
                            }
                        }
                    }
                }

                for(SimpleRequestTask anotherTask : campListListeners){
                    anotherTask.doTask(statusCode);
                }
                campListListeners = null;

                currentTask = null;
            }
        };


        currentTask = ApiConnection.get(URL_CAMPS, jsonTask, campsTask, null);
        return currentTask;

    }

    public CancellableTask toggleBookmark(long campId, final SimpleRequestTask afterTask){
        String authKey = JoetzApplication.getContext().getLoginManager().getAuthKey();
        if(authKey == null){
            throw new IllegalStateException("need to be logged in to toggle bookmarks");
        }

        String url = String.format(URL_BOOKMARK, campId);

        return ApiConnection.post(url, null, JsonUtils.createSimpleDoAfterTask(afterTask),
                new RequestParams().setAuthKey(authKey));
    }

    public void clearCamps(){
        camps = null;
        lastSearchedForCamp = null;
        minPrice = null;
        maxPrice = null;
        minAge = Integer.MAX_VALUE;
        maxAge = Integer.MIN_VALUE;
    }

    public BigDecimal getMinPrice() {
        return minPrice;
    }

    public BigDecimal getMaxPrice() {
        return maxPrice;
    }

    public int getMinAge() {
        return minAge;
    }

    public int getMaxAge() {
        return maxAge;
    }
}




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