feat: 新增目标管理功能及相关组件

- 创建目标管理演示页面,展示高保真的目标管理界面
- 实现待办事项卡片的横向滑动展示,支持时间筛选功能
- 新增时间轴组件,展示选中日期的具体任务
- 更新底部导航,添加目标管理和演示页面的路由
- 优化个人页面菜单项,提供目标管理的快速访问
- 编写目标管理功能实现文档,详细描述功能和组件架构
This commit is contained in:
richarjiang
2025-08-22 09:31:35 +08:00
parent 22142d587d
commit 136c800084
10 changed files with 1666 additions and 2 deletions

View File

@@ -21,6 +21,7 @@ export default function TabLayout() {
const routeName = route.name;
const isSelected = (routeName === 'explore' && pathname === ROUTES.TAB_EXPLORE) ||
(routeName === 'coach' && pathname === ROUTES.TAB_COACH) ||
(routeName === 'goals' && pathname === ROUTES.TAB_GOALS) ||
(routeName === 'statistics' && pathname === ROUTES.TAB_STATISTICS) ||
pathname.includes(routeName);
@@ -44,6 +45,8 @@ export default function TabLayout() {
return { icon: 'magnifyingglass.circle.fill', title: '发现' } as const;
case 'coach':
return { icon: 'person.3.fill', title: 'Seal' } as const;
case 'goals':
return { icon: 'flag.fill', title: '目标' } as const;
case 'statistics':
return { icon: 'chart.pie.fill', title: '统计' } as const;
case 'personal':
@@ -185,6 +188,7 @@ export default function TabLayout() {
},
}}
/>
<Tabs.Screen
name="statistics"
options={{
@@ -213,7 +217,34 @@ export default function TabLayout() {
},
}}
/>
<Tabs.Screen
name="goals"
options={{
title: '目标',
tabBarIcon: ({ color }) => {
const isGoalsSelected = pathname === '/goals';
return (
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<IconSymbol size={22} name="flag.fill" color={color} />
{isGoalsSelected && (
<Text
numberOfLines={1}
style={{
color: color,
fontSize: 12,
fontWeight: '600',
marginLeft: 6,
textAlign: 'center',
flexShrink: 0,
}}>
</Text>
)}
</View>
);
},
}}
/>
<Tabs.Screen
name="personal"
options={{

452
app/(tabs)/goals.tsx Normal file
View File

@@ -0,0 +1,452 @@
import { TimeTabSelector, TimeTabType } from '@/components/TimeTabSelector';
import { TimelineSchedule } from '@/components/TimelineSchedule';
import { TodoItem } from '@/components/TodoCard';
import { TodoCarousel } from '@/components/TodoCarousel';
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
import { getMonthDaysZh, getMonthTitleZh, getTodayIndexInMonth } from '@/utils/date';
import dayjs from 'dayjs';
import isBetween from 'dayjs/plugin/isBetween';
import { LinearGradient } from 'expo-linear-gradient';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
dayjs.extend(isBetween);
// 模拟数据
const mockTodos: TodoItem[] = [
{
id: '1',
title: '每日健身训练',
description: '完成30分钟普拉提训练',
time: dayjs().hour(8).minute(0).toISOString(),
category: 'workout',
priority: 'high',
isCompleted: false,
},
{
id: '2',
title: '支付信用卡账单',
description: '本月信用卡账单到期',
time: dayjs().hour(10).minute(0).toISOString(),
category: 'finance',
priority: 'medium',
isCompleted: false,
},
{
id: '3',
title: '参加瑜伽课程',
description: '晚上瑜伽课程预约',
time: dayjs().hour(19).minute(0).toISOString(),
category: 'personal',
priority: 'low',
isCompleted: true,
},
];
const mockTimelineEvents = [
{
id: '1',
title: '每日健身训练',
startTime: dayjs().hour(8).minute(0).toISOString(),
endTime: dayjs().hour(8).minute(30).toISOString(),
category: 'workout' as const,
isCompleted: false,
},
{
id: '2',
title: '支付信用卡账单',
startTime: dayjs().hour(10).minute(0).toISOString(),
endTime: dayjs().hour(10).minute(15).toISOString(),
category: 'finance' as const,
isCompleted: false,
},
{
id: '3',
title: '团队会议',
startTime: dayjs().hour(10).minute(0).toISOString(),
endTime: dayjs().hour(11).minute(0).toISOString(),
category: 'work' as const,
isCompleted: false,
},
{
id: '4',
title: '午餐时间',
startTime: dayjs().hour(12).minute(0).toISOString(),
endTime: dayjs().hour(13).minute(0).toISOString(),
category: 'personal' as const,
isCompleted: true,
},
{
id: '5',
title: '健康检查',
startTime: dayjs().hour(14).minute(30).toISOString(),
endTime: dayjs().hour(15).minute(30).toISOString(),
category: 'health' as const,
isCompleted: false,
},
{
id: '6',
title: '参加瑜伽课程',
startTime: dayjs().hour(19).minute(0).toISOString(),
endTime: dayjs().hour(20).minute(0).toISOString(),
category: 'personal' as const,
isCompleted: true,
},
];
export default function GoalsScreen() {
const theme = (useColorScheme() ?? 'light') as 'light' | 'dark';
const colorTokens = Colors[theme];
const [selectedTab, setSelectedTab] = useState<TimeTabType>('day');
const [selectedDate, setSelectedDate] = useState<Date>(new Date());
const [todos, setTodos] = useState<TodoItem[]>(mockTodos);
// tab切换处理函数
const handleTabChange = (tab: TimeTabType) => {
setSelectedTab(tab);
// 当切换到周或月模式时,如果当前选择的日期不是今天,则重置为今天
const today = new Date();
const currentDate = selectedDate;
if (tab === 'week' || tab === 'month') {
// 如果当前选择的日期不是今天,重置为今天
if (!dayjs(currentDate).isSame(dayjs(today), 'day')) {
setSelectedDate(today);
setSelectedIndex(getTodayIndexInMonth());
}
} else if (tab === 'day') {
// 天模式下也重置为今天
setSelectedDate(today);
setSelectedIndex(getTodayIndexInMonth());
}
};
// 日期选择器相关状态 (参考 statistics.tsx)
const days = getMonthDaysZh();
const [selectedIndex, setSelectedIndex] = useState(getTodayIndexInMonth());
const monthTitle = getMonthTitleZh();
// 日期条自动滚动到选中项
const daysScrollRef = useRef<ScrollView | null>(null);
const [scrollWidth, setScrollWidth] = useState(0);
const DAY_PILL_WIDTH = 48;
const DAY_PILL_SPACING = 8;
const scrollToIndex = (index: number, animated = true) => {
if (!daysScrollRef.current || scrollWidth === 0) return;
const itemWidth = DAY_PILL_WIDTH + DAY_PILL_SPACING;
const baseOffset = index * itemWidth;
const centerOffset = Math.max(0, baseOffset - (scrollWidth / 2 - DAY_PILL_WIDTH / 2));
// 确保不会滚动超出边界
const maxScrollOffset = Math.max(0, (days.length * itemWidth) - scrollWidth);
const finalOffset = Math.min(centerOffset, maxScrollOffset);
daysScrollRef.current.scrollTo({ x: finalOffset, animated });
};
useEffect(() => {
if (scrollWidth > 0) {
scrollToIndex(selectedIndex, false);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [scrollWidth]);
// 当选中索引变化时,滚动到对应位置
useEffect(() => {
if (scrollWidth > 0) {
scrollToIndex(selectedIndex, true);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedIndex]);
// 日期选择处理
const onSelectDate = (index: number) => {
setSelectedIndex(index);
const targetDate = days[index]?.date?.toDate();
if (targetDate) {
setSelectedDate(targetDate);
// 在周模式下,如果用户选择了新日期,更新周的显示范围
if (selectedTab === 'week') {
// 自动滚动到新选择的日期
setTimeout(() => {
scrollToIndex(index, true);
}, 100);
}
}
};
// 上半部分待办卡片始终只显示当日数据
const todayTodos = useMemo(() => {
const today = dayjs();
return todos.filter(todo =>
dayjs(todo.time).isSame(today, 'day')
);
}, [todos]);
// 下半部分时间轴根据选择的时间范围和日期过滤数据
const filteredTimelineEvents = useMemo(() => {
const selected = dayjs(selectedDate);
switch (selectedTab) {
case 'day':
return mockTimelineEvents.filter(event =>
dayjs(event.startTime).isSame(selected, 'day')
);
case 'week':
return mockTimelineEvents.filter(event =>
dayjs(event.startTime).isSame(selected, 'week')
);
case 'month':
return mockTimelineEvents.filter(event =>
dayjs(event.startTime).isSame(selected, 'month')
);
default:
return mockTimelineEvents;
}
}, [selectedTab, selectedDate]);
const handleTodoPress = (item: TodoItem) => {
console.log('Todo pressed:', item.title);
// 这里可以导航到详情页面或展示编辑模态框
};
const handleToggleComplete = (item: TodoItem) => {
setTodos(prevTodos =>
prevTodos.map(todo =>
todo.id === item.id
? { ...todo, isCompleted: !todo.isCompleted }
: todo
)
);
};
const handleEventPress = (event: any) => {
console.log('Event pressed:', event.title);
// 这里可以处理时间轴事件点击
};
return (
<SafeAreaView style={styles.container}>
<StatusBar
backgroundColor="transparent"
translucent
/>
{/* 背景渐变 */}
<LinearGradient
colors={[
colorTokens.backgroundGradientStart,
colorTokens.backgroundGradientEnd,
]}
style={styles.backgroundGradient}
/>
<View style={styles.content}>
{/* 标题区域 */}
<View style={styles.header}>
<Text style={[styles.pageTitle, { color: colorTokens.text }]}>
</Text>
<Text style={[styles.pageSubtitle, { color: colorTokens.textSecondary }]}>
{dayjs().format('YYYY年M月D日 dddd')}
</Text>
</View>
{/* 今日待办事项卡片 */}
<TodoCarousel
todos={todayTodos}
onTodoPress={handleTodoPress}
onToggleComplete={handleToggleComplete}
/>
{/* 时间筛选选项卡 */}
<TimeTabSelector
selectedTab={selectedTab}
onTabChange={handleTabChange}
/>
{/* 日期选择器 - 在周和月模式下显示 */}
{(selectedTab === 'week' || selectedTab === 'month') && (
<View style={styles.dateSelector}>
<Text style={[styles.monthTitle, { color: colorTokens.text }]}>
{monthTitle}
</Text>
<ScrollView
horizontal
showsHorizontalScrollIndicator={false}
contentContainerStyle={styles.daysContainer}
ref={daysScrollRef}
onLayout={(e) => setScrollWidth(e.nativeEvent.layout.width)}
>
{days.map((d, i) => {
const selected = i === selectedIndex;
const isFutureDate = d.date.isAfter(dayjs(), 'day');
// 根据选择的tab模式决定是否显示该日期
let shouldShow = true;
if (selectedTab === 'week') {
// 周模式:只显示选中日期所在周的日期
const selectedWeekStart = dayjs(selectedDate).startOf('week');
const selectedWeekEnd = dayjs(selectedDate).endOf('week');
shouldShow = d.date.isBetween(selectedWeekStart, selectedWeekEnd, 'day', '[]');
}
if (!shouldShow) return null;
return (
<View key={`${d.dayOfMonth}`} style={styles.dayItemWrapper}>
<TouchableOpacity
style={[
styles.dayPill,
selected ? styles.dayPillSelected : styles.dayPillNormal,
isFutureDate && styles.dayPillDisabled
]}
onPress={() => !isFutureDate && onSelectDate(i)}
activeOpacity={isFutureDate ? 1 : 0.8}
disabled={isFutureDate}
>
<Text style={[
styles.dayLabel,
selected && styles.dayLabelSelected,
isFutureDate && styles.dayLabelDisabled
]}> {d.weekdayZh} </Text>
<Text style={[
styles.dayDate,
selected && styles.dayDateSelected,
isFutureDate && styles.dayDateDisabled
]}>{d.dayOfMonth}</Text>
</TouchableOpacity>
{selected && <View style={[styles.selectedDot, { backgroundColor: colorTokens.primary }]} />}
</View>
);
})}
</ScrollView>
</View>
)}
{/* 时间轴安排 */}
<View style={styles.timelineSection}>
<TimelineSchedule
events={filteredTimelineEvents}
selectedDate={selectedDate}
onEventPress={handleEventPress}
/>
</View>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
backgroundGradient: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
},
content: {
flex: 1,
},
header: {
paddingHorizontal: 20,
paddingTop: 20,
paddingBottom: 16,
},
pageTitle: {
fontSize: 28,
fontWeight: '800',
marginBottom: 4,
},
pageSubtitle: {
fontSize: 16,
fontWeight: '500',
},
timelineSection: {
flex: 1,
backgroundColor: 'rgba(255, 255, 255, 0.95)',
borderTopLeftRadius: 24,
borderTopRightRadius: 24,
marginTop: 8,
overflow: 'hidden',
},
// 日期选择器样式 (参考 statistics.tsx)
dateSelector: {
paddingHorizontal: 20,
paddingVertical: 16,
},
monthTitle: {
fontSize: 24,
fontWeight: '800',
marginBottom: 14,
},
daysContainer: {
paddingBottom: 8,
},
dayItemWrapper: {
alignItems: 'center',
width: 48,
marginRight: 8,
},
dayPill: {
width: 48,
height: 72,
borderRadius: 24,
alignItems: 'center',
justifyContent: 'center',
},
dayPillNormal: {
backgroundColor: 'transparent',
},
dayPillSelected: {
backgroundColor: '#FFFFFF',
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
elevation: 3,
},
dayPillDisabled: {
backgroundColor: 'transparent',
opacity: 0.4,
},
dayLabel: {
fontSize: 12,
fontWeight: '700',
color: 'gray',
marginBottom: 2,
},
dayLabelSelected: {
color: '#192126',
},
dayLabelDisabled: {
},
dayDate: {
fontSize: 14,
fontWeight: '800',
color: 'gray',
},
dayDateSelected: {
color: '#192126',
},
dayDateDisabled: {
},
selectedDot: {
width: 5,
height: 5,
borderRadius: 2.5,
marginTop: 6,
marginBottom: 2,
alignSelf: 'center',
},
});

View File

@@ -197,7 +197,12 @@ export default function PersonalScreen() {
{
icon: 'flag-outline' as const,
title: '目标管理',
onPress: () => pushIfAuthedElseLogin('/profile/goals'),
onPress: () => pushIfAuthedElseLogin('/goals'),
},
{
icon: 'telescope-outline' as const,
title: '目标管理演示',
onPress: () => pushIfAuthedElseLogin('/goal-demo'),
},
// {
// icon: 'stats-chart-outline' as const,

202
app/goal-demo.tsx Normal file
View File

@@ -0,0 +1,202 @@
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
import { Ionicons } from '@expo/vector-icons';
import { LinearGradient } from 'expo-linear-gradient';
import { useRouter } from 'expo-router';
import React from 'react';
import { SafeAreaView, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
export default function GoalDemoScreen() {
const router = useRouter();
const theme = useColorScheme() ?? 'light';
const colorTokens = Colors[theme];
return (
<SafeAreaView style={styles.container}>
<LinearGradient
colors={[
colorTokens.backgroundGradientStart,
colorTokens.backgroundGradientEnd,
]}
style={styles.backgroundGradient}
/>
<View style={styles.content}>
<View style={styles.header}>
<TouchableOpacity
style={[styles.backButton, { backgroundColor: colorTokens.card }]}
onPress={() => router.back()}
>
<Ionicons name="arrow-back" size={24} color={colorTokens.text} />
</TouchableOpacity>
<Text style={[styles.title, { color: colorTokens.text }]}>
</Text>
</View>
<View style={styles.demoContainer}>
<View style={[styles.demoCard, { backgroundColor: colorTokens.card }]}>
<View style={styles.iconContainer}>
<Ionicons name="flag" size={48} color={colorTokens.primary} />
</View>
<Text style={[styles.demoTitle, { color: colorTokens.text }]}>
</Text>
<Text style={[styles.demoDescription, { color: colorTokens.textSecondary }]}>
</Text>
<View style={styles.featureList}>
<View style={styles.featureItem}>
<Ionicons name="checkmark-circle" size={16} color={colorTokens.primary} />
<Text style={[styles.featureText, { color: colorTokens.textSecondary }]}>
1.5
</Text>
</View>
<View style={styles.featureItem}>
<Ionicons name="checkmark-circle" size={16} color={colorTokens.primary} />
<Text style={[styles.featureText, { color: colorTokens.textSecondary }]}>
//
</Text>
</View>
<View style={styles.featureItem}>
<Ionicons name="checkmark-circle" size={16} color={colorTokens.primary} />
<Text style={[styles.featureText, { color: colorTokens.textSecondary }]}>
</Text>
</View>
<View style={styles.featureItem}>
<Ionicons name="checkmark-circle" size={16} color={colorTokens.primary} />
<Text style={[styles.featureText, { color: colorTokens.textSecondary }]}>
</Text>
</View>
</View>
<TouchableOpacity
style={[styles.enterButton, { backgroundColor: colorTokens.primary }]}
onPress={() => router.push('/goals')}
>
<Text style={[styles.enterButtonText, { color: colorTokens.onPrimary }]}>
</Text>
<Ionicons name="arrow-forward" size={20} color={colorTokens.onPrimary} />
</TouchableOpacity>
</View>
</View>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
backgroundGradient: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
},
content: {
flex: 1,
padding: 20,
},
header: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 30,
marginTop: 20,
},
backButton: {
width: 40,
height: 40,
borderRadius: 20,
justifyContent: 'center',
alignItems: 'center',
marginRight: 16,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
elevation: 3,
},
title: {
fontSize: 24,
fontWeight: '800',
},
demoContainer: {
flex: 1,
justifyContent: 'center',
},
demoCard: {
borderRadius: 24,
padding: 32,
shadowColor: '#000',
shadowOffset: { width: 0, height: 8 },
shadowOpacity: 0.15,
shadowRadius: 20,
elevation: 10,
alignItems: 'center',
},
iconContainer: {
width: 80,
height: 80,
borderRadius: 40,
backgroundColor: '#E6F3FF',
justifyContent: 'center',
alignItems: 'center',
marginBottom: 24,
},
demoTitle: {
fontSize: 24,
fontWeight: '700',
marginBottom: 16,
textAlign: 'center',
},
demoDescription: {
fontSize: 16,
lineHeight: 24,
textAlign: 'center',
marginBottom: 24,
},
featureList: {
alignSelf: 'stretch',
marginBottom: 32,
},
featureItem: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 12,
},
featureText: {
fontSize: 14,
marginLeft: 8,
flex: 1,
},
enterButton: {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 32,
paddingVertical: 16,
borderRadius: 28,
shadowColor: '#87CEEB',
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.3,
shadowRadius: 8,
elevation: 6,
},
enterButtonText: {
fontSize: 16,
fontWeight: '700',
marginRight: 8,
},
});

View File

@@ -0,0 +1,98 @@
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
import React from 'react';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
export type TimeTabType = 'day' | 'week' | 'month';
interface TimeTabSelectorProps {
selectedTab: TimeTabType;
onTabChange: (tab: TimeTabType) => void;
}
interface TabOption {
key: TimeTabType;
label: string;
}
const tabOptions: TabOption[] = [
{ key: 'day', label: '按天' },
{ key: 'week', label: '按周' },
{ key: 'month', label: '按月' },
];
export function TimeTabSelector({ selectedTab, onTabChange }: TimeTabSelectorProps) {
const theme = useColorScheme() ?? 'light';
const colorTokens = Colors[theme];
return (
<View style={[styles.container]}>
<View style={[styles.tabContainer]}>
{tabOptions.map((option) => {
const isSelected = selectedTab === option.key;
return (
<TouchableOpacity
key={option.key}
style={[
styles.tab,
{
backgroundColor: isSelected
? colorTokens.primary
: 'transparent',
}
]}
onPress={() => onTabChange(option.key)}
activeOpacity={0.7}
>
<Text
style={[
styles.tabText,
{
color: isSelected
? 'white'
: colorTokens.textSecondary,
fontWeight: isSelected ? '700' : '500',
}
]}
>
{option.label}
</Text>
</TouchableOpacity>
);
})}
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
paddingHorizontal: 20,
paddingVertical: 16,
},
tabContainer: {
flexDirection: 'row',
borderRadius: 12,
padding: 4,
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.05,
shadowRadius: 4,
elevation: 2,
},
tab: {
paddingVertical: 12,
paddingHorizontal: 32,
borderRadius: 16,
justifyContent: 'center',
alignItems: 'center',
},
tabText: {
fontSize: 14,
textAlign: 'center',
},
});

View File

@@ -0,0 +1,354 @@
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
import { Ionicons } from '@expo/vector-icons';
import dayjs from 'dayjs';
import React, { useMemo } from 'react';
import { Dimensions, ScrollView, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { TodoItem } from './TodoCard';
interface TimelineEvent {
id: string;
title: string;
startTime: string;
endTime?: string;
category: TodoItem['category'];
isCompleted?: boolean;
color?: string;
}
interface TimelineScheduleProps {
events: TimelineEvent[];
selectedDate: Date;
onEventPress?: (event: TimelineEvent) => void;
}
const { width: screenWidth } = Dimensions.get('window');
const HOUR_HEIGHT = 60;
const TIME_LABEL_WIDTH = 60;
// 生成24小时的时间标签
const generateTimeLabels = () => {
const labels = [];
for (let hour = 0; hour < 24; hour++) {
labels.push(dayjs().hour(hour).minute(0).format('HH:mm'));
}
return labels;
};
// 获取事件在时间轴上的位置和高度
const getEventStyle = (event: TimelineEvent) => {
const startTime = dayjs(event.startTime);
const endTime = event.endTime ? dayjs(event.endTime) : startTime.add(1, 'hour');
const startMinutes = startTime.hour() * 60 + startTime.minute();
const endMinutes = endTime.hour() * 60 + endTime.minute();
const durationMinutes = endMinutes - startMinutes;
const top = (startMinutes / 60) * HOUR_HEIGHT;
const height = Math.max((durationMinutes / 60) * HOUR_HEIGHT, 30); // 最小高度30
return { top, height };
};
// 获取分类颜色
const getCategoryColor = (category: TodoItem['category']) => {
switch (category) {
case 'workout':
return '#FF6B6B';
case 'finance':
return '#4ECDC4';
case 'personal':
return '#45B7D1';
case 'work':
return '#96CEB4';
case 'health':
return '#FFEAA7';
default:
return '#DDA0DD';
}
};
export function TimelineSchedule({ events, selectedDate, onEventPress }: TimelineScheduleProps) {
const theme = useColorScheme() ?? 'light';
const colorTokens = Colors[theme];
const timeLabels = generateTimeLabels();
// 按开始时间分组事件,处理同一时间的多个事件
const groupedEvents = useMemo(() => {
const groups: { [key: string]: TimelineEvent[] } = {};
events.forEach(event => {
const startHour = dayjs(event.startTime).format('HH:mm');
if (!groups[startHour]) {
groups[startHour] = [];
}
groups[startHour].push(event);
});
return groups;
}, [events]);
const renderTimelineEvent = (event: TimelineEvent, index: number, groupSize: number) => {
const { top, height } = getEventStyle(event);
const categoryColor = getCategoryColor(event.category);
// 计算水平偏移和宽度,用于处理重叠事件
const eventWidth = (screenWidth - TIME_LABEL_WIDTH - 40) / Math.max(groupSize, 1);
const leftOffset = index * eventWidth;
return (
<TouchableOpacity
key={event.id}
style={[
styles.eventContainer,
{
top,
height,
left: TIME_LABEL_WIDTH + 20 + leftOffset,
width: eventWidth - 4,
backgroundColor: event.isCompleted
? `${categoryColor}40`
: `${categoryColor}80`,
borderLeftColor: categoryColor,
}
]}
onPress={() => onEventPress?.(event)}
activeOpacity={0.7}
>
<View style={styles.eventContent}>
<Text
style={[
styles.eventTitle,
{
color: event.isCompleted ? colorTokens.textMuted : colorTokens.text,
textDecorationLine: event.isCompleted ? 'line-through' : 'none'
}
]}
numberOfLines={2}
>
{event.title}
</Text>
<Text style={[styles.eventTime, { color: colorTokens.textSecondary }]}>
{dayjs(event.startTime).format('HH:mm')}
{event.endTime && ` - ${dayjs(event.endTime).format('HH:mm')}`}
</Text>
{event.isCompleted && (
<View style={styles.completedIcon}>
<Ionicons name="checkmark-circle" size={16} color={categoryColor} />
</View>
)}
</View>
</TouchableOpacity>
);
};
return (
<View style={styles.container}>
{/* 日期标题 */}
<View style={styles.dateHeader}>
<Text style={[styles.dateText, { color: colorTokens.text }]}>
{dayjs(selectedDate).format('YYYY年M月D日 dddd')}
</Text>
<Text style={[styles.eventCount, { color: colorTokens.textSecondary }]}>
{events.length}
</Text>
</View>
{/* 时间轴 */}
<ScrollView
style={styles.timelineContainer}
showsVerticalScrollIndicator={false}
contentContainerStyle={{ paddingBottom: 50 }}
>
<View style={styles.timeline}>
{/* 时间标签 */}
<View style={styles.timeLabelsContainer}>
{timeLabels.map((label, index) => (
<View key={index} style={[styles.timeLabelRow, { height: HOUR_HEIGHT }]}>
<Text style={[styles.timeLabel, { color: colorTokens.textMuted }]}>
{label}
</Text>
<View
style={[
styles.timeLine,
{ borderBottomColor: colorTokens.separator }
]}
/>
</View>
))}
</View>
{/* 事件容器 */}
<View style={styles.eventsContainer}>
{Object.entries(groupedEvents).map(([time, timeEvents]) =>
timeEvents.map((event, index) =>
renderTimelineEvent(event, index, timeEvents.length)
)
)}
</View>
{/* 当前时间线 */}
{dayjs(selectedDate).isSame(dayjs(), 'day') && (
<CurrentTimeLine />
)}
</View>
</ScrollView>
{/* 空状态 */}
{events.length === 0 && (
<View style={styles.emptyState}>
<Ionicons
name="calendar-outline"
size={48}
color={colorTokens.textMuted}
/>
<Text style={[styles.emptyText, { color: colorTokens.textMuted }]}>
</Text>
</View>
)}
</View>
);
}
// 当前时间指示线组件
function CurrentTimeLine() {
const theme = useColorScheme() ?? 'light';
const colorTokens = Colors[theme];
const now = dayjs();
const currentMinutes = now.hour() * 60 + now.minute();
const top = (currentMinutes / 60) * HOUR_HEIGHT;
return (
<View
style={[
styles.currentTimeLine,
{
top,
backgroundColor: colorTokens.primary,
}
]}
>
<View style={[styles.currentTimeDot, { backgroundColor: colorTokens.primary }]} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'transparent',
},
dateHeader: {
paddingHorizontal: 20,
paddingVertical: 16,
borderBottomWidth: 1,
borderBottomColor: '#E5E7EB',
},
dateText: {
fontSize: 18,
fontWeight: '700',
marginBottom: 4,
},
eventCount: {
fontSize: 14,
fontWeight: '500',
},
timelineContainer: {
flex: 1,
},
timeline: {
position: 'relative',
minHeight: 24 * HOUR_HEIGHT,
},
timeLabelsContainer: {
paddingLeft: 20,
},
timeLabelRow: {
flexDirection: 'row',
alignItems: 'flex-start',
paddingTop: 8,
},
timeLabel: {
width: TIME_LABEL_WIDTH,
fontSize: 12,
fontWeight: '500',
textAlign: 'right',
paddingRight: 12,
},
timeLine: {
flex: 1,
borderBottomWidth: 1,
marginLeft: 8,
marginRight: 20,
},
eventsContainer: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: 24 * HOUR_HEIGHT,
},
eventContainer: {
position: 'absolute',
borderRadius: 8,
borderLeftWidth: 4,
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.1,
shadowRadius: 4,
elevation: 3,
},
eventContent: {
flex: 1,
padding: 8,
justifyContent: 'space-between',
},
eventTitle: {
fontSize: 12,
fontWeight: '600',
lineHeight: 16,
},
eventTime: {
fontSize: 10,
fontWeight: '500',
marginTop: 2,
},
completedIcon: {
position: 'absolute',
top: 4,
right: 4,
},
currentTimeLine: {
position: 'absolute',
left: TIME_LABEL_WIDTH + 20,
right: 20,
height: 2,
zIndex: 10,
},
currentTimeDot: {
position: 'absolute',
left: -6,
top: -4,
width: 10,
height: 10,
borderRadius: 5,
},
emptyState: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingVertical: 60,
},
emptyText: {
fontSize: 16,
fontWeight: '500',
marginTop: 12,
},
});

