-
Notifications
You must be signed in to change notification settings - Fork 3
/
DetailView.swift
107 lines (90 loc) · 2.88 KB
/
DetailView.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// DetailView.swift
// X06 Friends
//
// Created by Julian Moorhouse on 04/04/2021.
//
import SwiftUI
struct DetailView: View {
let user: User
let userFriends: [User]
let allUsers: [User]
var body: some View {
Form {
Section {
VStack(alignment: .center, spacing: 10) {
Text(user.initials)
.font(.largeTitle)
.padding(20)
.background(Color.gray.opacity(0.55))
.clipShape(Circle())
Text(user.name)
.font(.largeTitle)
}
.frame(maxWidth: .infinity)
}
Section(header: Text("Details")) {
HStack {
Text("Age")
.font(.headline)
Spacer()
Text("\(user.age)")
}
HStack {
Text("Email")
.font(.headline)
Spacer()
Text(user.email)
}
HStack {
Text("Company")
.font(.headline)
Spacer()
Text(user.company)
}
HStack {
Text("Registered")
.font(.headline)
Spacer()
Text(user.formattedRegisteredDate)
}
}
Section(header: Text("Address")) {
Text(user.address)
}
Section(header: Text("About")) {
Text(user.about)
}
Section(header: Text("Tags")) {
Text(user.tags.joined(separator: ", "))
}
Section(header: Text("Friends")) {
ForEach(self.userFriends, id: \.id) { friend in
NavigationLink(destination: DetailView(user: friend, users: allUsers)) {
Text(friend.name)
}
}
}
}
.navigationBarTitle(Text(user.name), displayMode: .inline)
}
init(user: User, users: [User]) {
self.user = user
self.allUsers = users;
var matches = [User]()
for friend in user.friends {
if let match = users.first(where: { $0.id == friend.id}) {
matches.append(match)
} else {
fatalError("Missing \(friend)")
}
}
self.userFriends = matches
}
}
struct DetailView_Previews: PreviewProvider {
static var previews: some View {
let user: [User] = []
DetailView(user: user[0], users: user)
}
}