Music_Player3/relax.offline.mp3.music/MP/MPPositive/Models/ViewModels/LoadViewModels/MPPositive_BrowseLoadViewModel.swift
2024-06-15 23:52:09 +08:00

110 lines
4.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MPPositive_LoadMusicModel.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/4/25.
//
import UIKit
///bload
class MPPositive_BrowseLoadViewModel: NSObject {
static let shared = MPPositive_BrowseLoadViewModel()
///()
var browseModuleLists:[MPPositive_BrowseModuleListViewModel] = []
override init() {
super.init()
//browse
MP_NetWorkManager.shared.browseRequestStateBlock = {
[weak self] (lists,isCompleted) in
guard let self = self else {return}
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
//
browseModuleLists.append(contentsOf: lists)
//
browseModuleLists = browseModuleLists.filter{($0.items.count != 0)}
//UI
NotificationCenter.notificationKey.post(notificationName: .positive_browses_reload)
}
if isCompleted == true {
//
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
[weak self] in
guard let self = self else {
return
}
//
if let index = browseModuleLists.firstIndex(where: {$0.items.first?.browseItem.pageType == "MUSIC_VIDEO_TYPE_OMV"}) {
//
let removedElement = browseModuleLists.remove(at: index)
//
browseModuleLists.append(removedElement)
}
NotificationCenter.notificationKey.post(notificationName: .positive_browses_reload)
})
//
MP_AnalyticsManager.shared.home_b_module_showsucces_actionAction()
//
cacheResponseData(browseModuleLists)
}
}
//error
MP_NetWorkManager.shared.browseRequestErrorBlock = {
[weak self] in
guard let self = self else {return}
//
guard let array = chachedData() else {
//
NotificationCenter.notificationKey.post(notificationName: .netWork_error_deal)
return
}
//
browseModuleLists = array
//UI
NotificationCenter.notificationKey.post(notificationName: .positive_browses_reload)
}
}
///
func reloadBrowseLists() {
//
browseModuleLists.removeAll()
//
MP_NetWorkManager.shared.requestBrowseDatas()
}
///
private func cacheResponseData(_ array: [MPPositive_BrowseModuleListViewModel]) {
guard browseModuleLists.count != 0 else {
print("Home Browse数据未加载无法缓存")
return
}
//jsonData
do{
let jsonData = try JSONEncoder().encode(array)
//使UserDefaults
UserDefaults.standard.set(jsonData, forKey: "HomeBrowse")
print("已经将Home Browse数据缓存")
}catch{
//jsonData
print("Home Browse数据转为Data失败失败原因\(error)")
}
}
//
private func chachedData() -> [MPPositive_BrowseModuleListViewModel]? {
guard let cacheData = UserDefaults.standard.data(forKey: "HomeBrowse") else {
print("获取Home Browse缓存数据失败")
return nil
}
//cacheData[MPPositive_BrowseModuleListViewModel]
do {
let array:[MPPositive_BrowseModuleListViewModel] = try JSONDecoder().decode([MPPositive_BrowseModuleListViewModel].self, from: cacheData)
print("已经将Home Browse缓存数据取出")
return array
} catch {
//jsonData
print("获取Home Browse缓存数据失败失败原因\(error)")
return nil
}
}
}