2026-03-10 19:04:18 +08:00
|
|
|
|
import React, { useEffect, useState, useRef } from 'react';
|
2026-03-05 18:02:54 +08:00
|
|
|
|
import {
|
2026-03-10 19:04:18 +08:00
|
|
|
|
View, StyleSheet,
|
|
|
|
|
|
Text, TouchableOpacity, ActivityIndicator, Animated, Image,
|
2026-03-05 18:02:54 +08:00
|
|
|
|
} from 'react-native';
|
2026-03-10 19:04:18 +08:00
|
|
|
|
import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context';
|
2026-03-05 18:02:54 +08:00
|
|
|
|
import { useRouter } from 'expo-router';
|
|
|
|
|
|
import { Ionicons } from '@expo/vector-icons';
|
|
|
|
|
|
import { VideoCard } from '../components/VideoCard';
|
|
|
|
|
|
import { LoginModal } from '../components/LoginModal';
|
|
|
|
|
|
import { useVideoList } from '../hooks/useVideoList';
|
|
|
|
|
|
import { useAuthStore } from '../store/authStore';
|
|
|
|
|
|
import type { VideoItem } from '../services/types';
|
|
|
|
|
|
|
2026-03-10 19:04:18 +08:00
|
|
|
|
const HEADER_H = 44;
|
|
|
|
|
|
const TAB_H = 38;
|
|
|
|
|
|
const NAV_H = HEADER_H + TAB_H;
|
|
|
|
|
|
|
2026-03-05 18:02:54 +08:00
|
|
|
|
export default function HomeScreen() {
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
const { videos, loading, refreshing, load, refresh } = useVideoList();
|
2026-03-10 19:04:18 +08:00
|
|
|
|
const { isLoggedIn, face, logout } = useAuthStore();
|
2026-03-05 18:02:54 +08:00
|
|
|
|
const [showLogin, setShowLogin] = useState(false);
|
2026-03-10 19:04:18 +08:00
|
|
|
|
const insets = useSafeAreaInsets();
|
|
|
|
|
|
|
|
|
|
|
|
const scrollY = useRef(new Animated.Value(0)).current;
|
|
|
|
|
|
const clampedScroll = useRef(
|
|
|
|
|
|
Animated.diffClamp(scrollY, 0, NAV_H)
|
|
|
|
|
|
).current;
|
|
|
|
|
|
const headerTranslate = clampedScroll.interpolate({
|
|
|
|
|
|
inputRange: [0, NAV_H],
|
|
|
|
|
|
outputRange: [0, -NAV_H],
|
|
|
|
|
|
extrapolate: 'clamp',
|
|
|
|
|
|
});
|
2026-03-05 18:02:54 +08:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => { load(); }, []);
|
|
|
|
|
|
|
|
|
|
|
|
const renderItem = ({ item, index }: { item: VideoItem; index: number }) => (
|
|
|
|
|
|
<View style={index % 2 === 0 ? styles.leftCol : styles.rightCol}>
|
|
|
|
|
|
<VideoCard
|
|
|
|
|
|
item={item}
|
|
|
|
|
|
onPress={() => router.push(`/video/${item.bvid}` as any)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</View>
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2026-03-10 19:04:18 +08:00
|
|
|
|
<SafeAreaView style={styles.safe} edges={['left', 'right']}>
|
|
|
|
|
|
<Animated.FlatList
|
|
|
|
|
|
style={styles.listContainer}
|
2026-03-05 18:02:54 +08:00
|
|
|
|
data={videos}
|
2026-03-06 12:58:41 +08:00
|
|
|
|
keyExtractor={(item, index) => `${item.bvid}-${index}`}
|
2026-03-05 18:02:54 +08:00
|
|
|
|
numColumns={2}
|
|
|
|
|
|
columnWrapperStyle={styles.row}
|
2026-03-10 19:04:18 +08:00
|
|
|
|
contentContainerStyle={{ paddingTop: insets.top + NAV_H + 8, paddingBottom: insets.bottom + 16 }}
|
2026-03-05 18:02:54 +08:00
|
|
|
|
renderItem={renderItem}
|
|
|
|
|
|
onRefresh={refresh}
|
|
|
|
|
|
refreshing={refreshing}
|
|
|
|
|
|
onEndReached={() => load()}
|
|
|
|
|
|
onEndReachedThreshold={0.5}
|
|
|
|
|
|
ListFooterComponent={loading ? <ActivityIndicator style={styles.footer} color="#00AEEC" /> : null}
|
2026-03-10 19:04:18 +08:00
|
|
|
|
onScroll={Animated.event(
|
|
|
|
|
|
[{ nativeEvent: { contentOffset: { y: scrollY } } }],
|
|
|
|
|
|
{ useNativeDriver: true }
|
|
|
|
|
|
)}
|
|
|
|
|
|
scrollEventThrottle={16}
|
2026-03-05 18:02:54 +08:00
|
|
|
|
/>
|
|
|
|
|
|
|
2026-03-10 19:04:18 +08:00
|
|
|
|
{/* 绝对定位导航栏:paddingTop 手动适配刘海/状态栏 */}
|
|
|
|
|
|
<Animated.View
|
|
|
|
|
|
style={[
|
|
|
|
|
|
styles.navBar,
|
|
|
|
|
|
{ paddingTop: insets.top, transform: [{ translateY: headerTranslate }] },
|
|
|
|
|
|
]}
|
|
|
|
|
|
>
|
|
|
|
|
|
<View style={styles.header}>
|
|
|
|
|
|
<Text style={styles.logo}>哔哩哔哩</Text>
|
|
|
|
|
|
<View style={styles.headerRight}>
|
|
|
|
|
|
<TouchableOpacity style={styles.headerBtn}>
|
|
|
|
|
|
<Ionicons name="search" size={22} color="#212121" />
|
|
|
|
|
|
</TouchableOpacity>
|
|
|
|
|
|
<TouchableOpacity
|
|
|
|
|
|
style={styles.headerBtn}
|
|
|
|
|
|
onPress={() => isLoggedIn ? logout() : setShowLogin(true)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{isLoggedIn && face ? (
|
|
|
|
|
|
<Image source={{ uri: face }} style={styles.userAvatar} />
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<Ionicons name={isLoggedIn ? 'person' : 'person-outline'} size={22} color="#00AEEC" />
|
|
|
|
|
|
)}
|
|
|
|
|
|
</TouchableOpacity>
|
|
|
|
|
|
</View>
|
|
|
|
|
|
</View>
|
|
|
|
|
|
|
|
|
|
|
|
<View style={styles.tabRow}>
|
|
|
|
|
|
<Text style={styles.tabActive}>热门</Text>
|
|
|
|
|
|
<View style={styles.tabUnderline} />
|
|
|
|
|
|
</View>
|
|
|
|
|
|
</Animated.View>
|
|
|
|
|
|
|
2026-03-05 18:02:54 +08:00
|
|
|
|
<LoginModal visible={showLogin} onClose={() => setShowLogin(false)} />
|
|
|
|
|
|
</SafeAreaView>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
|
|
safe: { flex: 1, backgroundColor: '#f4f4f4' },
|
2026-03-10 19:04:18 +08:00
|
|
|
|
listContainer: { flex: 1 },
|
|
|
|
|
|
navBar: {
|
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
|
top: 0,
|
|
|
|
|
|
left: 0,
|
|
|
|
|
|
right: 0,
|
|
|
|
|
|
zIndex: 10,
|
|
|
|
|
|
backgroundColor: '#fff',
|
|
|
|
|
|
overflow: 'hidden',
|
|
|
|
|
|
// 安卓投影
|
|
|
|
|
|
elevation: 2,
|
|
|
|
|
|
// iOS 投影
|
|
|
|
|
|
shadowColor: '#000',
|
|
|
|
|
|
shadowOffset: { width: 0, height: 1 },
|
|
|
|
|
|
shadowOpacity: 0.06,
|
|
|
|
|
|
shadowRadius: 2,
|
|
|
|
|
|
},
|
|
|
|
|
|
header: {
|
|
|
|
|
|
height: HEADER_H,
|
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
|
paddingHorizontal: 16,
|
|
|
|
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
|
|
|
|
borderBottomColor: '#eee',
|
|
|
|
|
|
},
|
2026-03-05 18:02:54 +08:00
|
|
|
|
logo: { fontSize: 20, fontWeight: '800', color: '#00AEEC', letterSpacing: -0.5 },
|
|
|
|
|
|
headerRight: { flexDirection: 'row', gap: 8 },
|
2026-03-10 19:04:18 +08:00
|
|
|
|
headerBtn: { padding: 6 },
|
|
|
|
|
|
userAvatar: { width: 28, height: 28, borderRadius: 14, backgroundColor: '#eee' },
|
|
|
|
|
|
tabRow: {
|
|
|
|
|
|
height: TAB_H,
|
|
|
|
|
|
backgroundColor: '#fff',
|
|
|
|
|
|
paddingHorizontal: 16,
|
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
|
},
|
|
|
|
|
|
tabActive: { fontSize: 15, fontWeight: '700', color: '#00AEEC' },
|
|
|
|
|
|
tabUnderline: {
|
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
|
bottom: 0,
|
|
|
|
|
|
left: 16,
|
|
|
|
|
|
width: 24,
|
|
|
|
|
|
height: 2,
|
|
|
|
|
|
backgroundColor: '#00AEEC',
|
|
|
|
|
|
borderRadius: 1,
|
|
|
|
|
|
},
|
2026-03-05 18:02:54 +08:00
|
|
|
|
row: { paddingHorizontal: 8 },
|
|
|
|
|
|
leftCol: { marginLeft: 4, marginRight: 2 },
|
|
|
|
|
|
rightCol: { marginLeft: 2, marginRight: 4 },
|
|
|
|
|
|
footer: { marginVertical: 16 },
|
|
|
|
|
|
});
|