Better null result handling

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2016-11-01 09:01:10 -07:00
parent 469f41b955
commit 4bf3632b40
3 changed files with 12 additions and 7 deletions

View File

@ -42,7 +42,7 @@ public abstract class GiphyLoader extends AsyncLoader<List<GiphyImage>> {
return loadPage(0); return loadPage(0);
} }
public List<GiphyImage> loadPage(int offset) { public @NonNull List<GiphyImage> loadPage(int offset) {
try { try {
String url; String url;
@ -56,9 +56,12 @@ public abstract class GiphyLoader extends AsyncLoader<List<GiphyImage>> {
throw new IOException("Unexpected code " + response); throw new IOException("Unexpected code " + response);
} }
GiphyResponse giphyResponse = JsonUtils.fromJson(response.body().byteStream(), GiphyResponse.class); GiphyResponse giphyResponse = JsonUtils.fromJson(response.body().byteStream(), GiphyResponse.class);
List<GiphyImage> results = giphyResponse.getData();
if (results == null) return new LinkedList<>();
else return results;
return giphyResponse.getData();
} catch (IOException e) { } catch (IOException e) {
Log.w(TAG, e); Log.w(TAG, e);
return new LinkedList<>(); return new LinkedList<>();

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.giph.ui;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -106,7 +107,7 @@ public class GiphyAdapter extends RecyclerView.Adapter<GiphyAdapter.GiphyViewHol
this.images = images; this.images = images;
} }
public void setImages(List<GiphyImage> images) { public void setImages(@NonNull List<GiphyImage> images) {
this.images = images; this.images = images;
notifyDataSetChanged(); notifyDataSetChanged();
} }

View File

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.giph.ui;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager; import android.support.v4.app.LoaderManager;
@ -63,11 +64,11 @@ public abstract class GiphyFragment extends Fragment implements LoaderManager.Lo
} }
@Override @Override
public void onLoadFinished(Loader<List<GiphyImage>> loader, List<GiphyImage> data) { public void onLoadFinished(Loader<List<GiphyImage>> loader, @NonNull List<GiphyImage> data) {
this.loadingProgress.setVisibility(View.GONE); this.loadingProgress.setVisibility(View.GONE);
if (data == null || data.isEmpty()) noResultsView.setVisibility(View.VISIBLE); if (data.isEmpty()) noResultsView.setVisibility(View.VISIBLE);
else noResultsView.setVisibility(View.GONE); else noResultsView.setVisibility(View.GONE);
this.giphyAdapter.setImages(data); this.giphyAdapter.setImages(data);
} }