Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new release banner on the desktop app. #21

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions desktopApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ plugins {
group = "com.prof18"
version = "1.0-SNAPSHOT"


kotlin {
jvm {
jvmToolchain(17)
Expand All @@ -34,9 +33,8 @@ kotlin {
implementation(libs.compose.image.loader)
implementation(libs.moko.resourcesCompose)
implementation(libs.bundles.about.libraries)

implementation("org.slf4j:slf4j-nop:2.0.6")

implementation(libs.jsoup)
implementation(libs.slf4j.nop)
}
}
val jvmTest by getting
Expand All @@ -52,9 +50,9 @@ compose {
// obfuscate.set(true)
// }
//
buildTypes.release.proguard {
configurationFiles.from(project.file("compose-desktop.pro"))
}
buildTypes.release.proguard {
configurationFiles.from(project.file("compose-desktop.pro"))
}

nativeDistributions {

Expand Down
47 changes: 39 additions & 8 deletions desktopApp/src/jvmMain/kotlin/com/prof18/feedflow/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.prof18.feedflow

import androidx.compose.foundation.LocalScrollbarStyle
import androidx.compose.foundation.ScrollbarStyle
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
Expand All @@ -13,6 +14,7 @@ import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -46,11 +48,14 @@ import com.prof18.feedflow.navigation.ChildStack
import com.prof18.feedflow.navigation.ProvideComponentContext
import com.prof18.feedflow.navigation.Screen
import com.prof18.feedflow.presentation.HomeViewModel
import com.prof18.feedflow.ui.components.NewVersionBanner
import com.prof18.feedflow.ui.style.FeedFlowTheme
import com.prof18.feedflow.ui.style.rememberDesktopDarkTheme
import com.prof18.feedflow.utils.AppEnvironment
import com.prof18.feedflow.utils.UserFeedbackReporter
import com.prof18.feedflow.utils.initSentry
import com.prof18.feedflow.versionchecker.NewVersionChecker
import com.prof18.feedflow.versionchecker.NewVersionState
import dev.icerock.moko.resources.compose.stringResource
import kotlinx.coroutines.launch
import java.io.InputStream
Expand Down Expand Up @@ -102,12 +107,19 @@ fun main() = application {
LifecycleController(lifecycle, windowState)

val homeViewModel = desktopViewModel { DI.koin.get<HomeViewModel>() }
val newVersionChecker = DI.koin.get<NewVersionChecker>()

val listState = rememberLazyListState()
val scope = rememberCoroutineScope()

val snackbarHostState = remember { SnackbarHostState() }

val newVersionState by newVersionChecker.newVersionState.collectAsState()

scope.launch {
newVersionChecker.notifyIfNewVersionIsAvailable()
}

Window(
onCloseRequest = ::exitApplication,
state = windowState,
Expand Down Expand Up @@ -167,6 +179,10 @@ fun main() = application {
homeViewModel = homeViewModel,
listState = listState,
window = window,
newVersionState = newVersionState,
onCloseDownloadBannerClick = {
newVersionChecker.clearNewVersionState()
},
)
}
}
Expand All @@ -179,6 +195,8 @@ private fun MainContent(
homeViewModel: HomeViewModel,
listState: LazyListState,
window: ComposeWindow,
newVersionState: NewVersionState,
onCloseDownloadBannerClick: () -> Unit,
) {
Surface(modifier = Modifier.fillMaxSize()) {
FeedFlowTheme {
Expand All @@ -194,14 +212,27 @@ private fun MainContent(
animation = stackAnimation(fade() + scale()),
) { screen ->
when (screen) {
is Screen.Home -> HomeScreen(
paddingValues = paddingValues,
homeViewModel = homeViewModel,
listState = listState,
onAddFeedClick = {
navigation.push(Screen.FeedList)
},
)
is Screen.Home -> {
Column {
if (newVersionState is NewVersionState.NewVersion) {
NewVersionBanner(
onDownloadLinkClick = {
openInBrowser(newVersionState.downloadLink)
},
onCloseClick = onCloseDownloadBannerClick,
)
}

HomeScreen(
paddingValues = paddingValues,
homeViewModel = homeViewModel,
listState = listState,
onAddFeedClick = {
navigation.push(Screen.FeedList)
},
)
}
}

is Screen.FeedList ->
FeedSourceListScreen(
Expand Down
15 changes: 14 additions & 1 deletion desktopApp/src/jvmMain/kotlin/com/prof18/feedflow/di/DI.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package com.prof18.feedflow.di

import com.prof18.feedflow.utils.AppEnvironment
import com.prof18.feedflow.versionchecker.NewVersionChecker
import org.koin.core.Koin
import org.koin.dsl.module

object DI {
lateinit var koin: Koin

fun initKoin(appEnvironment: AppEnvironment) {
koin = initKoinDesktop(appEnvironment).koin
koin = initKoinDesktop(
appEnvironment = appEnvironment,
modules = listOf(
module {
factory {
NewVersionChecker(
dispatcherProvider = get(),
)
}
},
),
).koin
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.prof18.feedflow.ui.components

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import com.prof18.feedflow.MR
import com.prof18.feedflow.ui.style.FeedFlowTheme
import com.prof18.feedflow.ui.style.Spacing
import dev.icerock.moko.resources.compose.stringResource

@Composable
internal fun NewVersionBanner(
onDownloadLinkClick: () -> Unit,
onCloseClick: () -> Unit,
) {
Row(
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colorScheme.primaryContainer),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
modifier = Modifier
.padding(Spacing.regular),
text = stringResource(MR.strings.new_release_available_title),
color = MaterialTheme.colorScheme.onPrimaryContainer,
style = MaterialTheme.typography.titleMedium,
)

AnnotatedClickableText(
modifier = Modifier
.weight(1f),
onTextClick = onDownloadLinkClick,
)

IconButton(
onClick = {
onCloseClick()
},
) {
Icon(
imageVector = Icons.Default.Close,
contentDescription = null,
tint = MaterialTheme.colorScheme.onPrimaryContainer,
)
}
}
}

@Composable
private fun AnnotatedClickableText(
modifier: Modifier = Modifier,
onTextClick: () -> Unit,
) {
val annotatedText = buildAnnotatedString {
pushStringAnnotation(
tag = "URL",
annotation = "URL",
)
withStyle(
style = SpanStyle(
color = MaterialTheme.colorScheme.onPrimaryContainer,
fontWeight = FontWeight.Bold,
textDecoration = TextDecoration.Underline,
),
) {
append(stringResource(MR.strings.new_release_available_link))
}

pop()
}

ClickableText(
modifier = modifier,
text = annotatedText,
style = MaterialTheme.typography.bodyMedium,
onClick = { offset ->
annotatedText.getStringAnnotations(
tag = "URL",
start = offset,
end = offset,
).firstOrNull()?.let {
onTextClick()
}
},
)
}

@Preview
@Composable
private fun NewVersionBannerPreview() {
FeedFlowTheme {
NewVersionBanner(
onDownloadLinkClick = {},
onCloseClick = {},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.prof18.feedflow.versionchecker

import com.prof18.feedflow.di.DI
import com.prof18.feedflow.utils.DispatcherProvider
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import java.io.InputStream
import java.util.Properties

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

private val newVersionMutableState: MutableStateFlow<NewVersionState> =
MutableStateFlow(NewVersionState.NoNewVersion)

val newVersionState = newVersionMutableState.asStateFlow()

suspend fun notifyIfNewVersionIsAvailable() = withContext(dispatcherProvider.io) {
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 newVersionString = regex.find(content)?.value
?.replace("-desktop", "")
?.replace("Release ", "")

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 version = properties["version"]
?.toString()
?.replace(".", "")
?.toIntOrNull()
?: return@withContext

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
}
}
}

fun clearNewVersionState() {
newVersionMutableState.update {
NewVersionState.NoNewVersion
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.prof18.feedflow.versionchecker

internal sealed interface NewVersionState {
data class NewVersion(
val downloadLink: String,
) : NewVersionState

data object NoNewVersion : NewVersionState
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ koin-android = "3.4.2"
koin-compose = "3.4.5"
kotlin = "1.9.0"
nav-compose = "2.7.1"
slf4jNop = "2.0.6"
turbine = "1.0.0"
versions-ben-manes = "0.44.0"
viewModel-ktx = "2.6.1"
Expand Down Expand Up @@ -77,6 +78,7 @@ kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutine
kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
slf4j-nop = { module = "org.slf4j:slf4j-nop", version.ref = "slf4jNop" }
touchlab-kermit = { module = "co.touchlab:kermit", version.ref = "kermit" }
touchlab-kermit-simple = { module = "co.touchlab:kermit-simple", version.ref = "kermit" }
touchlab-kermit-crashlytics = { module = "co.touchlab:kermit-crashlytics", version.ref = "kermit" }
Expand Down
2 changes: 2 additions & 0 deletions i18n/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@
<string name="done_button">Okay</string>
<string name="wrong_link_report_title">The following feeds were not added because they are not valid</string>
<string name="feed_add_in_progress_message">Please wait, every feed availability is being checked</string>
<string name="new_release_available_title">A new version of FeedFlow is available:</string>
<string name="new_release_available_link">Download here</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.prof18.feedflow.utils

import kotlinx.coroutines.CoroutineDispatcher

internal interface DispatcherProvider {
interface DispatcherProvider {
val main: CoroutineDispatcher
val default: CoroutineDispatcher
val io: CoroutineDispatcher
Expand Down
Loading