Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.0 kB
0
Indexable
import SwiftUI
import Combine

struct OdkrywajView: View {
    @State private var roasters: [CoffeeRoasterResponse] = []
    @State private var isLoading = true
    @State private var errorMessage: String?
    
    private let apiService = ApiService()
    @State private var cancellables = Set<AnyCancellable>()
    
    var body: some View {
        NavigationView {
            VStack {
                if isLoading {
                    ProgressView("Loading...")
                } else if let errorMessage = errorMessage {
                    Text("Error: \(errorMessage)")
                        .foregroundColor(.red)
                } else {
                    List(roasters, id: \.id) { roaster in
                        VStack(alignment: .leading) {
                            Text(roaster.name ?? "Unknown")
                                .font(.headline)
                            Text("City ID: \(roaster.cityId)")
                                .font(.subheadline)
                        }
                    }
                }
            }
            .onAppear(perform: loadRoasters)
            .navigationTitle("Posiadane Kawy")
        }
    }
    
    private func loadRoasters() {
        apiService.fetchRoasters(cityId: "city-id") // Zastąp "city-id" odpowiednim identyfikatorem miasta
            .sink(receiveCompletion: { completion in
                switch completion {
                case .failure(let error):
                    DispatchQueue.main.async {
                        self.errorMessage = error.localizedDescription
                        self.isLoading = false
                    }
                case .finished:
                    break
                }
            }, receiveValue: { roasters in
                DispatchQueue.main.async {
                    self.roasters = roasters
                    self.isLoading = false
                }
            })
            .store(in: &cancellables)
    }
}
Leave a Comment