Commit 9b566df2 by Dmitriy Stepanets

Finished temperature widget integration

parent dc6c3e1a
//
// HourlyTemps.swift
// OneWeatherUI
//
// Created by Dmitry Stepanets on 07.07.2021.
//
import Foundation
struct HourlyTemps {
let currentTemp: Double
let maxTemp: Double
let lowTemp: Double
init(currentTemp: Double, temps: [Double]) {
self.currentTemp = currentTemp
self.maxTemp = temps.sorted{$0 > $1}.first ?? 0
self.lowTemp = temps.sorted{$0 < $1}.first ?? 0
}
}
//
// LargeTemperatureWidget.swift
// OneWeatherWidgetExtension
//
// Created by Dmitry Stepanets on 09.07.2021.
//
import SwiftUI
import WidgetKit
import OneWeatherUI
struct LargeTemperatureWidget: Widget {
private let kind = "com.onelouder.oneweather.widget.large.temperature"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: WeatherProvider()) { weatherEntry in
LargeTemperatureWidgetView(widgetViewModel: ForecastWidgetViewModel(location: weatherEntry.location))
}
.configurationDisplayName("widget.large.title".localized())
.description("widget.large.description".localized())
.supportedFamilies([.systemLarge])
}
}
struct LargeTemperatureWidgetView_Preview: PreviewProvider {
public static var previews: some View {
LargeTemperatureWidgetView(widgetViewModel: nil)
.previewContext(WidgetPreviewContext(family: .systemLarge))
}
}
//
// MediumTemperatureWidget.swift
// OneWeatherWidgetExtension
//
// Created by Demid Merzlyakov on 28.06.2021.
//
import SwiftUI
import WidgetKit
import OneWeatherUI
struct MediumTemperatureWidget: Widget {
private let kind = "com.onelouder.oneweather.widget.medium.temperature"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: WeatherProvider()) { weatherEntry in
MediumTemperatureWidgetView(widgetViewModel: ForecastWidgetViewModel(location: weatherEntry.location))
}
.configurationDisplayName("widget.medium.title".localized())
.description("widget.medium.description".localized())
.supportedFamilies([.systemMedium])
}
}
struct MediumTemperatureWidgetView_Preview: PreviewProvider {
public static var previews: some View {
MediumTemperatureWidgetView(widgetViewModel: nil)
.preferredColorScheme(.dark)
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
......@@ -10,23 +10,46 @@ import SwiftUI
import OneWeatherUI
import Localize_Swift
struct SmallTemperatureWidget: Widget {
private let kind = "com.onelouder.oneweather.widget.small.temperature"
struct TemperatureWidget: Widget {
private let kind = "com.onelouder.oneweather.widget.temperature"
var body: some WidgetConfiguration {
// We currently display selectedLocation, so it's not really configurable, but we'll probably need to switch to an IntentConfiguration at some point.
StaticConfiguration(kind: kind, provider: WeatherProvider()) { weatherEntry in
SmallTemperatureWidgetView(widgetViewModel: ForecastWidgetViewModel(location: weatherEntry.location))
StaticConfiguration(kind: kind,
provider: WeatherProvider()
) { weatherEntry in
WidgetView(entry: weatherEntry)
}
.configurationDisplayName("widget.small.title".localized())
.description("widget.small.description".localized())
.supportedFamilies([.systemSmall])
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
}
}
private struct WidgetView: View {
@Environment(\.widgetFamily) private var widgetSize
let entry: WeatherEntry
var body: some View {
let viewModel = ForecastWidgetViewModel(location: entry.location)
switch widgetSize {
case .systemSmall:
SmallTemperatureWidgetView(widgetViewModel: viewModel)
case .systemMedium:
MediumTemperatureWidgetView(widgetViewModel: viewModel)
case .systemLarge:
LargeTemperatureWidgetView(widgetViewModel: viewModel)
@unknown default:
SmallTemperatureWidgetView(widgetViewModel: viewModel)
}
}
}
struct SmallTemperatureWidgetView_Preview: PreviewProvider {
public static var previews: some View {
SmallTemperatureWidgetView(widgetViewModel: nil)
.preferredColorScheme(.dark)
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment