Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ytRino
Last active August 29, 2015 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytRino/81e46e325520f6362a99 to your computer and use it in GitHub Desktop.
Save ytRino/81e46e325520f6362a99 to your computer and use it in GitHub Desktop.
package secret
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.Optional;
import com.caraquri.gdonews.R;
import com.caraquri.gdonews.ui.BaseFragment;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshWebView;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
*
*/
public class CategoryContentFragment extends BaseFragment {
private static final String TAG = CategoryContentFragment.class.getSimpleName();
public static final String ARG_URL = "url";
@Optional @InjectView(R.id.refresh)
protected PullToRefreshWebView mPullToRefreshWebView;
@Optional @InjectView(R.id.webview)
protected WebView mWebView;
@Optional @InjectView(R.id.list)
protected ListView mListView;
public static CategoryContentFragment newInstance(String url) {
return new CategoryContentFragment().putStringArgument(ARG_URL, url);
}
public CategoryContentFragment() {
}
@Override
protected int getLayoutRes() {
return R.layout.fragment_category_content;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (mWebView != null) {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.setWebViewClient(new WebViewClient());
//mWebView.loadUrl(getStringArgument(ARG_URL, "about:blank"));
mWebView.setVisibility(View.GONE);
setupPullToRefreshLayout();
return;
}
mListView.setAdapter(new SampleAdapter(getActivity()));
Subscription sub = getMovie().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<YouList>() {
@Override
public void onCompleted() {
Log.v("Rx Json", "Comp");
}
@Override
public void onError(Throwable e) {
Log.w("Rx Json", "Err", e);
}
@Override
public void onNext(YouList youList) {
Log.v("Rx Json", "Next");
SampleAdapter adapter = (SampleAdapter) mListView.getAdapter();
adapter.clear();
adapter.addAll(youList.videoInfo);
adapter.notifyDataSetChanged();
}
});
}
private void setupPullToRefreshLayout() {
FrameLayout innerLayout = (FrameLayout) mPullToRefreshWebView.findViewById(R.id.fl_inner);
TextView headerText = (TextView) innerLayout.findViewById(R.id.pull_to_refresh_text);
TextView subHeaderText = (TextView) innerLayout.findViewById(R.id.pull_to_refresh_sub_text);
headerText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
// refresg.setPullLabel()
}
private Observable<YouList> getMovie() {
return Observable.create(makeStringRequest()).map(new Func1<String, YouList>() {
@Override
public YouList call(String s) {
return new Gson().fromJson(s, YouList.class);
}
});
}
private Observable.OnSubscribe<String> makeStringRequest() {
return new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
Request request = new Request.Builder().url("http://54.176.3.189:50000/youlist").build();
try {
Response response = new OkHttpClient().newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("unexpected code: " + response);
}
subscriber.onNext(response.body().string());
subscriber.onCompleted();
} catch (IOException e) {
subscriber.onError(e);
}
}
};
}
public static class YouList {
public final List<VideoInfo> videoInfo;
public YouList(List<VideoInfo> videos) {
this.videoInfo = videos;
}
}
public static class VideoInfo {
public final String img;
public final String published;
public final String title;
public final String updated;
public final String url;
public VideoInfo(String img, String published, String title, String updated, String url) {
this.img = img;
this.published = published;
this.title = title;
this.updated = updated;
this.url = url;
}
@Override
public String toString() {
return title + " : " + url;
}
}
public static class SampleAdapter extends ArrayAdapter<VideoInfo> {
static class Holder {
//ImageView img;
@InjectView(android.R.id.text1) TextView text;
Subscription imgSub;
public Holder(View v) {
ButterKnife.inject(this, v);
}
}
public SampleAdapter(Context ctx) {
super(ctx, 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Holder h;
if (v == null) {
v = View.inflate(getContext(), android.R.layout.simple_list_item_1, null);
h = new Holder(v);
v.setTag(h);
} else {
h = (Holder) v.getTag();
}
final VideoInfo item = getItem(position);
h.text.setText(item.toString());
subscribeImageLoader(h, item);
return v;
}
private void subscribeImageLoader(final Holder h, final VideoInfo item) {
h.imgSub = bitmapObservable(h.text.getContext(), item.img).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Bitmap>() {
@Override
public void onCompleted() {
Log.v("Rx Image", "Comp: " + item.img);
}
@Override
public void onError(Throwable e) {
Log.w("Rx Image", "Err : " + item.img, e);
}
@Override
public void onNext(Bitmap bitmap) {
Log.v("Rx Image", "Next: " + item.img);
h.text.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(bitmap), null, null, null);
}
});
}
private Observable<Bitmap> bitmapObservable(final Context ctx, final String url) {
return Observable.create(new Observable.OnSubscribe<Bitmap>() {
@Override
public void call(Subscriber<? super Bitmap> subscriber) {
try {
Bitmap b = Picasso.with(ctx).load(url).get();
subscriber.onNext(b);
subscriber.onCompleted();
} catch (IOException e) {
subscriber.onError(e);
}
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment