fix index view

This commit is contained in:
anlicheng 2025-04-11 11:47:00 +08:00
parent 2846ad65f9
commit acb51093c4
2 changed files with 52 additions and 35 deletions

View File

@ -28,9 +28,8 @@ struct IndexMainView: View {
@State private var footerRefreshing: Bool = false @State private var footerRefreshing: Bool = false
@State private var noMore: Bool = false @State private var noMore: Bool = false
// // globalminY
@State private var footerPublisher = PassthroughSubject<Int, Never>() @State private var footerOffset: Int = 0
@State var cancel: AnyCancellable?
var body: some View { var body: some View {
VStack(alignment: .center) { VStack(alignment: .center) {
@ -76,18 +75,33 @@ struct IndexMainView: View {
} }
} }
Rectangle()
.frame(height: 0)
.background(GeometryReader { geometry in
Color.clear.onChange(of: geometry.frame(in: .global).minY) { _, newValue in
footerPublisher.send(Int(newValue))
}
})
ProgressView() ProgressView()
.background(GeometryReader { geometry in
let minY = geometry.frame(in: .global).minY
Color.clear.preference(key: FooterOffsetPreferenceKey.self, value: Int(minY))
})
} }
.frame(width: 370) .frame(width: 370)
.coordinateSpace(name: "indexScrollView") .coordinateSpace(name: "indexScrollView")
.onScrollPhaseChange { oldPhase, newPhase in
print("scroll old: \(oldPhase), new: \(newPhase), footer-offset: \(footerOffset)")
guard !footerRefreshing && !showDateNavPopover else {
return
}
//
let height = Int(UIScreen.main.bounds.height)
if newPhase == .idle && self.footerOffset < height + 500 {
self.footerRefreshing = true
Task {
await self.indexModel.loadMoreUpdateDramas(userId: self.userId, mode: .next)
await MainActor.run {
self.footerRefreshing = false
}
}
}
}
.refreshable { .refreshable {
guard !self.showDateNavPopover && !self.headerRefreshing else { guard !self.showDateNavPopover && !self.headerRefreshing else {
return return
@ -143,29 +157,13 @@ struct IndexMainView: View {
indexModel.setFixedDrameGroup(groupId: minFrame.key) indexModel.setFixedDrameGroup(groupId: minFrame.key)
} }
} }
.onAppear { .onPreferenceChange(FooterOffsetPreferenceKey.self) { offset in
cancel = footerPublisher // state
.debounce(for: 0.1, scheduler: RunLoop.main) DispatchQueue.main.async {
.sink { offset in self.footerOffset = offset
guard !footerRefreshing && !showDateNavPopover 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()
} }
} }
} }
@ -249,6 +247,12 @@ extension IndexMainView {
} }
} }
}
// MARK: PreferenceKey
extension IndexMainView {
// , [groupId : minY] // , [groupId : minY]
struct DramaGroupElementPreferenceKey: PreferenceKey { struct DramaGroupElementPreferenceKey: PreferenceKey {
static var defaultValue: [String: CGFloat] = [:] static var defaultValue: [String: CGFloat] = [:]
@ -258,8 +262,21 @@ extension IndexMainView {
} }
} }
//
struct FooterOffsetPreferenceKey: PreferenceKey {
static var defaultValue: Int = 0
static func reduce(value: inout Int, nextValue: () -> Int) {
let newValue = nextValue()
if newValue > 0 && abs(newValue - value) >= 50 {
value = newValue
}
}
}
} }
#Preview { #Preview {
IndexMainView() IndexMainView()
} }

View File

@ -43,8 +43,8 @@ struct dimensionhubApp: App {
var body: some Scene { var body: some Scene {
WindowGroup { WindowGroup {
NavigationStack(path: $appNav.path) { NavigationStack(path: $appNav.path) {
//IndexView() IndexView()
TestView() //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):