Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

Commit

Permalink
feat(YouTube): Add donation link to settings about screen (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios authored Sep 9, 2024
1 parent 0f5dfb4 commit b816c45
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.revanced.integrations.shared.settings.preference;

import static app.revanced.integrations.shared.StringRef.sf;
import static app.revanced.integrations.shared.StringRef.str;
import static app.revanced.integrations.youtube.requests.Route.Method.GET;

Expand Down Expand Up @@ -71,7 +72,7 @@ protected int getDarkColor() {
return Color.BLACK;
}

private String createDialogHtml(ReVancedSocialLink[] socialLinks) {
private String createDialogHtml(WebLink[] socialLinks) {
final boolean isNetworkConnected = Utils.isNetworkConnected();

StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -122,7 +123,7 @@ private String createDialogHtml(ReVancedSocialLink[] socialLinks) {
.append("</h2>");

builder.append("<div>");
for (ReVancedSocialLink social : socialLinks) {
for (WebLink social : socialLinks) {
builder.append("<div style=\"margin-bottom: 20px;\">");
builder.append(String.format("<a href=\"%s\">%s</a>", social.url, social.name));
builder.append("</div>");
Expand Down Expand Up @@ -151,7 +152,7 @@ private String createDialogHtml(ReVancedSocialLink[] socialLinks) {
}

private void fetchLinksAndShowDialog(@Nullable ProgressDialog progress) {
ReVancedSocialLink[] socialLinks = SocialLinksRoutes.fetchSocialLinks();
WebLink[] socialLinks = SocialLinksRoutes.fetchSocialLinks();
String htmlDialog = createDialogHtml(socialLinks);

Utils.runOnMainThreadNowOrLater(() -> {
Expand Down Expand Up @@ -221,19 +222,19 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
}
}

class ReVancedSocialLink {
class WebLink {
final boolean preferred;
final String name;
final String url;

ReVancedSocialLink(JSONObject json) throws JSONException {
WebLink(JSONObject json) throws JSONException {
this(json.getBoolean("preferred"),
json.getString("name"),
json.getString("url")
);
}

ReVancedSocialLink(boolean preferred, String name, String url) {
WebLink(boolean preferred, String name, String url) {
this.preferred = preferred;
this.name = name;
this.url = url;
Expand All @@ -251,24 +252,33 @@ public String toString() {
}

class SocialLinksRoutes {
/**
* Simple link to the website donate page,
* rather than fetching and parsing the donation links using the API.
*/
public static final WebLink DONATE_LINK = new WebLink(true,
sf("revanced_settings_about_links_donate").toString(),
"https://revanced.app/donate");

/**
* Links to use if fetch links api call fails.
*/
private static final ReVancedSocialLink[] NO_CONNECTION_STATIC_LINKS = {
new ReVancedSocialLink(true, "ReVanced.app", "https://revanced.app")
private static final WebLink[] NO_CONNECTION_STATIC_LINKS = {
new WebLink(true, "ReVanced.app", "https://revanced.app"),
DONATE_LINK,
};

private static final String SOCIAL_LINKS_PROVIDER = "https://api.revanced.app/v2";
private static final Route.CompiledRoute GET_SOCIAL = new Route(GET, "/socials").compile();

@Nullable
private static volatile ReVancedSocialLink[] fetchedLinks;
private static volatile WebLink[] fetchedLinks;

static boolean hasFetchedLinks() {
return fetchedLinks != null;
}

static ReVancedSocialLink[] fetchSocialLinks() {
static WebLink[] fetchSocialLinks() {
try {
if (hasFetchedLinks()) return fetchedLinks;

Expand All @@ -290,14 +300,17 @@ static ReVancedSocialLink[] fetchSocialLinks() {
JSONObject json = Requester.parseJSONObjectAndDisconnect(connection);
JSONArray socials = json.getJSONArray("socials");

List<ReVancedSocialLink> links = new ArrayList<>();
List<WebLink> links = new ArrayList<>();

links.add(DONATE_LINK); // Show donate link first.
for (int i = 0, length = socials.length(); i < length; i++) {
ReVancedSocialLink link = new ReVancedSocialLink(socials.getJSONObject(i));
WebLink link = new WebLink(socials.getJSONObject(i));
links.add(link);
}

Logger.printDebug(() -> "links: " + links);

return fetchedLinks = links.toArray(new ReVancedSocialLink[0]);
return fetchedLinks = links.toArray(new WebLink[0]);

} catch (SocketTimeoutException ex) {
Logger.printInfo(() -> "Could not fetch social links", ex); // No toast.
Expand Down

0 comments on commit b816c45

Please sign in to comment.