37 lines
951 B
Swift
37 lines
951 B
Swift
import Foundation
|
||
|
||
@MainActor
|
||
final class MainViewModel: ObservableObject {
|
||
@Published var sharedURL: String = ""
|
||
@Published var isLoading: Bool = false
|
||
@Published var result: GetInfoResponse?
|
||
@Published var errorMessage: String?
|
||
|
||
func consumeSharedURLIfAny() {
|
||
guard let incoming = SharedPayloadStore.consumeIncomingURL(), !incoming.isEmpty else {
|
||
return
|
||
}
|
||
sharedURL = incoming
|
||
Task { await fetch() }
|
||
}
|
||
|
||
func fetch() async {
|
||
guard !sharedURL.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
||
errorMessage = "Paylaşılan URL boş olamaz."
|
||
return
|
||
}
|
||
|
||
isLoading = true
|
||
errorMessage = nil
|
||
result = nil
|
||
|
||
do {
|
||
result = try await APIClient.shared.getInfo(url: sharedURL)
|
||
} catch {
|
||
errorMessage = error.localizedDescription
|
||
}
|
||
|
||
isLoading = false
|
||
}
|
||
}
|