refactor(MessageManager): enhance conversation list handling and integrate local source fallback
- Update MessageManager to support both remote and local conversation list sources, improving data retrieval and offline capabilities. - Refactor hooks and components to utilize the new MessageManager structure, ensuring seamless integration of cursor pagination and local caching. - Introduce new types and methods for better management of conversation data and loading states. - Modify MessageListScreen to leverage the updated hooks for fetching conversations, enhancing user experience with improved loading and pagination handling.
This commit is contained in:
@@ -16,17 +16,20 @@ interface UseConversationsReturn {
|
||||
conversations: ConversationResponse[];
|
||||
isLoading: boolean;
|
||||
refresh: () => Promise<void>;
|
||||
loadMore: () => Promise<void>;
|
||||
hasMore: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话列表
|
||||
* 用于MessageListScreen等需要显示会话列表的组件
|
||||
* 获取会话列表(仅消费 MessageManager;网络游标与 SQLite 回退均在 Manager 内完成,调用方无感)
|
||||
* 用于 MessageListScreen 等需要显示会话列表的组件
|
||||
*/
|
||||
export function useConversations(): UseConversationsReturn {
|
||||
const [conversations, setConversations] = useState<ConversationResponse[]>(
|
||||
() => messageManager.getConversations()
|
||||
);
|
||||
const [isLoading, setIsLoading] = useState(() => messageManager.isLoading());
|
||||
const [hasMore, setHasMore] = useState(() => messageManager.canLoadMoreConversations());
|
||||
|
||||
useEffect(() => {
|
||||
// 订阅MessageManager的事件
|
||||
@@ -34,6 +37,10 @@ export function useConversations(): UseConversationsReturn {
|
||||
switch (event.type) {
|
||||
case 'conversations_updated':
|
||||
setConversations(messageManager.getConversations());
|
||||
setHasMore(messageManager.canLoadMoreConversations());
|
||||
break;
|
||||
case 'conversations_loading':
|
||||
setIsLoading(!!event.payload?.loading);
|
||||
break;
|
||||
case 'connection_changed':
|
||||
// 连接状态变化时可能需要刷新
|
||||
@@ -60,15 +67,19 @@ export function useConversations(): UseConversationsReturn {
|
||||
}, []);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
setIsLoading(true);
|
||||
await messageManager.fetchConversations(true);
|
||||
setIsLoading(false);
|
||||
}, []);
|
||||
|
||||
const loadMore = useCallback(async () => {
|
||||
await messageManager.loadMoreConversations();
|
||||
}, []);
|
||||
|
||||
return {
|
||||
conversations,
|
||||
isLoading,
|
||||
refresh,
|
||||
loadMore,
|
||||
hasMore,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -672,6 +683,8 @@ interface UseMessageListReturn {
|
||||
conversations: ConversationResponse[];
|
||||
isLoading: boolean;
|
||||
refresh: () => Promise<void>;
|
||||
loadMore: () => Promise<void>;
|
||||
hasMore: boolean;
|
||||
totalUnreadCount: number;
|
||||
systemUnreadCount: number;
|
||||
markAllAsRead: () => Promise<void>;
|
||||
@@ -679,7 +692,7 @@ interface UseMessageListReturn {
|
||||
}
|
||||
|
||||
export function useMessageList(): UseMessageListReturn {
|
||||
const { conversations, isLoading, refresh } = useConversations();
|
||||
const { conversations, isLoading, refresh, loadMore, hasMore } = useConversations();
|
||||
const { totalUnreadCount, systemUnreadCount } = useUnreadCount();
|
||||
const { markAllAsRead, isMarking } = useMarkAsRead(null);
|
||||
|
||||
@@ -687,6 +700,8 @@ export function useMessageList(): UseMessageListReturn {
|
||||
conversations,
|
||||
isLoading,
|
||||
refresh,
|
||||
loadMore,
|
||||
hasMore,
|
||||
totalUnreadCount,
|
||||
systemUnreadCount,
|
||||
markAllAsRead,
|
||||
|
||||
Reference in New Issue
Block a user