避免阻塞主线程
This commit is contained in:
parent
7753040a6f
commit
010ee0d38c
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
// 增加了一个缓存的逻辑
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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 = []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user