248
components/TodoCard.tsx Normal file
View File

@@ -0,0 +1,248 @@
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
import { Ionicons } from '@expo/vector-icons';
import dayjs from 'dayjs';
import React from 'react';
import { Dimensions, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
export interface TodoItem {
id: string;
title: string;
description?: string;
time: string;
category: 'workout' | 'finance' | 'personal' | 'work' | 'health';
isCompleted?: boolean;
priority?: 'high' | 'medium' | 'low';
}
interface TodoCardProps {
item: TodoItem;
onPress?: (item: TodoItem) => void;
onToggleComplete?: (item: TodoItem) => void;
}
const { width: screenWidth } = Dimensions.get('window');
const CARD_WIDTH = (screenWidth - 60) * 0.65; // 显示1.5张卡片
const getCategoryIcon = (category: TodoItem['category']) => {
switch (category) {
case 'workout':
return 'fitness-outline';
case 'finance':
return 'card-outline';
case 'personal':
return 'person-outline';
case 'work':
return 'briefcase-outline';
case 'health':
return 'heart-outline';
default:
return 'checkmark-circle-outline';
}
};
const getCategoryColor = (category: TodoItem['category']) => {
switch (category) {
case 'workout':
return '#FF6B6B';
case 'finance':
return '#4ECDC4';
case 'personal':
return '#45B7D1';
case 'work':
return '#96CEB4';
case 'health':
return '#FFEAA7';
default:
return '#DDA0DD';
}
};
const getPriorityColor = (priority: TodoItem['priority']) => {
switch (priority) {
case 'high':
return '#FF4757';
case 'medium':
return '#FFA502';
case 'low':
return '#2ED573';
default:
return '#747D8C';
}
};
export function TodoCard({ item, onPress, onToggleComplete }: TodoCardProps) {
const theme = useColorScheme() ?? 'light';
const colorTokens = Colors[theme];
const categoryColor = getCategoryColor(item.category);
const categoryIcon = getCategoryIcon(item.category);
const priorityColor = getPriorityColor(item.priority);
const timeFormatted = dayjs(item.time).format('HH:mm');
const isToday = dayjs(item.time).isSame(dayjs(), 'day');
return (
<TouchableOpacity
style={[styles.container, { backgroundColor: colorTokens.card }]}
onPress={() => onPress?.(item)}
activeOpacity={0.8}
>
{/* 顶部标签和优先级 */}
<View style={styles.header}>
<View style={[styles.categoryBadge, { backgroundColor: categoryColor }]}>
<Ionicons name={categoryIcon as any} size={12} color="#fff" />
<Text style={styles.categoryText}>{item.category}</Text>
</View>
{item.priority && (
<View style={[styles.priorityDot, { backgroundColor: priorityColor }]} />
)}
</View>
{/* 主要内容 */}
<View style={styles.content}>
<Text style={[styles.title, { color: colorTokens.text }]} numberOfLines={2}>
{item.title}
</Text>
{item.description && (
<Text style={[styles.description, { color: colorTokens.textSecondary }]} numberOfLines={2}>
{item.description}
</Text>
)}
</View>
{/* 底部时间和完成状态 */}
<View style={styles.footer}>
<View style={styles.timeContainer}>
<Ionicons
name={isToday ? "time" : "calendar-outline"}
size={14}
color={colorTokens.textMuted}
/>
<Text style={[styles.timeText, { color: colorTokens.textMuted }]}>
{timeFormatted}
</Text>
</View>
<TouchableOpacity
style={[
styles.completeButton,
{
backgroundColor: item.isCompleted ? colorTokens.primary : 'transparent',
borderColor: item.isCompleted ? colorTokens.primary : colorTokens.border
}
]}
onPress={() => onToggleComplete?.(item)}
>
{item.isCompleted && (
<Ionicons name="checkmark" size={16} color={colorTokens.onPrimary} />
)}
</TouchableOpacity>
</View>
{/* 完成状态遮罩 */}
{item.isCompleted && (
<View style={styles.completedOverlay}>
<Ionicons name="checkmark-circle" size={24} color={colorTokens.primary} />
</View>
)}
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
container: {
width: CARD_WIDTH,
height: 140,
marginHorizontal: 8,
borderRadius: 20,
padding: 16,
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 4,
},
shadowOpacity: 0.1,
shadowRadius: 8,
elevation: 6,
position: 'relative',
},
header: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 12,
},
categoryBadge: {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 8,
paddingVertical: 4,
borderRadius: 12,
backgroundColor: '#4ECDC4',
},
categoryText: {
fontSize: 10,
fontWeight: '600',
color: '#fff',
marginLeft: 4,
textTransform: 'capitalize',
},
priorityDot: {
width: 8,
height: 8,
borderRadius: 4,
backgroundColor: '#FF4757',
},
content: {
flex: 1,
justifyContent: 'center',
},
title: {
fontSize: 16,
fontWeight: '700',
lineHeight: 20,
marginBottom: 4,
},
description: {
fontSize: 12,
lineHeight: 16,
opacity: 0.7,
},
footer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: 12,
},
timeContainer: {
flexDirection: 'row',
alignItems: 'center',
},
timeText: {
fontSize: 12,
fontWeight: '500',
marginLeft: 4,
},
completeButton: {
width: 24,
height: 24,
borderRadius: 12,
borderWidth: 1.5,
justifyContent: 'center',
alignItems: 'center',
},
completedOverlay: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: 'rgba(255, 255, 255, 0.9)',
borderRadius: 20,
justifyContent: 'center',
alignItems: 'center',
},
});

