2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-17 15:26:23 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/25264 - Refactor toShortUrl extension function from TabSessionState to String

This commit is contained in:
Gabriel Luong 2022-05-18 12:27:55 -04:00 committed by mergify[bot]
parent 7d02d4cbf2
commit 4c967a37ee
3 changed files with 24 additions and 45 deletions

View File

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

View File

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

View File

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