From 5b3afc72dec5e13e3b85d6216647a27141651de3 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Tue, 24 Jan 2023 21:19:14 +0100 Subject: [PATCH] Fix some polls crashing at decoding --- Packages/Models/Sources/Models/Poll.swift | 16 ++++++++++++++-- .../Sources/Status/Poll/StatusPollView.swift | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Packages/Models/Sources/Models/Poll.swift b/Packages/Models/Sources/Models/Poll.swift index 2a859421..ca04f48d 100644 --- a/Packages/Models/Sources/Models/Poll.swift +++ b/Packages/Models/Sources/Models/Poll.swift @@ -20,12 +20,24 @@ public struct Poll: Codable, Equatable, Hashable { } public let id: String - public let expiresAt: ServerDate + public let expiresAt: NullableString public let expired: Bool public let multiple: Bool public let votesCount: Int - public let votersCount: Int? public let voted: Bool? public let ownVotes: [Int]? public let options: [Option] } + +public struct NullableString: Codable, Equatable, Hashable { + public let value: String? + + public init(from decoder: Decoder) throws { + do { + let container = try decoder.singleValueContainer() + self.value = try container.decode(String.self) + } catch { + self.value = nil + } + } +} diff --git a/Packages/Status/Sources/Status/Poll/StatusPollView.swift b/Packages/Status/Sources/Status/Poll/StatusPollView.swift index b18107e2..73aa9563 100644 --- a/Packages/Status/Sources/Status/Poll/StatusPollView.swift +++ b/Packages/Status/Sources/Status/Poll/StatusPollView.swift @@ -72,9 +72,9 @@ public struct StatusPollView: View { Text(" βΈ± ") if viewModel.poll.expired { Text("status.poll.closed") - } else { + } else if let date = viewModel.poll.expiresAt.value?.asDate { Text("status.poll.closes-in") - Text(viewModel.poll.expiresAt.asDate, style: .timer) + Text(date, style: .timer) } } .font(.scaledFootnote)