mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-27 15:59:29 +00:00
Create SafeAsyncTasks manager
This commit is contained in:
parent
2a266aaa7a
commit
c66365b002
@ -50,6 +50,13 @@ public abstract class SafeAsyncTask<Params, Progress, Result>
|
|||||||
this.onPostExecuteListener = onPostExecuteListener;
|
this.onPostExecuteListener = onPostExecuteListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove current onPostExecuteListener
|
||||||
|
*/
|
||||||
|
public void removeOnPostExecuteListener(){
|
||||||
|
this.onPostExecuteListener = null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether the a OnPostExecuteListener has been set or not
|
* Check whether the a OnPostExecuteListener has been set or not
|
||||||
*
|
*
|
||||||
|
@ -0,0 +1,108 @@
|
|||||||
|
package org.communiquons.android.comunic.client.ui.asynctasks;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SafeAsyncTask manager
|
||||||
|
*
|
||||||
|
* Allows an easy management of AsyncTasks
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
public class SafeAsyncTasksManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of tasks
|
||||||
|
*/
|
||||||
|
private ArrayList<SafeAsyncTask> mTasks = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a task to the manager
|
||||||
|
*
|
||||||
|
* @param task The task to add
|
||||||
|
*/
|
||||||
|
public void addTask(@NonNull SafeAsyncTask task){
|
||||||
|
addTask(task, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a task to the manager
|
||||||
|
*
|
||||||
|
* @param task The task to add
|
||||||
|
* @param unsetSimilar Set to true to remove any similar class
|
||||||
|
*/
|
||||||
|
public void addTask(@NonNull SafeAsyncTask task, boolean unsetSimilar){
|
||||||
|
|
||||||
|
if(unsetSimilar)
|
||||||
|
unsetSpecificTasks(task.getClass());
|
||||||
|
|
||||||
|
mTasks.add(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first task that belongs to a specific class
|
||||||
|
*
|
||||||
|
* @param cls The kind of class to search for
|
||||||
|
* @return The first task, or null if none found
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public SafeAsyncTask getTask(Class<?> cls){
|
||||||
|
for(SafeAsyncTask task : mTasks){
|
||||||
|
if(task.getClass().equals(cls))
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a task from the manager
|
||||||
|
*
|
||||||
|
* @param task The task to removes
|
||||||
|
*/
|
||||||
|
public void removeTask(SafeAsyncTask task){
|
||||||
|
mTasks.remove(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unset a task
|
||||||
|
*
|
||||||
|
* @param task The task to unset
|
||||||
|
*/
|
||||||
|
public void unsetTask(@Nullable SafeAsyncTask task){
|
||||||
|
if(task != null) {
|
||||||
|
task.removeOnPostExecuteListener();
|
||||||
|
removeTask(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unset all running tasks
|
||||||
|
*/
|
||||||
|
public synchronized void unsetAllTasks(){
|
||||||
|
for(SafeAsyncTask task : mTasks){
|
||||||
|
if(task != null)
|
||||||
|
task.removeOnPostExecuteListener();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
mTasks = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unset only a specific kind of class
|
||||||
|
*
|
||||||
|
* @param cls The tasks to disable
|
||||||
|
*/
|
||||||
|
public void unsetSpecificTasks(Class<?> cls){
|
||||||
|
for (SafeAsyncTask task : mTasks){
|
||||||
|
if(task.getClass().equals(cls))
|
||||||
|
unsetTask(task);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package org.communiquons.android.comunic.client.ui.fragments;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.v4.app.Fragment;
|
||||||
|
|
||||||
|
import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTasksManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base fragment for the fragments of the application
|
||||||
|
*
|
||||||
|
* Integrates task manager
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
public abstract class AbstractFragment extends Fragment {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tasks manager
|
||||||
|
*/
|
||||||
|
private SafeAsyncTasksManager mTasksManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
mTasksManager = new SafeAsyncTasksManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
|
||||||
|
getTasksManager().unsetAllTasks();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the task manager associated with the fragment
|
||||||
|
*
|
||||||
|
* @return The task manager
|
||||||
|
*/
|
||||||
|
SafeAsyncTasksManager getTasksManager() {
|
||||||
|
return mTasksManager;
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,6 @@ import android.os.Bundle;
|
|||||||
import android.support.annotation.CallSuper;
|
import android.support.annotation.CallSuper;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.Fragment;
|
|
||||||
import android.support.v4.app.FragmentTransaction;
|
import android.support.v4.app.FragmentTransaction;
|
||||||
import android.support.v7.widget.DividerItemDecoration;
|
import android.support.v7.widget.DividerItemDecoration;
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
@ -56,7 +55,7 @@ import java.util.Objects;
|
|||||||
* Created by pierre on 3/18/18.
|
* Created by pierre on 3/18/18.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public abstract class AbstractPostsListFragment extends Fragment
|
public abstract class AbstractPostsListFragment extends AbstractFragment
|
||||||
implements onPostUpdateListener, OnScrollChangeDetectListener, PostsCreateFormFragment.OnPostCreated {
|
implements onPostUpdateListener, OnScrollChangeDetectListener, PostsCreateFormFragment.OnPostCreated {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,11 +24,6 @@ public class LatestPostsFragment extends AbstractPostsListFragment {
|
|||||||
*/
|
*/
|
||||||
private static final String TAG = "LatestPostsFragment";
|
private static final String TAG = "LatestPostsFragment";
|
||||||
|
|
||||||
/**
|
|
||||||
* Load posts task
|
|
||||||
*/
|
|
||||||
private GetLatestPostsTask mGetLatestPostsTask;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
@ -41,16 +36,7 @@ public class LatestPostsFragment extends AbstractPostsListFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
unset_all_load_tasks();
|
getTasksManager().unsetAllTasks();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unset all pending load tasks
|
|
||||||
*/
|
|
||||||
private void unset_all_load_tasks(){
|
|
||||||
if(mGetLatestPostsTask != null)
|
|
||||||
mGetLatestPostsTask.setOnPostExecuteListener(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,25 +46,28 @@ public class LatestPostsFragment extends AbstractPostsListFragment {
|
|||||||
*/
|
*/
|
||||||
private boolean is_loading_posts() {
|
private boolean is_loading_posts() {
|
||||||
|
|
||||||
return mGetLatestPostsTask != null &&
|
SafeAsyncTask task = getTasksManager().getTask(GetLatestPostsTask.class);
|
||||||
mGetLatestPostsTask.hasOnPostExecuteListener() &&
|
|
||||||
!mGetLatestPostsTask.isCancelled() &&
|
return task != null &&
|
||||||
mGetLatestPostsTask.getStatus() != AsyncTask.Status.FINISHED;
|
task.hasOnPostExecuteListener() &&
|
||||||
|
!task.isCancelled() &&
|
||||||
|
task.getStatus() != AsyncTask.Status.FINISHED;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoadPosts() {
|
public void onLoadPosts() {
|
||||||
//Get the list of latest posts
|
//Get the list of latest posts
|
||||||
unset_all_load_tasks();
|
getTasksManager().unsetSpecificTasks(GetLatestPostsTask.class);
|
||||||
mGetLatestPostsTask = new GetLatestPostsTask(getActivity());
|
GetLatestPostsTask getLatestPostsTask = new GetLatestPostsTask(getActivity());
|
||||||
mGetLatestPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
|
getLatestPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
|
||||||
@Override
|
@Override
|
||||||
public void OnPostExecute(PostsList posts) {
|
public void OnPostExecute(PostsList posts) {
|
||||||
onGotNewPosts(posts);
|
onGotNewPosts(posts);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mGetLatestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
getLatestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
|
getTasksManager().addTask(getLatestPostsTask, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -100,14 +89,15 @@ public class LatestPostsFragment extends AbstractPostsListFragment {
|
|||||||
int start = last_post_id - 1;
|
int start = last_post_id - 1;
|
||||||
|
|
||||||
//Get older posts
|
//Get older posts
|
||||||
mGetLatestPostsTask = new GetLatestPostsTask(getActivity());
|
GetLatestPostsTask getLastestPostsTask = new GetLatestPostsTask(getActivity());
|
||||||
mGetLatestPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
|
getLastestPostsTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<PostsList>() {
|
||||||
@Override
|
@Override
|
||||||
public void OnPostExecute(PostsList posts) {
|
public void OnPostExecute(PostsList posts) {
|
||||||
onGotNewPosts(posts);
|
onGotNewPosts(posts);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mGetLatestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, start);
|
getLastestPostsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, start);
|
||||||
|
getTasksManager().addTask(getLastestPostsTask, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user