# klipy-com/ios-demo-app architecture

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

- Interactive diagram: https://gitdiagram.com/klipy-com/ios-demo-app
- Repository: https://github.com/klipy-com/ios-demo-app

## Overview

##### Project type and purpose
- This is a SwiftUI-based iOS demo application showcasing a Masonry-style media grid, chat UI, ad integration, and a robust networking layer.
- It follows MVVM + Use-Case + Service + Repository patterns to fetch/display media (GIFs, stickers, clips) from the Klipy API and to render ads securely via a custom WebView.

##### File structure
- Top-level “KlipyiOS-demo” Xcode project, plus:
- App/
– ChatFeature/ (chat views, view-model, coordinator)
– Components/ (reusable SwiftUI controls: ChatMessage, ChatPreview, CustomMenu, MediaSearchBar, MessageInput, WebView)
– Core/ (domain models + utilities)
– MasonryLayout/ (layout algorithm & views)
– DynamicMediaView/ (media browsing/search)
– ViewUtilities/
– AppDelegate.swift, ContentView.swift, RootView.swift, KlipyiOS_demoApp.swift
- Infrastructure/ (Models, Services, Use Cases, UserAgentManager)
- Networking/ (Moya-based core, async wrapper, error mapping, protocols)
- Utilities/ (JSON decoder)
- Assets, GoogleService-Info.plist, Info.plist
- Build scripts/config: buildServer.json
- README files in modules describe responsibilities.

##### Architecture from README
- Presentation: SwiftUI Views + ViewModels (MVVM)
- Business logic: Use-Case layer for each service
- Service layer: domain-agnostic API calls, ad-parameter injection
- Networking: Moya/Alamofire with async/await wrappers, error mapping
- Repository pattern for data abstraction (Infrastructure Models)
- MasonryLayoutCalculator drives the perfect grid display.

##### Main components & interactions
a. Presentation Layer
- SwiftUI Views (RootView → ContentView → Module Views: MasonryGridView, ChatView, DynamicMediaView)
- ViewModels (ChatFeatureViewModel, DynamicMediaViewModel, PreviewViewModel)
- MasonryLayoutCalculator & GridItemLayout/RowLayout utility
b. Domain / Use-Case Layer
- UseCase classes (GifServiceUseCase, ClipsServiceUseCase, StickersServiceUseCase, MediaServiceUseCase, HealthCheckServiceUseCase)
c. Service Layer
- Services (GifService, ClipsService, StickersService, HealthCheckService)
- AdParameters + UserAgentManager (header & param injection)
d. Networking Layer
- NetworkingProviderProtocol / RestApiProtocol → Moya targets (KlipyTargetType, KlipyMultiTarget)
- AsyncMoyaRequestWrapper → request() → API endpoints
- Error mapping (ApiErrorResponse, ApiErrorWrapper, MoyaErrorMapping)
e. Data Models & Caching
- Infrastructure Models → DomainModels (MediaFile, ClipItem, GifItem, StickerItem, Paging)
- JSONCustomDecoder / SDWebImage caching / LazyGIFView
f. External Systems
- Klipy REST API (media endpoints, ad endpoints)
- Ad rendering via KlipyWebView (UIViewRepresentable)
- Device resources (network, local cache, audio)

##### Relationships & data flows
- User taps UI → SwiftUI View invokes ViewModel → calls UseCase → UseCase uses Service → Service adds ad parameters & headers → Service asks Networking layer → Moya sends HTTP to Klipy API → Response mapped to Infrastructure Models → forwarded to UseCase → ViewModel transforms to UI Models → SwiftUI updates.
- MasonryLayoutCalculator receives media items → computes rows & item positions → MasonryGridView renders.

