Skip to content

视图层概览

text
# Related Code
- Sources/GitHubNotifier/Views/
- Sources/GitHubNotifier/App/GitHubNotifierApp.swift

视图层次结构

视图职责

视图文件职责
MenuBarViewViews/MenuBarView.swift菜单栏弹出窗口主容器
HeaderViewViews/MenuBar/HeaderView.swift顶部导航和用户信息
FilterBarViewViews/MenuBar/FilterBarView.swift通知过滤条件
NotificationListViewViews/Notifications/NotificationListView.swift通知列表
NotificationRowViewViews/Notifications/NotificationRowView.swift单条通知行
ActivityListViewViews/Activity/ActivityListView.swift活动列表
SearchListViewViews/Search/SearchListView.swift搜索结果列表
SettingsViewViews/SettingsView.swift设置页面容器
swift
// GitHubNotifierApp.swift:68-105

var body: some Scene {
    MenuBarExtra {
        MenuBarView()
            .environment(notificationService)
            .environment(activityService)
            .environment(searchService)
    } label: {
        MenuBarLabel(unreadCount: notificationService.unreadCount)
    }
    .menuBarExtraStyle(.window)

    WindowGroup(for: WindowIdentifier.self) { $id in
        WindowView(identifier: id)
            .environment(notificationService)
            // ...
    }
}

组件库

通用组件

组件文件用途
AvatarViewComponents/AvatarView.swift用户头像 (Kingfisher 加载)
StateIconComponents/StateIcon.swiftPR/Issue 状态图标
CIStatusBadgeComponents/CIStatusBadge.swiftCI 状态徽章
TimeAgoTextComponents/TimeAgoText.swift相对时间显示
TabButtonComponents/TabButton.swift标签页按钮

状态图标映射

swift
// StateIcon.swift

enum StateIcon {
    static func icon(for state: PRState) -> String {
        switch state {
        case .open: return "PullRequestOpen"
        case .merged: return "PullRequestMerged"
        case .closed: return "PullRequestClosed"
        case .draft: return "PullRequestDraft"
        }
    }
}

数据绑定模式

视图通过 SwiftUI Environment 获取 Services:

swift
struct NotificationListView: View {
    @Environment(NotificationService.self) var service

    var body: some View {
        List(service.notifications) { notification in
            NotificationRowView(notification: notification)
        }
    }
}

Services 的 @Published 属性变化自动触发视图更新。

设置页面结构

每个 Tab 是独立的 SwiftUI View,通过 TabView 组织。