Music_Player3/relax.offline.mp3.music/MP/MPPositive/Models/ViewModels/LoadViewModels/MPPositive_SearchResultsLoadViewModel.swift
2024-09-06 18:21:15 +08:00

57 lines
1.7 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_LoadSearchResultsViewModel.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/5/13.
//
import UIKit
///
class MPPositive_SearchResultsLoadViewModel: NSObject {
///
var sectionLists:[MPPositive_SearchResultListViewModel]!{
didSet{
DispatchQueue.main.async {
[weak self] in
guard let self = self else {return}
//
if let block = resultReloadBlock {
block()
}
}
}
}
var resultReloadBlock:(() -> Void)?
//
init(_ text:String){
super.init()
getSearchResults(text)
}
//
private func getSearchResults(_ text:String) {
MP_HUD.loading()
//
MPPositive_SearchTagModel.fetch(predicate: .init(format: "text==%@", text)) { results in
if results.count == 0 {
//
//
let tag = try? MPPositive_SearchTagModel.create()
tag?.date = Date()
tag?.text = text
MPPositive_SearchTagModel.save()
}else if let first = results.first {
//
first.date = Date()
MPPositive_SearchTagModel.save()
}
}
MP_NetWorkManager.shared.requestSearchPreviewResults(text) { [weak self] results in
guard let self = self else {return}
MP_HUD.hideNow()
sectionLists = results
}
}
}