Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 3x 6x 6x 3x 3x 3x 6x 6x 6x 2x 4x | import { useState, useEffect, useCallback } from 'react';
import { TouchableOpacity, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useRouter, useFocusEffect } from 'expo-router';
import Ionicons from '@expo/vector-icons/Ionicons';
import { ScrollView, Text, YStack } from 'tamagui';
import { supabaseChatService, supabase } from '@/src/lib/supabase';
import { Button } from '@/src/design-system/components';
import { useThemeColors, useIsDarkMode } from '@/src/hooks/useThemeColors';
import {
AddPetModal,
PetDetailModal,
ProfileHeader,
ProfileTabs,
ReputationCard,
BadgeGrid,
BadgeDetailModal,
} from '../components';
import { usePetManagement, useProfileData, useReputation } from '../hooks';
import { BADGE_CONFIGS } from '@/src/constants/badges';
/**
* Profile 主屏幕组件
*/
export function ProfileScreen() {
const router = useRouter();
const insets = useSafeAreaInsets();
const colors = useThemeColors();
const isDark = useIsDarkMode();
// 使用自定义 hooks
const {
user,
isLoading,
isAuthenticated,
_hasHydrated,
fetchCurrentUser,
handleUnauthenticated,
} = useProfileData();
const {
petModalVisible,
selectedPet,
handleAddPet,
handleDeletePet,
openAddPetModal,
closeAddPetModal,
selectPet,
} = usePetManagement();
// 信誉分和勋章数据
const {
reputation,
badges,
loading: reputationLoading,
equipBadge,
unequipBadge,
refresh,
} = useReputation(user?.id);
// 勋章详情模态框
const [selectedBadge, setSelectedBadge] = useState<any>(null);
// 未读消息计数
const [unreadCount, setUnreadCount] = useState(0);
useEffect(() => {
loadUnreadCount();
// 订阅会话变化
const unsubscribeConversations = supabaseChatService.subscribeToConversations(() => {
loadUnreadCount();
});
// 订阅未读计数变化
const unreadChannel = supabase
.channel('profile_unread_counts')
.on(
'postgres_changes',
{
event: '*',
schema: 'public',
table: 'unread_counts',
},
() => {
loadUnreadCount();
}
)
.subscribe();
return () => {
unsubscribeConversations();
supabase.removeChannel(unreadChannel);
};
}, []);
// 当页面获得焦点时刷新未读计数和信誉分
useFocusEffect(
useCallback(() => {
loadUnreadCount();
refresh(); // 自动刷新信誉分
}, [refresh])
);
const loadUnreadCount = async () => {
const response = await supabaseChatService.getTotalUnreadCount();
Eif (response.success && response.data !== null) {
setUnreadCount(response.data);
}
};
// 获取已装备的勋章配置
const equippedBadge = badges.find((b) => b.is_equipped);
const equippedBadgeConfig = equippedBadge?.badge?.code
? BADGE_CONFIGS[equippedBadge.badge.code]
: null;
// 未认证视图
if (_hasHydrated && !isAuthenticated) {
return (
<YStack
flex={1}
backgroundColor={colors.background as any}
alignItems="center"
justifyContent="center"
padding="$6"
>
<YStack alignItems="center" gap="$4" maxWidth={400}>
<Text fontSize={24} fontWeight="700" color={colors.text as any}>
会话已过期
</Text>
<Text fontSize={16} color={colors.icon as any} textAlign="center">
您的登录状态已失效,请重新登录以继续查看个人资料与宠物信息。
</Text>
<Button
size="$5"
backgroundColor="$blue10"
color="white"
onPress={handleUnauthenticated}
marginTop="$4"
>
前往登录
</Button>
</YStack>
</YStack>
);
}
return (
<View testID="profile-screen" style={{ flex: 1 }}>
<ScrollView
flex={1}
backgroundColor={colors.background as any}
contentContainerStyle={{
paddingTop: insets.top,
paddingBottom: insets.bottom + 30,
}}
>
<YStack flex={1} alignItems="center" position="relative">
{/* 消息按钮 - 浮动在左上角 */}
<YStack position="absolute" top={20} left={20} zIndex={100}>
<TouchableOpacity
onPress={() => router.push('/(tabs)/profile/messages' as any)}
activeOpacity={0.7}
>
<YStack
width={44}
height={44}
borderRadius="$10"
backgroundColor={isDark ? 'rgba(30, 30, 30, 0.95)' : 'rgba(255, 255, 255, 0.95)'}
alignItems="center"
justifyContent="center"
shadowColor={isDark ? '#000' : '#000'}
shadowOffset={{ width: 0, height: 2 }}
shadowOpacity={isDark ? 0.3 : 0.15}
shadowRadius={4}
elevation={4}
>
<Ionicons name="chatbubbles-outline" size={22} color={colors.icon} />
{unreadCount > 0 && (
<View
style={{
position: 'absolute',
top: -4,
right: -4,
backgroundColor: colors.error,
borderRadius: 10,
minWidth: 20,
height: 20,
alignItems: 'center',
justifyContent: 'center',
paddingHorizontal: 4,
}}
>
<Text style={{ fontSize: 11, fontWeight: '700', color: '#FFFFFF' }}>
{unreadCount > 99 ? '99+' : unreadCount}
</Text>
</View>
)}
</YStack>
</TouchableOpacity>
</YStack>
{/* 设置按钮 - 浮动在右上角 */}
<YStack position="absolute" top={20} right={20} zIndex={100}>
<TouchableOpacity
testID="settings-button"
onPress={() => router.push('/(tabs)/profile/settings' as any)}
activeOpacity={0.7}
>
<YStack
width={44}
height={44}
borderRadius="$10"
backgroundColor={isDark ? 'rgba(30, 30, 30, 0.95)' : 'rgba(255, 255, 255, 0.95)'}
alignItems="center"
justifyContent="center"
shadowColor="#000"
shadowOffset={{ width: 0, height: 2 }}
shadowOpacity={isDark ? 0.3 : 0.15}
shadowRadius={4}
elevation={4}
>
<Ionicons name="settings-outline" size={22} color={colors.icon} />
</YStack>
</TouchableOpacity>
</YStack>
{/* 个人资料头部 - 用户头像和信息 */}
<ProfileHeader
username={user?.username}
bio={user?.bio || '这个人很懒,什么都没留下~'}
onAvatarUpdate={fetchCurrentUser}
equippedBadge={
equippedBadgeConfig
? {
icon: equippedBadgeConfig.icon,
color: equippedBadgeConfig.color,
gradient: equippedBadgeConfig.gradient,
}
: null
}
/>
{/* 我的好友入口 */}
<YStack width="100%" paddingHorizontal="$4" marginTop="$4">
<TouchableOpacity
onPress={() => router.push('/(tabs)/profile/friends' as any)}
activeOpacity={0.8}
style={{
backgroundColor: colors.cardBackground,
borderRadius: 16,
padding: 16,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
}}
>
<YStack flexDirection="row" alignItems="center" gap="$3">
<YStack
width={40}
height={40}
borderRadius="$10"
backgroundColor="$blue2"
alignItems="center"
justifyContent="center"
>
<Ionicons name="people-outline" size={22} color="#FEBE98" />
</YStack>
<YStack>
<Text fontSize={16} fontWeight="600" color={colors.text as any}>
我的好友
</Text>
<Text fontSize={13} color={colors.icon as any}>
管理你的好友和请求
</Text>
</YStack>
</YStack>
<Ionicons name="chevron-forward" size={20} color={colors.icon} />
</TouchableOpacity>
</YStack>
{/* 信誉分和勋章 */}
<YStack width="100%" paddingHorizontal="$4" gap="$3" marginTop="$4" marginBottom="$2">
{/* 信誉分卡片 */}
{reputation && <ReputationCard reputation={reputation} />}
{/* 勋章展示 */}
{badges.length > 0 && (
<BadgeGrid
badges={badges}
onBadgePress={(badge) => setSelectedBadge(badge)}
maxDisplay={8}
/>
)}
</YStack>
{/* 个人资料标签页 - 宠物、评论、点赞 */}
<ProfileTabs
pets={user?.pets}
isLoading={isLoading && !user}
onAddPet={openAddPetModal}
onDeletePet={handleDeletePet}
/>
</YStack>
{/* 模态框 */}
<AddPetModal
open={petModalVisible}
onOpenChange={closeAddPetModal}
onSubmit={handleAddPet}
/>
<PetDetailModal
pet={selectedPet}
open={!!selectedPet}
onOpenChange={(open) => !open && selectPet(null)}
onDelete={handleDeletePet}
/>
<BadgeDetailModal
visible={!!selectedBadge}
badge={selectedBadge}
onClose={() => setSelectedBadge(null)}
onEquip={equipBadge}
onUnequip={unequipBadge}
/>
</ScrollView>
</View>
);
}
|