Created
June 7, 2017 14:04
-
-
Save Savchukv/90384ce4d3326c873a76c51bc9f4dd25 to your computer and use it in GitHub Desktop.
Example viewModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RandomMusicViewModel.swift | |
// | |
// Created by Vasiliy Savchuk on 2/6/17. | |
// Copyright © 2017 All rights reserved. | |
// | |
class RandomMusicViewModel: ViewModelTitleProtocol, ViewModelProtocol, ViewModelTypeProtocol { | |
var dataSource: Array<JSONDecodable> = Array() | |
var type: CellType = .randomMusicCell | |
private let backendService: BackendService = BackendService() | |
var canLoad = true | |
var perPage = 10 | |
var page = 1 | |
var imageUrl: URL { | |
let track: Track = self.dataSource.first as! Track | |
if let album = track.albums.first { return URL(string: API.url + album.cover.thumblUrlX3)! } | |
return URL(string: API.url + "")! | |
} | |
//MARK: - Lifecicle Object | |
init() { | |
NotificationCenter.observe(self, selector: #selector(loadNext(notification:)), name: Notification.Name.Player.loadNext) | |
} | |
//MARK: - Methods | |
func loadDataFromDB() -> Results<Object>? { | |
return nil | |
} | |
func load(perPage: Int = 10, page: Int = 1, handler:@escaping () -> Void) { | |
if canLoad == false { return } | |
let existIds = dataSource.map { ($0 as! Track).id } | |
backendService.parameters = ["per_page": perPage, "page": page, "exist_ids" : existIds] | |
backendService.getRandom { [weak self] json in | |
let items = json.map({ Track.init(json: $0.1) }) | |
if items.count < perPage { self?.canLoad = false } | |
if page > 1 { | |
let _ = items.map({self?.dataSource.append($0)}) | |
self?.updatePlayerIfNeededWith(tracks: items) | |
} else { | |
self?.dataSource = items | |
} | |
handler() | |
} | |
} | |
private func updatePlayerIfNeededWith(tracks: [Track]) { | |
guard let viewModel = Player.sharedInstance.viewModel as? RandomMusicViewModel else { return } | |
if viewModel == self { | |
Player.sharedInstance.addTracksToTheEnd(array: tracks) | |
} | |
} | |
@objc func loadNext(notification: Notification) { | |
let viewModel = notification.userInfo?["viewModel"] | |
guard let vm = viewModel as? RandomMusicViewModel else { return } | |
if vm == self { | |
if canLoad == false { return } | |
page += 1 | |
load(perPage: perPage, page: page, handler:{}) | |
} | |
} | |
func nameOfTitle() -> String { | |
return "" | |
} | |
public static func == (lhs: RandomMusicViewModel, rhs: RandomMusicViewModel) -> Bool { | |
if lhs.dataSource.count != rhs.dataSource.count { return false } | |
for (index, item) in lhs.dataSource.enumerated() { | |
let item2 = rhs.dataSource[index] as! Track | |
if (item as! Track).id != item2.id { | |
return false | |
} | |
} | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment