refactor: 架构重构 - 解耦过度耦合模块

主要改动:
1. 创建乐观更新工具函数 (optimisticUpdate.ts)
   - 消除 userStore.ts 中的重复代码

2. 拆分 useResponsive.ts (485行 -> 12个专注模块)
   - useBreakpoint: 断点检测
   - useOrientation: 方向检测
   - usePlatform: 平台检测
   - useScreenSize: 屏幕尺寸
   - useResponsiveValue: 响应式值
   - useResponsiveStyle: 响应式样式
   - useMediaQuery: 媒体查询
   - useColumnCount: 列数计算
   - useResponsiveSpacing: 响应式间距

3. 整理数据层 (Repository 层)
   - ApiDataSource: API数据源
   - LocalDataSource: 本地数据源
   - CacheDataSource: 缓存数据源
   - MessageRepository: 消息仓库

4. 重构 messageManager.ts (2194行 -> 4个模块)
   - MessageStateManager: 状态管理
   - WebSocketMessageHandler: WebSocket处理
   - MessageSyncService: 消息同步
   - ReadReceiptManager: 已读管理

5. 导航解耦 (MainNavigator.tsx: 1118行 -> 100行)
   - 创建 NavigationService 解耦层
   - 拆分多个 Navigator 组件

架构改进:
- 单一职责原则: 每个模块职责明确
- 依赖倒置: 通过接口解耦
- 代码复用: 工具函数可被多处使用
- 可测试性: 各模块可独立测试
This commit is contained in:
lafay
2026-03-18 12:11:49 +08:00
parent 1ffbb63753
commit a6cdb97e24
70 changed files with 8175 additions and 1728 deletions

153
src/data/models/index.ts Normal file
View File

@@ -0,0 +1,153 @@
/**
* Repository 层模型定义
* 定义应用内部使用的数据模型,与 API 响应和数据库结构解耦
*/
// ==================== 消息模型 ====================
export interface MessageSegment {
type: string;
data: Record<string, any>;
}
export interface MessageModel {
id: string;
conversationId: string;
senderId: string;
content?: string;
type: 'text' | 'image' | 'file' | 'system' | string;
isRead: boolean;
createdAt: Date;
seq: number;
status: 'normal' | 'recalled' | 'deleted' | string;
segments?: MessageSegment[];
replyToId?: string;
sender?: UserModel;
}
// ==================== 会话模型 ====================
export type ConversationType = 'private' | 'group';
export interface ConversationModel {
id: string;
type: ConversationType;
participantId: string;
lastMessageId?: string;
lastMessage?: MessageModel;
lastSeq: number;
myLastReadSeq: number;
otherLastReadSeq: number;
unreadCount: number;
isPinned: boolean;
participants?: UserModel[];
group?: GroupModel;
createdAt: Date;
updatedAt: Date;
}
// ==================== 用户模型 ====================
export interface UserModel {
id: string;
username: string;
nickname?: string;
avatar?: string;
bio?: string;
website?: string;
location?: string;
email?: string;
phone?: string;
followersCount?: number;
followingCount?: number;
postsCount?: number;
isFollowing?: boolean;
isBlocked?: boolean;
createdAt?: Date;
updatedAt?: Date;
}
// ==================== 帖子模型 ====================
export interface PostModel {
id: string;
authorId: string;
author?: UserModel;
title: string;
content: string;
images?: string[];
likeCount: number;
commentCount: number;
shareCount: number;
viewCount: number;
favoriteCount: number;
isLiked: boolean;
isFavorited: boolean;
isTop: boolean;
status: 'published' | 'draft' | 'deleted';
communityId?: string;
tags?: string[];
createdAt: Date;
updatedAt: Date;
}
// ==================== 群组模型 ====================
export type GroupMemberRole = 'owner' | 'admin' | 'member';
export type GroupJoinType = 'anyone' | 'approval' | 'invite';
export interface GroupMemberModel {
userId: string;
user?: UserModel;
role: GroupMemberRole;
nickname?: string;
joinTime: Date;
muteUntil?: Date;
isMuted?: boolean;
}
export interface GroupModel {
id: string;
name: string;
avatar?: string;
description?: string;
announcement?: string;
ownerId: string;
owner?: UserModel;
memberCount: number;
maxMemberCount: number;
joinType: GroupJoinType;
isMuted: boolean;
createdAt: Date;
updatedAt: Date;
}
// ==================== 分页模型 ====================
export interface PaginatedResult<T> {
list: T[];
total: number;
page: number;
pageSize: number;
totalPages: number;
hasMore: boolean;
}
// ==================== 通用操作结果 ====================
export interface OperationResult<T = void> {
success: boolean;
data?: T;
error?: string;
code?: string;
}
// ==================== 同步状态模型 ====================
export interface SyncStatus {
entityType: string;
entityId: string;
lastSyncedAt: Date;
syncVersion: number;
pendingChanges: boolean;
}