StatRootView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import Charts
  2. import CoreData
  3. import SwiftDate
  4. import SwiftUI
  5. import Swinject
  6. extension Stat {
  7. struct RootView: BaseView {
  8. let resolver: Resolver
  9. @StateObject var state = StateModel()
  10. @FetchRequest(
  11. entity: TDD.entity(),
  12. sortDescriptors: [NSSortDescriptor(key: "timestamp", ascending: false)]
  13. ) var fetchedTDD: FetchedResults<TDD>
  14. @FetchRequest(
  15. entity: InsulinDistribution.entity(),
  16. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  17. ) var fetchedInsulin: FetchedResults<InsulinDistribution>
  18. enum Duration: String, CaseIterable, Identifiable {
  19. case Today
  20. case Day
  21. case Week
  22. case Month
  23. case Total
  24. var id: Self { self }
  25. }
  26. @State private var selectedDuration: Duration = .Today
  27. @State var paddingAmount: CGFloat? = 10
  28. @State var headline: Color = .secondary
  29. @State var days: Double = 0
  30. @State var pointSize: CGFloat = 3
  31. @State var conversionFactor = 0.0555
  32. @ViewBuilder func stats() -> some View {
  33. ZStack {
  34. Color.gray.opacity(0.05).ignoresSafeArea(.all)
  35. let filter = DateFilter()
  36. switch selectedDuration {
  37. case .Today:
  38. StatsView(
  39. filter: filter.today,
  40. $state.highLimit,
  41. $state.lowLimit,
  42. $state.units,
  43. $state.overrideUnit
  44. )
  45. case .Day:
  46. StatsView(
  47. filter: filter.day,
  48. $state.highLimit,
  49. $state.lowLimit,
  50. $state.units,
  51. $state.overrideUnit
  52. )
  53. case .Week:
  54. StatsView(
  55. filter: filter.week,
  56. $state.highLimit,
  57. $state.lowLimit,
  58. $state.units,
  59. $state.overrideUnit
  60. )
  61. case .Month:
  62. StatsView(
  63. filter: filter.month,
  64. $state.highLimit,
  65. $state.lowLimit,
  66. $state.units,
  67. $state.overrideUnit
  68. )
  69. case .Total:
  70. StatsView(
  71. filter: filter.total,
  72. $state.highLimit,
  73. $state.lowLimit,
  74. $state.units,
  75. $state.overrideUnit
  76. )
  77. }
  78. }
  79. }
  80. @ViewBuilder func chart() -> some View {
  81. let filter = DateFilter()
  82. switch selectedDuration {
  83. case .Today:
  84. ChartsView(
  85. filter: filter.today,
  86. $state.highLimit,
  87. $state.lowLimit,
  88. $state.units,
  89. $state.overrideUnit,
  90. $state.layingChart
  91. )
  92. case .Day:
  93. ChartsView(
  94. filter: filter.day,
  95. $state.highLimit,
  96. $state.lowLimit,
  97. $state.units,
  98. $state.overrideUnit,
  99. $state.layingChart
  100. )
  101. case .Week:
  102. ChartsView(
  103. filter: filter.week,
  104. $state.highLimit,
  105. $state.lowLimit,
  106. $state.units,
  107. $state.overrideUnit,
  108. $state.layingChart
  109. )
  110. case .Month:
  111. ChartsView(
  112. filter: filter.month,
  113. $state.highLimit,
  114. $state.lowLimit,
  115. $state.units,
  116. $state.overrideUnit,
  117. $state.layingChart
  118. )
  119. case .Total:
  120. ChartsView(
  121. filter: filter.total,
  122. $state.highLimit,
  123. $state.lowLimit,
  124. $state.units,
  125. $state.overrideUnit,
  126. $state.layingChart
  127. )
  128. }
  129. }
  130. var body: some View {
  131. VStack(alignment: .center) {
  132. chart().padding(.top, 20)
  133. Picker("Duration", selection: $selectedDuration) {
  134. ForEach(Duration.allCases) { duration in
  135. Text(NSLocalizedString(duration.rawValue, comment: "")).tag(Optional(duration))
  136. }
  137. }
  138. .pickerStyle(.segmented).background(.cyan.opacity(0.2))
  139. stats()
  140. }
  141. .onAppear(perform: configureView)
  142. .navigationBarTitle("Statistics")
  143. .navigationBarTitleDisplayMode(.inline)
  144. .navigationBarItems(trailing: Button("Close", action: state.hideModal))
  145. }
  146. }
  147. }