Skip to content

Commit

Permalink
Added setting to change favicon provider
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHalvdansson committed Oct 4, 2023
1 parent 7a99028 commit 6ebd0fe
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ private void initializeRecyclerView() {
SettingsUtils.shouldShowTopLevelDepthIndicator(getContext()),
SettingsUtils.shouldShowWebviewExpandButton(getContext()),
ThemeUtils.isDarkMode(getContext()),
Utils.isTablet(getResources()));
Utils.isTablet(getResources()),
SettingsUtils.getPreferredFaviconProvider(getContext()));

adapter.setOnHeaderClickListener(story1 -> Utils.launchCustomTab(getActivity(), story1.url));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.simon.harmonichackernews.data.Comment;
import com.simon.harmonichackernews.data.PollOption;
import com.simon.harmonichackernews.data.Story;
import com.simon.harmonichackernews.network.FaviconLoader;
import com.simon.harmonichackernews.network.UserActions;
import com.simon.harmonichackernews.utils.DialogUtils;
import com.simon.harmonichackernews.utils.FontUtils;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
public boolean showNavigationBar;
public boolean showInvert;
public boolean showExpand;
public String faviconProvider;
public boolean integratedWebview;
public boolean showTopLevelDepthIndicator;
public String username;
Expand Down Expand Up @@ -132,7 +134,8 @@ public CommentsRecyclerViewAdapter(boolean useIntegratedWebview,
boolean shouldShowTopLevelDepthIndicator,
boolean shouldShowExpand,
boolean darkTheme,
boolean tablet) {
boolean tablet,
String favProvider) {
integratedWebview = useIntegratedWebview;
bottomSheet = sheet;
fragmentManager = fm;
Expand All @@ -150,6 +153,7 @@ public CommentsRecyclerViewAdapter(boolean useIntegratedWebview,
showExpand = shouldShowExpand;
darkThemeActive = darkTheme;
isTablet = tablet;
faviconProvider = favProvider;
}

@NotNull
Expand Down Expand Up @@ -265,17 +269,7 @@ public void onClick(View view) {
headerViewHolder.linkInfoContainer.setVisibility(!story.isComment && story.isLink ? View.VISIBLE : View.GONE);

if (showThumbnail && !TextUtils.isEmpty(story.url)) {
try {
Picasso.get()
.load("https://api.faviconkit.com/" + Utils.getDomainName(story.url) + "")
.resize(80, 80)
.onlyScaleDown()
.placeholder(Objects.requireNonNull(ContextCompat.getDrawable(ctx, R.drawable.ic_action_web)))
.into(headerViewHolder.favicon);

} catch (Exception e) {
e.printStackTrace();
}
FaviconLoader.loadFavicon(story.url, headerViewHolder.favicon, ctx, faviconProvider);
}

headerViewHolder.bookmarkIcon.setBackgroundResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ private void showUpdateDialog() {
AlertDialog dialog = new MaterialAlertDialogBuilder(this)
.setTitle("Changelog")
.setMessage(Html.fromHtml("<b>Version 1.11:</b><br>" +
"- Added experimental support for submitting HN posts<br>" +
"- Added alternative comment sortings (thanks John Rapp Farnes)<br>" +
"- Fixed user bio bottom padding<br>" +
"- Added option to choose favicon provider<br>" +
"- Improved old Android navigation scrim (thanks AppearamidGuy)<br>" +
"- Optimized AdBlock adlist loading (thanks AppearamidGuy)<br>" +
"- New comment opening animation when not using SwipeBack<br>" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ private void setupAdapter() {
SettingsUtils.shouldUseCompactHeader(getContext()),
SettingsUtils.shouldUseLeftAlign(getContext()),
SettingsUtils.getPreferredHotness(getContext()),
SettingsUtils.getPreferredFaviconProvider(getContext()),
null);

adapter.setOnLinkClickListener(position -> {
Expand Down Expand Up @@ -382,6 +383,11 @@ public void onResume() {
attemptRefresh();
}

if (adapter.faviconProvider != SettingsUtils.getPreferredFaviconProvider(getContext())) {
adapter.faviconProvider = SettingsUtils.getPreferredFaviconProvider(getContext());
adapter.notifyItemRangeChanged(1, stories.size());
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.google.android.material.button.MaterialButton;
import com.simon.harmonichackernews.data.Story;
import com.simon.harmonichackernews.network.FaviconLoader;
import com.simon.harmonichackernews.utils.FontUtils;
import com.simon.harmonichackernews.utils.SettingsUtils;
import com.simon.harmonichackernews.utils.Utils;
Expand Down Expand Up @@ -75,6 +76,7 @@ public class StoryRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.
public boolean hideJobs;
public boolean compactHeader;
public boolean leftAlign;
public String faviconProvider;
public int hotness;
public int type = 0;
public boolean searching = false;
Expand All @@ -89,6 +91,7 @@ public StoryRecyclerViewAdapter(List<Story> items,
boolean shouldUseCompactHeader,
boolean shouldLeftAlign,
int preferredHotness,
String faviconProv,
String submissionsUserName) {
stories = items;
showPoints = shouldShowPoints;
Expand All @@ -98,6 +101,7 @@ public StoryRecyclerViewAdapter(List<Story> items,
compactHeader = shouldUseCompactHeader;
leftAlign = shouldLeftAlign;
hotness = preferredHotness;
faviconProvider = faviconProv;

atSubmissions = !TextUtils.isEmpty(submissionsUserName);
submitter = submissionsUserName;
Expand Down Expand Up @@ -158,38 +162,28 @@ public void onBindViewHolder(@NotNull final RecyclerView.ViewHolder holder, int
}

storyViewHolder.commentsView.setText(Integer.toString(storyViewHolder.story.descendants));
String url = "";
String host = "";

try {
if (storyViewHolder.story.url != null) {
url = Utils.getDomainName(storyViewHolder.story.url);
host = Utils.getDomainName(storyViewHolder.story.url);
}
} catch (Exception e) {
url = "Unknown";
host = "Unknown";
}

String ptsString = " points";
if (storyViewHolder.story.score == 1) {
ptsString = " point";
}
if (showPoints && !storyViewHolder.story.isComment) {
storyViewHolder.metaView.setText(storyViewHolder.story.score + ptsString + " • " + url + " • " + storyViewHolder.story.getTimeFormatted());
storyViewHolder.metaView.setText(storyViewHolder.story.score + ptsString + " • " + host + " • " + storyViewHolder.story.getTimeFormatted());
} else {
storyViewHolder.metaView.setText(url + " • " + storyViewHolder.story.getTimeFormatted());
storyViewHolder.metaView.setText(host + " • " + storyViewHolder.story.getTimeFormatted());
}

if (thumbnails) {
//Picasso sometimes loses its context, that should just be ignored
try {
Picasso.get()
//.load("https://www.google.com/s2/favicons?domain="+ url + "&sz=80")
.load("https://api.faviconkit.com/" + url + "")
//.load("https://icons.duckduckgo.com/ip3/" + url + ".ico")
.resize(80, 80)
.onlyScaleDown()
.placeholder(Objects.requireNonNull(ContextCompat.getDrawable(ctx, R.drawable.ic_action_web)))
.into(storyViewHolder.metaFavicon);
} catch (Exception ignored){};
FaviconLoader.loadFavicon(storyViewHolder.story.url, storyViewHolder.metaFavicon, ctx, faviconProvider);
}

storyViewHolder.commentsIcon.setImageResource(hotness > 0 && storyViewHolder.story.score + storyViewHolder.story.descendants > hotness ? R.drawable.ic_action_whatshot : R.drawable.ic_action_comment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ protected void onCreate(Bundle savedInstanceState) {
SettingsUtils.shouldUseCompactHeader(this),
SettingsUtils.shouldUseLeftAlign(this),
SettingsUtils.getPreferredHotness(this),
SettingsUtils.getPreferredFaviconProvider(this),
getIntent().getStringExtra(KEY_USER));

adapter.setOnRefreshListener(this::loadSubmissions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.simon.harmonichackernews.network;

import android.content.Context;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.simon.harmonichackernews.R;
import com.simon.harmonichackernews.utils.SettingsUtils;
import com.simon.harmonichackernews.utils.Utils;
import com.squareup.picasso.Picasso;

import java.util.Objects;

public class FaviconLoader {

public static void loadFavicon(String url, ImageView into, Context ctx, String faviconProvider) {
try {
String host = Utils.getDomainName(url);

Picasso.get()
.load(getFaviconUrl(host, faviconProvider))
.resize(80, 80)
.onlyScaleDown()
.placeholder(Objects.requireNonNull(ContextCompat.getDrawable(ctx, R.drawable.ic_action_web)))
.into(into);
} catch (Exception ignored){};
}

private static String getFaviconUrl(String host, String faviconProvider) {
switch (faviconProvider) {
case "Faviconkit":
return "https://api.faviconkit.com/" + host;
case "Google":
return "https://www.google.com/s2/favicons?domain="+ host + "&sz=80";
case "DuckDuckGo":
return "https://icons.duckduckgo.com/ip3/" + host + ".ico";
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public static String getPreferredCommentSorting(Context ctx) {
return prefs.getString("pref_comment_sorting", "Default");
}

public static String getPreferredFaviconProvider(Context ctx) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
return prefs.getString("pref_favicon_provider", "Faviconkit");
}

public static int getBookmarksIndex(Resources res) {
String[] sortingOptions = res.getStringArray(R.array.sorting_options);

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@
<item>Reply count</item>
</string-array>

<string-array name="favicon_providers">
<item>Faviconkit</item>
<item>Google</item>
<item>DuckDuckGo</item>
</string-array>

</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@
app:icon="@drawable/ic_action_visibility"
app:title="Transparent status bar" />

<DropDownPreference
app:singleLineTitle="false"
app:icon="@drawable/ic_action_web"
app:title="Favicon provider"
app:key="pref_favicon_provider"
app:useSimpleSummaryProvider="true"
app:entries="@array/favicon_providers"
app:defaultValue="Faviconkit"
app:entryValues="@array/favicon_providers" />

<DropDownPreference
app:singleLineTitle="false"
app:icon="@drawable/ic_action_whatshot"
Expand Down

0 comments on commit 6ebd0fe

Please sign in to comment.