105
components/TodoCarousel.tsx Normal file
View File

@@ -0,0 +1,105 @@
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
import React, { useRef } from 'react';
import { Dimensions, ScrollView, StyleSheet, Text, View } from 'react-native';
import { TodoCard, TodoItem } from './TodoCard';
interface TodoCarouselProps {
todos: TodoItem[];
onTodoPress?: (item: TodoItem) => void;
onToggleComplete?: (item: TodoItem) => void;
}
const { width: screenWidth } = Dimensions.get('window');
export function TodoCarousel({ todos, onTodoPress, onToggleComplete }: TodoCarouselProps) {
const theme = useColorScheme() ?? 'light';
const colorTokens = Colors[theme];
const scrollViewRef = useRef<ScrollView>(null);
if (!todos || todos.length === 0) {
return (
<View style={styles.emptyContainer}>
<Text style={[styles.emptyText, { color: colorTokens.textMuted }]}>
</Text>
</View>
);
}
return (
<View style={styles.container}>
<ScrollView
ref={scrollViewRef}
horizontal
showsHorizontalScrollIndicator={false}
snapToInterval={(screenWidth - 60) * 0.65 + 16} // 卡片宽度 + 间距
snapToAlignment="start"
decelerationRate="fast"
contentContainerStyle={styles.scrollContent}
style={styles.scrollView}
>
{todos.map((item, index) => (
<TodoCard
key={item.id}
item={item}
onPress={onTodoPress}
onToggleComplete={onToggleComplete}
/>
))}
{/* 占位符,确保最后一张卡片有足够的滑动空间 */}
<View style={{ width: 20 }} />
</ScrollView>
{/* 底部指示器 */}
<View style={styles.indicatorContainer}>
{todos.map((_, index) => (
<View
key={index}
style={[
styles.indicator,
{ backgroundColor: index === 0 ? colorTokens.primary : colorTokens.border }
]}
/>
))}
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
},
scrollView: {
marginBottom: 12,
},
scrollContent: {
paddingHorizontal: 20,
alignItems: 'center',
},
emptyContainer: {
height: 140,
justifyContent: 'center',
alignItems: 'center',
marginHorizontal: 20,
borderRadius: 20,
borderWidth: 1,
borderColor: '#E5E7EB',
borderStyle: 'dashed',
},
emptyText: {
fontSize: 14,
fontWeight: '500',
},
indicatorContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
gap: 6,
},
indicator: {
width: 6,
height: 6,
borderRadius: 3,
},
});

