mirror of
https://github.com/revanced/revanced-integrations.git
synced 2025-01-11 12:35:50 +01:00
make loading stats async
This commit is contained in:
parent
c589b41990
commit
6f63df0ce6
@ -20,10 +20,8 @@ import android.widget.Toast;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import pl.jakubweg.objects.UserStats;
|
||||
import pl.jakubweg.requests.Requester;
|
||||
|
||||
import static android.text.Html.fromHtml;
|
||||
import static pl.jakubweg.SponsorBlockSettings.DefaultBehaviour;
|
||||
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP;
|
||||
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_COUNT_SKIPS;
|
||||
@ -37,15 +35,13 @@ import static pl.jakubweg.SponsorBlockSettings.adjustNewSegmentMillis;
|
||||
import static pl.jakubweg.SponsorBlockSettings.countSkips;
|
||||
import static pl.jakubweg.SponsorBlockSettings.setSeenGuidelines;
|
||||
import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically;
|
||||
import static pl.jakubweg.SponsorBlockSettings.skippedSegments;
|
||||
import static pl.jakubweg.SponsorBlockSettings.skippedTime;
|
||||
import static pl.jakubweg.SponsorBlockSettings.uuid;
|
||||
import static pl.jakubweg.StringRef.str;
|
||||
|
||||
@SuppressWarnings({"unused", "deprecation"}) // injected
|
||||
public class SponsorBlockPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
private static final DecimalFormat FORMATTER = new DecimalFormat("#,###,###");
|
||||
private static final String SAVED_TEMPLATE = "%dh %.1f minutes";
|
||||
public static final DecimalFormat FORMATTER = new DecimalFormat("#,###,###");
|
||||
public static final String SAVED_TEMPLATE = "%dh %.1f minutes";
|
||||
private final ArrayList<Preference> preferencesToDisableWhenSBDisabled = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
@ -174,59 +170,12 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
|
||||
category.setTitle(str("stats"));
|
||||
preferencesToDisableWhenSBDisabled.add(category);
|
||||
|
||||
UserStats stats = Requester.getUserStats();
|
||||
|
||||
{
|
||||
EditTextPreference preference = new EditTextPreference(context);
|
||||
category.addPreference(preference);
|
||||
String userName = stats.getUserName();
|
||||
preference.setTitle(fromHtml(str("stats_username", userName)));
|
||||
preference.setSummary(str("stats_username_change"));
|
||||
preference.setText(userName);
|
||||
preference.setOnPreferenceChangeListener((preference1, newUsername) -> {
|
||||
Requester.setUsername((String) newUsername);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
Preference preference = new Preference(context);
|
||||
category.addPreference(preference);
|
||||
String formatted = FORMATTER.format(stats.getSegmentCount());
|
||||
preference.setTitle(fromHtml(str("stats_submissions", formatted)));
|
||||
}
|
||||
preference.setTitle(str("stats_loading"));
|
||||
|
||||
{
|
||||
Preference preference = new Preference(context);
|
||||
category.addPreference(preference);
|
||||
String formatted = FORMATTER.format(stats.getViewCount());
|
||||
|
||||
double saved = stats.getMinutesSaved();
|
||||
int hoursSaved = (int) (saved / 60);
|
||||
double minutesSaved = saved % 60;
|
||||
String formattedSaved = String.format(SAVED_TEMPLATE, hoursSaved, minutesSaved);
|
||||
|
||||
preference.setTitle(fromHtml(str("stats_saved", formatted)));
|
||||
preference.setSummary(fromHtml(str("stats_saved_sum", formattedSaved)));
|
||||
preference.setOnPreferenceClickListener(preference1 -> {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse("https://sponsor.ajay.app/stats/"));
|
||||
preference1.getContext().startActivity(i);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
Preference preference = new Preference(context);
|
||||
category.addPreference(preference);
|
||||
String formatted = FORMATTER.format(skippedSegments);
|
||||
|
||||
long hoursSaved = skippedTime / 3600000;
|
||||
double minutesSaved = (skippedTime / 60000d) % 60;
|
||||
String formattedSaved = String.format(SAVED_TEMPLATE, hoursSaved, minutesSaved);
|
||||
|
||||
preference.setTitle(fromHtml(str("stats_self_saved", formatted)));
|
||||
preference.setSummary(fromHtml(str("stats_self_saved_sum", formattedSaved)));
|
||||
Requester.retrieveUserStats(category, preference);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,13 @@
|
||||
package pl.jakubweg.requests;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceCategory;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.json.JSONArray;
|
||||
@ -23,6 +28,11 @@ import pl.jakubweg.SponsorBlockUtils.VoteOption;
|
||||
import pl.jakubweg.objects.SponsorSegment;
|
||||
import pl.jakubweg.objects.UserStats;
|
||||
|
||||
import static android.text.Html.fromHtml;
|
||||
import static pl.jakubweg.SponsorBlockPreferenceFragment.FORMATTER;
|
||||
import static pl.jakubweg.SponsorBlockPreferenceFragment.SAVED_TEMPLATE;
|
||||
import static pl.jakubweg.SponsorBlockSettings.skippedSegments;
|
||||
import static pl.jakubweg.SponsorBlockSettings.skippedTime;
|
||||
import static pl.jakubweg.StringRef.str;
|
||||
|
||||
public class Requester {
|
||||
@ -141,22 +151,82 @@ public class Requester {
|
||||
}
|
||||
}
|
||||
|
||||
public static UserStats getUserStats() {
|
||||
UserStats defaultStats = new UserStats("N/A", -1, -1, -1);
|
||||
if (!SponsorBlockSettings.isSponsorBlockEnabled)
|
||||
return defaultStats;
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void retrieveUserStats(PreferenceCategory category, Preference loadingPreference) {
|
||||
if (!SponsorBlockSettings.isSponsorBlockEnabled) {
|
||||
loadingPreference.setTitle(str("stats_sb_disabled"));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
HttpURLConnection connection = getConnectionFromRoute(Route.GET_USER_STATS, SponsorBlockSettings.uuid);
|
||||
JSONObject json = new JSONObject(parseJson(connection));
|
||||
connection.disconnect();
|
||||
return new UserStats(json.getString("userName"), json.getDouble("minutesSaved"), json.getInt("segmentCount"),
|
||||
json.getInt("viewCount"));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return defaultStats;
|
||||
Context context = category.getContext();
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
HttpURLConnection connection = getConnectionFromRoute(Route.GET_USER_STATS, SponsorBlockSettings.uuid);
|
||||
JSONObject json = new JSONObject(parseJson(connection));
|
||||
connection.disconnect();
|
||||
UserStats stats = new UserStats(json.getString("userName"), json.getDouble("minutesSaved"), json.getInt("segmentCount"),
|
||||
json.getInt("viewCount"));
|
||||
|
||||
category.removePreference(loadingPreference);
|
||||
|
||||
{
|
||||
EditTextPreference preference = new EditTextPreference(context);
|
||||
category.addPreference(preference);
|
||||
String userName = stats.getUserName();
|
||||
preference.setTitle(fromHtml(str("stats_username", userName)));
|
||||
preference.setSummary(str("stats_username_change"));
|
||||
preference.setText(userName);
|
||||
preference.setOnPreferenceChangeListener((preference1, newUsername) -> {
|
||||
Requester.setUsername((String) newUsername);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
Preference preference = new Preference(context);
|
||||
category.addPreference(preference);
|
||||
String formatted = FORMATTER.format(stats.getSegmentCount());
|
||||
preference.setTitle(fromHtml(str("stats_submissions", formatted)));
|
||||
}
|
||||
|
||||
{
|
||||
Preference preference = new Preference(context);
|
||||
category.addPreference(preference);
|
||||
String formatted = FORMATTER.format(stats.getViewCount());
|
||||
|
||||
double saved = stats.getMinutesSaved();
|
||||
int hoursSaved = (int) (saved / 60);
|
||||
double minutesSaved = saved % 60;
|
||||
String formattedSaved = String.format(SAVED_TEMPLATE, hoursSaved, minutesSaved);
|
||||
|
||||
preference.setTitle(fromHtml(str("stats_saved", formatted)));
|
||||
preference.setSummary(fromHtml(str("stats_saved_sum", formattedSaved)));
|
||||
preference.setOnPreferenceClickListener(preference1 -> {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse("https://sponsor.ajay.app/stats/"));
|
||||
preference1.getContext().startActivity(i);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
Preference preference = new Preference(context);
|
||||
category.addPreference(preference);
|
||||
String formatted = FORMATTER.format(skippedSegments);
|
||||
|
||||
long hoursSaved = skippedTime / 3600000;
|
||||
double minutesSaved = (skippedTime / 60000d) % 60;
|
||||
String formattedSaved = String.format(SAVED_TEMPLATE, hoursSaved, minutesSaved);
|
||||
|
||||
preference.setTitle(fromHtml(str("stats_self_saved", formatted)));
|
||||
preference.setSummary(fromHtml(str("stats_self_saved_sum", formattedSaved)));
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public static void setUsername(String username) {
|
||||
|
@ -181,6 +181,8 @@
|
||||
<string name="skip_showbutton">Show a skip button</string>
|
||||
<string name="skip_ignore">Don\'t do anything</string>
|
||||
<string name="stats">Stats</string>
|
||||
<string name="stats_loading">Loading..</string>
|
||||
<string name="stats_sb_disabled">SponsorBlock is disabled</string>
|
||||
<string name="stats_username">Your username: <b>%s</b></string>
|
||||
<string name="stats_username_change">Click to change your username</string>
|
||||
<string name="stats_submissions">Submissions: <b>%s</b></string>
|
||||
|
Loading…
Reference in New Issue
Block a user