xdripswift/xDrip Watch App/Views/SubViews/DataSourceView.swift

136 lines
5.1 KiB
Swift

//
// DataSourceView.swift
// xDrip Watch App
//
// Created by Paul Plant on 21/2/24.
// Copyright © 2024 Johan Degraeve. All rights reserved.
//
import Foundation
import SwiftUI
struct DataSourceView: View {
@EnvironmentObject var watchState: WatchStateModel
var body: some View {
VStack(spacing: 0) {
if (watchState.activeSensorDescription != "" || watchState.sensorAgeInMinutes > 0) || !watchState.isMaster {
ProgressView(value: Float(watchState.activeSensorProgress().progress))
.tint(ConstantsHomeView.sensorProgressViewNormalColorSwiftUI)
.scaleEffect(x: 1, y: 0.3, anchor: .center)
HStack {
if !watchState.isMaster {
HStack(alignment: .center, spacing: 4) {
watchState.getFollowerConnectionStatusImage().networkImage
.font(.system(size: 14))
.foregroundStyle(watchState.getFollowerConnectionStatusImage().tintColor)
.padding(.bottom, -2)
watchState.followerBackgroundKeepAliveType.keepAliveImage
.font(.system(size: 14))
.foregroundStyle(Color(white: 0.7))
.padding(.bottom, -3)
Text(watchState.followerDataSourceType.fullDescription)
.font(.system(size: 14)).fontWeight(.semibold)
}
} else {
Text(watchState.activeSensorDescription)
.font(.system(size: 14)).fontWeight(.semibold)
}
Spacer()
if watchState.sensorAgeInMinutes > 0 {
Text(watchState.sensorAgeInMinutes.minutesToDaysAndHours())
.font(.system(size: 14))
.foregroundStyle(watchState.activeSensorProgress().textColor)
}
}
.padding([.leading, .trailing], 10)
} else {
ProgressView(value: 0)
.tint(ConstantsHomeView.sensorProgressViewNormalColorSwiftUI)
.scaleEffect(x: 1, y: 0.3, anchor: .center)
HStack {
Text(" ⚠️ " + Texts_HomeView.noDataSourceConnectedWatch)
.font(.system(size: 14)).bold()
Spacer()
}
.padding([.leading, .trailing], 10)
}
}
}
}
struct DataSourceView_Previews: PreviewProvider {
static func bgDateArray() -> [Date] {
let endDate = Date()
let startDate = endDate.addingTimeInterval(-3600 * 12)
var currentDate = startDate
var dateArray: [Date] = []
while currentDate < endDate {
dateArray.append(currentDate)
currentDate = currentDate.addingTimeInterval(60 * 5)
}
return dateArray
}
static func bgValueArray() -> [Double] {
var bgValueArray:[Double] = Array(repeating: 0, count: 144)
var currentValue: Double = 120
var increaseValues: Bool = true
for index in bgValueArray.indices {
let randomValue = Double(Int.random(in: -10..<30))
if currentValue < 70 {
increaseValues = true
bgValueArray[index] = currentValue + abs(randomValue)
} else if currentValue > 180 {
increaseValues = false
bgValueArray[index] = currentValue - abs(randomValue)
} else {
bgValueArray[index] = currentValue + (increaseValues ? randomValue : -randomValue)
}
currentValue = bgValueArray[index]
}
return bgValueArray
}
static var previews: some View {
let watchState = WatchStateModel()
watchState.bgReadingValues = bgValueArray()
watchState.bgReadingDates = bgDateArray()
watchState.isMgDl = true
watchState.slopeOrdinal = 5
watchState.deltaChangeInMgDl = -2
watchState.urgentLowLimitInMgDl = 60
watchState.lowLimitInMgDl = 80
watchState.highLimitInMgDl = 140
watchState.urgentHighLimitInMgDl = 180
watchState.updatedDate = Date().addingTimeInterval(-400)
watchState.activeSensorDescription = "Data Source"
watchState.sensorAgeInMinutes = 0
watchState.sensorMaxAgeInMinutes = 14400
watchState.isMaster = false
watchState.followerDataSourceType = .libreLinkUp
watchState.secondsUntilFollowerDisconnectWarning = 60 * 7
watchState.timeStampOfLastFollowerConnection = Date().addingTimeInterval(-60 * 6)
return Group {
DataSourceView()
}.environmentObject(watchState)
}
}