// // BooksView.swift // Bible-Week // // Aba Bíblia: lista os livros agrupados por testamento. // import SwiftUI struct BooksView: View { @State private var books: [Book] = [] @State private var isLoading = false @State private var errorMessage: String? @State private var showingAbout = false /// Livros do Antigo Testamento, em ordem canônica. private var oldTestament: [Book] { books.filter { $0.testament == "OT" }.sorted { $0.bookOrder < $1.bookOrder } } /// Livros do Novo Testamento, em ordem canônica. private var newTestament: [Book] { books.filter { $0.testament == "NT" }.sorted { $0.bookOrder < $1.bookOrder } } var body: some View { NavigationStack { Group { if isLoading { ProgressView("Carregando livros...") } else if let errorMessage { ContentUnavailableView { Label("Não foi possível carregar", systemImage: "wifi.slash") } description: { Text(errorMessage) } actions: { Button("Tentar novamente") { Task { await loadBooks() } } } } else { bookList } } .navigationTitle("Bíblia") .toolbar { ToolbarItem(placement: .topBarTrailing) { Button { showingAbout = true } label: { Image(systemName: "info.circle") } .accessibilityLabel("Sobre o app") } } .sheet(isPresented: $showingAbout) { AboutView() } } .task { if books.isEmpty { await loadBooks() } } } private var bookList: some View { List { Section("Antigo Testamento") { ForEach(oldTestament) { book in bookRow(book) } } Section("Novo Testamento") { ForEach(newTestament) { book in bookRow(book) } } } .navigationDestination(for: Book.self) { book in ChapterReaderView(book: book) } } private func bookRow(_ book: Book) -> some View { NavigationLink(value: book) { HStack { Text(book.name) Spacer() Text("\(book.chapterCount) cap.") .font(.caption) .foregroundStyle(.secondary) } } } private func loadBooks() async { isLoading = true errorMessage = nil defer { isLoading = false } do { books = try await BibleAPI.books() } catch { errorMessage = "Verifique sua conexão e tente novamente." } } } #Preview { BooksView() }