Android Open Source - Joetz-Android-V2 Activity 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   ww  w  .j  av a2s . co  m*/
import com.example.jens.myapplication.activities.JoetzActivity;
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.Person;
import com.example.jens.myapplication.domain.User;
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.util.LinkedList;
import java.util.List;

/**
 * Manager to manage the list of activities
 */
public class ActivityManager {
    private static final String URL_ACTIVITIES = "api/activities";
    private static final String URL_REGISTER = "api/activities/%d/register";
    private static final String URL_DECLINE = "api/activities/%d/register";

    private List<JoetzActivity> activities;
    private LinkedList<SimpleRequestTask> activityListeners;
    private CancellableTask activityTask;

    public ActivityManager(){

    }

    /**
     * Retrieve all the activities from the back-end
     * @param afterTask The task to be executed when the activities are loaded
     * @return
     */
    public CancellableTask retrieveActivities(final SimpleRequestTask afterTask){
        if(activityTask != null){
            if(afterTask != null){
                activityListeners.add(afterTask);
            }
            return activityTask;
        }

        activityListeners = new LinkedList<>();
        if(afterTask != null){
            activityListeners.add(afterTask);
        }

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

        PostRequestTask<List<JoetzActivity>> postTask = new PostRequestTask<List<JoetzActivity>>() {
            @Override
            public void doTask(List<JoetzActivity> obj, int statusCode) {
                activities = obj;

                for(SimpleRequestTask task : activityListeners){
                    task.doTask(statusCode);
                }

                activityListeners = null;
                activityTask = null;
            }
        };

        RequestParams params = new RequestParams()
                .setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey());

        activityTask = ApiConnection.get(URL_ACTIVITIES, jsonTask, postTask, params);
        return activityTask;
    }

    public void addAcceptedUser(long activityId, Person person){
        findActivityById(activityId).getAcceptedUsers().add(person);
    }

    /**
     * Remove a listener from the tasks listening to the task retrieving all the activities
     * @param task listener to be removed
     */
    public void unsubscribeListActivities(SimpleRequestTask task){
        if(activityListeners == null){
            return;
        }

        activityListeners.remove(task);
    }

    /**
     * Registers the user for an activity
     * @param activityId The activity ID of the activity the user wants to sign up for
     * @param afterTask The task to be executed when the request is finished
     * @return The task
     */
    public static CancellableTask registerForActivity(final long activityId, final SimpleRequestTask afterTask){
        String action = String.format(URL_REGISTER, activityId);

        RequestParams params = new RequestParams()
                .setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey());

        PostRequestTask<User> postTask = new PostRequestTask<User>() {
            @Override
            public void doTask(User obj, int statusCode) {
                if(ApiConnection.isSuccessCode(statusCode)){
                    JoetzApplication.getContext().getActivityManager().addAcceptedUser(
                            activityId, JoetzApplication.getContext().getLoginManager().getUser().getPerson().clone());
                }
                JsonUtils.createSimpleRefreshUserTask(afterTask).doTask(obj, statusCode);
            }
        };

        return ApiConnection.post(action, JsonUtils.createSimpleUserTask(),
                postTask, params);
    }

    /**
     * Declines the activity for the user
     * @param activityId The activity ID of the activity the user wants to decline
     * @param afterTask The task to be executed when the request is finished
     * @return The task
     */
    public static CancellableTask declineActivity(long activityId, SimpleRequestTask afterTask){
        String action = String.format(URL_DECLINE, activityId);

        RequestParams params = new RequestParams()
                .setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey());

        return ApiConnection.delete(action, JsonUtils.createSimpleUserTask(),
                JsonUtils.createSimpleRefreshUserTask(afterTask), params);
    }

    /**
     * Find an activity based on the activity's ID
     * @param id ID of the target activity
     * @return JoetzActivity if found, null if not found
     */
    public JoetzActivity findActivityById(long id){
        if(activities == null){
            return null;
        }
        for(JoetzActivity a : activities){
            if(a.getId() == id){
                return a;
            }
        }
        return null;
    }

    /**
     * Get a list of all the activities if they are loaded
     * @return list of activities or null if they are not loaded
     */
    public List<JoetzActivity> getActivities() {
        return activities;
    }

    /**
     *
     * @return true if the activities are loaded
     */
    public boolean isActivitiesLoaded(){
        return activities != null;
    }
}




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