mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-06-18 05:00:35 +00:00
4189a59cf6
* Initial iOS 17 + Observable migration * More Observation * More observation * Checkpoint * Checkpoint * Bump version to 1.8.0 * SwiftFormat * Fix home timeline switch on login * Fix sidebar routerPath * Fixes on detail view * Remove print changes * Simply detail view * More opt * Migrate DisplaySettingsLocalValues * Better post detail transition * Status detail animation finally right * Cleanup
56 lines
1.2 KiB
Swift
56 lines
1.2 KiB
Swift
import Combine
|
|
import Models
|
|
import Network
|
|
import Observation
|
|
import SwiftUI
|
|
|
|
@MainActor
|
|
@Observable public class StatusPollViewModel {
|
|
public var client: Client?
|
|
public var instance: Instance?
|
|
|
|
var poll: Poll
|
|
var votes: [Int] = []
|
|
|
|
var showResults: Bool {
|
|
poll.ownVotes?.isEmpty == false || poll.expired
|
|
}
|
|
|
|
public init(poll: Poll) {
|
|
self.poll = poll
|
|
votes = poll.ownVotes ?? []
|
|
}
|
|
|
|
public func fetchPoll() async {
|
|
guard let client else { return }
|
|
do {
|
|
poll = try await client.get(endpoint: Polls.poll(id: poll.id))
|
|
votes = poll.ownVotes ?? []
|
|
} catch {}
|
|
}
|
|
|
|
public func postVotes() async {
|
|
guard let client, !poll.expired else { return }
|
|
do {
|
|
poll = try await client.post(endpoint: Polls.vote(id: poll.id, votes: votes))
|
|
withAnimation {
|
|
votes = poll.ownVotes ?? []
|
|
}
|
|
} catch {
|
|
print(error)
|
|
}
|
|
}
|
|
|
|
public func handleSelection(_ pollIndex: Int) {
|
|
if poll.multiple {
|
|
if let voterIndex = votes.firstIndex(of: pollIndex) {
|
|
votes.remove(at: voterIndex)
|
|
} else {
|
|
votes.append(pollIndex)
|
|
}
|
|
} else {
|
|
votes = [pollIndex]
|
|
}
|
|
}
|
|
}
|