53 lines
1.5 KiB
Swift
53 lines
1.5 KiB
Swift
//
|
|
// PatientMonthlyHistoryViewModel.swift
|
|
// medmap
|
|
//
|
|
|
|
import Foundation
|
|
import Combine
|
|
import SwiftUI
|
|
@MainActor
|
|
final class PatientMonthlyHistoryViewModel: ViewModel {
|
|
|
|
@Published private(set) var history: PatientMonthlyHistoryResponse?
|
|
@Published private(set) var errorMessage: String?
|
|
|
|
private let api: MedMapAPI = DIContainer.shared.resolve()
|
|
private let calendar = Calendar(identifier: .gregorian)
|
|
private var selectedMonth = Date.now
|
|
|
|
|
|
|
|
func loadHistory() async {
|
|
guard !isLoading else { return }
|
|
|
|
isLoading = true
|
|
errorMessage = nil
|
|
defer { isLoading = false }
|
|
|
|
do {
|
|
history = try await api.getMyMonthlyHistory(date: formattedMonth)
|
|
} catch {
|
|
errorMessage = "Nao foi possivel carregar o historico mensal."
|
|
fluxlog("Monthly history load failed: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
|
|
func showPreviousMonth() async {
|
|
selectedMonth = calendar.date(byAdding: .month, value: -1, to: selectedMonth) ?? selectedMonth
|
|
await loadHistory()
|
|
}
|
|
|
|
func showNextMonth() async {
|
|
selectedMonth = calendar.date(byAdding: .month, value: 1, to: selectedMonth) ?? selectedMonth
|
|
await loadHistory()
|
|
}
|
|
|
|
private var formattedMonth: String {
|
|
let formatter = DateFormatter()
|
|
formatter.locale = Locale(identifier: "en_US_POSIX")
|
|
formatter.dateFormat = "yyyy-MM-dd"
|
|
return formatter.string(from: selectedMonth)
|
|
}
|
|
}
|