ContentView.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2022 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import FirebaseAnalytics
  17. import FirebaseRemoteConfig
  18. import SwiftUI
  19. struct Recipe: Decodable {
  20. var recipeName: String
  21. var cookTime: Int
  22. var notes: String
  23. }
  24. struct ContentView: View {
  25. @RemoteConfigProperty(key: "Color", fallback: nil) var colorValue: String?
  26. @RemoteConfigProperty(key: "toggleStyleSquare", fallback: false) var toggleStyleSquare: Bool
  27. @RemoteConfigProperty(key: "fruits", fallback: []) var fruits: [String]
  28. @RemoteConfigProperty(key: "counter", fallback: 1) var counter: Int
  29. @RemoteConfigProperty(key: "mobileweek", fallback: ["section 0": "breakfast"]) var sessions:
  30. [String: String]
  31. @RemoteConfigProperty(
  32. key: "recipe", fallback: Recipe(recipeName: "banana bread", cookTime: 40, notes: "yum!")
  33. )
  34. var recipe: Recipe
  35. @State var isChecked = false
  36. var body: some View {
  37. VStack {
  38. Button(action: fetchAndActivate) {
  39. Text("fetchAndActivate")
  40. }
  41. List(fruits, id: \.self) { fruit in
  42. HStack {
  43. Button(action: toggle) {
  44. if toggleStyleSquare {
  45. Image(systemName: isChecked ? "checkmark.square.fill" : "square")
  46. } else {
  47. Image(systemName: isChecked ? "checkmark.circle.fill" : "circle")
  48. }
  49. }
  50. Text(fruit)
  51. }
  52. }
  53. List {
  54. ForEach(sessions.sorted(by: >), id: \.key) { key, value in
  55. Section(header: Text(key)) {
  56. Text(value)
  57. }
  58. }
  59. }
  60. List {
  61. Text(recipe.recipeName)
  62. Text(recipe.notes)
  63. Text("cook time: \(recipe.cookTime)")
  64. .analyticsScreen(name: "recipe")
  65. }
  66. // Test non exist key
  67. if colorValue != nil {
  68. Text(colorValue!)
  69. .padding()
  70. }
  71. }
  72. }
  73. func toggle() {
  74. isChecked.toggle()
  75. }
  76. func fetchAndActivate() {
  77. RemoteConfig.remoteConfig().fetchAndActivate()
  78. Analytics.logEvent("activate", parameters: [:])
  79. }
  80. }
  81. struct ContentView_Previews: PreviewProvider {
  82. static var previews: some View {
  83. ContentView()
  84. }
  85. }