解决系统通知时候的跳转问题
This commit is contained in:
parent
add63e503f
commit
c84ee6d630
@ -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)
|
||||
@ -33,12 +36,15 @@ final class AppNavigation: ObservableObject {
|
||||
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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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))
|
||||
|
||||
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
if urls.count > skipNum {
|
||||
let preloadUrls = urls.suffix(from: skipNum)
|
||||
Task.detached {
|
||||
try? await CacheManager.shared.preloadImages(urls: urls)
|
||||
try? await CacheManager.shared.preloadImages(urls: Array(preloadUrls))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user