From f6aca0e9a0510e520f6fb1e81f85df5c6e8d0f81 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 13 Jul 2021 13:01:18 +0800 Subject: Show error message when loading RPAN broadcasts failed. --- .../activities/LinkResolverActivity.java | 2 + .../infinityforreddit/activities/RPANActivity.java | 47 ++++++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java index 2a26ae10..b5452989 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java @@ -7,6 +7,7 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Bundle; +import android.util.Log; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; @@ -189,6 +190,7 @@ public class LinkResolverActivity extends AppCompatActivity { startActivity(intent); } else if (path.matches(RPAN_BROADCAST_PATTERN)) { Intent intent = new Intent(this, RPANActivity.class); + Log.i("asdfasdf", "sd " + path); intent.putExtra(RPANActivity.EXTRA_RPAN_BROADCAST_FULLNAME_OR_ID, path.substring(path.lastIndexOf('/') + 1)); startActivity(intent); } else if (authority.equals("redd.it") && path.matches(REDD_IT_POST_PATTERN)) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RPANActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RPANActivity.java index 38dd995f..76ec456b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RPANActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RPANActivity.java @@ -27,12 +27,14 @@ import androidx.viewpager2.adapter.FragmentStateAdapter; import androidx.viewpager2.widget.ViewPager2; import com.evernote.android.state.State; +import com.google.android.material.snackbar.Snackbar; import com.livefront.bridge.Bridge; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.concurrent.Executor; @@ -53,6 +55,7 @@ import ml.docilealligator.infinityforreddit.font.TitleFontFamily; import ml.docilealligator.infinityforreddit.fragments.ViewRPANBroadcastFragment; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; +import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -131,8 +134,26 @@ public class RPANActivity extends AppCompatActivity { if (response.isSuccessful()) { parseRPANBroadcasts(response.body()); } else { - Toast.makeText(RPANActivity.this, - R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + try { + ResponseBody responseBody = response.errorBody(); + if (responseBody != null) { + JSONObject errorObject = new JSONObject(responseBody.string()); + String errorMessage = errorObject.getString(JSONUtils.DATA_KEY); + if (!errorMessage.isEmpty() && !errorMessage.equals("null")) { + Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_LONG).show(); + } else { + Toast.makeText(RPANActivity.this, + R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + } + } else { + Toast.makeText(RPANActivity.this, + R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + } + } catch (IOException | JSONException e) { + e.printStackTrace(); + Toast.makeText(RPANActivity.this, + R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + } } } @@ -162,8 +183,26 @@ public class RPANActivity extends AppCompatActivity { } }); } else { - Toast.makeText(RPANActivity.this, - R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + try { + ResponseBody responseBody = response.errorBody(); + if (responseBody != null) { + JSONObject errorObject = new JSONObject(responseBody.string()); + String errorMessage = errorObject.getString(JSONUtils.DATA_KEY); + if (!errorMessage.isEmpty() && !errorMessage.equals("null")) { + Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_LONG).show(); + } else { + Toast.makeText(RPANActivity.this, + R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + } + } else { + Toast.makeText(RPANActivity.this, + R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + } + } catch (IOException | JSONException e) { + e.printStackTrace(); + Toast.makeText(RPANActivity.this, + R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + } } } -- cgit v1.2.3