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. // Created by on 2025/4/8.
// //
import SwiftUI import SwiftUI
import Refresh import Combine
// //
struct IndexMainView: View { struct IndexMainView: View {
@ -29,7 +29,8 @@ struct IndexMainView: View {
@State private var noMore: Bool = false @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 { var body: some View {
VStack(alignment: .center) { VStack(alignment: .center) {
@ -54,7 +55,6 @@ struct IndexMainView: View {
} }
) )
.zIndex(1) .zIndex(1)
.border(Color.red)
} }
.padding([.leading, .trailing], 15) .padding([.leading, .trailing], 15)
.frame(height: 50) .frame(height: 50)
@ -67,7 +67,7 @@ struct IndexMainView: View {
// //
LazyVStack(alignment: .center, spacing: 10) { LazyVStack(alignment: .center, spacing: 10) {
ForEach(indexModel.updateDramaGroups, id: \.group_id) { group in ForEach(indexModel.updateDramaGroups, id: \.group_id) { group in
DramaGroupView(visibleIndices: $visibleIndices, group: group, model: indexModel) { DramaGroupView(group: group, model: indexModel) {
selectGroupId = group.group_id selectGroupId = group.group_id
indexModel.selectedDate = group.group_id indexModel.selectedDate = group.group_id
showDateNavPopover = true showDateNavPopover = true
@ -79,19 +79,10 @@ struct IndexMainView: View {
Rectangle() Rectangle()
.frame(height: 0) .frame(height: 0)
.background(GeometryReader { geometry in .background(GeometryReader { geometry in
Color.clear.onChange(of: geometry.frame(in: .global).minY) {_, offset in Color.clear
let frame = geometry.frame(in: .global) .onChange(of: geometry.frame(in: .global).minY) { oldValue, newValue in
let screenBounds = UIScreen.main.bounds footerPublisher.send(Int(newValue))
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
}
} }
}
}) })
if self.footerRefreshing { if self.footerRefreshing {
@ -155,12 +146,32 @@ struct IndexMainView: View {
indexModel.setFixedDrameGroup(groupId: minFrame.key) indexModel.setFixedDrameGroup(groupId: minFrame.key)
} }
} }
.onPreferenceChange(DramaOffsetPreferenceKey.self) { offsets in .onAppear {
self.visibleIndices = Set(offsets.compactMap { (id: Int, offset: CGFloat) in cancel = footerPublisher
return offset >= 0 ? id : nil .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 { extension IndexMainView {
@ -168,7 +179,6 @@ extension IndexMainView {
// //
struct DramaGroupView: View { struct DramaGroupView: View {
@EnvironmentObject var appNav: AppNavigation @EnvironmentObject var appNav: AppNavigation
@Binding var visibleIndices: Set<Int>
let group: IndexModel.UpdateDramaGroup let group: IndexModel.UpdateDramaGroup
let model: IndexModel let model: IndexModel
@ -236,11 +246,8 @@ extension IndexMainView {
Color.clear Color.clear
.preference(key: DramaGroupElementPreferenceKey.self, value: [ group.group_id : y]) .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 { #Preview {

View File

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