D
D
Dima Grib2020-12-14 00:34:42
Swift
Dima Grib, 2020-12-14 00:34:42

How to update dynamically string in mvc model?

I get links every minute in a different number
How to correctly update them in Services in order to display media files in

view 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. HomeViewModel
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. In this class, you need to receive dynamic strings from the structure
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. Structure
struct GetManager {
        static func showContent(pasteboard: String) {
        switch onlyLinkAbsoluteString.validURL {
               case true:
        // Ссылки приходят тут
        }
        }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
briahas, 2020-12-17
@briahas

Try to create a simple project that will simply receive different text and send it to another object. No conversions. And when it works for you, make the main project in its likeness.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question