This commit is contained in:
anlicheng 2025-07-29 15:07:34 +08:00
parent 88a82545ee
commit ea022e56d3
3 changed files with 80 additions and 19 deletions

View 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()
}
}
}

View File

@ -26,7 +26,8 @@ struct IndexMainView: View {
// //
@State private var noMoreNewest: Bool = false @State private var noMoreNewest: Bool = false
@State private var hasMoreOldest: Bool = false
// //
@State private var headerRefreshing: Bool = false @State private var headerRefreshing: Bool = false
@State private var footerRefreshing: Bool = false @State private var footerRefreshing: Bool = false
@ -105,13 +106,10 @@ struct IndexMainView: View {
} }
.scrollTargetLayout() .scrollTargetLayout()
if indexModel.hasMoreOldest { if hasMoreOldest {
ProgressView() ProgressView()
} else {
Text("没有了")
.foregroundColor(.black)
.autoDismiss(after: 1.5)
} }
} }
.frame(width: 370) .frame(width: 370)
.coordinateSpace(name: "indexScrollView") .coordinateSpace(name: "indexScrollView")
@ -159,13 +157,32 @@ struct IndexMainView: View {
.popover(isPresented: $showDateNavPopover) { .popover(isPresented: $showDateNavPopover) {
DateNavView(selectGroupId: self.$selectGroupId, showDateNavPopover: $showDateNavPopover) { selectedDate in DateNavView(selectGroupId: self.$selectGroupId, showDateNavPopover: $showDateNavPopover) { selectedDate in
Task { @MainActor 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(isPresented: $showPrompt) {
Alert(title: Text("提示"), message: Text(self.promptMessage), dismissButton: .default(Text("OK"))) 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 { .onAppear {
Task { @MainActor in Task { @MainActor in
await self.indexModel.loadData(userId: self.userId) await self.indexModel.loadData(userId: self.userId)
@ -173,6 +190,10 @@ struct IndexMainView: View {
} }
} }
}
extension IndexMainView {
struct ScrollViewOffsetReader: View { struct ScrollViewOffsetReader: View {
@Binding var offset: CGFloat @Binding var offset: CGFloat
@ -200,7 +221,6 @@ struct IndexMainView: View {
value = nextValue() value = nextValue()
} }
} }
} }
extension IndexMainView { extension IndexMainView {

View File

@ -85,7 +85,8 @@ final class IndexModel {
// group_name // group_name
var fixedDramaGroup: UpdateDramaGroup? = nil var fixedDramaGroup: UpdateDramaGroup? = nil
var hasMoreOldest: Bool = true //
var loadMoreTag: UUID? = nil
@ObservationIgnored @ObservationIgnored
private var isLoaded = false private var isLoaded = false
@ -151,8 +152,8 @@ final class IndexModel {
let timeDistance = self.updateInterval.distance(to: Date()) let timeDistance = self.updateInterval.distance(to: Date())
// //
if timeDistance > 1.0 && isCloseBottom { if timeDistance > 1.0 && isCloseBottom {
Task { @MainActor in DispatchQueue.main.async {
await self.loadMoreUpdateDramasTask(userId: userId) self.loadMoreTag = UUID()
} }
self.updateInterval = Date() 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 { guard !self.isMoreLoading else {
return return 0
} }
// id // id
let dramaIds = self.getDramaIds(self.updateDramaGroups) let dramaIds = self.getDramaIds(self.updateDramaGroups)
//print("current ids: \(dramaIds)") //print("current ids: \(dramaIds)")
var loadNum: Int = 0
if let lastId = dramaIds.last { if let lastId = dramaIds.last {
self.isMoreLoading = true self.isMoreLoading = true
let response = await API.loadMoreUpdateDramas(userId: userId, mode: .next, id: lastId, as: [UpdateDramaGroup].self) 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) self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups)
displayDramaGroups(self.updateDramaGroups, label: "after") displayDramaGroups(self.updateDramaGroups, label: "after")
self.hasMoreOldest = true
} else { loadNum = groups.reduce(0) { acc, group in acc + group.items.count}
self.hasMoreOldest = false
} }
} else {
self.hasMoreOldest = false
} }
self.isMoreLoading = false self.isMoreLoading = false
} }
return loadNum
} }
func loadPrevUpdateDramasTask(userId: String, callback: (Bool) -> Void) async { 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.updateDramaGroups.removeAll()
self.dramaGroupElements.removeAll() self.dramaGroupElements.removeAll()
@ -333,7 +334,12 @@ final class IndexModel {
self.updateDramaGroups = groups self.updateDramaGroups = groups
self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups) self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups)
self.fixedDramaGroup = groups.first self.fixedDramaGroup = groups.first
let itemCount = groups.reduce(0) { acc, group in acc + group.items.count }
return itemCount
} }
return 0
} }
// groups // groups