-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use AsyncTask plus Lifecycle object instead of Loader
Loaders have now been deprecated in favor of the lifecycle support library
- Loading branch information
Showing
3 changed files
with
52 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 31 additions & 44 deletions
75
AnkiDroid/src/main/java/com/ichi2/async/CollectionLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,57 @@ | ||
package com.ichi2.async; | ||
|
||
import android.content.Context; | ||
import android.support.v4.content.AsyncTaskLoader; | ||
import android.arch.lifecycle.Lifecycle; | ||
import android.arch.lifecycle.LifecycleOwner; | ||
import android.os.AsyncTask; | ||
|
||
import com.ichi2.anki.AnkiDroidApp; | ||
import com.ichi2.anki.CollectionHelper; | ||
import com.ichi2.libanki.Collection; | ||
|
||
import timber.log.Timber; | ||
|
||
public class CollectionLoader extends AsyncTaskLoader<Collection> { | ||
public final class CollectionLoader extends AsyncTask<Void, Void, Collection> { | ||
public interface Callback { | ||
void execute(Collection col); | ||
} | ||
|
||
private LifecycleOwner mLifecycleOwner; | ||
private Callback mCallback; | ||
|
||
public static void load(LifecycleOwner lifecycleOwner, Callback callback) { | ||
CollectionLoader loader = new CollectionLoader(lifecycleOwner, callback); | ||
loader.execute(); | ||
} | ||
|
||
public CollectionLoader(Context context) { | ||
super(context); | ||
private CollectionLoader(LifecycleOwner lifecycleOwner, Callback callback) { | ||
mLifecycleOwner = lifecycleOwner; | ||
mCallback = callback; | ||
} | ||
|
||
@Override | ||
public Collection loadInBackground() { | ||
protected Collection doInBackground(Void... params) { | ||
// Don't touch collection if lockCollection flag is set | ||
if (CollectionHelper.getInstance().isCollectionLocked()) { | ||
Timber.w("onStartLoading() :: Another thread has requested to keep the collection closed."); | ||
return null; | ||
} | ||
// load collection | ||
try { | ||
Timber.d("CollectionLoader accessing collection"); | ||
return CollectionHelper.getInstance().getCol(getContext()); | ||
return CollectionHelper.getInstance().getCol(AnkiDroidApp.getInstance().getApplicationContext()); | ||
} catch (RuntimeException e) { | ||
Timber.e(e, "loadInBackground - RuntimeException on opening collection"); | ||
AnkiDroidApp.sendExceptionReport(e, "CollectionLoader.loadInBackground"); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void deliverResult(Collection col) { | ||
Timber.d("CollectionLoader.deliverResult()"); | ||
// Loader has been reset so don't forward data to listener | ||
if (isReset()) { | ||
if (col != null) { | ||
return; | ||
} | ||
} | ||
// Loader is running so forward data to listener | ||
if (isStarted()) { | ||
super.deliverResult(col); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
protected void onStartLoading() { | ||
// Don't touch collection if lockCollection flag is set | ||
if (CollectionHelper.getInstance().isCollectionLocked()) { | ||
Timber.w("onStartLoading() :: Another thread has requested to keep the collection closed."); | ||
return; | ||
protected void onPostExecute(Collection col) { | ||
super.onPostExecute(col); | ||
if (mLifecycleOwner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.CREATED)) { | ||
mCallback.execute(col); | ||
} | ||
// Since the CollectionHelper only opens if necessary, we can just force every time | ||
forceLoad(); | ||
} | ||
|
||
@Override | ||
protected void onStopLoading() { | ||
// The Loader has been put in a stopped state, so we should attempt to cancel the current load (if there is one). | ||
Timber.d("CollectionLoader.onStopLoading()"); | ||
cancelLoad(); | ||
} | ||
|
||
@Override | ||
protected void onReset() { | ||
// Ensure the loader is stopped. | ||
Timber.d("CollectionLoader.onReset()"); | ||
onStopLoading(); | ||
} | ||
|
||
} |