2026-03-11 14:38:31 +08:00
|
|
|
|
import React, { useEffect, useState, useRef, useMemo } from "react";
|
2026-03-05 18:02:54 +08:00
|
|
|
|
import {
|
2026-03-11 14:38:31 +08:00
|
|
|
|
View,
|
|
|
|
|
|
StyleSheet,
|
|
|
|
|
|
Text,
|
|
|
|
|
|
TouchableOpacity,
|
|
|
|
|
|
ActivityIndicator,
|
|
|
|
|
|
Animated,
|
|
|
|
|
|
Image,
|
|
|
|
|
|
RefreshControl,
|
|
|
|
|
|
ViewToken,
|
|
|
|
|
|
} from "react-native";
|
|
|
|
|
|
import {
|
|
|
|
|
|
SafeAreaView,
|
|
|
|
|
|
useSafeAreaInsets,
|
|
|
|
|
|
} from "react-native-safe-area-context";
|
|
|
|
|
|
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 { toListRows, type ListRow, type BigRow } from "../utils/videoRows";
|
|
|
|
|
|
import { BigVideoCard } from "../components/BigVideoCard";
|
2026-03-05 18:02:54 +08:00
|
|
|
|
|
2026-03-10 19:04:18 +08:00
|
|
|
|
const HEADER_H = 44;
|
2026-03-11 14:38:31 +08:00
|
|
|
|
const TAB_H = 38;
|
|
|
|
|
|
const NAV_H = HEADER_H + TAB_H;
|
|
|
|
|
|
|
|
|
|
|
|
const VIEWABILITY_CONFIG = { itemVisiblePercentThreshold: 50 };
|
2026-03-10 19:04:18 +08:00
|
|
|
|
|
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();
|
|
|
|
|
|
|
2026-03-11 14:38:31 +08:00
|
|
|
|
const [visibleBigKey, setVisibleBigKey] = useState<string | null>(null);
|
|
|
|
|
|
const rows = useMemo(() => toListRows(videos), [videos]);
|
|
|
|
|
|
|
|
|
|
|
|
const onViewableItemsChangedRef = useRef(
|
|
|
|
|
|
({ viewableItems }: { viewableItems: ViewToken[] }) => {
|
|
|
|
|
|
const bigRow = viewableItems.find(
|
|
|
|
|
|
(v) => v.item && (v.item as ListRow).type === 'big',
|
|
|
|
|
|
);
|
|
|
|
|
|
setVisibleBigKey(
|
|
|
|
|
|
bigRow ? (bigRow.item as BigRow).item.bvid : null,
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
2026-03-10 19:04:18 +08:00
|
|
|
|
).current;
|
2026-03-11 14:38:31 +08:00
|
|
|
|
|
|
|
|
|
|
const scrollY = useRef(new Animated.Value(0)).current;
|
|
|
|
|
|
const headerTranslate = scrollY.interpolate({
|
|
|
|
|
|
inputRange: [0, NAV_H],
|
2026-03-10 19:04:18 +08:00
|
|
|
|
outputRange: [0, -NAV_H],
|
2026-03-11 14:38:31 +08:00
|
|
|
|
extrapolate: "clamp",
|
2026-03-10 19:04:18 +08:00
|
|
|
|
});
|
2026-03-05 18:02:54 +08:00
|
|
|
|
|
2026-03-11 14:38:31 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
load();
|
|
|
|
|
|
}, []);
|
2026-03-05 18:02:54 +08:00
|
|
|
|
|
2026-03-11 14:38:31 +08:00
|
|
|
|
const renderItem = ({ item: row }: { item: ListRow }) => {
|
|
|
|
|
|
if (row.type === 'big') {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<BigVideoCard
|
|
|
|
|
|
item={row.item}
|
|
|
|
|
|
isVisible={visibleBigKey === row.item.bvid}
|
|
|
|
|
|
onPress={() => router.push(`/video/${row.item.bvid}` as any)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
// Normal pair row
|
|
|
|
|
|
return (
|
|
|
|
|
|
<View style={styles.row}>
|
|
|
|
|
|
<View style={styles.leftCol}>
|
|
|
|
|
|
<VideoCard
|
|
|
|
|
|
item={row.left}
|
|
|
|
|
|
onPress={() => router.push(`/video/${row.left.bvid}` as any)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</View>
|
|
|
|
|
|
{row.right && (
|
|
|
|
|
|
<View style={styles.rightCol}>
|
|
|
|
|
|
<VideoCard
|
|
|
|
|
|
item={row.right}
|
|
|
|
|
|
onPress={() => router.push(`/video/${row.right!.bvid}` as any)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</View>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</View>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
2026-03-05 18:02:54 +08:00
|
|
|
|
|
|
|
|
|
|
return (
|
2026-03-11 14:38:31 +08:00
|
|
|
|
<SafeAreaView style={styles.safe} edges={["left", "right"]}>
|
2026-03-10 19:04:18 +08:00
|
|
|
|
<Animated.FlatList
|
|
|
|
|
|
style={styles.listContainer}
|
2026-03-11 14:38:31 +08:00
|
|
|
|
data={rows}
|
|
|
|
|
|
keyExtractor={(row: any, index) =>
|
|
|
|
|
|
row.type === 'big'
|
|
|
|
|
|
? `big-${row.item.bvid}`
|
|
|
|
|
|
: `pair-${row.left.bvid}-${row.right?.bvid ?? 'empty'}-${index}`
|
|
|
|
|
|
}
|
|
|
|
|
|
contentContainerStyle={{
|
|
|
|
|
|
paddingTop: insets.top + NAV_H + 6,
|
|
|
|
|
|
paddingBottom: insets.bottom + 16,
|
|
|
|
|
|
}}
|
2026-03-05 18:02:54 +08:00
|
|
|
|
renderItem={renderItem}
|
2026-03-10 21:10:01 +08:00
|
|
|
|
refreshControl={
|
|
|
|
|
|
<RefreshControl
|
|
|
|
|
|
refreshing={refreshing}
|
|
|
|
|
|
onRefresh={refresh}
|
|
|
|
|
|
progressViewOffset={insets.top + NAV_H}
|
|
|
|
|
|
/>
|
|
|
|
|
|
}
|
2026-03-05 18:02:54 +08:00
|
|
|
|
onEndReached={() => load()}
|
|
|
|
|
|
onEndReachedThreshold={0.5}
|
2026-03-11 14:38:31 +08:00
|
|
|
|
viewabilityConfig={VIEWABILITY_CONFIG}
|
|
|
|
|
|
onViewableItemsChanged={onViewableItemsChangedRef}
|
2026-03-10 21:07:40 +08:00
|
|
|
|
ListFooterComponent={
|
|
|
|
|
|
<View style={styles.footer}>
|
|
|
|
|
|
{loading && <ActivityIndicator color="#00AEEC" />}
|
|
|
|
|
|
</View>
|
|
|
|
|
|
}
|
2026-03-10 19:04:18 +08:00
|
|
|
|
onScroll={Animated.event(
|
|
|
|
|
|
[{ nativeEvent: { contentOffset: { y: scrollY } } }],
|
2026-03-11 14:38:31 +08:00
|
|
|
|
{ useNativeDriver: true },
|
2026-03-10 19:04:18 +08:00
|
|
|
|
)}
|
|
|
|
|
|
scrollEventThrottle={16}
|
2026-03-05 18:02:54 +08:00
|
|
|
|
/>
|
|
|
|
|
|
|
2026-03-10 19:04:18 +08:00
|
|
|
|
{/* 绝对定位导航栏:paddingTop 手动适配刘海/状态栏 */}
|
|
|
|
|
|
<Animated.View
|
|
|
|
|
|
style={[
|
|
|
|
|
|
styles.navBar,
|
2026-03-11 14:38:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
paddingTop: insets.top,
|
|
|
|
|
|
transform: [{ translateY: headerTranslate }],
|
|
|
|
|
|
},
|
2026-03-10 19:04:18 +08:00
|
|
|
|
]}
|
|
|
|
|
|
>
|
|
|
|
|
|
<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}
|
2026-03-11 14:38:31 +08:00
|
|
|
|
onPress={() => (isLoggedIn ? logout() : setShowLogin(true))}
|
2026-03-10 19:04:18 +08:00
|
|
|
|
>
|
|
|
|
|
|
{isLoggedIn && face ? (
|
|
|
|
|
|
<Image source={{ uri: face }} style={styles.userAvatar} />
|
|
|
|
|
|
) : (
|
2026-03-11 14:38:31 +08:00
|
|
|
|
<Ionicons
|
|
|
|
|
|
name={isLoggedIn ? "person" : "person-outline"}
|
|
|
|
|
|
size={22}
|
|
|
|
|
|
color="#00AEEC"
|
|
|
|
|
|
/>
|
2026-03-10 19:04:18 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</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({
|
2026-03-11 14:38:31 +08:00
|
|
|
|
safe: { flex: 1, backgroundColor: "#f4f4f4" },
|
2026-03-10 19:04:18 +08:00
|
|
|
|
listContainer: { flex: 1 },
|
|
|
|
|
|
navBar: {
|
2026-03-11 14:38:31 +08:00
|
|
|
|
position: "absolute",
|
2026-03-10 19:04:18 +08:00
|
|
|
|
top: 0,
|
|
|
|
|
|
left: 0,
|
|
|
|
|
|
right: 0,
|
|
|
|
|
|
zIndex: 10,
|
2026-03-11 14:38:31 +08:00
|
|
|
|
backgroundColor: "#fff",
|
|
|
|
|
|
overflow: "hidden",
|
2026-03-10 19:04:18 +08:00
|
|
|
|
// 安卓投影
|
|
|
|
|
|
elevation: 2,
|
|
|
|
|
|
// iOS 投影
|
2026-03-11 14:38:31 +08:00
|
|
|
|
shadowColor: "#000",
|
2026-03-10 19:04:18 +08:00
|
|
|
|
shadowOffset: { width: 0, height: 1 },
|
|
|
|
|
|
shadowOpacity: 0.06,
|
|
|
|
|
|
shadowRadius: 2,
|
|
|
|
|
|
},
|
|
|
|
|
|
header: {
|
|
|
|
|
|
height: HEADER_H,
|
2026-03-11 14:38:31 +08:00
|
|
|
|
flexDirection: "row",
|
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
|
justifyContent: "space-between",
|
2026-03-10 19:04:18 +08:00
|
|
|
|
paddingHorizontal: 16,
|
|
|
|
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
2026-03-11 14:38:31 +08:00
|
|
|
|
borderBottomColor: "#eee",
|
|
|
|
|
|
},
|
|
|
|
|
|
logo: {
|
|
|
|
|
|
fontSize: 20,
|
|
|
|
|
|
fontWeight: "800",
|
|
|
|
|
|
color: "#00AEEC",
|
|
|
|
|
|
letterSpacing: -0.5,
|
2026-03-10 19:04:18 +08:00
|
|
|
|
},
|
2026-03-11 14:38:31 +08:00
|
|
|
|
headerRight: { flexDirection: "row", gap: 8 },
|
2026-03-10 19:04:18 +08:00
|
|
|
|
headerBtn: { padding: 6 },
|
2026-03-11 14:38:31 +08:00
|
|
|
|
userAvatar: {
|
|
|
|
|
|
width: 28,
|
|
|
|
|
|
height: 28,
|
|
|
|
|
|
borderRadius: 14,
|
|
|
|
|
|
backgroundColor: "#eee",
|
|
|
|
|
|
},
|
2026-03-10 19:04:18 +08:00
|
|
|
|
tabRow: {
|
|
|
|
|
|
height: TAB_H,
|
2026-03-11 14:38:31 +08:00
|
|
|
|
backgroundColor: "#fff",
|
2026-03-10 19:04:18 +08:00
|
|
|
|
paddingHorizontal: 16,
|
2026-03-11 14:38:31 +08:00
|
|
|
|
flexDirection: "row",
|
|
|
|
|
|
alignItems: "center",
|
2026-03-10 19:04:18 +08:00
|
|
|
|
},
|
2026-03-11 14:38:31 +08:00
|
|
|
|
tabActive: { fontSize: 15, fontWeight: "700", color: "#00AEEC" },
|
2026-03-10 19:04:18 +08:00
|
|
|
|
tabUnderline: {
|
2026-03-11 14:38:31 +08:00
|
|
|
|
position: "absolute",
|
2026-03-10 19:04:18 +08:00
|
|
|
|
bottom: 0,
|
|
|
|
|
|
left: 16,
|
|
|
|
|
|
width: 24,
|
|
|
|
|
|
height: 2,
|
2026-03-11 14:38:31 +08:00
|
|
|
|
backgroundColor: "#00AEEC",
|
2026-03-10 19:04:18 +08:00
|
|
|
|
borderRadius: 1,
|
|
|
|
|
|
},
|
2026-03-11 14:38:31 +08:00
|
|
|
|
row: { flexDirection: 'row', paddingHorizontal: 1, justifyContent: "flex-start" },
|
2026-03-05 18:02:54 +08:00
|
|
|
|
leftCol: { marginLeft: 4, marginRight: 2 },
|
|
|
|
|
|
rightCol: { marginLeft: 2, marginRight: 4 },
|
2026-03-11 14:38:31 +08:00
|
|
|
|
footer: { height: 48, alignItems: "center", justifyContent: "center" },
|
2026-03-05 18:02:54 +08:00
|
|
|
|
});
|