make votes and ownVotes optional

This commit is contained in:
Thomas Ricouard 2023-01-03 15:51:36 +01:00
parent 1c827d1f7e
commit 5fe72e36d2
2 changed files with 5 additions and 5 deletions

View file

@ -17,7 +17,7 @@ public struct Poll: Codable {
public let multiple: Bool public let multiple: Bool
public let votesCount: Int public let votesCount: Int
public let votersCount: Int? public let votersCount: Int?
public let voted: Bool public let voted: Bool?
public let ownVotes: [Int] public let ownVotes: [Int]?
public let options: [Option] public let options: [Option]
} }

View file

@ -16,14 +16,14 @@ public class StatusPollViewModel: ObservableObject {
public init(poll: Poll) { public init(poll: Poll) {
self.poll = poll self.poll = poll
self.votes = poll.ownVotes self.votes = poll.ownVotes ?? []
} }
public func fetchPoll() async { public func fetchPoll() async {
guard let client else { return } guard let client else { return }
do { do {
poll = try await client.get(endpoint: Polls.poll(id: poll.id)) poll = try await client.get(endpoint: Polls.poll(id: poll.id))
votes = poll.ownVotes votes = poll.ownVotes ?? []
} catch { } } catch { }
} }
@ -32,7 +32,7 @@ public class StatusPollViewModel: ObservableObject {
do { do {
poll = try await client.post(endpoint: Polls.vote(id: poll.id, votes: votes)) poll = try await client.post(endpoint: Polls.vote(id: poll.id, votes: votes))
withAnimation { withAnimation {
votes = poll.ownVotes votes = poll.ownVotes ?? []
} }
} catch { } catch {
print(error) print(error)