避免阻塞主线程

This commit is contained in:
anlicheng 2025-04-02 10:55:39 +08:00
parent 7753040a6f
commit 010ee0d38c
6 changed files with 70 additions and 53 deletions

View File

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

View File

@ -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,6 +93,7 @@ final class DetailModel {
print(code)
print(message)
case .result(let detail):
await MainActor.run {
self.name = detail.name
self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
self.thumb = detail.thumb
@ -110,14 +110,13 @@ final class DetailModel {
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,6 +197,7 @@ struct DetailView: View {
case .success:
()
case .error(let title, let message):
DispatchQueue.main.async {
self.errorInfo = (title, message)
self.showAlert = true
}
@ -205,6 +205,7 @@ struct DetailView: View {
}
}
}
}
//
HStack(alignment: .center, spacing: 15) {
@ -213,9 +214,11 @@ struct DetailView: View {
.font(.system(size: 13))
.foregroundColor(idx == detailModel.selectedChannelIdx ? Color(hex: "#169BD5") : Color(hex: "#666666"))
.onTapGesture {
DispatchQueue.main.async {
detailModel.toggleChannel(channelIdx: idx)
}
}
}
Spacer()
}
.padding(.leading, 10)

View File

@ -40,17 +40,18 @@ 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):
await MainActor.run {
self.dramas = result.dramas
}
}
}
}
struct FollowListView: View {
@AppStorage("userId") private var userId: String = Utils.defaultUserId()

View File

@ -66,7 +66,6 @@ final class IndexModel {
self.selectedDate = ""
}
@MainActor
func loadData(userId: String) async {
guard !isLoaded else {
return
@ -77,10 +76,12 @@ final class IndexModel {
case .error(let code, let message):
print("index load data get error_code: \(code), message: \(message)")
case .result(let result):
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)
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)
await MainActor.run {
self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
}
print("----------after-----------")
displayDramaGroups(self.updateDramaGroups)
@ -133,15 +137,16 @@ 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 {
await MainActor.run {
self.updateDramaGroups = groups
self.fixedDramaGroup = groups.first
}
}
}
// groups
private func preappendMergeDramaGroups(groups: [UpdateDramaGroup], mergeGroups: [UpdateDramaGroup]) -> [UpdateDramaGroup] {

View File

@ -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,6 +52,7 @@ final class ListModel {
print(code)
print(message)
case .result(let detail):
await MainActor.run {
self.name = detail.name
self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
self.thumb = detail.thumb
@ -62,6 +62,7 @@ final class ListModel {
self.selectedEpisodes = detail.channels[0].episodes
}
}
}
func toggleChannel(channelIdx: Int) {
self.selectedChannelIdx = channelIdx

View File

@ -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,13 +211,17 @@ final class SearchModel {
switch response {
case .result(let dramaGroups):
await MainActor.run {
self.dramaGroups = dramaGroups
}
case .error(let int32, let string):
print("error_code: \(int32), message: \(string)")
await MainActor.run {
self.dramaGroups = []
}
}
}
}
#Preview {
SearchView()