Skip to content
This repository was archived by the owner on Dec 17, 2020. It is now read-only.

Commit f8c0049

Browse files
committed
Renamed the existing attachListener() method to attach() and added convenient attachAll(), detach() and detachAll() methods.
1 parent dc399c6 commit f8c0049

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

demo/src/main/java/org/neotech/app/retainabletasksdemo/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ protected void onCreate(Bundle savedInstanceState) {
4848
protected void onStart() {
4949
super.onStart();
5050
progressDialog = ProgressDialog.getExistingInstance(getSupportFragmentManager(), DIALOG_PROGRESS);
51-
getTaskManager().attachListener(TASK_PROGRESS, this);
51+
getTaskManager().attach(TASK_PROGRESS, this);
5252

5353

54-
getTaskManager().attachListener(TASK_RETAIN_UI_STATE, new TaskManager.TaskAttachListener() {
54+
getTaskManager().attach(TASK_RETAIN_UI_STATE, new TaskManager.TaskAttachListener() {
5555
@Override
5656
public Task.Callback onPreAttach(Task<?, ?> task) {
5757
/**

library/src/main/java/org/neotech/library/retainabletasks/BaseTaskManager.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.support.annotation.MainThread;
44
import android.support.annotation.NonNull;
5+
import android.support.v4.app.Fragment;
56
import android.util.Log;
67

78
import java.util.HashMap;
@@ -23,33 +24,67 @@ public class BaseTaskManager extends TaskManager {
2324

2425
@Override
2526
@MainThread
26-
public Task<?, ?> attachListener(@NonNull String tag, @NonNull Task.Callback callback){
27+
public Task<?, ?> attach(@NonNull String tag, @NonNull Task.Callback callback){
2728
final Task<?, ?> task = tasks.get(tag);
2829
if(task == null){
2930
return null;
3031
}
31-
task.setCallback(new CallbackShadow(callback));
32-
return task;
32+
return attach(task, callback);
3333
}
3434

3535
@Override
3636
@MainThread
37-
public Task<?, ?> attachListener(@NonNull String tag, @NonNull TaskAttachListener listener){
37+
public Task<?, ?> attach(@NonNull String tag, @NonNull TaskAttachListener listener){
3838
final Task<?, ?> task = tasks.get(tag);
3939
if(task == null){
4040
return null;
4141
}
42-
task.setCallback(new CallbackShadow(listener.onPreAttach(task)));
43-
return task;
42+
return attach(task, listener.onPreAttach(task));
4443
}
4544

4645
@Override
47-
@MainThread
48-
public Task<?, ?> attachListener(@NonNull Task<?, ?> task, @NonNull Task.Callback callback) {
46+
public Task<?, ?> attach(@NonNull Task<?, ?> task, @NonNull Task.Callback callback) {
47+
logCallback(callback);
4948
task.setCallback(new CallbackShadow(callback));
5049
return task;
5150
}
5251

52+
@Override
53+
public void attachAll(@NonNull Task.Callback callback, @NonNull String... tags) {
54+
for(String tag: tags){
55+
attach(tag, callback);
56+
}
57+
}
58+
59+
@Override
60+
public void attachAll(@NonNull TaskAttachListener attachListener, @NonNull String... tags) {
61+
for(String tag: tags){
62+
attach(tag, attachListener);
63+
}
64+
}
65+
66+
@Override
67+
public Task<?, ?> detach(@NonNull String tag) {
68+
final Task<?, ?> task = tasks.get(tag);
69+
if(task != null){
70+
task.removeCallback();
71+
}
72+
return task;
73+
}
74+
75+
@Override
76+
public void detachAll(@NonNull String... tags) {
77+
for(String tag: tags){
78+
detach(tag);
79+
}
80+
}
81+
82+
private void logCallback(final Task.Callback callback){
83+
if(callback instanceof Fragment || callback instanceof android.app.Fragment){
84+
Log.i(TAG, "Important: Found attached Callback which is an instance of Fragment. If your Fragment doesn't follow the activity lifecycle make sure to detach the task in Fragment.onStop()!");
85+
}
86+
}
87+
5388
@Override
5489
@MainThread
5590
public <Progress, Result> void execute(@NonNull Task<Progress, Result> task, @NonNull Task.Callback callback){

library/src/main/java/org/neotech/library/retainabletasks/TaskManager.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.support.annotation.MainThread;
44
import android.support.annotation.NonNull;
5+
import android.support.v4.app.Fragment;
56
import android.support.v4.app.FragmentManager;
67

78
/**
@@ -37,14 +38,27 @@ public interface TaskAttachListener {
3738
@MainThread
3839
public abstract Task<?, ?> getTask(@NonNull String tag);
3940

41+
42+
@MainThread
43+
public abstract Task<?, ?> attach(@NonNull String tag, @NonNull Task.Callback callback);
44+
45+
@MainThread
46+
public abstract Task<?, ?> attach(@NonNull String tag, @NonNull TaskAttachListener attachListener);
47+
48+
@MainThread
49+
public abstract Task<?, ?> attach(@NonNull Task<?, ?> task, @NonNull Task.Callback callback);
50+
51+
@MainThread
52+
public abstract void attachAll(@NonNull Task.Callback callback, @NonNull String... tags);
53+
4054
@MainThread
41-
public abstract Task<?, ?> attachListener(@NonNull String tag, @NonNull Task.Callback callback);
55+
public abstract void attachAll(@NonNull TaskAttachListener attachListener, @NonNull String... tags);
4256

4357
@MainThread
44-
public abstract Task<?, ?> attachListener(@NonNull String tag, @NonNull TaskAttachListener attachListener);
58+
public abstract Task<?, ?> detach(@NonNull String tag);
4559

4660
@MainThread
47-
public abstract Task<?, ?> attachListener(@NonNull Task<?, ?> task, @NonNull Task.Callback callback);
61+
public abstract void detachAll(@NonNull String... tags);
4862

4963
@MainThread
5064
public abstract Task<?, ?> cancel(@NonNull String tag);

0 commit comments

Comments
 (0)