From 44bb1999afa6081901e41309e5227f3ca2766d2b Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Fri, 11 Oct 2019 17:51:47 +0200 Subject: [PATCH] When opening a post via "Open As", if post lookup from the target instance fails, display an error instead of opening the post in the browser. (#1531) Addresses #1526 --- .../tusky/BottomSheetActivity.kt | 19 ++++++++++++++++--- .../com/keylesspalace/tusky/MainActivity.java | 2 +- .../tusky/fragment/SFragment.java | 3 ++- app/src/main/res/values/strings.xml | 1 + .../tusky/BottomSheetActivityTest.kt | 15 +++++++++++++++ 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/BottomSheetActivity.kt b/app/src/main/java/com/keylesspalace/tusky/BottomSheetActivity.kt index e370ec70a..c7389e323 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BottomSheetActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/BottomSheetActivity.kt @@ -19,6 +19,7 @@ import android.content.Intent import android.os.Bundle import android.view.View import android.widget.LinearLayout +import android.widget.Toast import androidx.annotation.VisibleForTesting import androidx.lifecycle.Lifecycle import com.google.android.material.bottomsheet.BottomSheetBehavior @@ -62,7 +63,7 @@ abstract class BottomSheetActivity : BaseActivity() { } - open fun viewUrl(url: String) { + open fun viewUrl(url: String, lookupFallbackBehavior: PostLookupFallbackBehavior = PostLookupFallbackBehavior.OPEN_IN_BROWSER) { if (!looksLikeMastodonUrl(url)) { openLink(url) return @@ -88,11 +89,11 @@ abstract class BottomSheetActivity : BaseActivity() { return@subscribe } - openLink(url) + performUrlFallbackAction(url, lookupFallbackBehavior) }, { if (!getCancelSearchRequested(url)) { onEndSearch(url) - openLink(url) + performUrlFallbackAction(url, lookupFallbackBehavior) } }) @@ -113,6 +114,13 @@ abstract class BottomSheetActivity : BaseActivity() { startActivityWithSlideInAnimation(intent) } + protected open fun performUrlFallbackAction(url: String, fallbackBehavior: PostLookupFallbackBehavior) { + when (fallbackBehavior) { + PostLookupFallbackBehavior.OPEN_IN_BROWSER -> openLink(url) + PostLookupFallbackBehavior.DISPLAY_ERROR -> Toast.makeText(this, getString(R.string.post_lookup_error_format, url), Toast.LENGTH_SHORT).show() + } + } + @VisibleForTesting fun onBeginSearch(url: String) { searchUrl = url @@ -187,3 +195,8 @@ fun looksLikeMastodonUrl(urlString: String): Boolean { path.matches("^/notice/\\d+$".toRegex()) || path.matches("^/objects/[-a-f0-9]+$".toRegex()) } + +enum class PostLookupFallbackBehavior { + OPEN_IN_BROWSER, + DISPLAY_ERROR, +} diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index c10bbff84..1f85b0348 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -311,7 +311,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut if (intent != null) { String statusUrl = intent.getStringExtra(STATUS_URL); if (statusUrl != null) { - viewUrl(statusUrl); + viewUrl(statusUrl, PostLookupFallbackBehavior.DISPLAY_ERROR); } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java index 81fa8a8d5..0a4246012 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java @@ -45,6 +45,7 @@ import com.keylesspalace.tusky.BottomSheetActivity; import com.keylesspalace.tusky.ComposeActivity; import com.keylesspalace.tusky.MainActivity; import com.keylesspalace.tusky.R; +import com.keylesspalace.tusky.PostLookupFallbackBehavior; import com.keylesspalace.tusky.ViewMediaActivity; import com.keylesspalace.tusky.ViewTagActivity; import com.keylesspalace.tusky.components.report.ReportActivity; @@ -134,7 +135,7 @@ public abstract class SFragment extends BaseFragment implements Injectable { } public void onViewUrl(String url) { - bottomSheetActivity.viewUrl(url); + bottomSheetActivity.viewUrl(url, PostLookupFallbackBehavior.OPEN_IN_BROWSER); } protected void reply(Status status) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a1f78a520..cff97ad37 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -536,5 +536,6 @@ Multiple choices Choice %d Edit + Error looking up post %s diff --git a/app/src/test/java/com/keylesspalace/tusky/BottomSheetActivityTest.kt b/app/src/test/java/com/keylesspalace/tusky/BottomSheetActivityTest.kt index 2db1cd0d7..da80f784a 100644 --- a/app/src/test/java/com/keylesspalace/tusky/BottomSheetActivityTest.kt +++ b/app/src/test/java/com/keylesspalace/tusky/BottomSheetActivityTest.kt @@ -214,6 +214,16 @@ class BottomSheetActivityTest { Assert.assertEquals(nonMastodonQuery, activity.link) } + @Test + fun search_withNoResults_appliesRequestedFallbackBehavior() { + for (fallbackBehavior in listOf(PostLookupFallbackBehavior.OPEN_IN_BROWSER, PostLookupFallbackBehavior.DISPLAY_ERROR)) { + activity.viewUrl(nonMastodonQuery, fallbackBehavior) + testScheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS) + Assert.assertEquals(nonMastodonQuery, activity.link) + Assert.assertEquals(fallbackBehavior, activity.fallbackBehavior) + } + } + @Test fun search_withCancellation_doesNotLoadUrl_forAccount() { activity.viewUrl(accountQuery) @@ -263,6 +273,7 @@ class BottomSheetActivityTest { var statusId: String? = null var accountId: String? = null var link: String? = null + var fallbackBehavior: PostLookupFallbackBehavior? = null init { mastodonApi = api @@ -282,5 +293,9 @@ class BottomSheetActivityTest { this.statusId = statusId } + override fun performUrlFallbackAction(url: String, fallbackBehavior: PostLookupFallbackBehavior) { + this.link = url + this.fallbackBehavior = fallbackBehavior + } } } \ No newline at end of file