基于坐标系计算

This commit is contained in:
anlicheng 2025-06-10 21:08:01 +08:00
parent e784d46299
commit 276ab30a07
2 changed files with 44 additions and 8 deletions

View File

@ -79,13 +79,13 @@ struct IndexMainView: View {
LazyVStack(alignment: .center, spacing: 10) { LazyVStack(alignment: .center, spacing: 10) {
ForEach(Array(indexModel.dramaGroupElements.enumerated()), id: \.offset) { idx, item in ForEach(Array(indexModel.dramaGroupElements.enumerated()), id: \.offset) { idx, item in
switch item { switch item {
case .label(let groupId, let groupName): case .label(let groupId, let groupName, _):
DramaGroupLabelView(group_name: groupName) { DramaGroupLabelView(group_name: groupName) {
selectGroupId = groupId selectGroupId = groupId
indexModel.selectedDate = groupId indexModel.selectedDate = groupId
showDateNavPopover = true showDateNavPopover = true
} }
case .item(let groupId, let item): case .item(let groupId, let item, _):
DramaGroupItemView(groupId: groupId, item: item) DramaGroupItemView(groupId: groupId, item: item)
.id(idx) .id(idx)
} }
@ -100,6 +100,7 @@ struct IndexMainView: View {
.scrollPosition(id: $scrollID) .scrollPosition(id: $scrollID)
.onChange(of: scrollOffset) { .onChange(of: scrollOffset) {
self.showFloatGroupLabel = scrollOffset < 0 self.showFloatGroupLabel = scrollOffset < 0
indexModel.offsetChanged(offset: scrollOffset)
} }
.refreshable { .refreshable {
guard !self.showDateNavPopover && !self.headerRefreshing else { guard !self.showDateNavPopover && !self.headerRefreshing else {

View File

@ -52,9 +52,9 @@ final class IndexModel {
enum GroupElement { enum GroupElement {
// groupId, groupName // groupId, groupName
case label(String, String) case label(String, String, Double)
// groupId, UpdateDramaGroup.Item // groupId, UpdateDramaGroup.Item
case item(String, UpdateDramaGroup.Item) case item(String, UpdateDramaGroup.Item, Double)
} }
var selectedDate: String var selectedDate: String
@ -95,6 +95,10 @@ final class IndexModel {
@ObservationIgnored @ObservationIgnored
private var cancel: AnyCancellable? private var cancel: AnyCancellable?
//
@ObservationIgnored
private var elementHeight: (Double, Double, Double) = (10, 21.6, 180) // spacing: 10, label height: 21.6, item: 180
init() { init() {
self.selectedDate = "" self.selectedDate = ""
@ -102,9 +106,9 @@ final class IndexModel {
.sink { userId, scrollID in .sink { userId, scrollID in
// grouplabel // grouplabel
switch self.dramaGroupElements[scrollID] { switch self.dramaGroupElements[scrollID] {
case .label(_, _): case .label(_, _, _):
() ()
case .item(let groupId, _): case .item(let groupId, _, _):
DispatchQueue.main.async { DispatchQueue.main.async {
self.setFixedDrameGroup(groupId: groupId) self.setFixedDrameGroup(groupId: groupId)
} }
@ -122,6 +126,32 @@ final class IndexModel {
} }
} }
//
func offsetChanged(offset: Double) {
// offset
var rangeGroupId: String? = nil
let (spacing, labelHeight, elementHeight) = self.elementHeight
self.dramaGroupElements.forEach { element in
switch element {
case .label(let groupId, _, let y):
if offset + y >= -labelHeight && offset + y <= 0 {
rangeGroupId = groupId
}
case .item(let groupId, _, let y):
if offset + y >= -elementHeight && offset + y <= 0 {
rangeGroupId = groupId
}
}
}
if let rangeGroupId {
DispatchQueue.main.async {
self.setFixedDrameGroup(groupId: rangeGroupId)
}
}
}
func loadData(userId: String) async { func loadData(userId: String) async {
guard !isLoaded else { guard !isLoaded else {
return return
@ -219,11 +249,16 @@ final class IndexModel {
// //
private func transformUpdateDramaGroups(groups: [UpdateDramaGroup]) -> [GroupElement] { private func transformUpdateDramaGroups(groups: [UpdateDramaGroup]) -> [GroupElement] {
var offset: Double = 0
let (spacing, labelHeight, elementHeight) = self.elementHeight
var groupElements: [GroupElement] = [] var groupElements: [GroupElement] = []
for group in groups { for group in groups {
groupElements.append(.label(group.group_id, group.group_name)) groupElements.append(.label(group.group_id, group.group_name, offset))
offset += labelHeight + spacing
for item in group.items { for item in group.items {
groupElements.append(.item(group.group_id, item)) groupElements.append(.item(group.group_id, item, offset))
offset += elementHeight + spacing
} }
} }