nodeshiftmusicburmalda/app/(tabs)/index.tsx

212 lines
9.3 KiB
TypeScript
Raw Normal View History

2026-04-27 21:01:17 +05:00
import { useEffect, useState, useCallback } from 'react';
2026-04-28 16:54:42 +05:00
import { View, Text, ScrollView, StyleSheet, Pressable, Dimensions, RefreshControl } from 'react-native';
import Animated, { FadeIn, FadeInDown, useSharedValue, useAnimatedStyle, withSpring } from 'react-native-reanimated';
2026-04-27 21:01:17 +05:00
import { useSafeAreaInsets } from 'react-native-safe-area-context';
2026-04-28 16:54:42 +05:00
import Svg, { Path } from 'react-native-svg';
import * as Haptics from 'expo-haptics';
2026-04-27 21:01:17 +05:00
import { Colors, Font, Spacing, Radius, TAB_BAR_HEIGHT, MINI_PLAYER_HEIGHT } from '../../constants/theme';
import { getTracks, getAlbums, Track, Album, coverUrl } from '../../services/api';
2026-04-28 16:54:42 +05:00
import { usePlayer, useLikes } from '../../store';
2026-04-28 20:36:28 +05:00
import CoverImage from '../../components/CoverImage';
2026-04-27 21:01:17 +05:00
const { width } = Dimensions.get('window');
2026-04-28 16:54:42 +05:00
const CARD_W = (width - Spacing.md * 3) / 2;
2026-04-27 21:01:17 +05:00
export default function HomeScreen() {
const [tracks, setTracks] = useState<Track[]>([]);
const [albums, setAlbums] = useState<Album[]>([]);
const [refresh, setRefresh] = useState(false);
const insets = useSafeAreaInsets();
2026-04-28 16:54:42 +05:00
const { setQueue } = usePlayer();
const { toggleLike, isLiked } = useLikes();
2026-04-27 21:01:17 +05:00
const load = useCallback(async () => {
const [t, a] = await Promise.all([getTracks(), getAlbums()]);
setTracks(t);
setAlbums(a);
}, []);
useEffect(() => { load(); }, []);
const onRefresh = async () => {
setRefresh(true);
await load();
setRefresh(false);
};
2026-04-28 16:54:42 +05:00
const playTrack = (track: Track, list: Track[], idx: number) => {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
setQueue(list, idx);
2026-04-27 21:01:17 +05:00
usePlayer.getState().setExpanded(true);
};
return (
<ScrollView
style={styles.scroll}
2026-04-28 16:54:42 +05:00
contentContainerStyle={[styles.content, { paddingTop: insets.top + 14, paddingBottom: TAB_BAR_HEIGHT + MINI_PLAYER_HEIGHT + 24 }]}
refreshControl={<RefreshControl refreshing={refresh} onRefresh={onRefresh} tintColor={Colors.text2} />}
2026-04-27 21:01:17 +05:00
showsVerticalScrollIndicator={false}
>
2026-04-28 16:54:42 +05:00
<Animated.View entering={FadeIn.duration(400)} style={styles.pageHeader}>
<Text style={styles.pageTitle}>NodeShift Music</Text>
2026-04-27 21:01:17 +05:00
</Animated.View>
2026-04-28 16:54:42 +05:00
<NowPlayingStrip />
2026-04-27 21:01:17 +05:00
{albums.length > 0 && (
<Animated.View entering={FadeInDown.delay(100)} style={styles.section}>
2026-04-28 16:54:42 +05:00
<Text style={styles.secTitle}>Альбомы</Text>
<ScrollView horizontal showsHorizontalScrollIndicator={false} contentContainerStyle={styles.hRow}>
2026-04-27 21:01:17 +05:00
{albums.map((a, i) => (
2026-04-28 20:36:28 +05:00
<AlbumCard key={a.id} album={a} delay={i * 50} onPress={() => {
const albumTracks = tracks.filter(t => t.album === a.title);
if (albumTracks.length > 0) {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
setQueue(albumTracks, 0);
usePlayer.getState().setExpanded(true);
}
}} />
2026-04-27 21:01:17 +05:00
))}
</ScrollView>
</Animated.View>
)}
{tracks.length > 0 && (
<Animated.View entering={FadeInDown.delay(200)} style={styles.section}>
2026-04-28 16:54:42 +05:00
<Text style={styles.secTitle}>Треки</Text>
2026-04-27 21:01:17 +05:00
{tracks.map((t, i) => (
2026-04-28 16:54:42 +05:00
<TrackRow
key={t.id}
track={t}
index={i}
liked={isLiked(t.id)}
onPress={() => playTrack(t, tracks, i)}
onLike={() => toggleLike(t.id)}
/>
2026-04-27 21:01:17 +05:00
))}
</Animated.View>
)}
</ScrollView>
);
}
2026-04-28 16:54:42 +05:00
function NowPlayingStrip() {
const { queue, index, playing, setExpanded } = usePlayer();
const track = queue[index];
if (!track) return null;
const cover = coverUrl(track.cover_url);
2026-04-28 20:36:28 +05:00
2026-04-28 16:54:42 +05:00
return (
<Animated.View entering={FadeInDown.duration(300)} style={styles.npStrip}>
2026-04-28 20:36:28 +05:00
{/* Blurred cover background */}
<CoverImage
uri={cover}
title={track.title}
size={160}
radius={0}
blurRadius={40}
style={StyleSheet.absoluteFill as any}
/>
2026-04-28 16:54:42 +05:00
<View style={[StyleSheet.absoluteFill, styles.npDim]} />
<Pressable style={styles.npContent} onPress={() => setExpanded(true)}>
2026-04-28 20:36:28 +05:00
<CoverImage uri={cover} title={track.title} size={52} radius={Radius.sm} />
2026-04-28 16:54:42 +05:00
<View style={styles.npInfo}>
<Text style={styles.npLabel}>Сейчас играет</Text>
<Text style={styles.npTitle} numberOfLines={1}>{track.title}</Text>
<Text style={styles.npArtist} numberOfLines={1}>{track.artist}</Text>
</View>
<View style={styles.npDot}>
{playing
2026-04-28 20:36:28 +05:00
? <View style={styles.npEq}>{[1, 2, 3].map(i => <Animated.View key={i} style={[styles.npBar, { height: 4 + i * 4 }]} />)}</View>
2026-04-28 16:54:42 +05:00
: <Svg width={16} height={16} viewBox="0 0 24 24" fill={Colors.text}><Path d="M8 5v14l11-7z" /></Svg>
}
</View>
</Pressable>
</Animated.View>
);
}
2026-04-28 20:36:28 +05:00
function AlbumCard({ album, delay, onPress }: { album: Album; delay: number; onPress: () => void }) {
2026-04-27 21:01:17 +05:00
const scale = useSharedValue(1);
const style = useAnimatedStyle(() => ({ transform: [{ scale: scale.value }] }));
const cover = coverUrl(album.cover_url);
2026-04-28 20:36:28 +05:00
2026-04-27 21:01:17 +05:00
return (
2026-04-28 16:54:42 +05:00
<Animated.View entering={FadeInDown.delay(delay)} style={style}>
2026-04-27 21:01:17 +05:00
<Pressable
onPressIn={() => { scale.value = withSpring(0.95, { damping: 15 }); }}
onPressOut={() => { scale.value = withSpring(1, { damping: 15 }); }}
2026-04-28 20:36:28 +05:00
onPress={onPress}
2026-04-27 21:01:17 +05:00
style={styles.albumCard}
>
2026-04-28 20:36:28 +05:00
<CoverImage uri={cover} title={album.title} size={CARD_W} radius={Radius.md} style={{ marginBottom: Spacing.sm }} />
2026-04-27 21:01:17 +05:00
<Text style={styles.albumTitle} numberOfLines={1}>{album.title}</Text>
<Text style={styles.albumArtist} numberOfLines={1}>{album.artist}</Text>
2026-04-28 20:36:28 +05:00
<Text style={styles.albumCount}>{album.track_count} треков</Text>
2026-04-27 21:01:17 +05:00
</Pressable>
</Animated.View>
);
}
2026-04-28 16:54:42 +05:00
function TrackRow({ track, index, liked, onPress, onLike }: {
track: Track; index: number; liked: boolean; onPress: () => void; onLike: () => void;
}) {
2026-04-27 21:01:17 +05:00
const { queue, index: qi } = usePlayer();
2026-04-28 16:54:42 +05:00
const active = queue[qi]?.id === track.id;
const cover = coverUrl(track.cover_url);
2026-04-27 21:01:17 +05:00
return (
2026-04-28 16:54:42 +05:00
<Pressable onPress={onPress} style={[styles.trackRow, active && styles.trackRowActive]}>
<Text style={styles.trackNum}>{index + 1}</Text>
2026-04-28 20:36:28 +05:00
<CoverImage uri={cover} title={track.title} size={44} radius={Radius.sm} />
2026-04-28 16:54:42 +05:00
<View style={styles.trackInfo}>
<Text style={[styles.trackTitle, active && { color: Colors.accent }]} numberOfLines={1}>{track.title}</Text>
<Text style={styles.trackArtist} numberOfLines={1}>{track.artist}</Text>
</View>
<Pressable onPress={onLike} hitSlop={12}>
<Svg width={16} height={16} viewBox="0 0 24 24" fill={liked ? Colors.pink : 'none'}>
<Path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z" stroke={liked ? Colors.pink : Colors.text3} strokeWidth={1.8} strokeLinecap="round" strokeLinejoin="round" />
</Svg>
2026-04-27 21:01:17 +05:00
</Pressable>
2026-04-28 16:54:42 +05:00
<Text style={styles.trackDur}>{fmt(track.duration)}</Text>
</Pressable>
2026-04-27 21:01:17 +05:00
);
}
function fmt(s: number) {
2026-04-28 16:54:42 +05:00
const m = Math.floor(s / 60), sec = Math.floor(s % 60);
2026-04-27 21:01:17 +05:00
return `${m}:${sec < 10 ? '0' : ''}${sec}`;
}
const styles = StyleSheet.create({
2026-04-28 20:36:28 +05:00
scroll: { flex: 1, backgroundColor: Colors.bg },
content: { paddingHorizontal: Spacing.md },
pageHeader: { marginBottom: Spacing.md },
pageTitle: { color: Colors.text, fontSize: Font.xxl, fontWeight: '800', letterSpacing: -0.5 },
section: { marginBottom: Spacing.xl },
secTitle: { color: Colors.text, fontSize: Font.lg, fontWeight: '700', marginBottom: Spacing.md },
hRow: { gap: Spacing.md, paddingRight: Spacing.md },
albumCard: { width: CARD_W },
albumTitle: { color: Colors.text, fontSize: Font.sm, fontWeight: '600' },
albumArtist: { color: Colors.text2, fontSize: Font.xs, marginTop: 2 },
albumCount: { color: Colors.text3, fontSize: Font.xs, marginTop: 2 },
trackRow: { flexDirection: 'row', alignItems: 'center', paddingVertical: 8, paddingHorizontal: Spacing.sm, borderRadius: Radius.sm, gap: Spacing.sm },
trackRowActive: { backgroundColor: Colors.surface },
trackNum: { width: 24, color: Colors.text3, fontSize: Font.sm, textAlign: 'center' },
trackInfo: { flex: 1 },
trackTitle: { color: Colors.text, fontSize: Font.md, fontWeight: '600' },
trackArtist: { color: Colors.text2, fontSize: Font.sm, marginTop: 2 },
trackDur: { color: Colors.text3, fontSize: Font.xs },
2026-04-28 16:54:42 +05:00
// Now playing strip
2026-04-28 20:36:28 +05:00
npStrip: { borderRadius: Radius.lg, overflow: 'hidden', marginBottom: Spacing.xl, height: 80 },
npDim: { backgroundColor: 'rgba(0,0,0,0.5)' },
npContent: { flex: 1, flexDirection: 'row', alignItems: 'center', padding: 14, gap: 12 },
npInfo: { flex: 1 },
npLabel: { color: 'rgba(255,255,255,0.45)', fontSize: 10, fontWeight: '600', letterSpacing: 0.5, marginBottom: 2 },
npTitle: { color: Colors.text, fontSize: Font.md, fontWeight: '600' },
npArtist: { color: Colors.text2, fontSize: Font.xs, marginTop: 1 },
npDot: { width: 32, height: 32, alignItems: 'center', justifyContent: 'center' },
npEq: { flexDirection: 'row', alignItems: 'flex-end', gap: 2 },
npBar: { width: 3, backgroundColor: Colors.accent, borderRadius: 2 },
2026-04-27 21:01:17 +05:00
});