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

View File

@@ -0,0 +1,60 @@
/**
* 导航状态管理 Hook
* 管理导航相关状态,如当前路由、导航历史等
*/
import { useState, useCallback, useEffect } from 'react';
import { useNavigationState as useRNNavigationState, Route } from '@react-navigation/native';
import type { RootStackParamList } from '../../../navigation/types';
type TabName = 'HomeTab' | 'MessageTab' | 'ScheduleTab' | 'ProfileTab';
interface NavigationState {
currentTab: TabName;
isCollapsed: boolean;
isReady: boolean;
}
interface NavigationActions {
setCurrentTab: (tab: TabName) => void;
toggleCollapse: () => void;
setIsReady: (ready: boolean) => void;
}
/**
* 导航状态管理 Hook
*/
export function useNavigationState(): NavigationState & NavigationActions {
const [currentTab, setCurrentTab] = useState<TabName>('HomeTab');
const [isCollapsed, setIsCollapsed] = useState(false);
const [isReady, setIsReady] = useState(false);
const toggleCollapse = useCallback(() => {
setIsCollapsed(prev => !prev);
}, []);
return {
currentTab,
isCollapsed,
isReady,
setCurrentTab,
toggleCollapse,
setIsReady,
};
}
/**
* 获取当前路由名称
*/
export function useCurrentRouteName(): string | null {
const routeName = useRNNavigationState(state => state?.routes[state.index]?.name ?? null);
return routeName;
}
/**
* 检查是否在指定路由
*/
export function useIsRoute(routeName: keyof RootStackParamList): boolean {
const currentRoute = useCurrentRouteName();
return currentRoute === routeName;
}

View File

@@ -0,0 +1,110 @@
/**
* 导航服务层
* 提供全局导航功能,解耦组件与导航状态的直接依赖
*/
import { NavigationContainerRef, Route } from '@react-navigation/native';
class NavigationService {
private navigationRef: NavigationContainerRef<any> | null = null;
private isReady: boolean = false;
/**
* 设置导航引用
*/
setNavigationRef(ref: NavigationContainerRef<any> | null): void {
this.navigationRef = ref;
this.isReady = ref !== null;
}
/**
* 检查导航是否就绪
*/
isNavigationReady(): boolean {
return this.isReady && this.navigationRef !== null;
}
/**
* 导航到指定路由
*/
navigate(routeName: string, params?: any): void {
if (!this.isNavigationReady()) {
console.warn('[NavigationService] Navigation not ready');
return;
}
this.navigationRef!.navigate(routeName as any, params);
}
/**
* 返回上一页
*/
goBack(): void {
if (!this.isNavigationReady()) {
console.warn('[NavigationService] Navigation not ready');
return;
}
this.navigationRef!.goBack();
}
/**
* 获取当前路由
*/
getCurrentRoute(): Route<string> | null {
if (!this.isNavigationReady()) {
return null;
}
return this.navigationRef!.getCurrentRoute();
}
/**
* 获取当前路由名称
*/
getCurrentRouteName(): string | null {
const route = this.getCurrentRoute();
return route?.name ?? null;
}
/**
* 重置导航到指定路由
*/
reset(routes: { name: string; params?: any }[], index: number = 0): void {
if (!this.isNavigationReady()) {
console.warn('[NavigationService] Navigation not ready');
return;
}
this.navigationRef!.reset({
index,
routes: routes.map(r => ({ name: r.name, params: r.params })),
});
}
/**
* 替换当前路由
*/
replace(routeName: string, params?: any): void {
if (!this.isNavigationReady()) {
console.warn('[NavigationService] Navigation not ready');
return;
}
this.navigationRef!.dispatch({
type: 'REPLACE',
payload: { name: routeName, params },
});
}
/**
* 导航到根路由
*/
navigateToRoot(): void {
this.reset([{ name: 'Main' }]);
}
/**
* 导航到认证页面
*/
navigateToAuth(): void {
this.reset([{ name: 'Auth' }]);
}
}
export const navigationService = new NavigationService();

View File

@@ -0,0 +1,58 @@
/**
* 导航基础设施类型定义
*/
import { NavigationContainerRef, Route } from '@react-navigation/native';
import type {
RootStackParamList,
MainTabParamList,
HomeStackParamList,
MessageStackParamList,
ScheduleStackParamList,
ProfileStackParamList,
AuthStackParamList,
} from '../../navigation/types';
// ==================== 导航服务类型 ====================
export interface NavigationServiceInterface {
setNavigationRef(ref: NavigationContainerRef<any> | null): void;
isNavigationReady(): boolean;
navigate(routeName: string, params?: any): void;
goBack(): void;
getCurrentRoute(): Route<string> | null;
getCurrentRouteName(): string | null;
reset(routes: { name: string; params?: any }[], index?: number): void;
replace(routeName: string, params?: any): void;
navigateToRoot(): void;
navigateToAuth(): void;
}
// ==================== Tab 类型 ====================
export type TabName = 'HomeTab' | 'MessageTab' | 'ScheduleTab' | 'ProfileTab';
export interface NavItemConfig {
name: TabName;
label: string;
icon: string;
iconOutline: string;
}
// ==================== 导航配置常量 ====================
export const NAVIGATION_CONSTANTS = {
SIDEBAR_WIDTH_DESKTOP: 240,
SIDEBAR_WIDTH_TABLET: 200,
SIDEBAR_COLLAPSED_WIDTH: 72,
MOBILE_TAB_FLOATING_MARGIN: 12,
LAST_ACTIVE_TAB_KEY: 'last_active_tab',
} as const;
// ==================== 重新导出导航类型 ====================
export type {
RootStackParamList,
MainTabParamList,
HomeStackParamList,
MessageStackParamList,
ScheduleStackParamList,
ProfileStackParamList,
AuthStackParamList,
};