避免阻塞主线程
This commit is contained in:
parent
7753040a6f
commit
010ee0d38c
@ -35,9 +35,11 @@ final class DateNavModel {
|
|||||||
self.dateModels = []
|
self.dateModels = []
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func loadDateCells(userId: String) async {
|
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 selectedChannelIdx: Int = 0
|
||||||
var selectedEpisodes: [Episode] = []
|
var selectedEpisodes: [Episode] = []
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func loadData(userId: String, id: Int) async {
|
func loadData(userId: String, id: Int) async {
|
||||||
let response = await API.getDramaDetail(userId: userId, id: id, as: DramaDetailResponse.self)
|
let response = await API.getDramaDetail(userId: userId, id: id, as: DramaDetailResponse.self)
|
||||||
switch response {
|
switch response {
|
||||||
@ -94,6 +93,7 @@ final class DetailModel {
|
|||||||
print(code)
|
print(code)
|
||||||
print(message)
|
print(message)
|
||||||
case .result(let detail):
|
case .result(let detail):
|
||||||
|
await MainActor.run {
|
||||||
self.name = detail.name
|
self.name = detail.name
|
||||||
self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
|
self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
|
||||||
self.thumb = detail.thumb
|
self.thumb = detail.thumb
|
||||||
@ -110,14 +110,13 @@ final class DetailModel {
|
|||||||
self.selectedEpisodes = detail.channels[0].episodes
|
self.selectedEpisodes = detail.channels[0].episodes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func toggleChannel(channelIdx: Int) {
|
func toggleChannel(channelIdx: Int) {
|
||||||
self.selectedChannelIdx = channelIdx
|
self.selectedChannelIdx = channelIdx
|
||||||
self.selectedEpisodes = self.channels[channelIdx].episodes
|
self.selectedEpisodes = self.channels[channelIdx].episodes
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func onTapFollowButton(userId: String, id: Int, status: String) async -> FollowResult {
|
func onTapFollowButton(userId: String, id: Int, status: String) async -> FollowResult {
|
||||||
let response = await API.followDrama(userId: userId, id: id, status: status, as: [String].self)
|
let response = await API.followDrama(userId: userId, id: id, status: status, as: [String].self)
|
||||||
switch response {
|
switch response {
|
||||||
@ -198,6 +197,7 @@ struct DetailView: View {
|
|||||||
case .success:
|
case .success:
|
||||||
()
|
()
|
||||||
case .error(let title, let message):
|
case .error(let title, let message):
|
||||||
|
DispatchQueue.main.async {
|
||||||
self.errorInfo = (title, message)
|
self.errorInfo = (title, message)
|
||||||
self.showAlert = true
|
self.showAlert = true
|
||||||
}
|
}
|
||||||
@ -205,6 +205,7 @@ struct DetailView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 渠道列表
|
// 渠道列表
|
||||||
HStack(alignment: .center, spacing: 15) {
|
HStack(alignment: .center, spacing: 15) {
|
||||||
@ -213,9 +214,11 @@ struct DetailView: View {
|
|||||||
.font(.system(size: 13))
|
.font(.system(size: 13))
|
||||||
.foregroundColor(idx == detailModel.selectedChannelIdx ? Color(hex: "#169BD5") : Color(hex: "#666666"))
|
.foregroundColor(idx == detailModel.selectedChannelIdx ? Color(hex: "#169BD5") : Color(hex: "#666666"))
|
||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
|
DispatchQueue.main.async {
|
||||||
detailModel.toggleChannel(channelIdx: idx)
|
detailModel.toggleChannel(channelIdx: idx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
.padding(.leading, 10)
|
.padding(.leading, 10)
|
||||||
|
|||||||
@ -40,16 +40,17 @@ final class FollowListModel {
|
|||||||
self.dramas = []
|
self.dramas = []
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func loadData(userId: String) async {
|
func loadData(userId: String) async {
|
||||||
let response = await API.getUserFollowList(userId: userId, as: FavorResponse.self)
|
let response = await API.getUserFollowList(userId: userId, as: FavorResponse.self)
|
||||||
switch response {
|
switch response {
|
||||||
case .error(let code, let message):
|
case .error(let code, let message):
|
||||||
print("index load data get error_code: \(code), message: \(message)")
|
print("index load data get error_code: \(code), message: \(message)")
|
||||||
case .result(let result):
|
case .result(let result):
|
||||||
|
await MainActor.run {
|
||||||
self.dramas = result.dramas
|
self.dramas = result.dramas
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FollowListView: View {
|
struct FollowListView: View {
|
||||||
|
|||||||
@ -66,7 +66,6 @@ final class IndexModel {
|
|||||||
self.selectedDate = ""
|
self.selectedDate = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func loadData(userId: String) async {
|
func loadData(userId: String) async {
|
||||||
guard !isLoaded else {
|
guard !isLoaded else {
|
||||||
return
|
return
|
||||||
@ -77,10 +76,12 @@ final class IndexModel {
|
|||||||
case .error(let code, let message):
|
case .error(let code, let message):
|
||||||
print("index load data get error_code: \(code), message: \(message)")
|
print("index load data get error_code: \(code), message: \(message)")
|
||||||
case .result(let result):
|
case .result(let result):
|
||||||
|
await MainActor.run {
|
||||||
self.updateDramaGroups = result.update_dramas
|
self.updateDramaGroups = result.update_dramas
|
||||||
self.fixedDramaGroup = result.update_dramas.first
|
self.fixedDramaGroup = result.update_dramas.first
|
||||||
self.follow_num = result.follow_num >= 100 ? "99+" : "\(result.follow_num)"
|
self.follow_num = result.follow_num >= 100 ? "99+" : "\(result.follow_num)"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
self.isLoaded = true
|
self.isLoaded = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +92,6 @@ final class IndexModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func loadMoreUpdateDramas(userId: String, mode: API.LoadMode) async {
|
func loadMoreUpdateDramas(userId: String, mode: API.LoadMode) async {
|
||||||
// 按照id来判断不一定正确,需要借助其他值
|
// 按照id来判断不一定正确,需要借助其他值
|
||||||
let dramaIds = self.getDramaIds(self.updateDramaGroups)
|
let dramaIds = self.getDramaIds(self.updateDramaGroups)
|
||||||
@ -107,7 +107,9 @@ final class IndexModel {
|
|||||||
|
|
||||||
print("--------- before ------------")
|
print("--------- before ------------")
|
||||||
displayDramaGroups(self.updateDramaGroups)
|
displayDramaGroups(self.updateDramaGroups)
|
||||||
|
await MainActor.run {
|
||||||
self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
|
self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
|
||||||
|
}
|
||||||
print("--------- after ------------")
|
print("--------- after ------------")
|
||||||
displayDramaGroups(self.updateDramaGroups)
|
displayDramaGroups(self.updateDramaGroups)
|
||||||
print("--------- ------------")
|
print("--------- ------------")
|
||||||
@ -121,7 +123,9 @@ final class IndexModel {
|
|||||||
if groups.count > 0 {
|
if groups.count > 0 {
|
||||||
print("--------- before ------------")
|
print("--------- before ------------")
|
||||||
displayDramaGroups(self.updateDramaGroups)
|
displayDramaGroups(self.updateDramaGroups)
|
||||||
|
await MainActor.run {
|
||||||
self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
|
self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
|
||||||
|
}
|
||||||
|
|
||||||
print("----------after-----------")
|
print("----------after-----------")
|
||||||
displayDramaGroups(self.updateDramaGroups)
|
displayDramaGroups(self.updateDramaGroups)
|
||||||
@ -133,15 +137,16 @@ final class IndexModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 指定日期,并更新日期下对应的数据
|
// 指定日期,并更新日期下对应的数据
|
||||||
@MainActor
|
|
||||||
func loadDateUpdateDramas(userId: String, date: String) async {
|
func loadDateUpdateDramas(userId: String, date: String) async {
|
||||||
self.updateDramaGroups.removeAll()
|
self.updateDramaGroups.removeAll()
|
||||||
let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self)
|
let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self)
|
||||||
if case let .result(groups) = response {
|
if case let .result(groups) = response {
|
||||||
|
await MainActor.run {
|
||||||
self.updateDramaGroups = groups
|
self.updateDramaGroups = groups
|
||||||
self.fixedDramaGroup = groups.first
|
self.fixedDramaGroup = groups.first
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 合并groups
|
// 合并groups
|
||||||
private func preappendMergeDramaGroups(groups: [UpdateDramaGroup], mergeGroups: [UpdateDramaGroup]) -> [UpdateDramaGroup] {
|
private func preappendMergeDramaGroups(groups: [UpdateDramaGroup], mergeGroups: [UpdateDramaGroup]) -> [UpdateDramaGroup] {
|
||||||
|
|||||||
@ -45,7 +45,6 @@ final class ListModel {
|
|||||||
var selectedChannelIdx: Int = 0
|
var selectedChannelIdx: Int = 0
|
||||||
var selectedEpisodes: [Episode] = []
|
var selectedEpisodes: [Episode] = []
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func loadData(userId: String, id: Int) async {
|
func loadData(userId: String, id: Int) async {
|
||||||
let response = await API.getDramaDetail(userId: userId, id: id, as: DramaDetailResponse.self)
|
let response = await API.getDramaDetail(userId: userId, id: id, as: DramaDetailResponse.self)
|
||||||
switch response {
|
switch response {
|
||||||
@ -53,6 +52,7 @@ final class ListModel {
|
|||||||
print(code)
|
print(code)
|
||||||
print(message)
|
print(message)
|
||||||
case .result(let detail):
|
case .result(let detail):
|
||||||
|
await MainActor.run {
|
||||||
self.name = detail.name
|
self.name = detail.name
|
||||||
self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
|
self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
|
||||||
self.thumb = detail.thumb
|
self.thumb = detail.thumb
|
||||||
@ -62,6 +62,7 @@ final class ListModel {
|
|||||||
self.selectedEpisodes = detail.channels[0].episodes
|
self.selectedEpisodes = detail.channels[0].episodes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func toggleChannel(channelIdx: Int) {
|
func toggleChannel(channelIdx: Int) {
|
||||||
self.selectedChannelIdx = channelIdx
|
self.selectedChannelIdx = channelIdx
|
||||||
|
|||||||
@ -29,7 +29,7 @@ struct SearchView: View {
|
|||||||
Spacer()
|
Spacer()
|
||||||
|
|
||||||
SearchBar(searchText: $searchText, placeholder: "搜索...") {
|
SearchBar(searchText: $searchText, placeholder: "搜索...") {
|
||||||
Task { @MainActor in
|
Task {
|
||||||
let trimmedSearchText = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
|
let trimmedSearchText = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
|
||||||
if !trimmedSearchText.isEmpty {
|
if !trimmedSearchText.isEmpty {
|
||||||
@ -136,15 +136,15 @@ extension SearchView {
|
|||||||
HStack(alignment: .center, spacing: 8) {
|
HStack(alignment: .center, spacing: 8) {
|
||||||
TextField(placeholder, text: $searchText)
|
TextField(placeholder, text: $searchText)
|
||||||
.padding(8)
|
.padding(8)
|
||||||
.padding(.horizontal, 30)
|
//.padding(.horizontal, 30)
|
||||||
.background(Color(.systemGray6))
|
.background(Color(.systemGray6))
|
||||||
.cornerRadius(8)
|
.cornerRadius(8)
|
||||||
.overlay(
|
.overlay(alignment: .trailing) {
|
||||||
HStack {
|
HStack {
|
||||||
Image(systemName: "magnifyingglass")
|
// Image(systemName: "magnifyingglass")
|
||||||
.foregroundColor(.gray)
|
// .foregroundColor(.gray)
|
||||||
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
// .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
||||||
.padding(.leading, 8)
|
// .padding(.leading, 8)
|
||||||
|
|
||||||
if isSearching {
|
if isSearching {
|
||||||
Button(action: {
|
Button(action: {
|
||||||
@ -156,7 +156,9 @@ extension SearchView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
.border(Color.red)
|
||||||
|
|
||||||
|
}
|
||||||
.frame(maxWidth: .infinity)
|
.frame(maxWidth: .infinity)
|
||||||
.onChange(of: searchText) {
|
.onChange(of: searchText) {
|
||||||
if searchText.trimmingCharacters(in: .whitespaces).isEmpty {
|
if searchText.trimmingCharacters(in: .whitespaces).isEmpty {
|
||||||
@ -199,7 +201,6 @@ final class SearchModel {
|
|||||||
|
|
||||||
var dramaGroups: [DramaGroup] = []
|
var dramaGroups: [DramaGroup] = []
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func search(userId: String, name: String) async {
|
func search(userId: String, name: String) async {
|
||||||
guard let encodeName = name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
|
guard let encodeName = name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
|
||||||
self.dramaGroups = []
|
self.dramaGroups = []
|
||||||
@ -210,12 +211,16 @@ final class SearchModel {
|
|||||||
|
|
||||||
switch response {
|
switch response {
|
||||||
case .result(let dramaGroups):
|
case .result(let dramaGroups):
|
||||||
|
await MainActor.run {
|
||||||
self.dramaGroups = dramaGroups
|
self.dramaGroups = dramaGroups
|
||||||
|
}
|
||||||
case .error(let int32, let string):
|
case .error(let int32, let string):
|
||||||
print("error_code: \(int32), message: \(string)")
|
print("error_code: \(int32), message: \(string)")
|
||||||
|
await MainActor.run {
|
||||||
self.dramaGroups = []
|
self.dramaGroups = []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user