-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Replace NewsTask with Retrofit/RxJava #26
base: f.ThirdPartyPlugins
Are you sure you want to change the base?
Changes from all commits
86f86d1
f47dd9a
ed586db
f1045c4
5120e3f
2070f46
5f27bb8
07b4be6
19b2c93
097c557
4981af8
faec60e
2005005
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
import android.widget.TextView; | ||
|
||
import com.spielpark.steve.bernieapp.R; | ||
import com.spielpark.steve.bernieapp.wrappers.NavDrawerItem; | ||
import com.spielpark.steve.bernieapp.model.NavDrawerItem; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
/** | ||
* Fragment used for managing interactions for and presentation of a navigation drawer. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,47 +7,135 @@ | |
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.Fragment; | ||
import android.text.Html; | ||
import android.text.method.ScrollingMovementMethod; | ||
import android.util.Pair; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.AdapterView; | ||
import android.widget.ListView; | ||
import android.widget.ProgressBar; | ||
import android.widget.TextView; | ||
|
||
import com.spielpark.steve.bernieapp.R; | ||
import com.spielpark.steve.bernieapp.tasks.NewsTask; | ||
import com.spielpark.steve.bernieapp.actMainPage; | ||
import com.spielpark.steve.bernieapp.misc.ImgTxtAdapter; | ||
import com.spielpark.steve.bernieapp.model.news.NewsArticle; | ||
import com.spielpark.steve.bernieapp.model.news.NewsManager; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import butterknife.Bind; | ||
import butterknife.ButterKnife; | ||
import butterknife.OnClick; | ||
import rx.Subscription; | ||
import rx.android.schedulers.AndroidSchedulers; | ||
import rx.functions.Action1; | ||
|
||
/** | ||
* A placeholder fragment containing a simple view. | ||
* A placeholder instance containing a simple view. | ||
*/ | ||
public class NewsFragment extends Fragment { | ||
|
||
private static NewsFragment mIntstance; | ||
|
||
private static NewsFragment instance; | ||
@Bind(R.id.listNews) ListView list; | ||
@Bind(R.id.progressBar) ProgressBar progressBar; | ||
@Bind(R.id.txtSubHeader) TextView subHeader; | ||
@Bind(R.id.txtHeader) TextView header; | ||
ImgTxtAdapter adapter; | ||
NewsArticle headerArticle; | ||
private Subscription newsSubscription; | ||
|
||
public static NewsFragment getInstance() { | ||
if (mIntstance == null) { | ||
mIntstance = new NewsFragment(); | ||
return mIntstance; | ||
if (instance == null) { | ||
instance = new NewsFragment(); | ||
return instance; | ||
} else { | ||
return mIntstance; | ||
return instance; | ||
} | ||
} | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
return inflater.inflate(R.layout.frag_newsarticles, container, false); | ||
} | ||
|
||
@Override | ||
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) { | ||
super.onViewCreated(view, savedInstanceState); | ||
final ListView newsList = (ListView) view.findViewById(R.id.listNews); | ||
((TextView) view.findViewById(R.id.txtSubHeader)).setMovementMethod( | ||
new ScrollingMovementMethod()); | ||
new NewsTask(getActivity(), newsList, (ProgressBar) view.findViewById(R.id.progressBar), | ||
(TextView) view.findViewById(R.id.txtSubHeader), | ||
(TextView) view.findViewById(R.id.txtHeader)).execute(); | ||
ButterKnife.bind(this, view); | ||
subHeader.setMovementMethod(new ScrollingMovementMethod()); | ||
adapter = new ImgTxtAdapter(view.getContext(), R.layout.list_news_item, new ArrayList()); | ||
list.setAdapter(adapter); | ||
} | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
return inflater.inflate(R.layout.frag_newsarticles, container, false); | ||
public void onResume() { | ||
super.onResume(); | ||
newsSubscription = NewsManager.get().getNews().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Pair<NewsArticle, List<NewsArticle>>>() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hard to read long lines like this, I tend to find it easier to read when wrapping at each method call. Up to you but should at least be wrapped a bit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this be easier if you created a observer field that you could pass to |
||
@Override | ||
public void call(Pair<NewsArticle, List<NewsArticle>> pair) { | ||
//Setup the header | ||
headerArticle = pair.first; | ||
if (pair.first != null) { | ||
setupHeader(headerArticle); | ||
} else { | ||
|
||
//TODO: We need to replace the header with something else. | ||
} | ||
|
||
//Setup the remaining articles | ||
if (pair.second != null && pair.second.size() > 0) { | ||
setupListView(pair.second); | ||
} else { | ||
//TODO: Handle an empty list. | ||
} | ||
|
||
} | ||
}, new Action1<Throwable>() { | ||
@Override | ||
public void call(Throwable throwable) { | ||
setupHeader(new NewsArticle("Unable to Load News", "Check your connection and try again.")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to /res/ string? |
||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onPause() { | ||
super.onPause(); | ||
if (newsSubscription != null && !newsSubscription.isUnsubscribed()) | ||
newsSubscription.unsubscribe(); | ||
} | ||
|
||
@OnClick({R.id.txtSubHeader, R.id.txtHeader}) | ||
void onHeaderClicked() { | ||
if (headerArticle != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. {... } |
||
((actMainPage) getActivity()).loadEvent(headerArticle); | ||
} | ||
|
||
private void setupHeader(NewsArticle article) { | ||
subHeader.setText(Html.fromHtml(article.getContent())); | ||
String s = article.getTitle(); | ||
s = s.length() > 40 ? s.substring(0, 40) + "..." : s; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line could use a comment, it's magical 🎅 |
||
header.setText(s); | ||
} | ||
|
||
private void setupListView(List<NewsArticle> articles) { | ||
Collections.sort(articles); | ||
adapter.addAll(articles); | ||
((ImgTxtAdapter) list.getAdapter()).notifyDataSetChanged(); | ||
list.setVisibility(View.VISIBLE); | ||
progressBar.setVisibility(View.GONE); | ||
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||
@Override | ||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||
((actMainPage) getActivity()).loadEvent((NewsArticle) list.getAdapter().getItem(position)); | ||
} | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.spielpark.steve.bernieapp.model; | ||
|
||
import android.util.Log; | ||
|
||
import com.squareup.okhttp.Interceptor; | ||
import com.squareup.okhttp.Request; | ||
import com.squareup.okhttp.Response; | ||
import com.squareup.okhttp.ResponseBody; | ||
|
||
import java.io.IOException; | ||
|
||
import retrofit.GsonConverterFactory; | ||
import retrofit.Retrofit; | ||
import retrofit.RxJavaCallAdapterFactory; | ||
|
||
public class ApiManager { | ||
|
||
private static ApiManager instance; | ||
public BernieApi api; | ||
|
||
public ApiManager(BernieApi api) { | ||
this.api = api; | ||
} | ||
|
||
public static ApiManager get() { | ||
if (instance == null) { | ||
|
||
LoggingInterceptor interceptor = new LoggingInterceptor(); | ||
Retrofit retrofit = new Retrofit.Builder() | ||
.baseUrl("https://berniesanders.com/") | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) | ||
.build(); | ||
|
||
retrofit.client().interceptors().add(interceptor); | ||
instance = new ApiManager(retrofit.create(BernieApi.class)); | ||
|
||
} | ||
return instance; | ||
} | ||
|
||
private static class LoggingInterceptor implements Interceptor { | ||
@Override | ||
public Response intercept(Interceptor.Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
|
||
long t1 = System.nanoTime(); | ||
Log.d("Retrofit", String.format("Sending request %s on %s%n%s", | ||
request.url(), chain.connection(), request.headers())); | ||
|
||
Response response = chain.proceed(request); | ||
|
||
long t2 = System.nanoTime(); | ||
Log.d("Retrofit", String.format("Received response for %s in %.1fms%n%s", | ||
response.request().url(), (t2 - t1) / 1e6d, response.headers())); | ||
|
||
|
||
final String responseString = new String(response.body().bytes()); | ||
|
||
Log.d("Retrofit", "Response: " + responseString); | ||
|
||
return response.newBuilder() | ||
.body(ResponseBody.create(response.body().contentType(), responseString)) | ||
.build(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.spielpark.steve.bernieapp.model; | ||
|
||
import com.spielpark.steve.bernieapp.model.news.NewsArticle; | ||
|
||
import java.util.List; | ||
|
||
import retrofit.http.GET; | ||
import retrofit.http.Query; | ||
import rx.Observable; | ||
|
||
public interface BernieApi { | ||
String NEWS = "news"; | ||
String DAILY = "daily"; | ||
|
||
@GET("?json=true&limit=12") | ||
Observable<List<NewsArticle>> getNews(@Query("which") String newsType); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?