fix
This commit is contained in:
parent
88a82545ee
commit
ea022e56d3
35
dimensionhub/Modifers/AutoDismissModifier.swift
Normal file
35
dimensionhub/Modifers/AutoDismissModifier.swift
Normal file
@ -0,0 +1,35 @@
|
||||
//
|
||||
// AutoDismissModifier.swift
|
||||
// dimensionhub
|
||||
//
|
||||
// Created by 安礼成 on 2025/7/28.
|
||||
//
|
||||
import SwiftUI
|
||||
|
||||
// 自定义修饰符
|
||||
extension View {
|
||||
func autoDismiss(after seconds: Double) -> some View {
|
||||
modifier(AutoDismissModifier(seconds: seconds))
|
||||
}
|
||||
}
|
||||
|
||||
struct AutoDismissModifier: ViewModifier {
|
||||
let seconds: Double
|
||||
@State private var isVisible = true
|
||||
|
||||
func body(content: Content) -> some View {
|
||||
if isVisible {
|
||||
content
|
||||
.onAppear {
|
||||
Task {
|
||||
try? await Task.sleep(for: .seconds(seconds))
|
||||
withAnimation(.easeOut) {
|
||||
isVisible = false
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -26,7 +26,8 @@ struct IndexMainView: View {
|
||||
|
||||
// 前向刷新提示信息
|
||||
@State private var noMoreNewest: Bool = false
|
||||
|
||||
@State private var hasMoreOldest: Bool = false
|
||||
|
||||
// 刷新逻辑
|
||||
@State private var headerRefreshing: Bool = false
|
||||
@State private var footerRefreshing: Bool = false
|
||||
@ -105,13 +106,10 @@ struct IndexMainView: View {
|
||||
}
|
||||
.scrollTargetLayout()
|
||||
|
||||
if indexModel.hasMoreOldest {
|
||||
if hasMoreOldest {
|
||||
ProgressView()
|
||||
} else {
|
||||
Text("没有了")
|
||||
.foregroundColor(.black)
|
||||
.autoDismiss(after: 1.5)
|
||||
}
|
||||
|
||||
}
|
||||
.frame(width: 370)
|
||||
.coordinateSpace(name: "indexScrollView")
|
||||
@ -159,13 +157,32 @@ struct IndexMainView: View {
|
||||
.popover(isPresented: $showDateNavPopover) {
|
||||
DateNavView(selectGroupId: self.$selectGroupId, showDateNavPopover: $showDateNavPopover) { selectedDate in
|
||||
Task { @MainActor in
|
||||
await indexModel.loadDateUpdateDramas(userId: self.userId, date: selectedDate)
|
||||
let num = await indexModel.loadDateUpdateDramas(userId: self.userId, date: selectedDate)
|
||||
if num > 3 {
|
||||
self.hasMoreOldest = true
|
||||
} else {
|
||||
self.hasMoreOldest = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.alert(isPresented: $showPrompt) {
|
||||
Alert(title: Text("提示"), message: Text(self.promptMessage), dismissButton: .default(Text("OK")))
|
||||
}
|
||||
.onChange(of: indexModel.loadMoreTag) { _, newTag in
|
||||
guard newTag != nil else {
|
||||
return
|
||||
}
|
||||
|
||||
Task { @MainActor in
|
||||
let num = await indexModel.loadMoreUpdateDramasTask(userId: self.userId)
|
||||
if num > 3 {
|
||||
self.hasMoreOldest = true
|
||||
} else {
|
||||
self.hasMoreOldest = false
|
||||
}
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
Task { @MainActor in
|
||||
await self.indexModel.loadData(userId: self.userId)
|
||||
@ -173,6 +190,10 @@ struct IndexMainView: View {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension IndexMainView {
|
||||
|
||||
struct ScrollViewOffsetReader: View {
|
||||
@Binding var offset: CGFloat
|
||||
|
||||
@ -200,7 +221,6 @@ struct IndexMainView: View {
|
||||
value = nextValue()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension IndexMainView {
|
||||
|
||||
@ -85,7 +85,8 @@ final class IndexModel {
|
||||
// 用来显示固定栏目的group_name
|
||||
var fixedDramaGroup: UpdateDramaGroup? = nil
|
||||
|
||||
var hasMoreOldest: Bool = true
|
||||
// 用来标记是否需要触发加载更多
|
||||
var loadMoreTag: UUID? = nil
|
||||
|
||||
@ObservationIgnored
|
||||
private var isLoaded = false
|
||||
@ -151,8 +152,8 @@ final class IndexModel {
|
||||
let timeDistance = self.updateInterval.distance(to: Date())
|
||||
// 滑动停止的时候,检测是否到达了底部
|
||||
if timeDistance > 1.0 && isCloseBottom {
|
||||
Task { @MainActor in
|
||||
await self.loadMoreUpdateDramasTask(userId: userId)
|
||||
DispatchQueue.main.async {
|
||||
self.loadMoreTag = UUID()
|
||||
}
|
||||
self.updateInterval = Date()
|
||||
}
|
||||
@ -227,14 +228,15 @@ final class IndexModel {
|
||||
}
|
||||
}
|
||||
|
||||
func loadMoreUpdateDramasTask(userId: String) async {
|
||||
func loadMoreUpdateDramasTask(userId: String) async -> Int {
|
||||
guard !self.isMoreLoading else {
|
||||
return
|
||||
return 0
|
||||
}
|
||||
|
||||
// 按照id来判断不一定正确,需要借助其他值
|
||||
let dramaIds = self.getDramaIds(self.updateDramaGroups)
|
||||
//print("current ids: \(dramaIds)")
|
||||
var loadNum: Int = 0
|
||||
if let lastId = dramaIds.last {
|
||||
self.isMoreLoading = true
|
||||
let response = await API.loadMoreUpdateDramas(userId: userId, mode: .next, id: lastId, as: [UpdateDramaGroup].self)
|
||||
@ -249,16 +251,15 @@ final class IndexModel {
|
||||
self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups)
|
||||
|
||||
displayDramaGroups(self.updateDramaGroups, label: "after")
|
||||
self.hasMoreOldest = true
|
||||
} else {
|
||||
self.hasMoreOldest = false
|
||||
|
||||
loadNum = groups.reduce(0) { acc, group in acc + group.items.count}
|
||||
}
|
||||
} else {
|
||||
self.hasMoreOldest = false
|
||||
}
|
||||
|
||||
self.isMoreLoading = false
|
||||
}
|
||||
|
||||
return loadNum
|
||||
}
|
||||
|
||||
func loadPrevUpdateDramasTask(userId: String, callback: (Bool) -> Void) async {
|
||||
@ -322,7 +323,7 @@ final class IndexModel {
|
||||
}
|
||||
|
||||
// 指定日期,并更新日期下对应的数据
|
||||
func loadDateUpdateDramas(userId: String, date: String) async {
|
||||
func loadDateUpdateDramas(userId: String, date: String) async -> Int {
|
||||
self.updateDramaGroups.removeAll()
|
||||
self.dramaGroupElements.removeAll()
|
||||
|
||||
@ -333,7 +334,12 @@ final class IndexModel {
|
||||
self.updateDramaGroups = groups
|
||||
self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups)
|
||||
self.fixedDramaGroup = groups.first
|
||||
|
||||
let itemCount = groups.reduce(0) { acc, group in acc + group.items.count }
|
||||
return itemCount
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// 合并groups
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user