Skip to content

Commit

Permalink
Prevent some crashes to happen on desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Oct 30, 2023
1 parent 06a57c4 commit 0398a57
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
1 change: 1 addition & 0 deletions desktopApp/src/jvmMain/kotlin/com/prof18/feedflow/di/DI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ object DI {
factory {
NewVersionChecker(
dispatcherProvider = get(),
logger = getWith("NewVersionChecker"),
)
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prof18.feedflow.versionchecker

import co.touchlab.kermit.Logger
import com.prof18.feedflow.di.DI
import com.prof18.feedflow.utils.DispatcherProvider
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -12,6 +13,7 @@ import java.util.Properties

internal class NewVersionChecker(
private val dispatcherProvider: DispatcherProvider,
private val logger: Logger,
) {

private val newVersionMutableState: MutableStateFlow<NewVersionState> =
Expand All @@ -20,43 +22,48 @@ internal class NewVersionChecker(
val newVersionState = newVersionMutableState.asStateFlow()

suspend fun notifyIfNewVersionIsAvailable() = withContext(dispatcherProvider.io) {
val doc = Jsoup.connect("https://github.com/prof18/feed-flow/releases/latest").get()
try {
val doc = Jsoup.connect("https://github.com/prof18/feed-flow/releases/latest").get()

val content = doc.text()
val regex = "Release (\\d+\\.\\d+\\.\\d+-\\w+)".toRegex()
val content = doc.text()
val regex = "Release (\\d+\\.\\d+\\.\\d+-\\w+)".toRegex()

val newVersionString = regex.find(content)?.value
?.replace("-desktop", "")
?.replace("Release ", "")
val newVersionString = regex.find(content)?.value
?.replace("-desktop", "")
?.replace("Release ", "")

val newVersion = newVersionString
?.replace(".", "")
?.toIntOrNull()
?: return@withContext
val newVersion = newVersionString
?.replace(".", "")
?.toIntOrNull()
?: return@withContext

val properties = Properties()
val propsFile = DI::class.java.classLoader?.getResourceAsStream("props.properties")
?: InputStream.nullInputStream()
properties.load(propsFile)
val properties = Properties()
val propsFile = DI::class.java.classLoader?.getResourceAsStream("props.properties")
?: InputStream.nullInputStream()
properties.load(propsFile)

val version = properties["version"]
?.toString()
?.replace(".", "")
?.toIntOrNull()
?: return@withContext
val version = properties["version"]
?.toString()
?.replace(".", "")
?.toIntOrNull()
?: return@withContext

val versionLink = """
https://github.com/prof18/feed-flow/releases/download/$newVersionString-desktop/FeedFlow-$newVersionString.dmg
""".trimIndent()
val versionLink = """
https://github.com/prof18/feed-flow/releases/download/$newVersionString-desktop/FeedFlow-$newVersionString.dmg
""".trimIndent()

newVersionMutableState.update {
if (newVersion > version) {
NewVersionState.NewVersion(
downloadLink = versionLink,
)
} else {
NewVersionState.NoNewVersion
newVersionMutableState.update {
if (newVersion > version) {
NewVersionState.NewVersion(
downloadLink = versionLink,
)
} else {
NewVersionState.NoNewVersion
}
}
} catch (e: Exception) {
// Do nothing
logger.e(e) { "Error while trying to notify if a new version is available" }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ internal actual val platformModule: Module = module {
factory<HtmlRetriever> {
JvmHtmlRetriever(
dispatcherProvider = get(),
logger = getWith("JvmHtmlRetriever"),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.prof18.feedflow.domain

import co.touchlab.kermit.Logger
import com.prof18.feedflow.utils.DispatcherProvider
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup

internal class JvmHtmlRetriever(
private val dispatcherProvider: DispatcherProvider,
private val logger: Logger,
) : HtmlRetriever {
override suspend fun retrieveHtml(url: String): String? = withContext(dispatcherProvider.io) {
return@withContext Jsoup.connect(url).get().html()
try {
return@withContext Jsoup.connect(url).get().html()
} catch (e: Exception) {
logger.e(e) { "Unable to retrieve HTML, skipping" }
return@withContext null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ internal actual val platformModule: Module = module {
factory<HtmlRetriever> {
JvmHtmlRetriever(
dispatcherProvider = get(),
logger = getWith("JvmHtmlRetriever"),
)
}
}

0 comments on commit 0398a57

Please sign in to comment.