# ianfagundes/cosmonow architecture

> Architecture diagram of the GitHub repository ianfagundes/cosmonow, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ianfagundes/cosmonow
- Repository: https://github.com/ianfagundes/cosmonow

## Overview

##### Project Type and Purpose
- Type: Native iOS application
- Purpose: Display space imagery/video/info (Cosmo), allow CRUD on favorites, support offline cache, dark mode
- Architecture: Clean Architecture (Presentation, Domain, Data layers), MVVM, SOLID, Dependency Injection, Combine/Async-Await

##### File Structure
- Top-level “Presentation”, “Domain”, “Data”, “Infra” folders → classic layered separation
- “Presentation” subdivided into Views, ViewModels, Components (SwiftUI + Combine)
- “Domain” holds business entities and use-cases (GetCosmoUseCase) + protocols
- “Data” contains concrete Repositories/Services implementing Domain protocols
- “Infra” provides low-level implementations: network, cache, storage, media managers
- Tests mirror these layers (DataTests, DomainTests, PresentationTests)

##### Review README Insights
- Technologies: SwiftUI, Combine, Async/Await, URLSession, local caching (images, thumbnails, API data), DI
- Key features: fetching space data, thumbnails (YouTube), caching layers, favorites CRUD, error handling, dark mode
- Emphasis on modularity and testability

A. Frontend (Presentation Layer)
– SwiftUI Views (ContentView, CosmoView, CosmoDetailView, FavoritesView, MainTabView)
– ViewModels (CosmoViewModel, FavoritesViewModel, CachedImageViewModel)
– Reusable UI Components (FavoriteButton, DatePickerModalView, CachedAsyncImage, MediaView, ThumbnailView, YouTubeWebView)
B. Domain Layer
– Entities (CosmoModel)
– Use Cases (GetCosmoUseCase) + Protocols
C. Data Layer
– Repositories (CosmoRepository conforming to CosmoRepositoryProtocol)
– Services (CosmoService conforming to CosmoServiceProtocol)
D. Infrastructure Layer
– Network: NetworkManager, APIConfig, URLSessionProtocol, NetworkError
– Cache: DataCacheManager, ImageCacheManager, Youtube thumbnail cache
– Storage: FavoritesManager (persistent favorites)
– MediaManager (video playback support)
E. External Services
– Remote Space Data API (e.g., NASA)
– YouTube API (video thumbnails)
– Device Local Storage (UserDefaults, FileSystem)

##### Relationships & Data Flows
4) Repository delegates to Service or Cache:
- If cached data exists → return from DataCacheManager
- Else → NetworkManager → Remote API → parse into CosmoModel → store in cache → return
5) FavoritesViewModel uses FavoritesManager in Infra (persistent CRUD)
6) Media caching handled by ImageCacheManager / MediaManager → View renders via CachedAsyncImage, YouTubeWebView
7) Errors propagate back up to ViewModel for UI error states

##### Architectural Patterns & Principles
- Clean Architecture: strict layer boundaries and dependency inversion via protocols
- MVVM: separation between UI (Views) and state/business logic (ViewModels)
- SOLID + DI: each component has a single responsibility, injected via initializers
- Reactive & Async: Combine pipelines and Swift’s async/await for concurrency
- Caching strategy: multi-tier (API data, images, video thumbnails)

