Skip to content

Commit

Permalink
more search work
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Nov 13, 2023
1 parent 3a6a1e7 commit 16c87c0
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import org.kodein.di.DI
import org.kodein.di.bindSingleton
import java.util.concurrent.TimeUnit

actual object PlatformModule {

Expand All @@ -15,6 +16,9 @@ actual object PlatformModule {
engine {
config {
followRedirects(true)
connectTimeout(3, TimeUnit.MINUTES)
readTimeout(3, TimeUnit.MINUTES)
writeTimeout(3, TimeUnit.MINUTES)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ import kotlinx.coroutines.flow.StateFlow
interface SearchComponent : Component {

val searchState: StateFlow<SearchState>

fun retryLoadingSearch(): Any?
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,52 @@
package dev.datlag.burningseries.ui.screen.initial.search

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import dev.datlag.burningseries.common.lifecycle.collectAsStateWithLifecycle
import dev.datlag.burningseries.model.state.SearchState
import dev.datlag.burningseries.shared.SharedRes
import dev.datlag.burningseries.ui.custom.state.ErrorState
import dev.datlag.burningseries.ui.custom.state.LoadingState

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SearchScreen(component: SearchComponent) {
val state by component.searchState.collectAsStateWithLifecycle()

when (val current = state) {
is SearchState.Loading -> {
LoadingState(SharedRes.strings.loading_search)
}
is SearchState.Error -> {
ErrorState(SharedRes.strings.error_loading_search) {
component.retryLoadingSearch()
}
}
is SearchState.Success -> {
val selectedGenre = current.genres.first()

LazyColumn(
modifier = Modifier.fillMaxWidth()
) {
stickyHeader {
Text(
text = selectedGenre.title,
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold
)
}
items(selectedGenre.items) {
Text(text = it.title)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import androidx.compose.runtime.Composable
import com.arkivanov.decompose.ComponentContext
import dev.datlag.burningseries.common.ioDispatcher
import dev.datlag.burningseries.common.ioScope
import dev.datlag.burningseries.common.launchIO
import dev.datlag.burningseries.model.state.SearchAction
import dev.datlag.burningseries.model.state.SearchState
import dev.datlag.burningseries.network.state.SearchStateMachine
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -18,11 +20,15 @@ class SearchScreenComponent(
override val di: DI
) : SearchComponent, ComponentContext by componentContext {

private val homeStateMachine: SearchStateMachine by di.instance()
override val searchState: StateFlow<SearchState> = homeStateMachine.state.flowOn(ioDispatcher()).stateIn(ioScope(), SharingStarted.Lazily, SearchState.Loading)
private val searchStateMachine: SearchStateMachine by di.instance()
override val searchState: StateFlow<SearchState> = searchStateMachine.state.flowOn(ioDispatcher()).stateIn(ioScope(), SharingStarted.Lazily, SearchState.Loading)

@Composable
override fun render() {
SearchScreen(this)
}

override fun retryLoadingSearch(): Any? = ioScope().launchIO {
searchStateMachine.dispatch(SearchAction.Retry)
}
}
2 changes: 2 additions & 0 deletions app/shared/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
<string name="restart">Restart</string>
<string name="downloading">Downloading</string>
<string name="downloading_text">Downloading required packages for your platform, please wait</string>
<string name="loading_search">Loading search information, please wait</string>
<string name="error_loading_search">Error while loading search information</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import org.kodein.di.DI
import org.kodein.di.bindSingleton
import java.util.concurrent.TimeUnit

actual object PlatformModule {

Expand All @@ -15,6 +16,9 @@ actual object PlatformModule {
engine {
config {
followRedirects(true)
connectTimeout(3, TimeUnit.MINUTES)
readTimeout(3, TimeUnit.MINUTES)
writeTimeout(3, TimeUnit.MINUTES)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ data object BurningSeries {

suspend fun getSearch(client: HttpClient): List<Genre> {
val doc = getDocument(client, BSUtil.SEARCH) ?: return emptyList()
return doc.querySelector("#seriesContainer")?.querySelectorAll(".genre")?.mapNotNull { element ->
val genre = element.querySelector("string")?.textContent()?.trim() ?: String()
return doc.getElementById("seriesContainer")?.querySelectorAll(".genre")?.mapNotNull { element ->
val genre = element.querySelector("strong")?.textContent()?.trim() ?: String()

if (genre.isNotBlank()) {
Genre(
Expand Down

0 comments on commit 16c87c0

Please sign in to comment.