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,6 +26,7 @@ struct IndexMainView: View {
//
@State private var noMoreNewest: Bool = false
@State private var hasMoreOldest: Bool = false
//
@State private var headerRefreshing: 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 {

View File

@ -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