fix index view

This commit is contained in:
anlicheng 2025-04-10 16:45:22 +08:00
parent 8f6047b1bc
commit 9127f5e852
2 changed files with 32 additions and 33 deletions

View File

@ -5,7 +5,7 @@
// Created by on 2025/4/8.
//
import SwiftUI
import Refresh
import Combine
//
struct IndexMainView: View {
@ -29,7 +29,8 @@ struct IndexMainView: View {
@State private var noMore: Bool = false
//
@State private var visibleIndices = Set<Int>()
@State private var footerPublisher = PassthroughSubject<Int, Never>()
@State var cancel: AnyCancellable?
var body: some View {
VStack(alignment: .center) {
@ -54,7 +55,6 @@ struct IndexMainView: View {
}
)
.zIndex(1)
.border(Color.red)
}
.padding([.leading, .trailing], 15)
.frame(height: 50)
@ -67,7 +67,7 @@ struct IndexMainView: View {
//
LazyVStack(alignment: .center, spacing: 10) {
ForEach(indexModel.updateDramaGroups, id: \.group_id) { group in
DramaGroupView(visibleIndices: $visibleIndices, group: group, model: indexModel) {
DramaGroupView(group: group, model: indexModel) {
selectGroupId = group.group_id
indexModel.selectedDate = group.group_id
showDateNavPopover = true
@ -79,19 +79,10 @@ struct IndexMainView: View {
Rectangle()
.frame(height: 0)
.background(GeometryReader { geometry in
Color.clear.onChange(of: geometry.frame(in: .global).minY) {_, offset in
let frame = geometry.frame(in: .global)
let screenBounds = UIScreen.main.bounds
let contextFrame = geometry.frame(in: .named("indexScrollView"))
if screenBounds.height - frame.minY > 50 && contextFrame.minY > 0 && !footerRefreshing {
Task {
self.footerRefreshing = true
await self.indexModel.loadMoreUpdateDramas(userId: self.userId, mode: .next)
self.footerRefreshing = false
}
Color.clear
.onChange(of: geometry.frame(in: .global).minY) { oldValue, newValue in
footerPublisher.send(Int(newValue))
}
}
})
if self.footerRefreshing {
@ -155,12 +146,32 @@ struct IndexMainView: View {
indexModel.setFixedDrameGroup(groupId: minFrame.key)
}
}
.onPreferenceChange(DramaOffsetPreferenceKey.self) { offsets in
self.visibleIndices = Set(offsets.compactMap { (id: Int, offset: CGFloat) in
return offset >= 0 ? id : nil
})
.onAppear {
cancel = footerPublisher
.debounce(for: 0.1, scheduler: RunLoop.main)
.sink { offset in
guard !footerRefreshing else {
return
}
let height = Int(UIScreen.main.bounds.height)
if offset < height + 500 && !footerRefreshing {
self.footerRefreshing = true
Task {
await self.indexModel.loadMoreUpdateDramas(userId: self.userId, mode: .next)
await MainActor.run {
self.footerRefreshing = false
}
}
}
}
}
.onDisappear {
self.cancel?.cancel()
}
}
}
extension IndexMainView {
@ -168,7 +179,6 @@ extension IndexMainView {
//
struct DramaGroupView: View {
@EnvironmentObject var appNav: AppNavigation
@Binding var visibleIndices: Set<Int>
let group: IndexModel.UpdateDramaGroup
let model: IndexModel
@ -236,11 +246,8 @@ extension IndexMainView {
Color.clear
.preference(key: DramaGroupElementPreferenceKey.self, value: [ group.group_id : y])
.preference(key: DramaOffsetPreferenceKey.self, value: [item.id : minY])
})
}
.allowsHitTesting(visibleIndices.contains(item.id))
.disabled(!visibleIndices.contains(item.id))
}
}
}
@ -255,15 +262,6 @@ extension IndexMainView {
}
}
// , [groupId : minY]
struct DramaOffsetPreferenceKey: PreferenceKey {
static var defaultValue: [Int: CGFloat] = [:]
static func reduce(value: inout [Int: CGFloat], nextValue: () -> [Int: CGFloat]) {
value.merge(nextValue()) { $1 }
}
}
}
#Preview {

View File

@ -44,6 +44,7 @@ struct dimensionhubApp: App {
WindowGroup {
NavigationStack(path: $appNav.path) {
IndexView()
//TestView()
.navigationDestination(for: AppNavigation.Destination.self) { dest in
switch dest {
case .detail(id: let id):