### SwiftUI App with Clean Architecture Source: https://github.com/nalexn/clean-architecture-swiftui/blob/master/README.md Showcases the setup of a SwiftUI app following Clean Architecture principles. It decouples presentation, business logic, and data access layers for maintainability and testability. The project utilizes Combine for asynchronous operations and SwiftData for persistence. ```Swift /* This project demonstrates the setup of a SwiftUI app with Clean Architecture. Key features: - Decoupled Presentation, Business Logic, and Data Access layers - Programmatic navigation - Redux-like centralized AppState - Native SwiftUI dependency injection - Handling of system events - Full test coverage - Networking layer with async/await - UI with SwiftUI + Combine - Data persistence with SwiftData */ // Example of a View in the Presentation Layer struct CountryListView: View { @EnvironmentObject var interactor: CountryListInteractor @StateObject var appState: AppState var body: some View { List(appState.countries) { CountryRow(country: $0) } .onAppear { interactor.fetchCountries() } } } // Example of an Interactor in the Business Logic Layer class CountryListInteractor { private let repository: CountryRepository @EnvironmentObject var appState: AppState init(repository: CountryRepository) { self.repository = repository } func fetchCountries() { Task { let countries = try await repository.getCountries() appState.countries = countries } } } // Example of a Repository in the Data Access Layer protocol CountryRepository { func getCountries() async throws -> [Country] } // Example of AppState class AppState: ObservableObject { @Published var countries: [Country] = [] } // Example of a Country model struct Country: Identifiable { let id = UUID() let name: String // ... other properties } ``` -------------------------------- ### Data Access Layer: Repositories and CRUD Operations Source: https://github.com/nalexn/clean-architecture-swiftui/blob/master/README.md Defines Repositories within the Data Access Layer, outlining their role in providing asynchronous API (Combine Publishers) for CRUD operations on backends or local databases. It clarifies that Repositories are devoid of business logic, do not mutate AppState, and are exclusively used by Interactors. ```APIDOC Data Access Layer: Components: Repositories API: - Asynchronous API (Publisher from Combine) - For CRUD operations on backend or local database Characteristics: - Do not contain business logic - Do not mutate AppState Accessibility: - Used only by Interactors ``` -------------------------------- ### Business Logic Layer: Interactors and Data Flow Source: https://github.com/nalexn/clean-architecture-swiftui/blob/master/README.md Explains the function of Interactors in the Business Logic Layer, detailing how they receive requests for work (data acquisition, computations) and forward results to AppState or Bindings, rather than returning data directly. It differentiates between using AppState for small data and Bindings for large, persistent data. ```APIDOC Business Logic Layer: Components: Interactors Responsibilities: - Receive requests to perform work (e.g., obtain data, computations) Data Flow: - Never return data directly - Forward results to AppState or a Binding Usage Scenarios: - AppState: For small data (e.g., last login email, loaded at launch) - Binding: For large amounts of data from a fully-fledged database (on-demand serving) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.