This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFlowAbstractDataLoader.java
More file actions
128 lines (110 loc) · 3.81 KB
/
FlowAbstractDataLoader.java
File metadata and controls
128 lines (110 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.uwflow.flow_android.loaders;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.LocalBroadcastManager;
import com.uwflow.flow_android.constant.Constants;
import com.uwflow.flow_android.dao.FlowDatabaseHelper;
public abstract class FlowAbstractDataLoader<T extends Object> extends AsyncTaskLoader<T> {
protected T mLastData = null;
protected abstract T loadDelegate();
protected FlowDatabaseHelper flowDatabaseHelper;
protected LoaderUpdateReceiver loaderUpdateReceiver;
// This is used to load data from the network to the fragment
protected Fragment mBaseFragment;
public FlowAbstractDataLoader(Context context, FlowDatabaseHelper flowDatabaseHelper) {
super(context);
this.flowDatabaseHelper = flowDatabaseHelper;
}
public FlowAbstractDataLoader(Context context, FlowDatabaseHelper flowDatabaseHelper, Fragment baseFragment) {
super(context);
this.flowDatabaseHelper = flowDatabaseHelper;
this.mBaseFragment = baseFragment;
}
protected void registerReceiver(){
// Start watching for changes in the app data.
if (loaderUpdateReceiver == null) {
loaderUpdateReceiver = new LoaderUpdateReceiver(this, Constants.BroadcastActionId.UPDATE_PROFILE_LOADER);
}
}
protected void unregisterReceiver(){
if (loaderUpdateReceiver != null) {
LocalBroadcastManager.getInstance(this.getContext().getApplicationContext()).unregisterReceiver(loaderUpdateReceiver);
loaderUpdateReceiver = null;
}
}
/**
* Runs on a worker thread, loading in our data. Delegates the real work to
* concrete subclass' buildCursor() method.
*/
@Override
public T loadInBackground() {
return loadDelegate();
}
/**
* Runs on the UI thread, routing the results from the background thread to
* whatever is using the dataList.
*/
@Override
public void deliverResult(T data) {
if (isReset()) {
data = null;
return;
}
T oldDataList = mLastData;
mLastData = data;
if (isStarted()) {
super.deliverResult(data);
}
if (oldDataList != null && oldDataList != data) {
oldDataList = null;
}
}
/**
* Starts an asynchronous load of the list data. When the result is ready
* the callbacks will be called on the UI thread. If a previous load has
* been completed and is still valid the result may be passed to the
* callbacks immediately.
* <p/>
* Must be called from the UI thread.
*/
@Override
protected void onStartLoading() {
if (mLastData != null) {
deliverResult(mLastData);
}
registerReceiver();
if (takeContentChanged() || mLastData == null) {
forceLoad();
}
}
/**
* Must be called from the UI thread, triggered by a call to stopLoading().
*/
@Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
/**
* Must be called from the UI thread, triggered by a call to cancel(). Here,
* we make sure our Cursor is closed, if it still exists and is not already
* closed.
*/
@Override
public void onCanceled(T dataList) {
}
/**
* Must be called from the UI thread, triggered by a call to reset(). Here,
* we make sure our Cursor is closed, if it still exists and is not already
* closed.
*/
@Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
mLastData = null;
unregisterReceiver();
}
}