Skip to content
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

Open
wants to merge 13 commits into
base: f.ThirdPartyPlugins
Choose a base branch
from
Open
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:appcompat-v7:22.2.0'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

compile 'se.emilsjolander:StickyScrollViewItems:1.1.0'
compile 'com.google.android.gms:play-services:7.5.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'io.reactivex:rxjava:1.0.14'
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_api_key"/>
android:value="@string/google_maps_api_key" />

<activity
android:name=".actMainPage"
Expand Down
18 changes: 3 additions & 15 deletions app/src/main/java/com/spielpark/steve/bernieapp/actMainPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
import com.spielpark.steve.bernieapp.fragments.OrganizeFragment;
import com.spielpark.steve.bernieapp.fragments.SingleIssueFragment;
import com.spielpark.steve.bernieapp.fragments.SingleNewsFragment;
import com.spielpark.steve.bernieapp.tasks.NewsTask;
import com.spielpark.steve.bernieapp.wrappers.Issue;
import com.spielpark.steve.bernieapp.wrappers.NewsArticle;
import com.spielpark.steve.bernieapp.model.Issue;
import com.spielpark.steve.bernieapp.model.news.NewsArticle;

public class actMainPage extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
Expand Down Expand Up @@ -156,7 +155,7 @@ public void onBackPressed() {
((ConnectFragment) curFrag).backPressed();
return;
} else if (curFrag instanceof OrganizeFragment) {
if (((OrganizeFragment) curFrag).canGoBack()) {
if (OrganizeFragment.canGoBack()) {
return;
}
}
Expand Down Expand Up @@ -189,17 +188,6 @@ public SharedPreferences getPrefs() {
return preferences;
}

public void loadHeaderArticle(View view) {
if (NewsTask.getData() != null) {
for (NewsArticle a : NewsTask.getData()) {
if (a.getUrl().contains("press-release")) {
this.loadEvent(a);
break;
}
}
}
}

public void adjustNavBarText(int selected) {
TextView[] views = new TextView[]{
(TextView) findViewById(R.id.newsTxt), (TextView) findViewById(R.id.issuesTxt),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.spielpark.steve.bernieapp.R;
import com.spielpark.steve.bernieapp.model.Event;
import com.spielpark.steve.bernieapp.tasks.ConnectTask;
import com.spielpark.steve.bernieapp.wrappers.Event;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


/**
* Fragment used for managing interactions for and presentation of a navigation drawer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>>>() {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 .subscribe(myObserver) ?

@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."));
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Up @@ -13,7 +13,7 @@
import android.widget.TextView;

import com.spielpark.steve.bernieapp.R;
import com.spielpark.steve.bernieapp.wrappers.Issue;
import com.spielpark.steve.bernieapp.model.Issue;

import butterknife.Bind;
import butterknife.ButterKnife;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.spielpark.steve.bernieapp.R;
import com.spielpark.steve.bernieapp.misc.Util;
import com.spielpark.steve.bernieapp.wrappers.NewsArticle;
import com.spielpark.steve.bernieapp.model.news.NewsArticle;

import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -56,14 +56,14 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
((TextView) root.findViewById(R.id.e_txtTitle)).setText(event.getTitle());
((TextView) root.findViewById(R.id.e_txtTitle)).setShadowLayer(13, 0, 0, Color.BLACK);
((TextView) root.findViewById(R.id.e_txtDate)).setText(event.getPubDate() + " at " + time);
((TextView) root.findViewById(R.id.e_txtDesc)).setText(Html.fromHtml(event.getDesc()));
((TextView) root.findViewById(R.id.e_txtDesc)).setText(Html.fromHtml(event.getContent()));
((TextView) root.findViewById(R.id.e_txtDesc)).setMovementMethod(new LinkMovementMethod());
Util.getPicasso(getActivity()).load(event.getImgSrc()).placeholder(R.drawable.logo).into((ImageView) root.findViewById(R.id.e_imgLogo));
root.findViewById(R.id.e_btnWebsite).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(event.getUrl()));
i.setData(Uri.parse(event.getPermalink()));
startActivity(i);
}
});
Expand All @@ -87,7 +87,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
title.setText(event.getTitle());
title.setShadowLayer(13, 0, 0, Color.BLACK);
date.setText(event.getPubDate());
description.setText(Html.fromHtml(event.getDesc()));
description.setText(Html.fromHtml(event.getContent()));
description.setMovementMethod(new LinkMovementMethod());
return view;
}
Expand All @@ -101,7 +101,7 @@ public void onDestroyView() {
@OnClick(R.id.e_btnWebsite)
void onWebsiteClicked() {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(event.getUrl()));
i.setData(Uri.parse(event.getPermalink()));
startActivity(i);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import android.widget.TextView;

import com.spielpark.steve.bernieapp.R;
import com.spielpark.steve.bernieapp.wrappers.ImgTxtItem;
import com.spielpark.steve.bernieapp.model.ImgTxtItem;

import java.util.List;

Expand Down Expand Up @@ -55,11 +55,11 @@ public View getView(int position, View convertView, ViewGroup parent) {
}

public static class ViewHolder {
@Bind(R.id.txtItem) TextView txt;
@Bind(R.id.picThumb) ImageView img;
@Bind(R.id.txtItem) TextView txt;

public ViewHolder(View convertView, Typeface typeface) {
ButterKnife.bind(this, convertView);
ButterKnife.bind(ViewHolder.this, convertView);
txt.setTypeface(typeface);
}
}
Expand Down
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);
}
Loading