diff --git a/dimensionhub/Views/Index/IndexMainView.swift b/dimensionhub/Views/Index/IndexMainView.swift index fa161ec..908fdfe 100644 --- a/dimensionhub/Views/Index/IndexMainView.swift +++ b/dimensionhub/Views/Index/IndexMainView.swift @@ -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() + @State private var footerPublisher = PassthroughSubject() + @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 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 { diff --git a/dimensionhub/dimensionhubApp.swift b/dimensionhub/dimensionhubApp.swift index 25b70d9..f2e2dc8 100644 --- a/dimensionhub/dimensionhubApp.swift +++ b/dimensionhub/dimensionhubApp.swift @@ -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):