diff --git a/dimensionhub.xcodeproj/project.pbxproj b/dimensionhub.xcodeproj/project.pbxproj index 305c8e1..e49c2aa 100644 --- a/dimensionhub.xcodeproj/project.pbxproj +++ b/dimensionhub.xcodeproj/project.pbxproj @@ -420,8 +420,7 @@ INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; - INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; - INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait; IPHONEOS_DEPLOYMENT_TARGET = 17.6; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", @@ -455,8 +454,7 @@ INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; - INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; - INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait; IPHONEOS_DEPLOYMENT_TARGET = 17.6; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", diff --git a/dimensionhub/Views/IndexView.swift b/dimensionhub/Views/IndexView.swift index 7fff574..bc98052 100644 --- a/dimensionhub/Views/IndexView.swift +++ b/dimensionhub/Views/IndexView.swift @@ -84,6 +84,13 @@ final class IndexModel { self.isLoaded = true } + func setFixedDrameGroup(groupId: String) { + if let newFixedDramaGroup = self.updateDramaGroups.first(where: {$0.group_id == groupId}), + newFixedDramaGroup.group_id != self.fixedDramaGroup?.group_id { + self.fixedDramaGroup = newFixedDramaGroup + } + } + @MainActor func loadMoreUpdateDramas(userId: String, mode: API.LoadMode) async { // 按照id来判断不一定正确,需要借助其他值 @@ -132,6 +139,7 @@ final class IndexModel { let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self) if case let .result(groups) = response { self.updateDramaGroups = groups + self.fixedDramaGroup = groups.first } } @@ -379,8 +387,14 @@ extension IndexView { self.isPrevLoading = false } .overlay(alignment: .topTrailing) { - HStack { - Text("🔍") + HStack(alignment: .center) { + NavigationLink { + SearchView() + } label: { + Image(systemName: "magnifyingglass") + .font(.system(size: 20)) + } + Spacer() if let fixedDramaGroup = indexModel.fixedDramaGroup { Text(fixedDramaGroup.group_name) @@ -393,7 +407,7 @@ extension IndexView { } } } - .padding(8) + .padding([.top, .bottom], 8) .background(.white) } } @@ -411,6 +425,12 @@ extension IndexView { .task { await self.indexModel.loadData(userId: self.userId) } + .onPreferenceChange(DramaGroupElementPreferenceKey.self) { frames in + let visibleFrames = frames.filter { $0.value >= 0} + if let minFrame = visibleFrames.min(by: { $0.value <= $1.value}) { + indexModel.setFixedDrameGroup(groupId: minFrame.key) + } + } } } @@ -432,14 +452,6 @@ extension IndexView { onTap() } } - .background(GeometryReader { - geometry in - Color.clear.onChange(of: geometry.frame(in: .named("indexScrollView")).minY) {_, y in - if abs(y) <= 5 { - model.fixedDramaGroup = group - } - } - }) ForEach(group.items, id: \.id) { item in NavigationLink(destination: DetailView(id: item.id)) { @@ -481,12 +493,32 @@ extension IndexView { .cornerRadius(5) .padding(8) } + .background(GeometryReader { + geometry in + + let height = geometry.size.height + let minY = geometry.frame(in: .named("indexScrollView")).minY + let y = minY >= 0 ? minY : minY + height + + Color.clear + .preference(key: DramaGroupElementPreferenceKey.self, value: [ group.group_id : y]) + + }) } } } } } + // 元素的坐标的距离变化, [groupId : minY] + struct DramaGroupElementPreferenceKey: PreferenceKey { + static var defaultValue: [String: CGFloat] = [:] + + static func reduce(value: inout [String: CGFloat], nextValue: () -> [String: CGFloat]) { + value.merge(nextValue()) { $1 } + } + } + } #Preview { diff --git a/dimensionhub/Views/SearchView.swift b/dimensionhub/Views/SearchView.swift new file mode 100644 index 0000000..658d288 --- /dev/null +++ b/dimensionhub/Views/SearchView.swift @@ -0,0 +1,18 @@ +// +// SearchView.swift +// dimensionhub +// +// Created by 安礼成 on 2025/4/1. +// + +import SwiftUI + +struct SearchView: View { + var body: some View { + Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + } +} + +#Preview { + SearchView() +} diff --git a/dimensionhub/dimensionhubApp.swift b/dimensionhub/dimensionhubApp.swift index 7a1f2df..1d1b4b8 100644 --- a/dimensionhub/dimensionhubApp.swift +++ b/dimensionhub/dimensionhubApp.swift @@ -44,5 +44,6 @@ struct dimensionhubApp: App { .tint(.black) } .modelContainer(sharedModelContainer) + } }