# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "iOS Device"
        subgraph "Wikipedia App"
            subgraph "Presentation Layer"
                VC[ViewControllers]
                Coord[Coordinators]
                SwiftUI[SwiftUI Components]
            end

            subgraph "Application Layer"
                AppVC[WMFAppViewController]
                Router[Router]
                AppDelegate[AppDelegate]
            end

            subgraph "Domain Layer"
                Fetcher[Fetchers]
                DC[Data Controllers]
                Models[Models]
            end

            subgraph "Infrastructure Layer"
                Session[Session]
                Service[WMFService]
                DataStore[MWKDataStore]
                CoreData[(Core Data)]
            end
        end

        subgraph "Extensions"
            Widget[Widgets Extension]
            Notif[Notification Service Extension]
            Sticker[Stickers Extension]
        end

        subgraph "Shared"
            WMFFramework[WMF Framework]
            WMFData[WMFData Package]
            WMFComponents[WMFComponents Package]
        end
    end

    subgraph "External Services"
        MediaWiki[MediaWiki API]
        RESTBase[RESTBase API]
        Wikidata[Wikidata API]
        Commons[Commons API]
        APNs[Apple Push Notification]
    end

    VC --> Coord
    Coord --> VC
    SwiftUI --> DC

    AppVC --> Router
    AppVC --> VC
    AppDelegate --> AppVC

    Fetcher --> Session
    DC --> Service
    DataStore --> CoreData

    Session --> MediaWiki
    Session --> RESTBase
    Service --> MediaWiki
    Service --> Wikidata
    Service --> Commons

    APNs --> Notif

    Widget --> WMFFramework
    Widget --> WMFData

    WMFComponents --> WMFData
```

## モジュール依存関係図

```mermaid
graph LR
    subgraph "Main App"
        Wikipedia[Wikipedia Target]
    end

    subgraph "Frameworks"
        WMF[WMF Framework]
    end

    subgraph "Swift Packages"
        WMFData[WMFData]
        WMFDataMocks[WMFDataMocks]
        WMFComponents[WMFComponents]
        WMFComponentsObjC[WMFComponentsObjC]
    end

    subgraph "Third Party"
        CocoaLumberjack[CocoaLumberjackSwift]
        SDWebImage[SDWebImage]
    end

    subgraph "Extensions"
        Widgets[Widgets Extension]
        NotifExt[Notification Extension]
    end

    Wikipedia --> WMF
    Wikipedia --> WMFData
    Wikipedia --> WMFComponents
    Wikipedia --> CocoaLumberjack

    WMFComponents --> WMFData
    WMFComponents --> WMFDataMocks
    WMFComponents --> WMFComponentsObjC
    WMFComponents --> SDWebImage

    WMFDataMocks --> WMFData

    Widgets --> WMF
    Widgets --> WMFData

    NotifExt --> WMF
```

## レイヤー構成図

```mermaid
graph TB
    subgraph "Presentation Layer"
        direction LR
        ArticleVC[ArticleViewController]
        SearchVC[SearchViewController]
        ExploreVC[ExploreViewController]
        SettingsVC[SettingsViewController]
        TalkPageVC[TalkPageViewController]

        ArticleCoord[ArticleCoordinator]
        SearchCoord[SearchCoordinator]
        LoginCoord[LoginCoordinator]
        ProfileCoord[ProfileCoordinator]
    end

    subgraph "Application Layer"
        direction LR
        AppVC[WMFAppViewController]
        Router[Router]
        Config[Configuration]
    end

    subgraph "Domain Layer"
        direction LR
        ArticleFetcher[ArticleFetcher]
        TalkPageFetcher[TalkPageFetcher]
        WikidataFetcher[WikidataFetcher]
        DiffFetcher[DiffFetcher]

        ArticleDC[ArticleDataController]
        WatchlistDC[WatchlistDataController]
        YearInReviewDC[YearInReviewDataController]
    end

    subgraph "Infrastructure Layer"
        direction LR
        Session[Session]
        WMFService[WMFBasicService]
        MWKDataStore[MWKDataStore]
        WMFCoreDataStore[WMFCoreDataStore]
        Cache[ImageCacheController]
    end

    Presentation --> Application
    Application --> Domain
    Domain --> Infrastructure
```

## データフロー図 (記事表示)

```mermaid
sequenceDiagram
    participant User
    participant Router
    participant Coordinator
    participant ViewController
    participant Fetcher
    participant Session
    participant API as MediaWiki API
    participant CoreData

    User->>Router: URL タップ
    Router->>Router: destination(for: url)
    Router->>Coordinator: .article(url)
    Coordinator->>ViewController: present()
    ViewController->>Fetcher: fetchArticle()
    Fetcher->>Session: performMediaWikiAPIGET()
    Session->>API: HTTPS Request
    API-->>Session: JSON Response
    Session-->>Fetcher: Parsed Data
    Fetcher-->>ViewController: Article Model
    ViewController->>ViewController: Render WebView
    ViewController->>CoreData: Save to History
