Skip to content

Commit

Permalink
Merge pull request #157 from amardeshbd/feature/150_search_card_ui
Browse files Browse the repository at this point in the history
Feature/150 search card ui
  • Loading branch information
hossain-khan authored Nov 22, 2016
2 parents 8de4694 + 285630e commit b821168
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CoreConfig {
/**
* Search query delay before making network request.
*/
public static final int SEARCH_DELAY_MS = 500;
public static final int SEARCH_DELAY_MS = 600;

/**
* Minimum character required before searching for feeds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import info.hossainkhan.android.core.CoreConfig;
import info.hossainkhan.android.core.base.BasePresenter;
import info.hossainkhan.android.core.model.CardItem;
import info.hossainkhan.android.core.util.ObjectUtils;
import info.hossainkhan.android.core.util.StringUtils;
import rx.Observable;
import rx.Subscriber;
Expand Down Expand Up @@ -88,7 +89,7 @@ private void listenForSearchTesting(final Observable<String> searchObservable) {
@Override
public Boolean call(final String searchQueryText) {
return (searchQueryText != null) &&
(searchQueryText.length() > CoreConfig.SEARCH_TEXT_MIN_LENGTH);
(searchQueryText.length() >= CoreConfig.SEARCH_TEXT_MIN_LENGTH);
}
})
.subscribe(new Action1<String>() {
Expand Down Expand Up @@ -169,7 +170,7 @@ private List<CardItem> convertArticleToCardItems(final List<FeedItem> feedItems)
feedItem.getFeedId().hashCode(), // id,
feedItem.getTitle(), // title,
feedItem.getDescription(), // description,
null, //extraText,
ObjectUtils.defaultIfNull(feedItem.getVisualUrl(), feedItem.getIconUrl()), //extraText, (Re-using for icon)
feedItem.getDescription(), //category,
ISODateTimeFormat.dateTime().print(feedItem.getLastUpdated()), // dateCreated,
getImageUrl(feedItem), // imageUrl,
Expand All @@ -178,7 +179,9 @@ private List<CardItem> convertArticleToCardItems(final List<FeedItem> feedItems)
null, // footerColor,
null, // selectedColor,
CardItem.Type.HEADLINES, // type,
0, // width,

/* Re-using width and height for other purpose */
ObjectUtils.defaultIfNull(feedItem.getSubscribers(), 0L).intValue(), // width,
0 // height
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
public class UserSourceManager implements UserSourceProvider {

private static final String PREF_KEY_NEWS_SOURCES = "PREF_KEY_user_news_source_feeds_map";
/**
* A hash map containing [URL => Title] for news source.
*/
private Map<String, String> mNewsMap;
private final SharedPreferences mSharedPreferences;

Expand Down Expand Up @@ -86,6 +89,13 @@ public Map<String, String> getSources() {
return mNewsMap;
}

@Override
public boolean isAdded(final String url) {
boolean isAlreadyAdded = mNewsMap.containsKey(url);
Timber.d("isAdded('%s') - returning %s", url, isAlreadyAdded);
return isAlreadyAdded;
}


//
// Internal methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ public interface UserSourceProvider {
* @return Map of news sources. URL is key, and Title is value.
*/
Map<String, String> getSources();

/**
* Check if a news source is already added.
*
* @param url URL to check.
* @return {@code true} if exist, {@code false} otherwise.
*/
boolean isAdded(String url);
}
4 changes: 2 additions & 2 deletions tv/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "info.hossainkhan.dailynewsheadlines"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 18
versionName "1.14-tv"
versionCode 19
versionName "1.15-tv"
versionNameSuffix "-${gitSha()}"

buildConfigField "String", "GIT_SHA", "\"${gitSha()}\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import info.hossainkhan.dailynewsheadlines.R;
import info.hossainkhan.dailynewsheadlines.cards.CardListRow;
import info.hossainkhan.dailynewsheadlines.cards.presenters.CardPresenterSelector;
import info.hossainkhan.dailynewsheadlines.cards.presenters.TextCardPresenter;
import info.hossainkhan.dailynewsheadlines.cards.presenters.TextFeedCardPresenter;


/**
Expand Down Expand Up @@ -78,7 +78,7 @@ public static Row buildCardRow(final Context context, final NavigationRow naviga

public static Row buildSearchResultCardRow(final Context context, final List<CardItem> cardItems) {
// Build rows using different presenter defined in "CardPresenterSelector"
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new TextCardPresenter(context));
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new TextFeedCardPresenter(context));
for (CardItem card : cardItems) {
listRowAdapter.add(card);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* MIT License
*
* Copyright (c) 2016 Hossain Khan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package info.hossainkhan.dailynewsheadlines.cards;

import android.content.Context;
import android.content.res.Resources;
import android.support.v17.leanback.widget.BaseCardView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.NumberFormat;

import info.hossainkhan.android.core.model.CardItem;
import info.hossainkhan.android.core.picasso.BlurTransformation;
import info.hossainkhan.android.core.picasso.GrayscaleTransformation;
import info.hossainkhan.android.core.usersource.UserSourceManager;
import info.hossainkhan.android.core.util.StringUtils;
import info.hossainkhan.dailynewsheadlines.R;
import timber.log.Timber;


/**
* Text based card view for feed.
*/
public class TextFeedCardView extends BaseCardView {

/**
* User source manager used to check if source is already added.
*/
private final UserSourceManager mUserSourceManager;

public TextFeedCardView(final Context context) {
super(context, null, R.style.TextCardStyle);
LayoutInflater.from(getContext()).inflate(R.layout.text_icon_card_feed, this);
setFocusable(true);

mUserSourceManager = new UserSourceManager(context);
}

public void updateUi(CardItem cardItem) {
final TextView primaryHeadline = (TextView) findViewById(R.id.primary_headline_text);
final TextView summaryText1 = (TextView) findViewById(R.id.summary_text_1);
final TextView summaryText2 = (TextView) findViewById(R.id.summary_text_2);
final ImageView mainContentBackground = (ImageView) findViewById(R.id.main_content_background);
final ImageView iconImage = (ImageView) findViewById(R.id.feed_provider_icon);
final ImageView bookmarkedBadge = (ImageView) findViewById(R.id.feed_subscribed_marker_badge);

if (mUserSourceManager.isAdded(cardItem.contentUrl())) {
bookmarkedBadge.setVisibility(View.VISIBLE);
} else {
bookmarkedBadge.setVisibility(View.INVISIBLE);
}


primaryHeadline.setText(cardItem.title());
summaryText1.setText(cardItem.description());

// Reusing the "CardItem.width" to get total subscriber stats
summaryText2.setText(String.format(getContext().getString(R.string.feed_stats_overview),
NumberFormat.getInstance().format(cardItem.width())));

Context context = getContext();
Picasso picasso = Picasso.with(context);
Resources resources = context.getResources();
if (StringUtils.isNotEmpty(cardItem.imageUrl())) {
picasso
.load(cardItem.imageUrl())
.resize((int) resources.getDimension(R.dimen.card_text_container_width),
(int) resources.getDimension(R.dimen.card_text_container_height))
.transform(new GrayscaleTransformation(picasso))
.transform(new BlurTransformation(context))
.centerInside()
.into(mainContentBackground);
} else {
Timber.w("Unable to load thumb image.");
}

// Reusing the "CardItem.extraText" to get the icon image URL
if (StringUtils.isNotEmpty(cardItem.extraText())) {
picasso
.load(cardItem.extraText())
.into(iconImage);
} else {
Timber.w("Unable to load icon image.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MIT License
*
* Copyright (c) 2016 Hossain Khan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package info.hossainkhan.dailynewsheadlines.cards.presenters;

import android.content.Context;

import info.hossainkhan.android.core.model.CardItem;
import info.hossainkhan.dailynewsheadlines.cards.TextFeedCardView;

/**
* The Presenter displays feed item.
*/
public class TextFeedCardPresenter extends AbstractCardPresenter<TextFeedCardView> {

public TextFeedCardPresenter(Context context) {
super(context);
}

@Override
protected TextFeedCardView onCreateView() {
return new TextFeedCardView(getContext());
}

@Override
public void onBindViewHolder(CardItem cardItem, TextFeedCardView cardView) {
cardView.updateUi(cardItem);
}

}
Loading

0 comments on commit b821168

Please sign in to comment.