mirror of
https://github.com/revanced/revanced-integrations.git
synced 2024-08-16 20:30:51 +02: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.text.DecimalFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import pl.jakubweg.objects.UserStats;
|
|
||||||
import pl.jakubweg.requests.Requester;
|
import pl.jakubweg.requests.Requester;
|
||||||
|
|
||||||
import static android.text.Html.fromHtml;
|
|
||||||
import static pl.jakubweg.SponsorBlockSettings.DefaultBehaviour;
|
import static pl.jakubweg.SponsorBlockSettings.DefaultBehaviour;
|
||||||
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP;
|
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_ADJUST_NEW_SEGMENT_STEP;
|
||||||
import static pl.jakubweg.SponsorBlockSettings.PREFERENCES_KEY_COUNT_SKIPS;
|
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.countSkips;
|
||||||
import static pl.jakubweg.SponsorBlockSettings.setSeenGuidelines;
|
import static pl.jakubweg.SponsorBlockSettings.setSeenGuidelines;
|
||||||
import static pl.jakubweg.SponsorBlockSettings.showToastWhenSkippedAutomatically;
|
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.SponsorBlockSettings.uuid;
|
||||||
import static pl.jakubweg.StringRef.str;
|
import static pl.jakubweg.StringRef.str;
|
||||||
|
|
||||||
@SuppressWarnings({"unused", "deprecation"}) // injected
|
@SuppressWarnings({"unused", "deprecation"}) // injected
|
||||||
public class SponsorBlockPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
public class SponsorBlockPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
private static final DecimalFormat FORMATTER = new DecimalFormat("#,###,###");
|
public static final DecimalFormat FORMATTER = new DecimalFormat("#,###,###");
|
||||||
private static final String SAVED_TEMPLATE = "%dh %.1f minutes";
|
public static final String SAVED_TEMPLATE = "%dh %.1f minutes";
|
||||||
private final ArrayList<Preference> preferencesToDisableWhenSBDisabled = new ArrayList<>();
|
private final ArrayList<Preference> preferencesToDisableWhenSBDisabled = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -174,59 +170,12 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement
|
|||||||
category.setTitle(str("stats"));
|
category.setTitle(str("stats"));
|
||||||
preferencesToDisableWhenSBDisabled.add(category);
|
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);
|
Preference preference = new Preference(context);
|
||||||
category.addPreference(preference);
|
category.addPreference(preference);
|
||||||
String formatted = FORMATTER.format(stats.getSegmentCount());
|
preference.setTitle(str("stats_loading"));
|
||||||
preference.setTitle(fromHtml(str("stats_submissions", formatted)));
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
Requester.retrieveUserStats(category, preference);
|
||||||
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)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package pl.jakubweg.requests;
|
package pl.jakubweg.requests;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
import android.preference.EditTextPreference;
|
||||||
|
import android.preference.Preference;
|
||||||
|
import android.preference.PreferenceCategory;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
@ -23,6 +28,11 @@ import pl.jakubweg.SponsorBlockUtils.VoteOption;
|
|||||||
import pl.jakubweg.objects.SponsorSegment;
|
import pl.jakubweg.objects.SponsorSegment;
|
||||||
import pl.jakubweg.objects.UserStats;
|
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;
|
import static pl.jakubweg.StringRef.str;
|
||||||
|
|
||||||
public class Requester {
|
public class Requester {
|
||||||
@ -141,22 +151,82 @@ public class Requester {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserStats getUserStats() {
|
@SuppressWarnings("deprecation")
|
||||||
UserStats defaultStats = new UserStats("N/A", -1, -1, -1);
|
public static void retrieveUserStats(PreferenceCategory category, Preference loadingPreference) {
|
||||||
if (!SponsorBlockSettings.isSponsorBlockEnabled)
|
if (!SponsorBlockSettings.isSponsorBlockEnabled) {
|
||||||
return defaultStats;
|
loadingPreference.setTitle(str("stats_sb_disabled"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Context context = category.getContext();
|
||||||
|
|
||||||
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
HttpURLConnection connection = getConnectionFromRoute(Route.GET_USER_STATS, SponsorBlockSettings.uuid);
|
HttpURLConnection connection = getConnectionFromRoute(Route.GET_USER_STATS, SponsorBlockSettings.uuid);
|
||||||
JSONObject json = new JSONObject(parseJson(connection));
|
JSONObject json = new JSONObject(parseJson(connection));
|
||||||
connection.disconnect();
|
connection.disconnect();
|
||||||
return new UserStats(json.getString("userName"), json.getDouble("minutesSaved"), json.getInt("segmentCount"),
|
UserStats stats = new UserStats(json.getString("userName"), json.getDouble("minutesSaved"), json.getInt("segmentCount"),
|
||||||
json.getInt("viewCount"));
|
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) {
|
catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
return defaultStats;
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setUsername(String username) {
|
public static void setUsername(String username) {
|
||||||
|
@ -181,6 +181,8 @@
|
|||||||
<string name="skip_showbutton">Show a skip button</string>
|
<string name="skip_showbutton">Show a skip button</string>
|
||||||
<string name="skip_ignore">Don\'t do anything</string>
|
<string name="skip_ignore">Don\'t do anything</string>
|
||||||
<string name="stats">Stats</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">Your username: <b>%s</b></string>
|
||||||
<string name="stats_username_change">Click to change your username</string>
|
<string name="stats_username_change">Click to change your username</string>
|
||||||
<string name="stats_submissions">Submissions: <b>%s</b></string>
|
<string name="stats_submissions">Submissions: <b>%s</b></string>
|
||||||
|
Loading…
Reference in New Issue
Block a user