Skip to content

Commit

Permalink
Fixes issue #2 Race Condition on refresh, getView
Browse files Browse the repository at this point in the history
Partial fix only.
replacing the underlying books after all books are loaded,
so the danger time is shortened.  Now the code looks cleaner.

Also, the item of listview is now recycled.
  • Loading branch information
Chiu Yue Chun committed Sep 1, 2015
1 parent a7bea66 commit d47a3e8
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions app/src/main/java/com/loopbook/cuhk_loopbook/BookFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public static class BookAdapter extends BaseAdapter {
private int viewResourceId;
private Context ctx;

public BookAdapter(Context ctx, int viewResourceId, ArrayList<LibConn.Book> books) {
public BookAdapter(Context ctx, int viewResourceId) {
this.ctx = ctx;
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
this.books = books;
this.books = new ArrayList<LibConn.Book>();
this.viewResourceId = viewResourceId;
}

Expand All @@ -70,37 +70,41 @@ public int getCount() {
}

public View getView(int position, View convertView, ViewGroup parent) {
View view = mInflater.inflate(viewResourceId, null);
if (convertView == null) { convertView = mInflater.inflate(viewResourceId, null); }
LibConn.Book book = books.get(position);

ImageView iv = (ImageView)view.findViewById(R.id.option_icon);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageResource(book.remainDays() >= DueChecker.getAlertDays(ctx) ?
R.drawable.green_circle :
R.drawable.red_circle);

TextView tv = (TextView)view.findViewById(R.id.option_text);
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(book.name + "\n" + formater.format(book.dueDate.getTime()));

return view;
return convertView;
}

public void setBooks(ArrayList<LibConn.Book> booksGot) {
/* I am afraid race condition may happen in these line (issue #2),
* but probability is very low */
books = booksGot;
notifyDataSetChanged();
}
}

private static class Data {

private ArrayList<LibConn.Book> books = new ArrayList<>();

private class AsyncBookLoader extends AsyncTask<Context, String, Void> {
private class AsyncBookLoader extends AsyncTask<Context, String, ArrayList<LibConn.Book>> {
private Exception caughtException = null;
private Context context;
private BookAdapter bookAdapter;
public BookAdapter bookAdapter;

@Override
protected Void doInBackground(Context... context) {
protected ArrayList<LibConn.Book> doInBackground(Context... context) {
this.context = context[0];
String msg = LibConn.isConnectable(this.context) ? "connecting" : "No connection";
publishProgress(msg);

books.clear();
ArrayList<LibConn.Book> booksGot;
try {
booksGot = DataIO.getBooks(this.context);
Expand All @@ -109,10 +113,7 @@ protected Void doInBackground(Context... context) {
return null;
}

for (LibConn.Book book: booksGot) { /* make deep copy */
books.add(book);
}
return null;
return booksGot;
}

@Override
Expand All @@ -121,26 +122,22 @@ protected void onProgressUpdate(String... msgs) {
}

@Override
protected void onPostExecute(Void nothing) {
protected void onPostExecute(ArrayList<LibConn.Book> booksGot) {
if (caughtException != null) {
Toast.makeText(context,
caughtException.getMessage(),
Toast.LENGTH_SHORT).show();
} else {
if (books.size() == 0) {
if (booksGot.size() == 0) {
Toast.makeText(context,
context.getString(R.string.no_books),
Toast.LENGTH_SHORT).show();
}
bookAdapter.notifyDataSetChanged();
bookAdapter.setBooks(booksGot);
}
}
}

public ArrayList<LibConn.Book> getbooks() {
return books;
}

public void refresh(BookAdapter adapter, Context context) {
AsyncBookLoader bookLoader = new AsyncBookLoader();
bookLoader.bookAdapter = adapter;
Expand All @@ -159,8 +156,7 @@ public void onCreate(Bundle savedInstanceState) {

bookAdapter = new BookAdapter(
getActivity(),
R.layout.list_item,
data.getbooks());
R.layout.list_item);
if (getArguments() != null ? getArguments().getBoolean("firstRun", false) : false) {
getArguments().putBoolean("firstRun", false);
} else {
Expand Down

0 comments on commit d47a3e8

Please sign in to comment.