ContentView.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 FirebaseRemoteConfig
  17. import FirebaseRemoteConfigSwift
  18. import FirebaseAnalyticsSwift
  19. import FirebaseAnalytics
  20. import SwiftUI
  21. struct Recipe: Decodable {
  22. var recipeName: String
  23. var cookTime: Int
  24. var notes: String
  25. }
  26. struct ContentView: View {
  27. @RemoteConfigProperty(key: "Color", fallback: nil) var colorValue: String?
  28. @RemoteConfigProperty(key: "toggleStyleSquare", fallback: false) var toggleStyleSquare: Bool
  29. @RemoteConfigProperty(key: "fruits", fallback: []) var fruits: [String]
  30. @RemoteConfigProperty(key: "counter", fallback: 1) var counter: Int
  31. @RemoteConfigProperty(key: "mobileweek", fallback: ["section 0": "breakfast"]) var sessions:
  32. [String: String]
  33. @RemoteConfigProperty(
  34. key: "recipe", fallback: Recipe(recipeName: "banana bread", cookTime: 40, notes: "yum!")
  35. )
  36. var recipe: Recipe
  37. @State var isChecked = false
  38. var body: some View {
  39. VStack {
  40. Button(action: fetchAndActivate) {
  41. Text("fetchAndActivate")
  42. }
  43. List(fruits, id: \.self) { fruit in
  44. HStack {
  45. Button(action: toggle) {
  46. if toggleStyleSquare {
  47. Image(systemName: isChecked ? "checkmark.square.fill" : "square")
  48. } else {
  49. Image(systemName: isChecked ? "checkmark.circle.fill" : "circle")
  50. }
  51. }
  52. Text(fruit)
  53. }
  54. }
  55. List {
  56. ForEach(sessions.sorted(by: >), id: \.key) { key, value in
  57. Section(header: Text(key)) {
  58. Text(value)
  59. }
  60. }
  61. }
  62. List {
  63. Text(recipe.recipeName)
  64. Text(recipe.notes)
  65. Text("cook time: \(recipe.cookTime)")
  66. .analyticsScreen(name: "recipe")
  67. }
  68. // Test non exist key
  69. if colorValue != nil {
  70. Text(colorValue!)
  71. .padding()
  72. }
  73. }
  74. }
  75. func toggle() {
  76. isChecked.toggle()
  77. }
  78. func fetchAndActivate() {
  79. RemoteConfig.remoteConfig().fetchAndActivate()
  80. Analytics.logEvent("activate", parameters: [:])
  81. }
  82. }
  83. struct ContentView_Previews: PreviewProvider {
  84. static var previews: some View {
  85. ContentView()
  86. }
  87. }