-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathComposedPlant.swift
44 lines (36 loc) · 1.08 KB
/
ComposedPlant.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
See the LICENSE.txt file for this sample’s licensing information.
Abstract:
The plant, composed of its parts.
*/
import BackyardBirdsData
import SwiftUI
public struct ComposedPlant: View {
var plant: Plant
public init(plant: Plant) {
self.plant = plant
}
public var body: some View {
ZStack {
if let species = plant.species {
ForEach(species.parts) { part in
if let imageName = imageName(for: part) {
Image(imageName, bundle: .module)
.resizable()
.scaledToFit()
} else {
Image(systemName: "questionmark")
}
}
}
}
}
func imageName(for part: PlantPart) -> String? {
guard let species = plant.species else { return nil }
var result = "\(species.id)/\(part.name)"
if part.variants != nil {
result.append(" \(plant.variant + 1)")
}
return result
}
}