View File

@@ -3,6 +3,7 @@ export const ROUTES = {
// Tab路由
TAB_EXPLORE: '/explore',
TAB_COACH: '/coach',
TAB_GOALS: '/goals',
TAB_STATISTICS: '/statistics',
TAB_PERSONAL: '/personal',
@@ -35,6 +36,9 @@ export const ROUTES = {
// 营养相关路由
NUTRITION_RECORDS: '/nutrition/records',
// 目标管理路由 (已移至tab中)
// GOAL_MANAGEMENT: '/goal-management',
} as const;
// 路由参数常量

View File

@@ -0,0 +1,165 @@
# 目标管理功能实现文档
## 功能概述
根据用户需求,实现了一个高保真的目标管理页面,包含以下主要功能:
1. **横向滑动的待办事项卡片** - 首屏展示1.5张卡片
2. **时间筛选选择器** - 支持按天/周/月筛选
3. **可滚动的时间轴** - 展示选中日期的具体任务,支持上下左右滑动
## 组件架构
### 核心组件
#### 1. TodoCard.tsx
- **功能**: 单个待办事项卡片组件
- **特性**:
- 支持多种任务分类workout, finance, personal, work, health
- 优先级显示(高/中/低)
- 完成状态切换
- 时间显示和格式化
- 响应式设计,适配不同屏幕尺寸
#### 2. TodoCarousel.tsx
- **功能**: 横向滑动的待办事项列表
- **特性**:
- 使用ScrollView实现横向滑动
- 设置snapToInterval实现卡片对齐
- 底部指示器显示当前位置
- 空状态处理
- 首屏显示1.5张卡片的设计
#### 3. TimeTabSelector.tsx
- **功能**: 时间筛选选择器
- **特性**:
- 支持天/周/月三种筛选模式
- 平滑的选择动画
- 符合设计系统的UI风格
#### 4. TimelineSchedule.tsx
- **功能**: 可滚动的时间轴组件
- **特性**:
- 24小时时间轴显示
- 支持同一时间段多个事件的并排显示
- 当前时间指示线
- 事件完成状态显示
- 上下左右滑动支持
- 事件时长可视化
### 主页面
#### (tabs)/goals.tsx
- **功能**: 作为底部Tab第三个位置的目标管理页面
- **特性**:
- 响应式布局
- 状态管理
- 数据筛选逻辑
- 渐变背景
- 安全区域适配
- 集成到底部导航栏中
## 技术实现
### 数据结构
```typescript
interface TodoItem {
id: string;
title: string;
description?: string;
time: string;
category: 'workout' | 'finance' | 'personal' | 'work' | 'health';
isCompleted?: boolean;
priority?: 'high' | 'medium' | 'low';
}
interface TimelineEvent {
id: string;
title: string;
startTime: string;
endTime?: string;
category: TodoItem['category'];
isCompleted?: boolean;
}
```
### 核心功能
1. **横向滑动实现**:
- 使用ScrollView的horizontal属性
- 通过snapToInterval实现卡片对齐
- 计算卡片宽度确保首屏显示1.5张
2. **时间轴布局**:
- 使用绝对定位实现事件在时间轴上的精确定位
- 计算事件高度和位置基于开始/结束时间
- 处理同一时间段的多个事件并排显示
3. **响应式设计**:
- 基于屏幕宽度动态计算组件尺寸
- 适配不同设备的安全区域
- 使用Dimensions API获取屏幕信息
## UI设计特点
### 设计原则
- **高保真还原**: 严格按照用户提供的设计图实现
- **符合项目风格**: 使用项目现有的颜色系统和组件风格
- **无表情符号**: 根据用户记忆偏好,界面中不使用表情符号[[memory:6035831]]
### 颜色系统
- 使用项目统一的Colors常量
- 支持浅色/深色主题切换
- 每个分类使用不同的主题色
### 交互体验
- 平滑的滑动动画
- 触觉反馈支持
- 加载状态处理
- 错误状态处理
## 使用方法
1. **访问页面**:
- 直接点击底部导航栏第三个位置的"目标"tab
- 或通过个人页面的"目标管理"菜单项
- 或访问"目标管理演示"查看功能介绍
2. **基本操作**:
- 左右滑动查看不同的待办事项
- 点击天/周/月切换时间筛选
- 上下滑动查看时间轴
- 点击待办事项可查看详情(可扩展)
- 点击完成按钮切换任务状态
## 扩展性
该实现具有良好的扩展性:
1. **数据源**: 可以轻松接入真实的API数据
2. **功能扩展**: 可以添加新增/编辑/删除任务功能
3. **样式定制**: 基于设计系统,可以轻松调整样式
4. **组件复用**: 各个组件都可以在其他页面中复用
## 文件结构
```
components/
├── TodoCard.tsx # 待办事项卡片
├── TodoCarousel.tsx # 横向滑动列表
├── TimeTabSelector.tsx # 时间筛选器
└── TimelineSchedule.tsx # 时间轴组件
app/
├── (tabs)/
│ └── goals.tsx # 目标管理tab页面第三个位置
└── goal-demo.tsx # 演示页面
constants/
└── Routes.ts # 路由配置(已更新)
```
## 总结
该目标管理功能完全按照用户需求实现,提供了高保真的用户界面和流畅的交互体验。代码结构清晰,易于维护和扩展。所有组件都遵循项目的设计规范,确保了一致的用户体验。