Skip to content

Commit 82c0e5d

Browse files
Refactored HttpClient,
Show the actual error when loading the home feed, Use the nested cause exception for description if known
1 parent 7c0908f commit 82c0e5d

File tree

14 files changed

+382
-190
lines changed

14 files changed

+382
-190
lines changed

app/src/main/java/com/mrboomdev/awery/AweryApp.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ public static <A extends Activity> A getActivity(Class<A> clazz) {
8989
return null;
9090
}
9191

92+
public static Context getContext() {
93+
return app;
94+
}
95+
9296
public static void toast(Activity activity, String text, int duration) {
9397
if(activity == null) {
9498
Toast.makeText(app, text, duration).show();

app/src/main/java/com/mrboomdev/awery/data/Constants.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@
22

33
public class Constants {
44
public static final String DEFAULT_UA = "Mozilla/5.0 (Linux; Android 13; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36";
5+
public static final String DIRECTORY_NET_CACHE = "network_cache";
6+
public static final String DIRECTORY_IMAGE_CACHE = "img";
7+
public static final String DIRECTORY_WEBVIEW_CACHE = "WebView";
58

9+
/**
10+
* Typically your IDE will warn if you have any code after a return statement,
11+
* but this value will let you keep the code uncommented!
12+
*/
613
public static boolean alwaysTrue() {
714
return true;
815
}
916

17+
/**
18+
* Typically your IDE will warn if you have any code after a return statement,
19+
* but this value will let you keep the code uncommented!
20+
*/
1021
public static boolean alwaysFalse() {
1122
return false;
1223
}
1324

25+
/**
26+
* Typically your IDE will warn if you have any code after a throw statement,
27+
* but this method will let you keep the code uncommented!
28+
*/
1429
public static void alwaysThrowException() {
1530
throw new RuntimeException();
1631
}

app/src/main/java/com/mrboomdev/awery/extensions/support/anilist/AnilistApi.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

app/src/main/java/com/mrboomdev/awery/extensions/support/anilist/query/AnilistQuery.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package com.mrboomdev.awery.extensions.support.anilist.query;
22

3-
import com.mrboomdev.awery.extensions.support.anilist.AnilistApi;
3+
import com.mrboomdev.awery.AweryApp;
4+
import com.mrboomdev.awery.R;
5+
import com.mrboomdev.awery.util.exceptions.HttpException;
46
import com.mrboomdev.awery.util.exceptions.ZeroResultsException;
57
import com.mrboomdev.awery.util.graphql.GraphQLAdapter;
68
import com.mrboomdev.awery.util.graphql.GraphQLParser;
9+
import com.mrboomdev.awery.util.io.HttpClient;
710
import com.squareup.moshi.JsonDataException;
11+
import com.squareup.moshi.Moshi;
12+
import com.squareup.moshi.Types;
813

914
import java.io.IOException;
1015
import java.lang.reflect.Type;
1116
import java.util.Collection;
17+
import java.util.HashMap;
1218
import java.util.List;
1319
import java.util.Map;
1420

@@ -31,11 +37,48 @@ public boolean useToken() {
3137
}
3238

3339
public int getCacheTime() {
34-
return 25;
40+
return 60_000;
41+
}
42+
43+
private void executeQueryHttp(ResponseCallback<String> callback) throws HttpClient.HttpException {
44+
var data = new HashMap<String, String>() {{
45+
put("query", getQuery());
46+
put("variables", getVariables());
47+
}};
48+
49+
if(useToken()/* && Anilist.INSTANCE.getToken() != null*/) {
50+
//headers.put("Authorization", "Bearer " + Anilist.INSTANCE.getToken());
51+
}
52+
53+
var moshi = new Moshi.Builder().build();
54+
var adapter = moshi.adapter(Types.newParameterizedType(Map.class, String.class, String.class));
55+
var json = adapter.toJson(data);
56+
57+
HttpClient.post("https://graphql.anilist.co")
58+
.setBody(json, HttpClient.ContentType.JSON)
59+
.setCache(getCacheTime(), HttpClient.CacheMode.CACHE_FIRST)
60+
.addHeader("Content-Type", "application/json")
61+
.addHeader("Accept", "application/json")
62+
.callAsync(new HttpClient.HttpCallback() {
63+
@Override
64+
public void onResponse(HttpClient.HttpResponse response) {
65+
if(!response.getText().startsWith("{")) {
66+
var context = AweryApp.getAnyContext();
67+
throw new HttpException(context.getString(R.string.anilist_down));
68+
}
69+
70+
callback.onResponse(response.getText());
71+
}
72+
73+
@Override
74+
public void onError(HttpClient.HttpException e) {
75+
resolveException(e);
76+
}
77+
});
3578
}
3679

3780
public AnilistQuery<T> executeQuery(ResponseCallback<T> callback) {
38-
AnilistApi.__executeQueryImpl(this, response -> {
81+
executeQueryHttp(response -> {
3982
T processed;
4083

4184
try {
@@ -63,7 +106,7 @@ public AnilistQuery<T> executeQuery(ResponseCallback<T> callback) {
63106
if(finallyCallback != null) {
64107
finallyCallback.run();
65108
}
66-
}, this::resolveException);
109+
});
67110

68111
return this;
69112
}

app/src/main/java/com/mrboomdev/awery/ui/activity/player/PlayerActivity.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,9 @@ public void onSuccess(List<CatalogVideo> catalogVideos) {
329329
@Override
330330
public void onFailure(Throwable throwable) {
331331
if(isDestroyed()) return;
332-
var error = new ExceptionDescriptor(throwable);
333332

334-
if(!error.isGenericError()) {
335-
Log.e(TAG, "Failed to load videos", throwable);
336-
}
333+
var error = new ExceptionDescriptor(throwable);
334+
Log.e(TAG, "Failed to load videos", throwable);
337335

338336
AweryApp.toast(error.getShortDescription(), 1);
339337
finish();

app/src/main/java/com/mrboomdev/awery/ui/fragments/AnimeFragment.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.mrboomdev.awery.ui.adapter.MediaPagerAdapter;
1919
import com.mrboomdev.awery.util.MediaUtils;
2020
import com.mrboomdev.awery.util.UniqueIdGenerator;
21+
import com.mrboomdev.awery.util.exceptions.ExceptionDescriptor;
2122
import com.mrboomdev.awery.util.exceptions.ZeroResultsException;
2223

2324
import java.io.File;
@@ -163,8 +164,17 @@ private void finishedLoading(int loadId, Throwable t) {
163164
getBinding().swipeRefresher.setRefreshing(false);
164165
getEmptyAdapter().setEnabledSuperForce(true);
165166

166-
setEmptyData(false, "Nothing found!",
167-
"It looks like there's nothing here. Try installing some extensions to see something different!");
167+
if(t == null) {
168+
setEmptyData(false,
169+
"Nothing found!",
170+
"It looks like there's nothing here. Try installing some extensions to see something different!");
171+
} else {
172+
var details = new ExceptionDescriptor(t);
173+
174+
setEmptyData(false,
175+
details.getTitle(requireContext()),
176+
details.getMessage(requireContext()));
177+
}
168178
});
169179
}
170180
}

app/src/main/java/com/mrboomdev/awery/ui/fragments/MediaPlayFragment.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,7 @@ private void handleExceptionMark(ExtensionProvider source, Throwable throwable)
387387
if(source != selectedSource) return;
388388
var error = new ExceptionDescriptor(throwable);
389389

390-
if(!error.isGenericError()) {
391-
Log.w(TAG, "Error isn't generic.", throwable);
392-
}
393-
394-
if(error.isProgramException()) {
390+
if(!error.isNetworkException()) {
395391
sourceStatuses.put(source, ExtensionStatus.BROKEN_PARSER);
396392
} else if(throwable instanceof ZeroResultsException) {
397393
sourceStatuses.put(source, ExtensionStatus.NOT_FOUND);

app/src/main/java/com/mrboomdev/awery/ui/widgets/YouTubeEmbedView.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.graphics.Color;
5+
import android.util.Log;
56
import android.widget.FrameLayout;
67

78
import androidx.annotation.NonNull;
@@ -10,14 +11,16 @@
1011
import com.squareup.moshi.Moshi;
1112

1213
import java.io.IOException;
14+
import java.util.Objects;
1315

1416
public class YouTubeEmbedView extends FrameLayout {
15-
private final String PIPED_INSTANCE = "https://pipedapi.kavin.rocks";
16-
private final String VIDEO_QUERY_URL = PIPED_INSTANCE + "/streams/";
17-
private final String YOUTUBE_SHORT_DOMAIN = "youtu.be";
18-
private final String YOUTUBE_DOMAIN = "youtube.com";
19-
private final String YOUTUBE_MOBILE_SUBDOMAIN = "m.";
20-
private final String WWW = "www.";
17+
private static final String TAG = "YouTubeEmbedView";
18+
private static final String PIPED_INSTANCE = "https://pipedapi.kavin.rocks";
19+
private static final String VIDEO_QUERY_URL = PIPED_INSTANCE + "/streams/";
20+
private static final String YOUTUBE_SHORT_DOMAIN = "youtu.be";
21+
private static final String YOUTUBE_DOMAIN = "youtube.com";
22+
private static final String YOUTUBE_MOBILE_SUBDOMAIN = "m.";
23+
private static final String WWW = "www.";
2124

2225
public YouTubeEmbedView(@NonNull Context context) {
2326
super(context);
@@ -27,28 +30,32 @@ public YouTubeEmbedView(@NonNull Context context) {
2730
}
2831

2932
public void loadById(String id) {
30-
HttpClient.get(VIDEO_QUERY_URL + id, (HttpClient.SimpleHttpCallback) (response, exception) -> {
31-
if(response != null) {
32-
var moshi = new Moshi.Builder().build();
33-
var adapter = moshi.adapter(PipedResponse.class);
34-
35-
try {
36-
var video = adapter.fromJson(response.getText());
37-
38-
if(video.error != null) {
33+
new HttpClient.Request()
34+
.setUrl(VIDEO_QUERY_URL + id)
35+
.setCache(24 * 60 * 60 * 1000, HttpClient.CacheMode.CACHE_FIRST)
36+
.callAsync((HttpClient.SimpleHttpCallback) (response, exception) -> {
37+
if(response != null) {
38+
var moshi = new Moshi.Builder().build();
39+
var adapter = moshi.adapter(PipedResponse.class);
40+
41+
try {
42+
var video = adapter.fromJson(response.getText());
43+
Objects.requireNonNull(video);
44+
45+
if(video.error != null) {
46+
createErrorUi();
47+
return;
48+
}
49+
50+
createInfoUi();
51+
} catch(IOException e) {
52+
Log.e(TAG, e.getMessage(), e);
53+
}
54+
} else if(exception != null) {
3955
createErrorUi();
40-
return;
56+
Log.e(TAG, exception.getMessage(), exception);
4157
}
42-
43-
createInfoUi();
44-
} catch(IOException e) {
45-
e.printStackTrace();
46-
}
47-
} else if(exception != null) {
48-
createErrorUi();
49-
exception.printStackTrace();
50-
}
51-
});
58+
});
5259
}
5360

5461
public void loadByUrl(@NonNull String url) {

0 commit comments

Comments
 (0)