Skip to content

Commit

Permalink
added dns hint if website not reachable
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Jan 3, 2024
1 parent 4eeaf5b commit c0d7442
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import dev.icerock.moko.resources.compose.painterResource
import dev.icerock.moko.resources.compose.stringResource
import kotlinx.coroutines.flow.StateFlow

actual fun LazyGridScope.DeviceContent(release: StateFlow<Release?>) {
actual fun LazyGridScope.DeviceContent(release: StateFlow<Release?>, onDeviceReachable: StateFlow<Boolean>) {
header {
val newRelease by release.collectAsStateWithLifecycle()
val reachable by onDeviceReachable.collectAsStateWithLifecycle()

if (newRelease != null) {
Row(
Expand Down Expand Up @@ -58,6 +59,8 @@ actual fun LazyGridScope.DeviceContent(release: StateFlow<Release?>) {
Text(text = stringResource(SharedRes.strings.github))
}
}
} else if (!reachable) {
Text(text = stringResource(SharedRes.strings.enable_custom_dns))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import dev.icerock.moko.resources.compose.painterResource
import dev.icerock.moko.resources.compose.stringResource

@Composable
fun ErrorState(text: String, onRetry: () -> Unit) {
fun ErrorState(text: String, customText: (@Composable () -> Unit)? = null, onRetry: () -> Unit) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically),
Expand All @@ -43,6 +43,7 @@ fun ErrorState(text: String, onRetry: () -> Unit) {
softWrap = true,
textAlign = TextAlign.Center
)
customText?.invoke()
Button(
onClick = {
onRetry()
Expand All @@ -54,4 +55,8 @@ fun ErrorState(text: String, onRetry: () -> Unit) {
}

@Composable
fun ErrorState(text: StringResource, onRetry: () -> Unit) = ErrorState(text = stringResource(text), onRetry)
fun ErrorState(
text: StringResource,
customText: (@Composable () -> Unit)? = null,
onRetry: () -> Unit
) = ErrorState(text = stringResource(text), customText, onRetry)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ interface HomeComponent : SeriesHolderComponent {
val homeState: StateFlow<HomeState>
val release: StateFlow<Release?>

val onDeviceReachable: StateFlow<Boolean>

fun retryLoadingHome(): Any?
fun itemClicked(config: HomeConfig)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.arkivanov.decompose.extensions.compose.jetbrains.subscribeAsState
import dev.datlag.burningseries.model.Home
Expand Down Expand Up @@ -45,7 +46,20 @@ fun HomeScreen(component: HomeComponent) {
LoadingState(SharedRes.strings.loading_home)
}
is HomeState.Error -> {
ErrorState(SharedRes.strings.error_loading_home) {
val reachable by component.onDeviceReachable.collectAsStateWithLifecycle()

ErrorState(
text = SharedRes.strings.error_loading_home,
customText = {
if (!reachable) {
Text(
modifier = Modifier.fillMaxWidth(0.85F),
text = stringResource(SharedRes.strings.enable_custom_dns),
textAlign = TextAlign.Center
)
}
}
) {
component.retryLoadingHome()
}
}
Expand Down Expand Up @@ -116,7 +130,7 @@ private fun MainView(home: Home, component: HomeComponent, modifier: Modifier =
horizontalArrangement = Arrangement.spacedBy(8.dp),
state = state
) {
DeviceContent(component.release)
DeviceContent(component.release, component.onDeviceReachable)
header {
Row(
modifier = Modifier.padding(top = 16.dp).fillMaxWidth(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dev.datlag.burningseries.model.Release
import dev.datlag.burningseries.model.Series
import dev.datlag.burningseries.model.Shortcut
import dev.datlag.burningseries.model.common.getDigitsOrNull
import dev.datlag.burningseries.model.common.safeCast
import dev.datlag.burningseries.model.state.HomeAction
import dev.datlag.burningseries.model.state.HomeState
import dev.datlag.burningseries.model.state.ReleaseState
Expand Down Expand Up @@ -40,6 +41,9 @@ class HomeScreenComponent(

private val homeStateMachine: HomeStateMachine by di.instance()
override val homeState: StateFlow<HomeState> = homeStateMachine.state.flowOn(ioDispatcher()).stateIn(ioScope(), SharingStarted.WhileSubscribed(), HomeState.Loading)
override val onDeviceReachable = homeState.map {
it.safeCast<HomeState.Success>()?.onDeviceReachable ?: (it is HomeState.Loading)
}.flowOn(ioDispatcher()).stateIn(ioScope(), SharingStarted.WhileSubscribed(), true)

private val appVersion: String? by di.instanceOrNull("APP_VERSION")
private val releaseStateMachine: ReleaseStateMachine by di.instance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import androidx.compose.foundation.lazy.grid.LazyGridScope
import dev.datlag.burningseries.model.Release
import kotlinx.coroutines.flow.StateFlow

expect fun LazyGridScope.DeviceContent(release: StateFlow<Release?>)
expect fun LazyGridScope.DeviceContent(release: StateFlow<Release?>, onDeviceReachable: StateFlow<Boolean>)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import org.kodein.di.DI
import org.kodein.di.instance
import kotlin.math.max

class VideoScreenComponent(
componentContext: ComponentContext,
Expand Down Expand Up @@ -127,7 +128,7 @@ class VideoScreenComponent(
val currentEpisode = episode.value

database.burningSeriesQueries.updateEpisodeLength(
length = millis,
length = max(millis, 0L),
href = currentEpisode.href,
number = currentEpisode.number,
title = currentEpisode.title,
Expand Down
1 change: 1 addition & 0 deletions app/shared/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@
<string name="muted">Muted</string>
<string name="unmuted">Unmuted</string>
<string name="loading_intent_series">Loading series</string>
<string name="enable_custom_dns">You should set a custom DNS to make sure you can access all features.</string>
</resources>
1 change: 1 addition & 0 deletions app/shared/src/commonMain/resources/MR/de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@
<string name="muted">Stumm</string>
<string name="unmuted">Nicht stumm</string>
<string name="loading_intent_series">Lade Serie</string>
<string name="enable_custom_dns">Du solltest einen DNS-Server einrichten, um alle Funktionen zuverlässig nutzen zu können.</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import dev.icerock.moko.resources.compose.painterResource
import dev.icerock.moko.resources.compose.stringResource
import kotlinx.coroutines.flow.StateFlow

actual fun LazyGridScope.DeviceContent(release: StateFlow<Release?>) {
actual fun LazyGridScope.DeviceContent(release: StateFlow<Release?>, onDeviceReachable: StateFlow<Boolean>) {
header {
val newRelease by release.collectAsStateWithLifecycle()

Expand Down Expand Up @@ -123,6 +123,16 @@ actual fun LazyGridScope.DeviceContent(release: StateFlow<Release?>) {
)
}
}
is CEFState.INITIALIZED -> {
val reachable by onDeviceReachable.collectAsStateWithLifecycle()

if (!reachable) {
Text(
modifier = Modifier.padding(top = 16.dp),
text = stringResource(SharedRes.strings.enable_custom_dns)
)
}
}
else -> { }
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
app = "5.1.0"
app = "5.2.0"
aboutlibraries = "10.9.2"
accompanist = "0.32.0"
activity = "1.8.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dev.datlag.burningseries.model.Home

sealed interface HomeState {
data object Loading : HomeState
data class Success(val home: Home) : HomeState
data class Success(val home: Home, val onDeviceReachable: Boolean) : HomeState
data object Error : HomeState
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ class HomeStateMachine(
}
onEnter { state ->
val result = suspendCatchResult {
var onDeviceReachable: Boolean = true
val loadedHome = BurningSeries.getHome(client) ?: run {
onDeviceReachable = false
wrapApiKey?.let {
json.decodeFromJsonElement<Home>(wrapApi.getBurningSeriesHome(it).data)
}
}!!

if (loadedHome.episodes.isNotEmpty() || loadedHome.series.isNotEmpty()) {
HomeState.Success(loadedHome)
HomeState.Success(loadedHome, onDeviceReachable)
} else {
HomeState.Error
}
Expand Down

0 comments on commit c0d7442

Please sign in to comment.