2026-01-08 19:16:32 +00:00
|
|
|
//
|
|
|
|
|
// FeedEnvelope.swift
|
|
|
|
|
// iris
|
|
|
|
|
//
|
|
|
|
|
// Created by Codex.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import WeatherKit
|
|
|
|
|
|
|
|
|
|
struct FeedEnvelope: Codable, Equatable {
|
|
|
|
|
let schema: Int
|
|
|
|
|
let generatedAt: Int
|
2026-01-10 00:25:36 +00:00
|
|
|
let feed: [FeedItem]
|
2026-01-08 19:16:32 +00:00
|
|
|
let meta: FeedMeta
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case schema
|
|
|
|
|
case generatedAt = "generated_at"
|
|
|
|
|
case feed
|
|
|
|
|
case meta
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-10 00:25:36 +00:00
|
|
|
struct FeedItem: Codable, Equatable {
|
2026-01-08 19:16:32 +00:00
|
|
|
enum Bucket: String, Codable {
|
|
|
|
|
case rightNow = "RIGHT_NOW"
|
|
|
|
|
case fyi = "FYI"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let id: String
|
2026-01-10 00:25:36 +00:00
|
|
|
let type: FeedItemType
|
2026-01-08 19:16:32 +00:00
|
|
|
let title: String
|
|
|
|
|
let subtitle: String
|
|
|
|
|
let priority: Double
|
|
|
|
|
let ttlSec: Int
|
|
|
|
|
let condition: WeatherKit.WeatherCondition?
|
2026-01-10 00:25:36 +00:00
|
|
|
let startsAt: Int?
|
2026-01-10 18:50:15 +00:00
|
|
|
let poiType: POIDataSource.POIType?
|
2026-01-08 19:16:32 +00:00
|
|
|
let bucket: Bucket
|
|
|
|
|
let actions: [String]
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case id
|
|
|
|
|
case type
|
|
|
|
|
case title
|
|
|
|
|
case subtitle
|
|
|
|
|
case priority
|
|
|
|
|
case ttlSec = "ttl_sec"
|
|
|
|
|
case condition
|
2026-01-10 00:25:36 +00:00
|
|
|
case startsAt = "starts_at"
|
2026-01-10 18:50:15 +00:00
|
|
|
case poiType = "poi_type"
|
2026-01-08 19:16:32 +00:00
|
|
|
case bucket
|
|
|
|
|
case actions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(id: String,
|
2026-01-10 00:25:36 +00:00
|
|
|
type: FeedItemType,
|
2026-01-08 19:16:32 +00:00
|
|
|
title: String,
|
|
|
|
|
subtitle: String,
|
|
|
|
|
priority: Double,
|
|
|
|
|
ttlSec: Int,
|
|
|
|
|
condition: WeatherKit.WeatherCondition? = nil,
|
2026-01-10 00:25:36 +00:00
|
|
|
startsAt: Int? = nil,
|
2026-01-10 18:50:15 +00:00
|
|
|
poiType: POIDataSource.POIType? = nil,
|
2026-01-08 19:16:32 +00:00
|
|
|
bucket: Bucket,
|
|
|
|
|
actions: [String]) {
|
|
|
|
|
self.id = id
|
|
|
|
|
self.type = type
|
|
|
|
|
self.title = title
|
|
|
|
|
self.subtitle = subtitle
|
|
|
|
|
self.priority = priority
|
|
|
|
|
self.ttlSec = ttlSec
|
|
|
|
|
self.condition = condition
|
2026-01-10 00:25:36 +00:00
|
|
|
self.startsAt = startsAt
|
2026-01-10 18:50:15 +00:00
|
|
|
self.poiType = poiType
|
2026-01-08 19:16:32 +00:00
|
|
|
self.bucket = bucket
|
|
|
|
|
self.actions = actions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(from decoder: Decoder) throws {
|
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
id = try container.decode(String.self, forKey: .id)
|
2026-01-10 00:25:36 +00:00
|
|
|
type = try container.decode(FeedItemType.self, forKey: .type)
|
2026-01-08 19:16:32 +00:00
|
|
|
title = try container.decode(String.self, forKey: .title)
|
|
|
|
|
subtitle = try container.decode(String.self, forKey: .subtitle)
|
|
|
|
|
priority = try container.decode(Double.self, forKey: .priority)
|
|
|
|
|
ttlSec = try container.decode(Int.self, forKey: .ttlSec)
|
|
|
|
|
bucket = try container.decode(Bucket.self, forKey: .bucket)
|
|
|
|
|
actions = try container.decode([String].self, forKey: .actions)
|
2026-01-10 00:25:36 +00:00
|
|
|
startsAt = try container.decodeIfPresent(Int.self, forKey: .startsAt)
|
2026-01-10 18:50:15 +00:00
|
|
|
if let raw = try container.decodeIfPresent(String.self, forKey: .poiType) {
|
|
|
|
|
poiType = POIDataSource.POIType(rawValue: raw) ?? .other
|
|
|
|
|
} else {
|
|
|
|
|
poiType = nil
|
|
|
|
|
}
|
2026-01-08 19:16:32 +00:00
|
|
|
|
|
|
|
|
if let encoded = try container.decodeIfPresent(String.self, forKey: .condition) {
|
|
|
|
|
condition = WeatherKit.WeatherCondition.irisDecode(encoded)
|
|
|
|
|
} else {
|
|
|
|
|
condition = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
|
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
try container.encode(id, forKey: .id)
|
|
|
|
|
try container.encode(type, forKey: .type)
|
|
|
|
|
try container.encode(title, forKey: .title)
|
|
|
|
|
try container.encode(subtitle, forKey: .subtitle)
|
|
|
|
|
try container.encode(priority, forKey: .priority)
|
|
|
|
|
try container.encode(ttlSec, forKey: .ttlSec)
|
|
|
|
|
try container.encode(bucket, forKey: .bucket)
|
|
|
|
|
try container.encode(actions, forKey: .actions)
|
2026-01-10 00:25:36 +00:00
|
|
|
try container.encodeIfPresent(startsAt, forKey: .startsAt)
|
2026-01-10 18:50:15 +00:00
|
|
|
if let poiType {
|
|
|
|
|
try container.encode(poiType.rawValue, forKey: .poiType)
|
|
|
|
|
}
|
2026-01-08 19:16:32 +00:00
|
|
|
if let condition {
|
|
|
|
|
try container.encode(condition.irisScreamingCase(), forKey: .condition)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct FeedMeta: Codable, Equatable {
|
|
|
|
|
let winnerId: String
|
|
|
|
|
let unreadCount: Int
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case winnerId = "winner_id"
|
|
|
|
|
case unreadCount = "unread_count"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension FeedEnvelope {
|
|
|
|
|
static func allQuiet(now: Int, reason: String = "no_candidates", source: String = "engine") -> FeedEnvelope {
|
2026-01-10 00:25:36 +00:00
|
|
|
let item = FeedItem(
|
2026-01-08 19:16:32 +00:00
|
|
|
id: "quiet-000",
|
|
|
|
|
type: .allQuiet,
|
|
|
|
|
title: "All Quiet",
|
|
|
|
|
subtitle: "No urgent updates",
|
|
|
|
|
priority: 0.05,
|
|
|
|
|
ttlSec: 300,
|
|
|
|
|
condition: nil,
|
2026-01-10 00:25:36 +00:00
|
|
|
startsAt: nil,
|
2026-01-08 19:16:32 +00:00
|
|
|
bucket: .rightNow,
|
|
|
|
|
actions: ["DISMISS"]
|
|
|
|
|
)
|
2026-01-10 00:25:36 +00:00
|
|
|
return FeedEnvelope(schema: 1, generatedAt: now, feed: [item], meta: FeedMeta(winnerId: item.id, unreadCount: 1))
|
2026-01-08 19:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-10 00:25:36 +00:00
|
|
|
func winnerItem() -> FeedItem? {
|
2026-01-08 19:16:32 +00:00
|
|
|
feed.first(where: { $0.id == meta.winnerId }) ?? feed.first
|
|
|
|
|
}
|
|
|
|
|
}
|