```

## Core Data モデル構成図

```mermaid
erDiagram
    WMFArticle {
        string key PK
        string displayTitle
        string wikidataDescription
        string snippet
        date viewedDate
        date savedDate
        boolean isDownloaded
    }

    WMFContentGroup {
        string key PK
        int contentGroupKind
        date midnightUTCDate
        date date
        int siteURLIndex
    }

    WMFContent {
        string key PK
        blob content
    }

    CDPage {
        string key PK
        string projectID
        date timestamp
    }

    CDPageView {
        date timestamp PK
    }

    CDCategory {
        string key PK
        string categoryType
    }

    WMFContentGroup ||--o{ WMFContent : contains
    WMFContentGroup ||--o{ WMFArticle : references
    CDPage ||--o{ CDPageView : hasViews
    CDPage }o--o{ CDCategory : belongsTo
```

## ネットワーク通信フロー図

```mermaid
flowchart LR
    subgraph "App"
        Fetcher[Fetcher Classes]
        Session[Session]
        WMFService[WMFBasicService]
        Config[Configuration]
    end

    subgraph "URL Building"
        APIBuilder[APIURLComponentsBuilder]
    end

    subgraph "External APIs"
        MediaWiki[MediaWiki API<br/>w/api.php]
        RESTBase[RESTBase API<br/>api/rest_v1]
        MediaWikiRest[MediaWiki REST<br/>w/rest.php]
    end

    Fetcher --> Session
    Fetcher --> WMFService
    Session --> Config
    WMFService --> Config
    Config --> APIBuilder

    APIBuilder --> MediaWiki
    APIBuilder --> RESTBase
    APIBuilder --> MediaWikiRest
```

## Coordinator パターン図

```mermaid
graph TB
    subgraph "Navigation Flow"
        AppVC[WMFAppViewController]

        subgraph "Coordinators"
            ArticleCoord[ArticleCoordinator]
            SearchCoord[SearchCoordinator]
            LoginCoord[LoginCoordinator]
            ProfileCoord[ProfileCoordinator]
            SettingsCoord[SettingsCoordinator]
            TalkPageCoord[TalkPageCoordinator]
            WatchlistCoord[WatchlistCoordinator]
            LinkCoord[LinkCoordinator]
        end

        subgraph "View Controllers"
            ArticleVC[ArticleViewController]
            SearchVC[SearchViewController]
            LoginVC[LoginViewController]
            ProfileVC[ProfileViewController]
            SettingsVC[SettingsViewController]
            TalkPageVC[TalkPageViewController]
        end
    end

    AppVC --> ArticleCoord
    AppVC --> SearchCoord
    AppVC --> LoginCoord

    ArticleCoord --> ArticleVC
    SearchCoord --> SearchVC
    LoginCoord --> LoginVC
    ProfileCoord --> ProfileVC
    SettingsCoord --> SettingsVC
    TalkPageCoord --> TalkPageVC

    ArticleCoord -.->|delegate| AppVC
    TalkPageCoord -.->|delegate| ArticleCoord
    LinkCoord -.->|handles links| ArticleCoord
```

## 環境設定・ビルド構成図

```mermaid
graph TB
    subgraph "Build Configurations"
        Production[Production<br/>wikipedia.org]
        Staging[Staging<br/>beta.wmcloud.org]
        Local[Local<br/>localhost]
    end

    subgraph "Configuration Options"
        StagingOpts[StagingOptions<br/>- appsLabsforPCS<br/>- betaCluster]
        LocalOpts[LocalOptions<br/>- localPCS<br/>- localAnnouncements]
    end

    subgraph "Schemes"
        WikipediaScheme[Wikipedia Scheme]
        StagingScheme[Staging Scheme]
        LocalScheme[Local PCS Scheme]
        ExperimentalScheme[Experimental Scheme]
    end

    WikipediaScheme --> Production
    StagingScheme --> Staging
    StagingScheme --> StagingOpts
    LocalScheme --> Local
    LocalScheme --> LocalOpts
    ExperimentalScheme --> Production
```

## テーマ・スタイル管理図

```mermaid
graph LR
    subgraph "Theme System"
        Theme[Theme Protocol]

        subgraph "Theme Types"
            Light[Light Theme]
            Sepia[Sepia Theme]
            Dark[Dark Theme]
            Black[Black Theme]
        end

        subgraph "Components"
            Colors[Colors]
            WMFTheme[WMFTheme]
            Themeable[Themeable Protocol]
        end
    end

    subgraph "Usage"
        VC[ViewControllers]
        Cells[CollectionViewCells]
        Views[Custom Views]
    end

    Theme --> Light
    Theme --> Sepia
    Theme --> Dark
    Theme --> Black

    Theme --> Colors
    Theme --> WMFTheme

    VC -.->|implements| Themeable
    Cells -.->|implements| Themeable
    Views -.->|implements| Themeable

    Themeable --> Theme
```

## バックグラウンドタスク管理図

```mermaid
sequenceDiagram
    participant System as iOS System
    participant BGScheduler as BGTaskScheduler
    participant AppDelegate
    participant AppVC as WMFAppViewController
    participant Fetcher as BackgroundFetcher
    participant CoreData

    AppDelegate->>BGScheduler: register(forTaskWithIdentifier:)

    Note over System,CoreData: App enters background

    AppDelegate->>BGScheduler: submit(BGAppRefreshTaskRequest)

    Note over System,CoreData: Later...

    System->>BGScheduler: Trigger background task
    BGScheduler->>AppDelegate: Execute handler
    AppDelegate->>AppVC: performBackgroundFetch()
    AppVC->>Fetcher: Fetch new content
    Fetcher->>CoreData: Save updates
    Fetcher-->>AppVC: Completion
    AppVC-->>BGScheduler: setTaskCompleted(success:)
    AppDelegate->>BGScheduler: Schedule next task
```
