Skip to content

Commit 9675f8d

Browse files
authored
UI changes for the sample app. (#5331)
* UI changes for the sample app. Moved the buttons out of the list, added a navigation title, and made Identity have optional properties since it's more appropriate (vs an empty string). * Fixed style, removed comment.
1 parent ed52d0d commit 9675f8d

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

Example/Messaging/Sample/Sample/ContentView.swift

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,32 @@ struct ContentView: View {
2323

2424
var body: some View {
2525
NavigationView {
26-
List {
26+
// Outer stack containing the list and the buttons.
27+
VStack {
28+
List {
29+
VStack(alignment: .leading) {
30+
Text("InstanceID").font(.subheadline)
31+
Text(identity.instanceID ?? "None").foregroundColor(.blue)
32+
}
33+
34+
VStack(alignment: .leading) {
35+
Text("Token").font(.subheadline)
36+
Text(identity.token ?? "None")
37+
.foregroundColor(.blue)
38+
// Increase the layout priority to allow more than one line to be shown. Without this, the
39+
// simulator renders a single truncated line even though the Preview renders it
40+
// appropriately. Potentially a bug in the simulator?
41+
.layoutPriority(1)
42+
}
43+
44+
NavigationLink(destination: DetailView()) {
45+
Text("Show Detail View")
46+
}
47+
}
48+
.navigationBarTitle("Firebase Messaging")
49+
50+
// MARK: Action buttons
51+
2752
Button(action: getToken) {
2853
HStack {
2954
Image(systemName: "arrow.clockwise.circle.fill").font(.body)
@@ -59,14 +84,6 @@ struct ContentView: View {
5984
}
6085
}
6186
.buttonStyle(IdentityButtonStyle())
62-
63-
Text("InstanceID: \(identity.instanceID)")
64-
.foregroundColor(.blue)
65-
Text("Token: \(identity.token)")
66-
.foregroundColor(.blue)
67-
NavigationLink(destination: DetailView()) {
68-
Text("Show Detail View")
69-
}
7087
}
7188
}
7289
}
@@ -92,7 +109,7 @@ struct ContentView: View {
92109
print("Failed delete token: ", error)
93110
return
94111
}
95-
self.identity.token = ""
112+
self.identity.token = nil
96113
}
97114
}
98115

@@ -102,8 +119,8 @@ struct ContentView: View {
102119
print("Failed delete ID: ", error)
103120
return
104121
}
105-
self.identity.instanceID = ""
106-
self.identity.token = ""
122+
self.identity.instanceID = nil
123+
self.identity.token = nil
107124
}
108125
}
109126

@@ -113,7 +130,7 @@ struct ContentView: View {
113130
print("Failed delete FID: ", error)
114131
return
115132
}
116-
self.identity.instanceID = ""
133+
self.identity.instanceID = nil
117134
}
118135
}
119136
}
@@ -123,15 +140,32 @@ struct DetailView: View {
123140

124141
var body: some View {
125142
VStack {
126-
Text("InstanceID: \(self.identity.instanceID)")
127-
Text("Token: \(self.identity.token)")
143+
Text("InstanceID: \(self.identity.instanceID ?? "None")")
144+
Text("Token: \(self.identity.token ?? "None")")
128145
}
129146
}
130147
}
131148

132149
struct ContentView_Previews: PreviewProvider {
150+
// A fake filled identity for testing rendering of a filled cell.
151+
static let filledIdentity: Identity = {
152+
var identity = Identity()
153+
identity.instanceID = UUID().uuidString
154+
155+
// The token is a long string, generate a very long repeating string of characters to see how the view
156+
// will react.
157+
let longString = UUID().uuidString.replacingOccurrences(of: "-", with: "")
158+
identity.token = Array(repeating: longString, count: 10).reduce("", +)
159+
160+
return identity
161+
}()
162+
133163
static var previews: some View {
134-
ContentView().environmentObject(Identity())
164+
Group {
165+
ContentView().environmentObject(Identity())
166+
167+
ContentView().environmentObject(filledIdentity)
168+
}
135169
}
136170
}
137171

@@ -144,5 +178,7 @@ struct IdentityButtonStyle: ButtonStyle {
144178
.background(LinearGradient(gradient: Gradient(colors: [Color.blue, Color.pink]),
145179
startPoint: .leading, endPoint: .trailing))
146180
.cornerRadius(40)
181+
// Push the button down a bit when it's pressed.
182+
.scaleEffect(configuration.isPressed ? 0.9 : 1)
147183
}
148184
}

Example/Messaging/Sample/Sample/Identity.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftUI
1616

1717
public final class Identity: ObservableObject {
1818
// Identity that is unique per app.
19-
@Published public var instanceID = ""
19+
@Published public var instanceID: String? = nil
2020
// The token that Firebase Messaging use to send notifications.
21-
@Published public var token = ""
21+
@Published public var token: String? = nil
2222
}

Example/Messaging/Sample/Sample/SceneDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate, MessagingDelegate {
4040
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
4141
identity.token = fcmToken
4242
InstanceID.instanceID().instanceID { result, error in
43-
self.identity.instanceID = result?.instanceID ?? ""
43+
self.identity.instanceID = result?.instanceID
4444
}
4545
}
4646
}

0 commit comments

Comments
 (0)