For #25264 - Refactor toShortUrl extension function from TabSessionState to String

pull/543/head
Gabriel Luong 2 years ago committed by mergify[bot]
parent a93e434f0d
commit 9e8ee28e40

@ -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()

@ -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)

@ -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)

Loading…
Cancel
Save