I wanted to do something like this with the new #preview
macro in SwiftUI,
New way of Preview
#Preview {
CoinDetailView(coin: dev.coin) //👈 *This gives me error* ❌
}
<----------------------------------------------------------------------------------------->
The old way of preview
struct CoinDetailView: View {
let coin: CoinModel
var body: some View {
Text(coin.name)
}
}
struct CoinDetailView_Previews: PreviewProvider {
static var previews: some View {
CoinDetailView(coin: dev.coin) // look at how clean that is 🌟
}
}
//DI Container
class DeveloperPreview {
static let shared = DeveloperPreview()
private init() {}
let coin = CoinModel(
id: "bitcoin",
symbol: "btc",
name: "Bitcoin",
image: "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
currentPrice: 61408,
marketCap: 1141731099010)
}
// extend PreviewProvider with a computed property of our DI container
extension PreviewProvider {
static var dev: DeveloperPreview {
return DeveloperPreview.shared
}
}