From ec700ca61fc4142822ea3ba95647bda9d763382d Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 23 Sep 2024 21:51:15 -0400 Subject: [PATCH] Refactoring --- .../notifications/PushDistributorHandler.kt | 19 ++++++++++--------- .../service/previews/UrlPreviewUtils.kt | 6 ++++-- .../amethyst/ui/navigation/AppNavigation.kt | 4 ++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/amethyst/src/fdroid/java/com/vitorpamplona/amethyst/service/notifications/PushDistributorHandler.kt b/amethyst/src/fdroid/java/com/vitorpamplona/amethyst/service/notifications/PushDistributorHandler.kt index 3f334b6ab7..b23eadd608 100644 --- a/amethyst/src/fdroid/java/com/vitorpamplona/amethyst/service/notifications/PushDistributorHandler.kt +++ b/amethyst/src/fdroid/java/com/vitorpamplona/amethyst/service/notifications/PushDistributorHandler.kt @@ -38,7 +38,6 @@ interface PushDistributorActions { } object PushDistributorHandler : PushDistributorActions { - private val appContext = Amethyst.instance.applicationContext private val unifiedPush: UnifiedPush = UnifiedPush private var endpointInternal = "" @@ -54,11 +53,13 @@ object PushDistributorHandler : PushDistributorActions { endpointInternal = "" } - override fun getSavedDistributor(): String = unifiedPush.getSavedDistributor(appContext) ?: "" + fun appContext(): Context = Amethyst.instance.applicationContext + + override fun getSavedDistributor(): String = unifiedPush.getSavedDistributor(appContext()) ?: "" fun savedDistributorExists(): Boolean = getSavedDistributor().isNotEmpty() - override fun getInstalledDistributors(): List = unifiedPush.getDistributors(appContext) + override fun getInstalledDistributors(): List = unifiedPush.getDistributors(appContext()) fun formattedDistributorNames(): List { val distributorsArray = getInstalledDistributors().toTypedArray() @@ -68,16 +69,16 @@ object PushDistributorHandler : PushDistributorActions { try { val ai = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - appContext.packageManager.getApplicationInfo( + appContext().packageManager.getApplicationInfo( it, PackageManager.ApplicationInfoFlags.of( PackageManager.GET_META_DATA.toLong(), ), ) } else { - appContext.packageManager.getApplicationInfo(it, 0) + appContext().packageManager.getApplicationInfo(it, 0) } - appContext.packageManager.getApplicationLabel(ai) + appContext().packageManager.getApplicationLabel(ai) } catch (e: PackageManager.NameNotFoundException) { it } @@ -87,12 +88,12 @@ object PushDistributorHandler : PushDistributorActions { } override fun saveDistributor(distributor: String) { - unifiedPush.saveDistributor(appContext, distributor) - unifiedPush.registerApp(appContext) + unifiedPush.saveDistributor(appContext(), distributor) + unifiedPush.registerApp(appContext()) } override fun removeSavedDistributor() { - unifiedPush.safeRemoveDistributor(appContext) + unifiedPush.safeRemoveDistributor(appContext()) } fun forceRemoveDistributor(context: Context) { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/previews/UrlPreviewUtils.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/previews/UrlPreviewUtils.kt index fc99a7cd38..3161f82bd9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/previews/UrlPreviewUtils.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/previews/UrlPreviewUtils.kt @@ -111,14 +111,16 @@ suspend fun parseHtml( // sniff charset from Content-Type header or BOM val sniffedCharset = type.charset() ?: source.readBomAsCharset() if (sniffedCharset != null) { - val metaTags = MetaTagsParser.parse(source.readByteArray().toString(sniffedCharset)) + val content = source.readByteArray().toString(sniffedCharset) + val metaTags = MetaTagsParser.parse(content) return@withContext extractUrlInfo(url, metaTags, type) } // if sniffing was failed, detect charset from content val bodyBytes = source.readByteArray() val charset = detectCharset(bodyBytes) - val metaTags = MetaTagsParser.parse(bodyBytes.toString(charset)) + val content = bodyBytes.toString(charset) + val metaTags = MetaTagsParser.parse(content) return@withContext extractUrlInfo(url, metaTags, type) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt index 9c7ae6135c..cb83f36989 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppNavigation.kt @@ -347,8 +347,8 @@ private fun NavigateIfIntentRequested( DisposableEffect(nav, activity) { val consumer = Consumer { intent -> - val uri = intent.data.toString() - if (uri.isNotBlank()) { + val uri = intent.data?.toString() + if (!uri.isNullOrBlank()) { // navigation functions val newPage = uriToRoute(uri)