Я получаю ссылки каждую минуту в разном кол-ве
Как корректно обновлять их в Services, что бы выводить в вью медиа файлы
1. View
var viewModel = HomeViewModel()
func configureViewModel() {
viewModel.resultClosure = { [weak self] listModel in
guard let image = listModel.uiImage else { return }
self?.updateImageViewConstraint(nil, image: image)
}
}
2. HomveViewModel
class HomeViewModel {
var service = Services()
var listModel = [ListModel]() {
didSet {
DispatchQueue.main.async { [weak self] in
guard let self = self else {
return
}
self.resultClosure?(self.listModel[0])
}
}
}
let placeholderImage = UIImage(named: "placeholder")
var updatedImge : UIImage?
var resultClosure: ((ListModel) -> Void)?
var stringUrl: StringUrl!
func fetchData() {
service.makeRequest() { (result: Result<[ListModel]>) in
switch result {
case .success(let listModel):
self.listModel = listModel
return
case .failure(let error):
print("\n HomeViewModel fetchData failure \(error) \n")
return
}
}
}
}
3. Services. В этом классе необходимо получать динамические стринги с структуры
class Services {
let urlSession: URLSession
let imageProvider = ImageProvider()
let dynamicLinkfromStruct = "Dynamic Link From Struct!!!"
init(urlSession: URLSession = .shared) {
self.urlSession = urlSession
}
func makeRequest(completionHandler: @escaping (Result<[ListModel]>) -> Void) {
guard let url = URL(string: dynamicLinkfromStruct) else { return }
imageProvider.loadImages(from: url) { (image) in
do {
let result = [ListModel.init(imageUrl: self.url, uiImage: image)]
completionHandler(.success(result))
} catch {
completionHandler(.failure(error ))
}
}
}
}
4. Model
struct ListModel {
let imageUrl: String?
let uiImage: UIImage?
}
5. Struct
struct GetManager {
static func showContent(pasteboard: String) {
switch onlyLinkAbsoluteString.validURL {
case true:
// Ссылки приходят тут
}
}
}