refactor(App, navigation): migrate to Expo Router and clean up navigation structure
- Updated App entry point to utilize Expo Router, simplifying the navigation setup. - Removed legacy navigation components and services to streamline the codebase. - Adjusted package.json to reflect the new entry point and updated dependencies for compatibility. - Enhanced overall application structure by consolidating navigation logic and improving maintainability.
This commit is contained in:
@@ -11,8 +11,15 @@
|
||||
import { AppState, AppStateStatus, Platform } from 'react-native';
|
||||
import * as BackgroundFetch from 'expo-background-fetch';
|
||||
import * as TaskManager from 'expo-task-manager';
|
||||
import * as Haptics from 'expo-haptics';
|
||||
import { sseService } from './sseService';
|
||||
import {
|
||||
triggerVibration,
|
||||
vibrateOnMessage,
|
||||
setVibrationConfig,
|
||||
getVibrationConfig,
|
||||
setVibrationEnabled,
|
||||
} from './messageVibrationService';
|
||||
export { triggerVibration, vibrateOnMessage, setVibrationConfig, getVibrationConfig, setVibrationEnabled };
|
||||
|
||||
// 后台任务名称
|
||||
const BACKGROUND_FETCH_TASK = 'background-fetch-keepalive';
|
||||
@@ -21,27 +28,8 @@ const REALTIME_KEEPALIVE_TASK = 'realtime-keepalive';
|
||||
// 后台任务间隔(Android 最小 15 分钟,iOS 最小 15 分钟)
|
||||
const BACKGROUND_INTERVAL = 15; // 15 分钟
|
||||
|
||||
// 震动配置
|
||||
interface VibrationConfig {
|
||||
enabled: boolean; // 是否启用震动
|
||||
onChatMessage: boolean; // 私聊消息震动
|
||||
onGroupMessage: boolean; // 群聊消息震动
|
||||
onNotification: boolean; // 系统通知震动
|
||||
style: Haptics.ImpactFeedbackStyle; // 震动样式
|
||||
}
|
||||
|
||||
// 默认震动配置
|
||||
const defaultVibrationConfig: VibrationConfig = {
|
||||
enabled: true,
|
||||
onChatMessage: true,
|
||||
onGroupMessage: true,
|
||||
onNotification: true,
|
||||
style: Haptics.ImpactFeedbackStyle.Light,
|
||||
};
|
||||
|
||||
// 后台服务状态
|
||||
let isInitialized = false;
|
||||
let vibrationConfig: VibrationConfig = { ...defaultVibrationConfig };
|
||||
let appStateSubscription: any = null;
|
||||
|
||||
// 定义后台任务
|
||||
@@ -73,96 +61,13 @@ TaskManager.defineTask(REALTIME_KEEPALIVE_TASK, async () => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* 触发震动反馈
|
||||
* @param type 震动类型
|
||||
*/
|
||||
export async function triggerVibration(
|
||||
type: 'light' | 'medium' | 'heavy' | 'success' | 'warning' | 'error' = 'light'
|
||||
): Promise<void> {
|
||||
if (!vibrationConfig.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
switch (type) {
|
||||
case 'light':
|
||||
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
|
||||
break;
|
||||
case 'medium':
|
||||
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
|
||||
break;
|
||||
case 'heavy':
|
||||
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
|
||||
break;
|
||||
case 'success':
|
||||
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
|
||||
break;
|
||||
case 'warning':
|
||||
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
|
||||
break;
|
||||
case 'error':
|
||||
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[BackgroundService] 震动反馈失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 收到消息时触发震动
|
||||
* @param messageType 消息类型
|
||||
*/
|
||||
export async function vibrateOnMessage(messageType: 'chat' | 'group_message' | 'notification'): Promise<void> {
|
||||
if (!vibrationConfig.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (messageType) {
|
||||
case 'chat':
|
||||
if (vibrationConfig.onChatMessage) {
|
||||
await triggerVibration('light');
|
||||
}
|
||||
break;
|
||||
case 'group_message':
|
||||
if (vibrationConfig.onGroupMessage) {
|
||||
await triggerVibration('light');
|
||||
}
|
||||
break;
|
||||
case 'notification':
|
||||
if (vibrationConfig.onNotification) {
|
||||
await triggerVibration('medium');
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置震动设置
|
||||
*/
|
||||
export function setVibrationConfig(config: Partial<VibrationConfig>): void {
|
||||
vibrationConfig = { ...vibrationConfig, ...config };
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前震动配置
|
||||
*/
|
||||
export function getVibrationConfig(): VibrationConfig {
|
||||
return { ...vibrationConfig };
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用/禁用震动
|
||||
*/
|
||||
export function setVibrationEnabled(enabled: boolean): void {
|
||||
vibrationConfig.enabled = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册后台任务
|
||||
*/
|
||||
async function registerBackgroundTasks(): Promise<void> {
|
||||
if (Platform.OS === 'web') {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 检查是否已注册
|
||||
const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_FETCH_TASK);
|
||||
@@ -192,6 +97,9 @@ async function registerBackgroundTasks(): Promise<void> {
|
||||
* 取消后台任务
|
||||
*/
|
||||
async function unregisterBackgroundTasks(): Promise<void> {
|
||||
if (Platform.OS === 'web') {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
|
||||
await BackgroundFetch.unregisterTaskAsync(REALTIME_KEEPALIVE_TASK);
|
||||
@@ -227,6 +135,12 @@ export async function initBackgroundService(): Promise<boolean> {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Platform.OS === 'web') {
|
||||
setupAppStateListener();
|
||||
isInitialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查后台任务状态
|
||||
const status = await BackgroundFetch.getStatusAsync();
|
||||
@@ -275,6 +189,13 @@ export async function checkBackgroundStatus(): Promise<{
|
||||
isInitialized: boolean;
|
||||
registeredTasks: string[];
|
||||
}> {
|
||||
if (Platform.OS === 'web') {
|
||||
return {
|
||||
isAvailable: false,
|
||||
isInitialized,
|
||||
registeredTasks: [],
|
||||
};
|
||||
}
|
||||
const status = await BackgroundFetch.getStatusAsync();
|
||||
const registeredTasks = await TaskManager.getRegisteredTasksAsync();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user