Skip to content

Commit

Permalink
Added
Browse files Browse the repository at this point in the history
 - Fixes
  • Loading branch information
n00ner committed Apr 7, 2019
1 parent 804ed86 commit 4b0ab2f
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 120 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.n00ner.newsbuddy"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionCode 3
versionName "1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down Expand Up @@ -51,4 +51,5 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'androidx.cardview:cardview:1.0.0'
implementation "com.airbnb.android:lottie:3.0.0"
implementation 'org.jsoup:jsoup:1.11.3'
}
131 changes: 105 additions & 26 deletions app/src/main/java/com/n00ner/newsbuddy/adapters/NewsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,35 @@
import com.n00ner.newsbuddy.models.Tag;
import com.n00ner.newsbuddy.storage.database.TagsController;
import com.n00ner.newsbuddy.themes.ThemesView;
import com.n00ner.newsbuddy.util.RSSParser;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import org.xml.sax.SAXException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import javax.xml.parsers.ParserConfigurationException;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Single;
import io.reactivex.SingleEmitter;
import io.reactivex.SingleObserver;
import io.reactivex.SingleOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NewsAdapter extends RecyclerView.Adapter<NewsHolder> {

Expand Down Expand Up @@ -61,6 +81,7 @@ public void addItems(ArrayList<NewsItem> data, boolean isLast){
if(items.size() == 0){
feedView.showEmptyList(R.string.empty_news, R.string.empty_news_sub, R.drawable.ic_receipt);
}else {
sort();
feedView.showList();
}
}
Expand All @@ -72,7 +93,7 @@ public void clearData(){
@Override
public void onBindViewHolder(@NonNull NewsHolder holder, int position) {

holder.share.setOnClickListener(new View.OnClickListener() {
/* holder.share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
Expand All @@ -83,14 +104,21 @@ public void onClick(View v) {
+ holder.share.getContext().getString(R.string.shared_from, BaseApp.getAppContext().getString(R.string.app_name)));
feedView.execIntent(Intent.createChooser(sharingIntent, holder.share.getContext().getString(R.string.share_via)));
}
});
});*/

holder.openLink.setOnClickListener(new View.OnClickListener() {
/* holder.openLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
feedView.openBrowser(items.get(position).getLink(), items.get(position).getTitle());
}
});
});*/

holder.wrapper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
feedView.openBrowser(items.get(position).getLink(), items.get(position).getTitle());
}
});

if(items.get(position).getTitle() != null && items.get(position).getDescription() != null){
holder.title.setText(items.get(position).getTitle());
Expand All @@ -103,36 +131,88 @@ public void onClick(View v) {
holder.pubSource.setVisibility(View.GONE);
}

if(items.get(position).getPubDate() != null){
holder.pubDate.setText(new SimpleDateFormat("d MMM yyyy").format(new Date(items.get(position).getPubDate())));
}

if(items.get(position).getImageUrl() != null){
holder.preview.setVisibility(View.VISIBLE);
Target target = new Target() {
imageLoader(items.get(position).getImageUrl(), holder.preview);
}else {
//holder.preview.setVisibility(View.GONE);
getPage(items.get(position).getLink())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<String>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onSuccess(String s) {
imageLoader(new RSSParser().fetchOGtag(s), holder.preview);
}

@Override
public void onError(Throwable e) {
holder.preview.setVisibility(View.GONE);
}
});
}
}

@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
holder.preview.setImageBitmap(bitmap);
Utils.setCornerImage(holder.preview, holder.preview.getContext(), 20);
}
private void imageLoader(String url, ImageView imageView){
Target target = new Target() {

@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
holder.preview.setVisibility(View.GONE);
}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
Utils.setCornerImage(imageView, imageView.getContext(), 20);
}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
holder.preview.setImageDrawable(placeHolderDrawable);
}
};
Picasso.get().load(items.get(position).getImageUrl()).placeholder(R.mipmap.ic_launcher).into(target);
}else {
holder.preview.setVisibility(View.GONE);
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
imageView.setVisibility(View.GONE);
}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
imageView.setImageDrawable(placeHolderDrawable);
}
};
Picasso.get().load(url).placeholder(R.mipmap.ic_launcher).into(target);
}

@Override
public int getItemCount() {
return items.size();
}

private void sort(){
for(int i = 0; i < items.size(); i++){
for (int j = 0; j < items.size() - i; j++){
if(j != items.size() - 1)
if( items.get(j).getPubDateInMillis() < items.get( j + 1 ).getPubDateInMillis()){
Collections.swap(items, j + 1, j);
}
}
}
}

private Single<String> getPage(String url){
return Single.create(emitter -> {
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(new Request.Builder().url(url).build()).execute();
if(response != null)
if(response.isSuccessful()){
emitter.onSuccess(response.body().string());
}else {
emitter.onError(new Throwable());
}

});
}
}

class NewsHolder extends RecyclerView.ViewHolder{
Expand All @@ -141,9 +221,8 @@ class NewsHolder extends RecyclerView.ViewHolder{
@BindView(R.id.news_description) TextView description;
@BindView(R.id.img_news_preview) ImageView preview;
@BindView(R.id.news_pub_source) TextView pubSource;
@BindView(R.id.news_open_browser) ImageView openLink;
@BindView(R.id.news_bookmark) ImageView addBookmark;
@BindView(R.id.news_share) ImageView share;
@BindView(R.id.news_pub_date) TextView pubDate;
@BindView(R.id.news_wrapper) CardView wrapper;

public NewsHolder(@NonNull View itemView) {
super(itemView);
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/n00ner/newsbuddy/menu/MenuActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
case R.id.navigation_feed:
setFeed();
return true;
case R.id.navigation_favorite:
setFavorite();
return true;
case R.id.navigation_themes:
setThemes();
return true;
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/n00ner/newsbuddy/models/NewsItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.n00ner.newsbuddy.models;

import java.util.Date;

public class NewsItem {
private String title;
private String link;
Expand Down Expand Up @@ -52,6 +54,10 @@ public String getDescription() {
return description;
}

public long getPubDateInMillis(){
return new Date(this.pubDate).getTime();
}

public void setDescription(String description) {
this.description = description;
}
Expand Down
33 changes: 24 additions & 9 deletions app/src/main/java/com/n00ner/newsbuddy/util/RSSParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@

import com.n00ner.newsbuddy.models.NewsItem;

import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -55,7 +53,7 @@ public ArrayList<NewsItem> anotherParser(String input) throws ParserConfiguratio
if(description != null)
item.setDescription(description.getFirstChild().getNodeValue());
if(pubDate != null){
item.setPubDate(new SimpleDateFormat("EEE, MMM d, yyyy").format(new Date(pubDate.getFirstChild().getNodeValue())));
item.setPubDate(pubDate.getFirstChild().getNodeValue());
}
if(link != null)
item.setLink(link.getFirstChild().getNodeValue());
Expand All @@ -70,4 +68,21 @@ public ArrayList<NewsItem> anotherParser(String input) throws ParserConfiguratio
return items;
}

public String fetchOGtag(String html) {
org.jsoup.nodes.Document document = Jsoup.parse(html);
Elements metaTags = document.select("meta");
String siteImgUrl = null;
for(org.jsoup.nodes.Element element: metaTags){
String name = element.attr("property");
String content = element.attr("content");
if(name != null && content != null){
if(name.equals("og:image")){
siteImgUrl = content;
}

}
}
return siteImgUrl;
}

}
Loading

0 comments on commit 4b0ab2f

Please sign in to comment.