##### Architectural patterns & design principles
- MVVM for clear separation of View vs. state/logic
- Use-Case layer isolates business rules
- Service/Repository abstracts API details
- Networking protocols for testability
- Single-responsibility: each module (Chat, Components, DynamicMediaView) is self-contained
- Modular directory layout for feature isolation

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Presentation Layer
  subgraph "Presentation Layer"
    direction TB
    App["KlipyiOS_demoApp"]:::ui
    Root["RootView"]:::ui
    Content["ContentView"]:::ui
    ChatView["ChatView"]:::ui
    ChatVM["ChatFeatureViewModel"]:::ui
    DynView["DynamicMediaView"]:::ui
    DynVM["DynamicMediaViewModel"]:::ui
    MasonryView["MasonryGridView"]:::ui
    MasonryCalc["MasonryLayoutCalculator"]:::ui
    LazyGIF["LazyGIFView"]:::ui
    WebViewRep["KlipyWebViewRepresentable"]:::ui
    App --> Root
    Root --> Content
    Content --> ChatView
    Content --> DynView
    Content --> MasonryView
    ChatView <--> ChatVM
    DynView <--> DynVM
    MasonryView --> MasonryCalc
    MasonryView --> LazyGIF
    ChatView --> WebViewRep
  end

  %% Domain / UseCase Layer
  subgraph "Use-Case Layer"
    direction TB
    UC_Gif["GifServiceUseCase"]:::domain
    UC_Clips["ClipsServiceUseCase"]:::domain
    UC_Stick["StickersServiceUseCase"]:::domain
    UC_Media["MediaServiceUseCase"]:::domain
    UC_Health["HealthCheckServiceUseCase"]:::domain
  end

  %% Service Layer
  subgraph "Service Layer"
    direction TB
    Svc_Gif["GifService"]:::service
    Svc_Clips["ClipsService"]:::service
    Svc_Stick["StickersService"]:::service
    Svc_Health["HealthCheckService"]:::service
    AdParams["AdParameters"]:::service
    UA["UserAgentManager"]:::service
  end

  %% Networking Layer
  subgraph "Networking Layer"
    direction TB
    Prot_Net["NetworkingProviderProtocol"]:::network
    Prot_API["RestApiProtocol"]:::network
    Target["KlipyTargetType"]:::network
    MultiTgt["KlipyMultiTarget"]:::network
    AsyncWrap["AsyncMoyaRequestWrapper"]:::network
    ErrResp["APIErrorResponse"]:::network
    ErrWrap["ApiErrorWrapper"]:::network
    ErrMap["MoyaErrorMapping"]:::network
  end

  %% External Systems
  subgraph "External"
    direction TB
    KlipyAPI["Klipy REST API"]:::external
    Device["Device Resources"]:::external
  end

  %% Flows
  ChatVM -->|calls| UC_Media
  DynVM -->|calls| UC_Gif
  DynVM -->|calls| UC_Clips
  DynVM -->|calls| UC_Stick
  UC_Gif -->|uses| Svc_Gif
  UC_Clips -->|uses| Svc_Clips
  UC_Stick -->|uses| Svc_Stick
  UC_Media -->|uses| Svc_Gif
  UC_Media -->|uses| Svc_Clips
  UC_Media -->|uses| Svc_Stick
  UC_Health -->|uses| Svc_Health
  Svc_Gif -->|injects| AdParams
  Svc_Gif -->|injects| UA
  Svc_Clips -->|injects| AdParams
  Svc_Clips -->|injects| UA
  Svc_Stick -->|injects| AdParams
  Svc_Stick -->|injects| UA
  Svc_Health -->|injects| UA
  Svc_Gif -->|calls| AsyncWrap
  Svc_Clips -->|calls| AsyncWrap
  Svc_Stick -->|calls| AsyncWrap
  Svc_Health -->|calls| AsyncWrap
  AsyncWrap -->|implements| Prot_Net
  AsyncWrap -->|implements| Prot_API
  Prot_Net --> Target
  Prot_API --> Target
  Target -->|multi| MultiTgt
  AsyncWrap -->|maps errors via| ErrResp
  AsyncWrap -->|maps errors via| ErrWrap
  AsyncWrap -->|maps errors via| ErrMap
  AsyncWrap -->|sends HTTP| KlipyAPI
  KlipyAPI -->|responds| AsyncWrap
  KlipyAPI -->|serves ads| WebViewRep
  WebViewRep --> Device

  %% Click events
  click App "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/KlipyiOS_demoApp.swift"
  click Root "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/RootView.swift"
  click Content "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/ContentView.swift"
  click ChatView "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/App/ChatFeature/ChatView.swift"
  click ChatVM "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/App/ChatFeature/ChatFeatureViewModel.swift"
  click DynView "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/App/DynamicMediaView/DynamicMediaView.swift"
  click DynVM "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/App/DynamicMediaView/DynamicMediaViewModel.swift"
  click MasonryView "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/App/MasonryLayout/MasonryGridView.swift"
  click LazyGIF "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/App/MasonryLayout/LazyGIFView.swift"
  click MasonryCalc "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/App/MasonryLayout/RenderingPerfectGridAlgorithm/MasonryLayoutCalculator.swift"
  click UC_Gif "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/UseCases/GifServiceUseCase.swift"
  click UC_Clips "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/UseCases/ClipsServiceUseCase.swift"
  click UC_Stick "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/UseCases/StickersServiceUseCase.swift"
  click UC_Media "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/UseCases/MediaServiceUseCase.swift"
  click UC_Health "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/UseCases/HealthCheckServiceUseCase.swift"
  click Svc_Gif "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/Services/GifService.swift"
  click Svc_Clips "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/Services/ClipsService.swift"
  click Svc_Stick "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/Services/StickersService.swift"
  click Svc_Health "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/Services/HealthCheckService.swift"
  click AdParams "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/Models/AdParameters.swift"
  click UA "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Infrastructure/UserAgentManager.swift"
  click Prot_Net "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Networking/Core/NetworkingProviderProtocol.swift"
  click Prot_API "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Networking/Core/RestApiProtocol.swift"
  click Target "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Networking/Core/KlipyTargetType.swift"
  click MultiTgt "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Networking/Core/KlipyMultiTarget.swift"
  click AsyncWrap "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Networking/Core/AsyncMoyaRequestWrapper.swift"
  click ErrResp "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Networking/Core/Error/APIErrorResponse.swift"
  click ErrWrap "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Networking/Core/Error/ApiErrorWrapper.swift"
  click ErrMap "https://github.com/klipy-com/ios-demo-app/blob/main/KlipyiOS-demo/Networking/Core/Error/MoyaErrorMapping.swift"

  %% Styles
  classDef ui fill:#D0E8FF,stroke:#0366d6,color:#0366d6
  classDef domain fill:#E6F4EA,stroke:#28A745,color:#19692C
  classDef service fill:#E8F0FE,stroke:#1967D2,color:#1967D2
  classDef network fill:#FEF4E5,stroke:#E37400,color:#A0522D
  classDef external fill:#F0F0F0,stroke:#6A737D,color:#6A737D
```
