Skip to content

Instantly share code, notes, and snippets.

@meyusufdemirci
Last active February 21, 2021 17:18
Show Gist options
  • Select an option

  • Save meyusufdemirci/8ca99cda23aba4c0bb7975d487f838c0 to your computer and use it in GitHub Desktop.

Select an option

Save meyusufdemirci/8ca99cda23aba4c0bb7975d487f838c0 to your computer and use it in GitHub Desktop.
import Foundation
protocol ListDelegate {
func coinsDidRefresh()
func coinsCouldNotRefresh()
}
class ListViewModel {
// MARK: Properties
var coins: [Coin] = []
var delegate: ListDelegate?
func refreshCoins() {
coins = getDummyCoins()
delegate?.coinsDidRefresh()
}
func search(_ text: String?) {
if let text = text, !text.isEmpty {
coins = getDummyCoins().filter { $0.symbol.lowercased().contains(text.lowercased()) }
} else {
coins = getDummyCoins()
}
delegate?.coinsDidRefresh()
}
}
private extension ListViewModel {
func getDummyCoins() -> [Coin] {
[
.init(symbol: "BTCUSDT", price: 42500),
.init(symbol: "ETHUSDT", price: 1500),
.init(symbol: "XRPUSDT", price: 0.5),
.init(symbol: "LTCUSDT", price: 175)
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment