Skip to content

Commit

Permalink
fix crash and added loading indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Nov 16, 2023
1 parent cb86bf5 commit 10871ee
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import org.kodein.di.bindSingleton

class App : MultiDexApplication(), DIAware {

override val di: DI = DI {
bindSingleton {
applicationContext
}
override val di: DI by lazy {
DI {
bindSingleton {
applicationContext
}

import(NetworkModule.di)
import(NetworkModule.di)
}
}

override fun onCreate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ private fun runWindow() {
AppIO.applyTitle(appTitle)
Napier.base(DebugAntilog())

NativeLoader.loadLibrary("sekret", systemProperty("compose.application.resources.dir")?.let { File(it) })

val windowState = WindowState()
val lifecycle = LifecycleRegistry()
val lifecycleOwner = object : LifecycleOwner {
Expand All @@ -67,8 +69,6 @@ private fun runWindow() {
resourcesFetcher()
}

NativeLoader.loadLibrary("sekret", systemProperty("compose.application.resources.dir")?.let { File(it) })

disposableSingleWindowApplication(
state = windowState,
title = appTitle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.datlag.burningseries.module
import de.jensklingenberg.ktorfit.Ktorfit
import de.jensklingenberg.ktorfit.ktorfitBuilder
import dev.datlag.burningseries.Sekret
import dev.datlag.burningseries.common.launchIO
import dev.datlag.burningseries.common.*
import dev.datlag.burningseries.getPackageName
import dev.datlag.burningseries.network.JsonBase
import dev.datlag.burningseries.network.state.EpisodeStateMachine
Expand All @@ -16,6 +16,7 @@ import io.realm.kotlin.mongodb.Credentials
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import org.kodein.di.DI
import org.kodein.di.bindEagerSingleton
import org.kodein.di.bindSingleton
import org.kodein.di.instance

Expand Down Expand Up @@ -49,10 +50,10 @@ object NetworkModule {
bindSingleton {
SearchStateMachine(instance())
}
bindSingleton {
bindEagerSingleton {
App.create(Sekret().mongoApplication(getPackageName())!!)
}
bindSingleton {
bindEagerSingleton {
EpisodeStateMachine(instance(), instance())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.datlag.burningseries.ui.screen.initial.series
import com.arkivanov.decompose.router.slot.ChildSlot
import com.arkivanov.decompose.value.Value
import dev.datlag.burningseries.model.Series
import dev.datlag.burningseries.model.state.EpisodeState
import dev.datlag.burningseries.model.state.SeriesState
import dev.datlag.burningseries.ui.navigation.Component
import dev.datlag.burningseries.ui.navigation.DialogComponent
Expand All @@ -12,6 +13,7 @@ import kotlinx.coroutines.flow.StateFlow
interface SeriesComponent : Component {

val seriesState: StateFlow<SeriesState>
val episodeState: StateFlow<EpisodeState>
val dialog: Value<ChildSlot<DialogConfig, DialogComponent>>

val title: StateFlow<String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import dev.datlag.burningseries.model.Series
import dev.datlag.burningseries.model.state.SeriesState
import dev.datlag.burningseries.other.StateSaver
import dev.datlag.burningseries.SharedRes
import dev.datlag.burningseries.model.state.EpisodeState
import dev.datlag.burningseries.ui.custom.CountryImage
import dev.datlag.burningseries.ui.custom.DefaultCollapsingToolbar
import dev.datlag.burningseries.ui.custom.VerticalScrollbar
Expand Down Expand Up @@ -91,7 +92,6 @@ fun SeriesScreen(component: SeriesComponent) {
private fun CompactScreen(component: SeriesComponent) {
val seriesState by component.seriesState.collectAsStateWithLifecycle()
val title by component.title.collectAsStateWithLifecycle()
val href by component.href.collectAsStateWithLifecycle()
val coverHref by component.coverHref.collectAsStateWithLifecycle()
val commonHref by component.commonHref.collectAsStateWithLifecycle()

Expand Down Expand Up @@ -224,6 +224,7 @@ private fun CompactScreen(component: SeriesComponent) {
}
}
is SeriesState.Success -> {
val episodeState by component.episodeState.collectAsStateWithLifecycle()
val state = rememberLazyListState(
initialFirstVisibleItemIndex = StateSaver.seriesListIndex,
initialFirstVisibleItemScrollOffset = StateSaver.seriesListOffset
Expand Down Expand Up @@ -263,7 +264,7 @@ private fun CompactScreen(component: SeriesComponent) {
)
}

SeriesContent(current.series) {
SeriesContent(current.series, episodeState) {
component.itemClicked(it)
}
}
Expand All @@ -284,7 +285,6 @@ private fun CompactScreen(component: SeriesComponent) {
private fun DefaultScreen(component: SeriesComponent) {
val seriesState by component.seriesState.collectAsStateWithLifecycle()
val title by component.title.collectAsStateWithLifecycle()
val href by component.href.collectAsStateWithLifecycle()
val coverHref by component.coverHref.collectAsStateWithLifecycle()
val commonHref by component.commonHref.collectAsStateWithLifecycle()

Expand All @@ -302,6 +302,7 @@ private fun DefaultScreen(component: SeriesComponent) {
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(2.dp)
) {
val episodeState by component.episodeState.collectAsStateWithLifecycle()
val state = rememberLazyListState(
initialFirstVisibleItemIndex = StateSaver.seriesListIndex,
initialFirstVisibleItemScrollOffset = StateSaver.seriesListOffset
Expand Down Expand Up @@ -394,7 +395,7 @@ private fun DefaultScreen(component: SeriesComponent) {
}
}

SeriesContent(current.series) {
SeriesContent(current.series, episodeState) {
component.itemClicked(it)
}
}
Expand All @@ -411,9 +412,15 @@ private fun DefaultScreen(component: SeriesComponent) {
}
}

private fun LazyListScope.SeriesContent(content: Series, onEpisodeClick: (Series.Episode) -> Unit) {
private fun LazyListScope.SeriesContent(content: Series, episodeState: EpisodeState, onEpisodeClick: (Series.Episode) -> Unit) {
val loadingEpisode = when (val current = episodeState) {
is EpisodeState.Loading -> current.episode.href
is EpisodeState.SuccessHoster -> current.episode.href
else -> null
}

items(content.episodes, key = { it.href }) { episode ->
EpisodeItem(episode) {
EpisodeItem(episode, loadingEpisode.equals(episode.href, true)) {
onEpisodeClick(episode)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import dev.datlag.burningseries.database.BurningSeries
import dev.datlag.burningseries.model.BSUtil
import dev.datlag.burningseries.model.Series
import dev.datlag.burningseries.model.state.EpisodeAction
import dev.datlag.burningseries.model.state.EpisodeState
import dev.datlag.burningseries.model.state.SeriesAction
import dev.datlag.burningseries.model.state.SeriesState
import dev.datlag.burningseries.network.state.EpisodeStateMachine
Expand Down Expand Up @@ -64,6 +65,7 @@ class SeriesScreenComponent(
}.stateIn(ioScope(), SharingStarted.Lazily, database.burningSeriesQueries.seriesByHref(commonHref.value).executeAsOneOrNull()?.favoriteSince?.let { it > 0 } ?: false)

private val episodeStateMachine by di.instance<EpisodeStateMachine>()
override val episodeState: StateFlow<EpisodeState> = episodeStateMachine.state.flowOn(ioDispatcher()).stateIn(ioScope(), SharingStarted.Lazily, EpisodeState.Waiting)

private val dialogNavigation = SlotNavigation<DialogConfig>()
private val _dialog = childSlot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PlayCircleFilled
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
Expand Down Expand Up @@ -32,7 +33,7 @@ import kotlinx.coroutines.delay
import kotlin.math.roundToInt

@Composable
fun EpisodeItem(content: Series.Episode, onClick: () -> Unit) {
fun EpisodeItem(content: Series.Episode, isLoading: Boolean, onClick: () -> Unit) {
val blurHash = remember(content.href) { BlurHash.random() }
val enabled = content.hosters.isNotEmpty()

Expand Down Expand Up @@ -68,6 +69,9 @@ fun EpisodeItem(content: Series.Episode, onClick: () -> Unit) {
contentDescription = content.title,
tint = Color.White
)
if (isLoading) {
CircularProgressIndicator()
}
}
Text(
text = buildAnnotatedString {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dev.datlag.burningseries.model.Series
sealed interface EpisodeState {
data object Waiting : EpisodeState
data class Loading(val episode: Series.Episode) : EpisodeState
data class SuccessHoster(val results: Collection<String>) : EpisodeState
data class SuccessHoster(val episode: Series.Episode, val results: Collection<String>) : EpisodeState
data class SuccessStream(val results: Collection<String>) : EpisodeState
data class Error(val msg: String) : EpisodeState
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ class EpisodeStateMachine(
}

if (jsonBaseResults.isNotEmpty() || mongoDBResults.isNotEmpty()) {
state.override { EpisodeState.SuccessHoster(setFrom(
jsonBaseResults,
mongoDBResults
)) }
state.override { EpisodeState.SuccessHoster(
episode = state.snapshot.episode,
results = setFrom(
jsonBaseResults,
mongoDBResults
)
) }
} else {
state.override { EpisodeState.Error(String()) }
}
Expand Down

0 comments on commit 10871ee

Please sign in to comment.