From c84ee6d630f3a7befcf991a137a401e1b3540a69 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 18 Aug 2025 13:09:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=B3=BB=E7=BB=9F=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E6=97=B6=E5=80=99=E7=9A=84=E8=B7=B3=E8=BD=AC=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dimensionhub/Core/AppNavigation.swift | 32 +++++++++++++++---- dimensionhub/Core/CacheManager.swift | 2 +- dimensionhub/Views/Detail/DetailModel.swift | 20 ++++++------ dimensionhub/Views/Detail/DetailView.swift | 5 +-- .../Views/FollowList/FollowListView.swift | 2 +- dimensionhub/Views/Index/IndexMainView.swift | 2 +- dimensionhub/Views/Index/IndexModel.swift | 17 ++++++---- dimensionhub/Views/List/ListModel.swift | 2 +- dimensionhub/Views/Search/SearchView.swift | 2 +- dimensionhub/dimensionhubApp.swift | 31 ++---------------- 10 files changed, 54 insertions(+), 61 deletions(-) diff --git a/dimensionhub/Core/AppNavigation.swift b/dimensionhub/Core/AppNavigation.swift index dd36b78..880dfe6 100644 --- a/dimensionhub/Core/AppNavigation.swift +++ b/dimensionhub/Core/AppNavigation.swift @@ -13,14 +13,17 @@ final class AppNavigation: ObservableObject { static var shared = AppNavigation() enum Destination: Hashable { - case detail(id: Int) + case detail(id: Int, channelName: String?) case followList(num: Int) case search } @Published var path = NavigationPath() + let userId: String - @Published var targetDetailId: Int = 0 + init() { + self.userId = KeychainHelper.getPersistentUserId() + } func append(dest: Destination) { path.append(dest) @@ -32,13 +35,16 @@ final class AppNavigation: ObservableObject { let params = customData["params"] as? [String : AnyObject] else { return } - - path = NavigationPath() - DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in + + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [unowned self] in switch target { case "detail": - if let dramaId = params["drama_id"] as? Int { - self.targetDetailId = dramaId + if let dramaId = params["drama_id"] as? Int, + let channelName = params["channel_name"] as? String { + self.append(dest: .detail(id: dramaId, channelName: channelName)) + Task { + await Self.updateUserUnreadNum(userId: self.userId, dramaId: dramaId) + } } default: () @@ -46,4 +52,16 @@ final class AppNavigation: ObservableObject { } } + private static func updateUserUnreadNum(userId: String, dramaId: Int) async { + let response = await API.updateUserUnreadNum(userId: userId, dramaId: dramaId, as: Int.self) + switch response { + case .result(let newUnreadNum): + NSLog("updateUserUnreadNum success, new unread_num:\(newUnreadNum)") + Task {@MainActor in + try? await UNUserNotificationCenter.current().setBadgeCount(newUnreadNum) + } + case .error(let errorCode, let error): + NSLog("updateUserUnreadNum error: \(error), error_code: \(errorCode)") + } + } } diff --git a/dimensionhub/Core/CacheManager.swift b/dimensionhub/Core/CacheManager.swift index fb9e82b..abb6d14 100644 --- a/dimensionhub/Core/CacheManager.swift +++ b/dimensionhub/Core/CacheManager.swift @@ -21,7 +21,7 @@ final class CacheManager { // 预加载图片 func preloadImages(urls: [String]) async throws { - let preloadUrls = urls.filter { url in !self.fileExists(urlString: url)} + let preloadUrls = urls.filter { url in !self.fileExists(urlString: url)} guard preloadUrls.count > 0 else { return diff --git a/dimensionhub/Views/Detail/DetailModel.swift b/dimensionhub/Views/Detail/DetailModel.swift index 279d21e..4e52626 100644 --- a/dimensionhub/Views/Detail/DetailModel.swift +++ b/dimensionhub/Views/Detail/DetailModel.swift @@ -88,30 +88,28 @@ final class DetailModel { var selectedChannelIdx: Int = 0 var selectedEpisodes: [Episode] = [] - func loadData(userId: String, id: Int) async { + func loadData(userId: String, id: Int, channelName: String?) async { let response = await API.getDramaDetail(userId: userId, id: id, as: DramaDetailResponse.self) switch response { case .error(let code, let message): print(code) print(message) case .result(let detail): - preloadImages(channels: detail.channels) + //preloadImages(channels: detail.channels) await MainActor.run { self.name = detail.name self.summary = Utils.converHtmlToString(html: detail.summary) ?? "" self.thumb = detail.thumb - self.statuses = detail.status.flatMap { s in - if let status = DramaStatus(s) { - return [status] - } else { - return [] - } - } + self.statuses = detail.status.compactMap { DramaStatus($0) } + + let selectedIndex = channelName.flatMap { name in + detail.channels.firstIndex(where: {$0.name == name}) + } ?? 0 self.channels = detail.channels - self.selectedChannelIdx = 0 - self.selectedEpisodes = detail.channels[0].episodes + self.selectedChannelIdx = selectedIndex + self.selectedEpisodes = detail.channels[selectedIndex].episodes } } } diff --git a/dimensionhub/Views/Detail/DetailView.swift b/dimensionhub/Views/Detail/DetailView.swift index 8eaf229..fc067ef 100644 --- a/dimensionhub/Views/Detail/DetailView.swift +++ b/dimensionhub/Views/Detail/DetailView.swift @@ -17,6 +17,7 @@ struct DetailView: View { @State var errorInfo: (String, String) = ("", "") let id: Int + let channelName: String? var body: some View { VStack(alignment: .center) { @@ -118,7 +119,7 @@ struct DetailView: View { Alert(title: Text(self.errorInfo.0), message: Text(self.errorInfo.1), dismissButton: .default(Text("OK"))) } .task { - await detailModel.loadData(userId: self.userId, id: self.id) + await detailModel.loadData(userId: self.userId, id: self.id, channelName: self.channelName) } } @@ -201,5 +202,5 @@ extension DetailView { } #Preview { - DetailView(id: 19625) + DetailView(id: 19625, channelName: nil) } diff --git a/dimensionhub/Views/FollowList/FollowListView.swift b/dimensionhub/Views/FollowList/FollowListView.swift index f419282..8df83dc 100644 --- a/dimensionhub/Views/FollowList/FollowListView.swift +++ b/dimensionhub/Views/FollowList/FollowListView.swift @@ -71,7 +71,7 @@ extension FollowListView { var body: some View { VStack(alignment: .leading, spacing: 8) { - NavigationLink(destination: DetailView(id: dramaModel.drama.id)) { + NavigationLink(destination: DetailView(id: dramaModel.drama.id, channelName: nil)) { HStack { Text(dramaModel.drama.title) .font(.system(size: 20)) diff --git a/dimensionhub/Views/Index/IndexMainView.swift b/dimensionhub/Views/Index/IndexMainView.swift index c4a6eb0..e63eb44 100644 --- a/dimensionhub/Views/Index/IndexMainView.swift +++ b/dimensionhub/Views/Index/IndexMainView.swift @@ -314,7 +314,7 @@ extension IndexMainView { } .contentShape(Rectangle()) .onTapGesture { - appNav.append(dest: .detail(id: item.id)) + appNav.append(dest: .detail(id: item.id, channelName: nil)) } } } diff --git a/dimensionhub/Views/Index/IndexModel.swift b/dimensionhub/Views/Index/IndexModel.swift index 4ece504..9d0b8e9 100644 --- a/dimensionhub/Views/Index/IndexModel.swift +++ b/dimensionhub/Views/Index/IndexModel.swift @@ -192,7 +192,7 @@ final class IndexModel { case .error(let code, let message): print("index load data get error_code: \(code), message: \(message)") case .result(let result): - preloadGroupImages(groups: result.update_dramas) + preloadGroupImages(groups: result.update_dramas, skipNum: 4) self.updateDramaGroups = result.update_dramas self.fixedDramaGroup = result.update_dramas.first @@ -233,7 +233,7 @@ final class IndexModel { let response = await API.loadMoreUpdateDramas(userId: userId, mode: .next, id: lastId, as: [UpdateDramaGroup].self) if case let .result(groups) = response { if groups.count > 0 { - preloadGroupImages(groups: groups) + preloadGroupImages(groups: groups, skipNum: 0) displayDramaGroups(self.updateDramaGroups, label: "before") self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) @@ -261,7 +261,7 @@ final class IndexModel { let response = await API.loadMoreUpdateDramas(userId: userId, mode: .prev, id: firstId, as: [UpdateDramaGroup].self) if case let .result(groups) = response { if groups.count > 0 { - preloadGroupImages(groups: groups) + preloadGroupImages(groups: groups, skipNum: 0) displayDramaGroups(self.updateDramaGroups, label: "before") @@ -313,7 +313,7 @@ final class IndexModel { let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self) if case let .result(groups) = response { - preloadGroupImages(groups: groups) + preloadGroupImages(groups: groups, skipNum: 4) self.updateDramaGroups = groups self.dramaGroupElements = transformUpdateDramaGroups(groups: self.updateDramaGroups) @@ -362,7 +362,7 @@ final class IndexModel { } // 预加载图片信息 - private func preloadGroupImages(groups: [UpdateDramaGroup]) { + private func preloadGroupImages(groups: [UpdateDramaGroup], skipNum: Int) { let urls = groups.flatMap { group in return group.items.map {item in item.thumb} } @@ -371,8 +371,11 @@ final class IndexModel { return } - Task.detached { - try? await CacheManager.shared.preloadImages(urls: urls) + if urls.count > skipNum { + let preloadUrls = urls.suffix(from: skipNum) + Task.detached { + try? await CacheManager.shared.preloadImages(urls: Array(preloadUrls)) + } } } diff --git a/dimensionhub/Views/List/ListModel.swift b/dimensionhub/Views/List/ListModel.swift index 18a5334..cdf4e4c 100644 --- a/dimensionhub/Views/List/ListModel.swift +++ b/dimensionhub/Views/List/ListModel.swift @@ -52,7 +52,7 @@ final class ListModel { print(code) print(message) case .result(let detail): - self.preloadImages(channels: detail.channels) + //self.preloadImages(channels: detail.channels) await MainActor.run { self.name = detail.name diff --git a/dimensionhub/Views/Search/SearchView.swift b/dimensionhub/Views/Search/SearchView.swift index 4166896..b30aa76 100644 --- a/dimensionhub/Views/Search/SearchView.swift +++ b/dimensionhub/Views/Search/SearchView.swift @@ -101,7 +101,7 @@ struct SearchDramaGroupView: View { var body: some View { LazyVStack(alignment: .center, spacing: 10) { ForEach(group.items, id: \.id) { item in - NavigationLink(destination: DetailView(id: item.id)) { + NavigationLink(destination: DetailView(id: item.id, channelName: nil)) { FlexImage(urlString: item.thumb, width: 370, height: 180, placeholder: "ph_img_big") .frame(width: 370, height: 180) .overlay(alignment: .topLeading) { diff --git a/dimensionhub/dimensionhubApp.swift b/dimensionhub/dimensionhubApp.swift index aa38585..8c2a8d0 100644 --- a/dimensionhub/dimensionhubApp.swift +++ b/dimensionhub/dimensionhubApp.swift @@ -49,29 +49,14 @@ struct dimensionhubApp: App { IndexView() .navigationDestination(for: AppNavigation.Destination.self) { dest in switch dest { - case .detail(id: let id): - DetailView(id: id) + case .detail(id: let id, channelName: let channelName): + DetailView(id: id, channelName: channelName) case .followList(num: let num): FollowListView(num: num) case .search: SearchView() } } - .onChange(of: appNav.targetDetailId) { _, dramaId in - if dramaId > 0 { - DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { - withAnimation { - appNav.append(dest: .detail(id: dramaId)) - } - - Task { - await self.updateUserUnreadNum(userId: self.userId, dramaId: dramaId) - } - - appNav.targetDetailId = 0 - } - } - } } .navigationViewStyle(.stack) .tint(.black) @@ -82,18 +67,6 @@ struct dimensionhubApp: App { .modelContainer(sharedModelContainer) } - private func updateUserUnreadNum(userId: String, dramaId: Int) async { - let response = await API.updateUserUnreadNum(userId: userId, dramaId: dramaId, as: Int.self) - switch response { - case .result(let newUnreadNum): - NSLog("updateUserUnreadNum success, new unread_num:\(newUnreadNum)") - Task {@MainActor in - try? await UNUserNotificationCenter.current().setBadgeCount(newUnreadNum) - } - case .error(let errorCode, let error): - NSLog("updateUserUnreadNum error: \(error), error_code: \(errorCode)") - } - } } class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {