-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve search suggestion load timings (#22)
* Fix progressBar not showing * Load PDB titles in a background thread
- Loading branch information
Showing
4 changed files
with
113 additions
and
47 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
59 changes: 59 additions & 0 deletions
59
app/src/main/java/io/ningyuan/palantir/utils/PdbTitleSearcher.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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.ningyuan.palantir.utils; | ||
|
||
import android.database.MatrixCursor; | ||
import android.os.AsyncTask; | ||
import android.util.Log; | ||
import android.widget.CursorAdapter; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedList; | ||
|
||
import io.ningyuan.jPdbApi.Pdb; | ||
import io.ningyuan.palantir.views.SearchView; | ||
|
||
/** | ||
* Represents the lazy loading of titles for each result in a PdbSearcher. | ||
*/ | ||
public class PdbTitleSearcher extends AsyncTask<Integer, Void, Integer> { | ||
private static final String TAG = String.format("PALANTIR::%s", PdbTitleSearcher.class.getSimpleName()); | ||
|
||
private LinkedList<Pdb> pdbs; | ||
private CursorAdapter adapter; | ||
private PdbSearcher pdbSearcher; | ||
|
||
public PdbTitleSearcher(PdbSearcher pdbSearcher, CursorAdapter adapter, LinkedList<Pdb> pdbs) { | ||
this.adapter = adapter; | ||
this.pdbSearcher = pdbSearcher; | ||
this.pdbs = pdbs; | ||
} | ||
|
||
@Override | ||
protected Integer doInBackground(Integer... integers) { | ||
int indexToLoad = integers[0]; | ||
|
||
try { | ||
pdbs.get(indexToLoad).load(); | ||
} catch (IOException e) { | ||
Log.e(TAG, null, e); | ||
} | ||
|
||
return indexToLoad + 1; | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(Integer nextIndex) { | ||
MatrixCursor cursor = SearchView.getEmptyCursor(false); | ||
int cursorIndex = 0; | ||
for (Pdb pdb : pdbs) { | ||
cursor.addRow(new Object[]{ | ||
cursorIndex++, pdb.getStructureId(), pdb.getTitle() | ||
}); | ||
} | ||
adapter.changeCursor(cursor); | ||
|
||
Log.d(TAG, String.format("pdbSearcher.isValid()=%b", pdbSearcher.isValid())); | ||
if (nextIndex < pdbs.size() && pdbSearcher.isValid()) { | ||
new PdbTitleSearcher(pdbSearcher, adapter, pdbs).execute(nextIndex); | ||
} | ||
} | ||
} |
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