diff --git a/dimensionhub/Views/DateNavView.swift b/dimensionhub/Views/DateNavView.swift index a126aec..2416bfe 100644 --- a/dimensionhub/Views/DateNavView.swift +++ b/dimensionhub/Views/DateNavView.swift @@ -35,9 +35,11 @@ final class DateNavModel { self.dateModels = [] } - @MainActor func loadDateCells(userId: String) async { - self.dateModels = await getDateModelData(userId: userId) + let dateModels = await getDateModelData(userId: userId) + await MainActor.run { + self.dateModels = dateModels + } } // 增加了一个缓存的逻辑 diff --git a/dimensionhub/Views/DetailView.swift b/dimensionhub/Views/DetailView.swift index 370b646..2e2627e 100644 --- a/dimensionhub/Views/DetailView.swift +++ b/dimensionhub/Views/DetailView.swift @@ -86,7 +86,6 @@ final class DetailModel { var selectedChannelIdx: Int = 0 var selectedEpisodes: [Episode] = [] - @MainActor func loadData(userId: String, id: Int) async { let response = await API.getDramaDetail(userId: userId, id: id, as: DramaDetailResponse.self) switch response { @@ -94,30 +93,30 @@ final class DetailModel { print(code) print(message) case .result(let detail): - 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.channels = detail.channels - - self.selectedChannelIdx = 0 - self.selectedEpisodes = detail.channels[0].episodes + 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.channels = detail.channels + + self.selectedChannelIdx = 0 + self.selectedEpisodes = detail.channels[0].episodes + } } } - @MainActor func toggleChannel(channelIdx: Int) { self.selectedChannelIdx = channelIdx self.selectedEpisodes = self.channels[channelIdx].episodes } - @MainActor func onTapFollowButton(userId: String, id: Int, status: String) async -> FollowResult { let response = await API.followDrama(userId: userId, id: id, status: status, as: [String].self) switch response { @@ -198,8 +197,10 @@ struct DetailView: View { case .success: () case .error(let title, let message): - self.errorInfo = (title, message) - self.showAlert = true + DispatchQueue.main.async { + self.errorInfo = (title, message) + self.showAlert = true + } } } } @@ -213,7 +214,9 @@ struct DetailView: View { .font(.system(size: 13)) .foregroundColor(idx == detailModel.selectedChannelIdx ? Color(hex: "#169BD5") : Color(hex: "#666666")) .onTapGesture { - detailModel.toggleChannel(channelIdx: idx) + DispatchQueue.main.async { + detailModel.toggleChannel(channelIdx: idx) + } } } Spacer() diff --git a/dimensionhub/Views/FollowListView.swift b/dimensionhub/Views/FollowListView.swift index 55932e6..bf13cc4 100644 --- a/dimensionhub/Views/FollowListView.swift +++ b/dimensionhub/Views/FollowListView.swift @@ -40,14 +40,15 @@ final class FollowListModel { self.dramas = [] } - @MainActor func loadData(userId: String) async { let response = await API.getUserFollowList(userId: userId, as: FavorResponse.self) switch response { case .error(let code, let message): print("index load data get error_code: \(code), message: \(message)") case .result(let result): - self.dramas = result.dramas + await MainActor.run { + self.dramas = result.dramas + } } } } diff --git a/dimensionhub/Views/IndexView.swift b/dimensionhub/Views/IndexView.swift index bc98052..00bd7b9 100644 --- a/dimensionhub/Views/IndexView.swift +++ b/dimensionhub/Views/IndexView.swift @@ -66,7 +66,6 @@ final class IndexModel { self.selectedDate = "" } - @MainActor func loadData(userId: String) async { guard !isLoaded else { return @@ -77,9 +76,11 @@ final class IndexModel { case .error(let code, let message): print("index load data get error_code: \(code), message: \(message)") case .result(let result): - self.updateDramaGroups = result.update_dramas - self.fixedDramaGroup = result.update_dramas.first - self.follow_num = result.follow_num >= 100 ? "99+" : "\(result.follow_num)" + await MainActor.run { + self.updateDramaGroups = result.update_dramas + self.fixedDramaGroup = result.update_dramas.first + self.follow_num = result.follow_num >= 100 ? "99+" : "\(result.follow_num)" + } } self.isLoaded = true } @@ -91,7 +92,6 @@ final class IndexModel { } } - @MainActor func loadMoreUpdateDramas(userId: String, mode: API.LoadMode) async { // 按照id来判断不一定正确,需要借助其他值 let dramaIds = self.getDramaIds(self.updateDramaGroups) @@ -107,7 +107,9 @@ final class IndexModel { print("--------- before ------------") displayDramaGroups(self.updateDramaGroups) - self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) + await MainActor.run { + self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) + } print("--------- after ------------") displayDramaGroups(self.updateDramaGroups) print("--------- ------------") @@ -121,7 +123,9 @@ final class IndexModel { if groups.count > 0 { print("--------- before ------------") displayDramaGroups(self.updateDramaGroups) - self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) + await MainActor.run { + self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups) + } print("----------after-----------") displayDramaGroups(self.updateDramaGroups) @@ -133,13 +137,14 @@ final class IndexModel { } // 指定日期,并更新日期下对应的数据 - @MainActor func loadDateUpdateDramas(userId: String, date: String) async { self.updateDramaGroups.removeAll() 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 + await MainActor.run { + self.updateDramaGroups = groups + self.fixedDramaGroup = groups.first + } } } diff --git a/dimensionhub/Views/ListView.swift b/dimensionhub/Views/ListView.swift index 9be056d..387b871 100644 --- a/dimensionhub/Views/ListView.swift +++ b/dimensionhub/Views/ListView.swift @@ -45,7 +45,6 @@ final class ListModel { var selectedChannelIdx: Int = 0 var selectedEpisodes: [Episode] = [] - @MainActor func loadData(userId: String, id: Int) async { let response = await API.getDramaDetail(userId: userId, id: id, as: DramaDetailResponse.self) switch response { @@ -53,13 +52,15 @@ final class ListModel { print(code) print(message) case .result(let detail): - self.name = detail.name - self.summary = Utils.converHtmlToString(html: detail.summary) ?? "" - self.thumb = detail.thumb - self.channels = detail.channels - - self.selectedChannelIdx = 0 - self.selectedEpisodes = detail.channels[0].episodes + await MainActor.run { + self.name = detail.name + self.summary = Utils.converHtmlToString(html: detail.summary) ?? "" + self.thumb = detail.thumb + self.channels = detail.channels + + self.selectedChannelIdx = 0 + self.selectedEpisodes = detail.channels[0].episodes + } } } diff --git a/dimensionhub/Views/SearchView.swift b/dimensionhub/Views/SearchView.swift index 8e44dca..6d713f7 100644 --- a/dimensionhub/Views/SearchView.swift +++ b/dimensionhub/Views/SearchView.swift @@ -29,7 +29,7 @@ struct SearchView: View { Spacer() SearchBar(searchText: $searchText, placeholder: "搜索...") { - Task { @MainActor in + Task { let trimmedSearchText = searchText.trimmingCharacters(in: .whitespacesAndNewlines) if !trimmedSearchText.isEmpty { @@ -136,15 +136,15 @@ extension SearchView { HStack(alignment: .center, spacing: 8) { TextField(placeholder, text: $searchText) .padding(8) - .padding(.horizontal, 30) + //.padding(.horizontal, 30) .background(Color(.systemGray6)) .cornerRadius(8) - .overlay( + .overlay(alignment: .trailing) { HStack { - Image(systemName: "magnifyingglass") - .foregroundColor(.gray) - .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) - .padding(.leading, 8) + // Image(systemName: "magnifyingglass") + // .foregroundColor(.gray) + // .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) + // .padding(.leading, 8) if isSearching { Button(action: { @@ -156,7 +156,9 @@ extension SearchView { } } } - ) + .border(Color.red) + + } .frame(maxWidth: .infinity) .onChange(of: searchText) { if searchText.trimmingCharacters(in: .whitespaces).isEmpty { @@ -199,7 +201,6 @@ final class SearchModel { var dramaGroups: [DramaGroup] = [] - @MainActor func search(userId: String, name: String) async { guard let encodeName = name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { self.dramaGroups = [] @@ -210,10 +211,14 @@ final class SearchModel { switch response { case .result(let dramaGroups): - self.dramaGroups = dramaGroups + await MainActor.run { + self.dramaGroups = dramaGroups + } case .error(let int32, let string): print("error_code: \(int32), message: \(string)") - self.dramaGroups = [] + await MainActor.run { + self.dramaGroups = [] + } } } }