Skip to content

Commit

Permalink
fix: 修复分类加载问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Greatwallcorner committed Jul 1, 2024
1 parent f9143a5 commit eb5b478
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.serialization.Transient

@Serializable
class Type {
constructor(id:String, name:String){
constructor(id: String, name: String) {
this.typeId = id
this.typeName = name
}
Expand All @@ -19,13 +19,13 @@ class Type {
var typeName: String = ""

@Transient
var selected:Boolean = false
var selected: Boolean = false

@Transient
var failTime:Int =0
var failTime: Int = 0

companion object{
fun home():Type{
companion object {
fun home(): Type {
val type = Type("home", "推荐")
type.selected = true
return type
Expand All @@ -41,6 +41,7 @@ class Type {
if (typeId != other.typeId) return false
if (typeName != other.typeName) return false
if (failTime != other.failTime) return false
if (selected != other.selected) return false

return true
}
Expand All @@ -49,6 +50,7 @@ class Type {
var result = typeId.hashCode()
result = 31 * result + typeName.hashCode()
result = 31 * result + failTime.hashCode()
result = 31 * result + selected.hashCode()
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import io.ktor.http.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancelChildren
import kotlinx.serialization.encodeToString
import okhttp3.Headers.Companion.toHeaders
import org.apache.commons.lang3.StringUtils
Expand All @@ -36,6 +37,10 @@ object SiteViewModel {
private val supervisorJob = SupervisorJob()
val viewModelScope = CoroutineScope(Dispatchers.IO + supervisorJob)

fun cancelAll(){
supervisorJob.cancelChildren()
}

fun getSearchResultActive(): Collect {
return search.value.first { it.isActivated().value }
}
Expand Down
77 changes: 40 additions & 37 deletions composeApp/src/commonMain/kotlin/com/corner/ui/video/Video.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ fun VideoItem(modifier: Modifier, vod: Vod, showSite: Boolean, click: (Vod) -> U
contentScale = ContentScale.Crop,
placeholderPainter = { painterResource("/pic/empty.png") },
errorPainter = { painterResource("/pic/empty.png") })
Box(Modifier.align(Alignment.BottomCenter)){
Box(Modifier.align(Alignment.BottomCenter)) {
ToolTipText(
text = vod.vodName!!,
textStyle = TextStyle(color = MaterialTheme.colorScheme.onSecondaryContainer, textAlign = TextAlign.Center),
textStyle = TextStyle(
color = MaterialTheme.colorScheme.onSecondaryContainer,
textAlign = TextAlign.Center
),
modifier = Modifier
.background(MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.6f))
.fillMaxWidth().padding(0.dp, 10.dp))
.fillMaxWidth().padding(0.dp, 10.dp)
)
}
// 左上角
Text(
Expand Down Expand Up @@ -151,7 +155,7 @@ fun VideoScene(
}
val listEmpty = derivedStateOf { model.value.homeVodResult.isEmpty() }
if (listEmpty.value) {
emptyShow(onRefresh = {component.homeLoad()})
emptyShow(onRefresh = { component.homeLoad() })
} else {
Box {
LazyVerticalGrid(
Expand All @@ -163,7 +167,7 @@ fun VideoScene(
horizontalArrangement = Arrangement.spacedBy(10.dp),
// userScrollEnabled = true
) {
itemsIndexed(list.value, key = {_,item -> item.vodId + item.vodName}) { _, item ->
itemsIndexed(list.value, key = { _, item -> item.vodId + item.vodName }) { _, item ->
VideoItem(Modifier.animateItemPlacement(), item, false) {
if (item.isFolder()) {
SiteViewModel.viewModelScope.launch {
Expand All @@ -190,7 +194,7 @@ fun VideoScene(
}

@Composable
fun FloatButton(component: DefaultVideoComponent, state: LazyGridState, scope:CoroutineScope) {
fun FloatButton(component: DefaultVideoComponent, state: LazyGridState, scope: CoroutineScope) {
val show = derivedStateOf { GlobalModel.chooseVod.value.isFolder() }
val model = component.model.subscribeAsState()
val showButton = derivedStateOf { !model.value.currentFilter.isEmpty() || state.firstVisibleItemIndex > 8 }
Expand Down Expand Up @@ -253,7 +257,7 @@ fun FloatButton(component: DefaultVideoComponent, state: LazyGridState, scope:Co
contentColor = MaterialTheme.colorScheme.onPrimaryContainer
)
if (it) {
if(model.value.currentFilter.isEmpty()) return@AnimatedContent
if (model.value.currentFilter.isEmpty()) return@AnimatedContent
ElevatedButton(
onClick = { showDialog = !showDialog },
modifier = modifier,
Expand Down Expand Up @@ -405,42 +409,41 @@ fun ClassRow(component: DefaultVideoComponent, onCLick: (Type) -> Unit) {
component.isLoading.set(true)
SiteViewModel.viewModelScope.launch {
showProgress()
// component.clear()
try {
for (tp in model.value.classList) {
tp.selected = type.typeId == tp.typeId
}
model.value.currentClass = type
model.value.classList = model.value.classList.toSet().toMutableSet()
val filterMap = model.value.filtersMap
if (filterMap.isNotEmpty()) {
component.model.update {
it.copy(
currentFilter = component.getFilters(type)
)
}
}
if (type.typeId == "home") {
SiteViewModel.homeContent()
} else {
model.value.page.set(1)
val result = SiteViewModel.categoryContent(
GlobalModel.home.value.key,
type.typeId,
model.value.page.get().toString(),
false,
HashMap()
for (tp in model.value.classList) {
tp.selected = type.typeId == tp.typeId
}
if (model.value.filtersMap.isNotEmpty()) {
component.model.update {
it.copy(
currentFilter = component.getFilters(type)
)
if (!result.isSuccess) {
model.value.currentClass?.failTime?.plus(1)
}
}
} finally {
hideProgress()
}
component.model.update {
it.copy(
currentClass = type, classList = model.value.classList
)
}
SiteViewModel.cancelAll()
if (type.typeId == "home") {
SiteViewModel.homeContent()
} else {
model.value.page.set(1)
val result = SiteViewModel.categoryContent(
GlobalModel.home.value.key,
type.typeId,
model.value.page.get().toString(),
false,
HashMap()
)
if (!result.isSuccess) {
model.value.currentClass?.failTime?.plus(1)
}
}
}.invokeOnCompletion {
onCLick(type)
component.isLoading.set(false)
hideProgress()
}
}, type.selected)
}
Expand Down

0 comments on commit eb5b478

Please sign in to comment.