Commit 6609f62d by Demid Merzlyakov

IOS-259: fix potential duplicate WIDGET_UPDATE.

parent c2fa601a
......@@ -133,31 +133,22 @@ public class WidgetManager {
params: [.ANALYTICS_KEY_WIDGET_NAME : name])
}
public func logUpdate(forLocation location: Location?) {
WidgetCenter.shared.getCurrentConfigurations {[weak self] result in
switch result {
case .success(let widgetInfo):
for info in widgetInfo {
if let option = WidgetOptions.option(forKind: info.kind, family: info.family) {
let name = WidgetOptions.name(for: option)
var params: [AnalyticsParameter : String] = [.ANALYTICS_KEY_WIDGET_NAME : name]
// Radar widget doesn't have smart text (IOS-260).
// TODO: this needs to be unified. Any widget not using smart text shouldn't have that parameter.
if option != .radarLarge {
if let updatedLocation = location {
let smartText = self?.smartTextProvider.smartText(for: updatedLocation) ?? ""
params[.ANALYTICS_KEY_WIDGET_SMART_TEXT] = #"\#(smartText)"#
}
}
AppAnalytics.shared.log(event: .ANALYTICS_WIDGET_UPDATED,
params: params)
}
public func logUpdate(forLocation location: Location?, kind: String, family: WidgetFamily) {
if let option = WidgetOptions.option(forKind: kind, family: family) {
let name = WidgetOptions.name(for: option)
var params: [AnalyticsParameter : String] = [.ANALYTICS_KEY_WIDGET_NAME : name]
// Radar widget doesn't have smart text (IOS-260).
// TODO: this needs to be unified. Any widget not using smart text shouldn't have that parameter.
if option != .radarLarge {
if let updatedLocation = location {
let smartText = smartTextProvider.smartText(for: updatedLocation)
params[.ANALYTICS_KEY_WIDGET_SMART_TEXT] = #"\#(smartText)"#
}
case .failure(let error):
self?.log.error(error.localizedDescription)
}
AppAnalytics.shared.log(event: .ANALYTICS_WIDGET_UPDATED,
params: params)
}
}
......
......@@ -21,10 +21,10 @@ class WeatherProvider: TimelineProvider {
private let needsRadar: Bool
private var weatherSource: WeatherSource
private let locationSource: WidgetLocationSource
private let widgetType: String
private let widgetKind: String
public init(widgetType: String, locationSource: WidgetLocationSource = WidgetLocationSource.shared, weatherSource: WeatherSource = WdtWeatherSource(), needsRadar: Bool) {
self.widgetType = widgetType
public init(widgetKind: String, locationSource: WidgetLocationSource = WidgetLocationSource.shared, weatherSource: WeatherSource = WdtWeatherSource(), needsRadar: Bool) {
self.widgetKind = widgetKind
self.locationSource = locationSource
self.weatherSource = weatherSource
self.needsRadar = needsRadar
......@@ -42,6 +42,7 @@ class WeatherProvider: TimelineProvider {
func getTimeline(in context: Context, completion: @escaping (Timeline<WeatherEntry>) -> Void) {
locationSource.getUpToDateLocation { [weak self] location in
guard let self = self else { return }
var needToLogUpdate = false
if
let fetchedLocation = location,
let coordinates = fetchedLocation.coordinates
......@@ -54,7 +55,7 @@ class WeatherProvider: TimelineProvider {
let nextRefresh = Calendar.current.date(byAdding: .minute, value: 30, to: Date())!
let entry = WeatherEntry(location: fetchedLocation, date: nextRefresh, radarMapImage: mapImage)
let timeline = Timeline(entries: [entry], policy: .atEnd)
WidgetManager.shared.logUpdate(forLocation: fetchedLocation)
needToLogUpdate = true
completion(timeline)
}
}
......@@ -62,7 +63,7 @@ class WeatherProvider: TimelineProvider {
let nextRefresh = Calendar.current.date(byAdding: .minute, value: 30, to: Date())!
let entry = WeatherEntry(location: fetchedLocation, date: nextRefresh, radarMapImage: nil)
let timeline = Timeline(entries: [entry], policy: .atEnd)
WidgetManager.shared.logUpdate(forLocation: fetchedLocation)
needToLogUpdate = true
completion(timeline)
}
}
......@@ -70,11 +71,16 @@ class WeatherProvider: TimelineProvider {
let nextRefresh = Calendar.current.date(byAdding: .minute, value: 30, to: Date())!
let entry = WeatherEntry(location: location, date: nextRefresh)
let timeline = Timeline(entries: [entry], policy: .atEnd)
WidgetManager.shared.logUpdate(forLocation: location)
needToLogUpdate = true
completion(timeline)
}
WidgetManager.shared.refreshAnalytics()
if !context.isPreview {
WidgetManager.shared.refreshAnalytics()
if needToLogUpdate {
WidgetManager.shared.logUpdate(forLocation: location, kind: self.widgetKind, family: context.family)
}
}
}
}
}
......@@ -17,7 +17,7 @@ struct PrecipitationWidget: Widget {
// 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(widgetType: kind.components(separatedBy: ".").last ?? "-", needsRadar: false)
provider: WeatherProvider(widgetKind: kind, needsRadar: false)
) { weatherEntry in
MediumPrecipitationWidgetView(widgetViewModel: ForecastWidgetViewModel(location: weatherEntry.location))
.widgetURL(URL(string: "ow-widget://precipitation-medium"))
......
......@@ -17,7 +17,7 @@ struct RadarWidget: Widget {
// 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(widgetType: kind.components(separatedBy: ".").last ?? "-", needsRadar: true)
provider: WeatherProvider(widgetKind: kind, needsRadar: true)
) { weatherEntry in
LargeRadarWidgetView(widgetViewModel: ForecastWidgetViewModel(location: weatherEntry.location,
radarImage: weatherEntry.radarMapImage))
......
......@@ -17,7 +17,7 @@ struct TemperatureWidget: Widget {
// 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(widgetType: kind.components(separatedBy: ".").last ?? "-", needsRadar: false)
provider: WeatherProvider(widgetKind: kind, needsRadar: false)
) { weatherEntry in
WidgetView(entry: weatherEntry)
}
......
......@@ -17,7 +17,7 @@ struct WindWidget: Widget {
// 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(widgetType: kind.components(separatedBy: ".").last ?? "-", needsRadar: false)
provider: WeatherProvider(widgetKind: kind, needsRadar: false)
) { weatherEntry in
WidgetView(entry: weatherEntry)
}
......
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