From 4c967a37ee1f8b4737b430eec0867d6424557c78 Mon Sep 17 00:00:00 2001 From: Gabriel Luong Date: Wed, 18 May 2022 12:27:55 -0400 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/25264 - Refactor toShortUrl extension function from TabSessionState to String --- .../main/java/org/mozilla/fenix/ext/String.kt | 23 ++++++++++++++++++ .../fenix/home/collections/CollectionItem.kt | 22 +---------------- .../fenix/tabstray/ext/TabSessionState.kt | 24 ------------------- 3 files changed, 24 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/ext/String.kt b/app/src/main/java/org/mozilla/fenix/ext/String.kt index 1608218fcc..9eb5567ee3 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/String.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/String.kt @@ -9,9 +9,13 @@ import android.os.Build import android.text.Editable import android.util.Patterns import android.webkit.URLUtil +import androidx.compose.runtime.Composable import androidx.core.net.toUri +import mozilla.components.browser.toolbar.MAX_URI_LENGTH import mozilla.components.lib.publicsuffixlist.PublicSuffixList import mozilla.components.support.ktx.android.net.hostWithoutCommonPrefixes +import org.mozilla.fenix.components.components +import org.mozilla.fenix.compose.inComposePreview import java.net.IDN import java.util.Locale @@ -71,6 +75,25 @@ fun String.toShortUrl(publicSuffixList: PublicSuffixList): String { .toUnicode() } +/** + * Shortens URLs to be more user friendly, by applying [String.toShortUrl] + * and making sure it's equal or below the [MAX_URI_LENGTH]. + */ +@Composable +fun String.toShortUrl(): String { + // Truncate to MAX_URI_LENGTH to prevent the UI from locking up for + // extremely large URLs such as data URIs or bookmarklets. The same + // is done in the toolbar and awesomebar: + // https://github.com/mozilla-mobile/fenix/issues/1824 + // https://github.com/mozilla-mobile/android-components/issues/6985 + return if (inComposePreview) { + this.take(MAX_URI_LENGTH) + } else { + this.toShortUrl(components.publicSuffixList) + .take(MAX_URI_LENGTH) + } +} + // impl via FFTV https://searchfox.org/mozilla-mobile/source/firefox-echo-show/app/src/main/java/org/mozilla/focus/utils/FormattedDomain.java#129 @Suppress("DEPRECATION") internal fun String.isIpv4(): Boolean = Patterns.IP_ADDRESS.matcher(this).matches() diff --git a/app/src/main/java/org/mozilla/fenix/home/collections/CollectionItem.kt b/app/src/main/java/org/mozilla/fenix/home/collections/CollectionItem.kt index 2c5ad9e2e1..ccb39e3375 100644 --- a/app/src/main/java/org/mozilla/fenix/home/collections/CollectionItem.kt +++ b/app/src/main/java/org/mozilla/fenix/home/collections/CollectionItem.kt @@ -33,7 +33,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.drawWithContent import androidx.compose.ui.graphics.drawscope.clipRect -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview @@ -44,9 +43,7 @@ import mozilla.components.concept.engine.Engine import mozilla.components.feature.tab.collections.Tab import org.mozilla.fenix.R.drawable import org.mozilla.fenix.R.string -import org.mozilla.fenix.compose.inComposePreview import org.mozilla.fenix.compose.list.FaviconListItem -import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.toShortUrl import org.mozilla.fenix.theme.FirefoxTheme import org.mozilla.fenix.theme.Theme @@ -116,7 +113,7 @@ fun CollectionItem( ) { FaviconListItem( label = tab.title, - description = shortenUrl(tab.url), + description = tab.url.toShortUrl(), onClick = onClick, url = tab.url, iconPainter = painterResource(drawable.ic_close), @@ -194,23 +191,6 @@ private fun Modifier.clipTop() = this.then( } ) -/** - * Get a friendlier short url for [url]. - * - * @param url Full url to be shortened. - * - * @see toShortUrl - */ -@Composable -private fun shortenUrl(url: String): String { - return if (inComposePreview) { - url - } else { - url.toShortUrl(LocalContext.current.components.publicSuffixList) - } -} - -@OptIn(ExperimentalMaterialApi::class) @Composable @Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) @Preview(uiMode = Configuration.UI_MODE_NIGHT_NO) diff --git a/app/src/main/java/org/mozilla/fenix/tabstray/ext/TabSessionState.kt b/app/src/main/java/org/mozilla/fenix/tabstray/ext/TabSessionState.kt index ea27897a3d..089d10fd54 100644 --- a/app/src/main/java/org/mozilla/fenix/tabstray/ext/TabSessionState.kt +++ b/app/src/main/java/org/mozilla/fenix/tabstray/ext/TabSessionState.kt @@ -4,31 +4,7 @@ package org.mozilla.fenix.tabstray.ext -import androidx.compose.runtime.Composable import mozilla.components.browser.state.state.TabSessionState -import mozilla.components.browser.toolbar.MAX_URI_LENGTH -import org.mozilla.fenix.components.components -import org.mozilla.fenix.compose.inComposePreview -import org.mozilla.fenix.ext.toShortUrl - -/** - * Shortens URLs to be more user friendly, by applying [String.toShortUrl] - * and making sure it's equal or below the [MAX_URI_LENGTH]. - */ -@Composable -fun TabSessionState.toShortUrl(): String { - // Truncate to MAX_URI_LENGTH to prevent the UI from locking up for - // extremely large URLs such as data URIs or bookmarklets. The same - // is done in the toolbar and awesomebar: - // https://github.com/mozilla-mobile/fenix/issues/1824 - // https://github.com/mozilla-mobile/android-components/issues/6985 - return if (inComposePreview) { - this.content.url.take(MAX_URI_LENGTH) - } else { - this.content.url.toShortUrl(components.publicSuffixList) - .take(MAX_URI_LENGTH) - } -} fun TabSessionState.isActive(maxActiveTime: Long): Boolean { val lastActiveTime = maxOf(lastAccess, createdAt)