##### Required Elements for Clarity
- Optional: zoomed-in inset for critical flows (e.g., caching path)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        App["CosmoNowApp"]:::presentation
        ContentView["ContentView"]:::presentation
        MainTabView["MainTabView"]:::presentation
        CosmoView["CosmoView"]:::presentation
        CosmoDetailView["CosmoDetailView"]:::presentation
        FavoritesView["FavoritesView"]:::presentation
        CosmoViewModel["CosmoViewModel"]:::presentation
        FavoritesViewModel["FavoritesViewModel"]:::presentation
        CachedImageViewModel["CachedImageViewModel"]:::presentation
        CachedAsyncImage["CachedAsyncImage"]:::presentation
        FavoriteButton["FavoriteButton"]:::presentation
        DatePickerModalView["DatePickerModalView"]:::presentation
        MediaView["MediaView"]:::presentation
        ThumbnailView["ThumbnailView"]:::presentation
        YouTubeWebView["YouTubeWebView"]:::presentation
    end

    %% Domain Layer
    subgraph "Domain Layer"
        direction TB
        CosmoModel["CosmoModel"]:::domain
        GetCosmoUseCaseProtocol["GetCosmoUseCaseProtocol"]:::domain
        GetCosmoUseCase["GetCosmoUseCase"]:::domain
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        CosmoRepositoryProtocol["CosmoRepositoryProtocol"]:::data
        CosmoServiceProtocol["CosmoServiceProtocol"]:::data
        CosmoRepository["CosmoRepository"]:::data
        CosmoService["CosmoService"]:::data
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        NetworkManager["NetworkManager"]:::infra
        APIConfig["APIConfig"]:::infra
        URLSessionProtocol["URLSessionProtocol"]:::infra
        NetworkError["NetworkError"]:::infra
        DataCacheManager["DataCacheManager"]:::infra
        ImageCacheManager["ImageCacheManager"]:::infra
        FavoritesManager["FavoritesManager"]:::infra
        MediaManager["MediaManager"]:::infra
    end

    %% External Services
    subgraph "External Services"
        direction TB
        SpaceAPI["Remote Space Data API"]:::external
        YouTubeAPI["YouTube API"]:::external
        LocalStorage["Device Local Storage"]:::external
    end

    %% Connections
    App -->|launches| ContentView
    ContentView -->|"binds to"| CosmoViewModel
    MainTabView -->|contains| CosmoView
    MainTabView -->|contains| FavoritesView
    CosmoView -->|"uses"| CosmoViewModel
    FavoritesView -->|"uses"| FavoritesViewModel
    CosmoViewModel -->|"calls"| GetCosmoUseCaseProtocol
    FavoritesViewModel -->|"uses"| FavoritesManager
    CachedImageViewModel -->|"uses"| ImageCacheManager
    GetCosmoUseCase -->|"implements"| GetCosmoUseCaseProtocol
    GetCosmoUseCase -->|"calls"| CosmoRepositoryProtocol
    CosmoRepository -->|"implements"| CosmoRepositoryProtocol
    CosmoRepository -->|"uses"| CosmoServiceProtocol
    CosmoRepository -->|"uses"| DataCacheManager
    CosmoService -->|"implements"| CosmoServiceProtocol
    CosmoService -->|"calls"| NetworkManager
    NetworkManager -->|"config via"| APIConfig
    NetworkManager -->|"uses"| URLSessionProtocol
    NetworkManager -->|"returns errors"| NetworkError
    DataCacheManager -->|"persists to"| LocalStorage
    ImageCacheManager -->|"persists to"| LocalStorage
    FavoritesManager -->|"persists to"| LocalStorage
    MediaManager -->|"handles media for"| MediaView
    CosmoService -->|"fetches from"| SpaceAPI
    ThumbnailView -->|"fetches thumbnail via"| YouTubeAPI
    YouTubeWebView -->|"renders video"| YouTubeAPI

    %% Click Events for Presentation
    click App "https://github.com/ianfagundes/cosmonow/blob/main/CosmoNow/CosmoNowApp.swift"
    click ContentView "https://github.com/ianfagundes/cosmonow/blob/main/CosmoNow/ContentView.swift"
    click MainTabView "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Views/MainTabView.swift"
    click CosmoView "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Views/CosmoView.swift"
    click CosmoDetailView "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Views/CosmoDetailView.swift"
    click FavoritesView "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Views/FavoritesView.swift"
    click CosmoViewModel "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/ViewModels/CosmoViewModel.swift"
    click FavoritesViewModel "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/ViewModels/FavoritesViewModel.swift"
    click CachedImageViewModel "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/ViewModels/CachedImageViewModel.swift"
    click CachedAsyncImage "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Components/CachedAsyncImage.swift"
    click FavoriteButton "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Components/FavoriteButton.swift"
    click DatePickerModalView "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Components/DatePickerModalView.swift"
    click MediaView "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Components/MediaView.swift"
    click ThumbnailView "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Components/ThumbnailView.swift"
    click YouTubeWebView "https://github.com/ianfagundes/cosmonow/blob/main/Presentation/Components/YouTubeWebView.swift"

    %% Click Events for Domain
    click CosmoModel "https://github.com/ianfagundes/cosmonow/blob/main/Domain/Models/CosmoModel.swift"
    click GetCosmoUseCase "https://github.com/ianfagundes/cosmonow/blob/main/Domain/UseCases/GetCosmoUseCase.swift"
    click GetCosmoUseCaseProtocol "https://github.com/ianfagundes/cosmonow/blob/main/Domain/UseCases/Protocols/GetCosmoUseCaseProtocol.swift"

    %% Click Events for Data
    click CosmoRepositoryProtocol "https://github.com/ianfagundes/cosmonow/blob/main/Data/Protocols/CosmoRespositoryProtocol.swift"
    click CosmoServiceProtocol "https://github.com/ianfagundes/cosmonow/blob/main/Data/Protocols/CosmoServiceProtocol.swift"
    click CosmoRepository "https://github.com/ianfagundes/cosmonow/blob/main/Data/Repositories/CosmoRepository.swift"
    click CosmoService "https://github.com/ianfagundes/cosmonow/blob/main/Data/Services/CosmoService.swift"

    %% Click Events for Infra
    click NetworkManager "https://github.com/ianfagundes/cosmonow/blob/main/Infra/Network/NetworkManager.swift"
    click APIConfig "https://github.com/ianfagundes/cosmonow/blob/main/Infra/Network/APIConfig.swift"
    click URLSessionProtocol "https://github.com/ianfagundes/cosmonow/blob/main/Infra/Network/URLSessionProtocol.swift"
    click NetworkError "https://github.com/ianfagundes/cosmonow/blob/main/Infra/Network/NetworkError.swift"
    click DataCacheManager "https://github.com/ianfagundes/cosmonow/blob/main/Infra/Cache/DataCacheManager.swift"
    click ImageCacheManager "https://github.com/ianfagundes/cosmonow/blob/main/Infra/Cache/ImageCacheManager.swift"
    click FavoritesManager "https://github.com/ianfagundes/cosmonow/blob/main/Infra/Storage/FavoritesManager.swift"
    click MediaManager "https://github.com/ianfagundes/cosmonow/blob/main/Infra/Media/MediaManager.swift"

    %% Styles
    classDef presentation fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef domain fill:#90EE90,stroke:#333,stroke-width:1px
    classDef data fill:#FFA500,stroke:#333,stroke-width:1px
    classDef infra fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef external fill:#D8BFD8,stroke:#333,stroke-width:1px
```
