Skip to content

Commit

Permalink
Merge branch 'pr/231'
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikburmester committed Dec 1, 2024
2 parents b41363d + 7eb7d17 commit 3e45adf
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 160 deletions.
11 changes: 7 additions & 4 deletions app/(auth)/(tabs)/(home)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { Chromecast } from "@/components/Chromecast";
import { HeaderBackButton } from "@/components/common/HeaderBackButton";
import { nestedTabPageScreenOptions } from "@/components/stacks/NestedTabPageStack";
import { useDownload } from "@/providers/DownloadProvider";
import { Feather } from "@expo/vector-icons";
import { Stack, useRouter } from "expo-router";
import { Platform, TouchableOpacity, View } from "react-native";

export default function IndexLayout() {
const router = useRouter();

return (
<Stack>
<Stack.Screen
Expand All @@ -35,11 +32,17 @@ export default function IndexLayout() {
}}
/>
<Stack.Screen
name="downloads"
name="downloads/index"
options={{
title: "Downloads",
}}
/>
<Stack.Screen
name="downloads/[seriesId]"
options={{
title: "TV-Series",
}}
/>
<Stack.Screen
name="settings"
options={{
Expand Down
94 changes: 94 additions & 0 deletions app/(auth)/(tabs)/(home)/downloads/[seriesId].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {Text} from "@/components/common/Text";
import {useDownload} from "@/providers/DownloadProvider";
import {router, useLocalSearchParams, useNavigation} from "expo-router";
import React, {useEffect, useMemo, useState} from "react";
import {ScrollView, View} from "react-native";
import {EpisodeCard} from "@/components/downloads/EpisodeCard";
import {BaseItemDto} from "@jellyfin/sdk/lib/generated-client/models";
import {SeasonDropdown, SeasonIndexState} from "@/components/series/SeasonDropdown";
import {storage} from "@/utils/mmkv";

export default function page() {
const navigation = useNavigation();
const local = useLocalSearchParams();
const {seriesId, episodeSeasonIndex} = local as {
seriesId: string,
episodeSeasonIndex: number | string | undefined
};

const [seasonIndexState, setSeasonIndexState] = useState<SeasonIndexState>({});
const {downloadedFiles} = useDownload();

const series = useMemo(() => {
try {
return downloadedFiles
?.filter((f) => f.item.SeriesId == seriesId)
?.sort((a, b) => a?.item.ParentIndexNumber! - b.item.ParentIndexNumber!)
|| [];
} catch {
return [];
}
}, [downloadedFiles]);

const seasonIndex = seasonIndexState[series?.[0]?.item?.ParentId ?? ""] || episodeSeasonIndex || "";

const groupBySeason = useMemo<BaseItemDto[]>(() => {
const seasons: Record<string, BaseItemDto[]> = {};

series?.forEach((episode) => {
if (!seasons[episode.item.ParentIndexNumber!]) {
seasons[episode.item.ParentIndexNumber!] = [];
}

seasons[episode.item.ParentIndexNumber!].push(episode.item);
});
return seasons[seasonIndex]
?.sort((a, b) => a.IndexNumber! - b.IndexNumber!)
?? []
}, [series, seasonIndex]);

const initialSeasonIndex = useMemo(() =>
Object.values(groupBySeason)?.[0]?.ParentIndexNumber ?? series?.[0]?.item?.ParentIndexNumber,
[groupBySeason]
);

useEffect(() => {
if (series.length > 0) {
navigation.setOptions({
title: series[0].item.SeriesName,
});
}
else {
storage.delete(seriesId);
router.back();
}
}, [series]);

return (
<>
{series.length > 0 && <View className="my-4 flex flex-row items-center justify-start">
<SeasonDropdown
item={series[0].item}
seasons={series.map(s => s.item)}
state={seasonIndexState}
initialSeasonIndex={initialSeasonIndex!}
onSelect={(season) => {
setSeasonIndexState((prev) => ({
...prev,
[series[0].item.ParentId ?? ""]: season.ParentIndexNumber,
}));
}}/>
<View className="bg-purple-600 rounded-full h-6 w-6 flex items-center justify-center">
<Text className="text-xs font-bold">{groupBySeason.length}</Text>
</View>
</View>}
<ScrollView key={seasonIndex}>
{groupBySeason.map((episode) => (
<View className="px-4 flex flex-col my-4">
<EpisodeCard item={episode}/>
</View>
))}
</ScrollView>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,28 @@ export default function page() {
</ScrollView>
</View>
)}
{groupedBySeries?.map((items, index) => (
<SeriesCard
items={items.map((i) => i.item)}
key={items[0].item.SeriesId}
/>
))}
{groupedBySeries.length > 0 && (
<View className="mb-4">
<View className="flex flex-row items-center justify-between mb-2 px-4">
<Text className="text-lg font-bold">TV-Series</Text>
<View className="bg-purple-600 rounded-full h-6 w-6 flex items-center justify-center">
<Text className="text-xs font-bold">{groupedBySeries?.length}</Text>
</View>
</View>
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
<View className="px-4 flex flex-row">
{groupedBySeries?.map((items) => (
<View className="mb-2 last:mb-0" key={items[0].item.SeriesId}>
<SeriesCard
items={items.map((i) => i.item)}
key={items[0].item.SeriesId}
/>
</View>
))}
</View>
</ScrollView>
</View>
)}
{downloadedFiles?.length === 0 && (
<View className="flex px-4">
<Text className="opacity-50">No downloaded items</Text>
Expand Down
13 changes: 11 additions & 2 deletions components/DownloadItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
BaseItemDto,
MediaSourceInfo,
} from "@jellyfin/sdk/lib/generated-client/models";
import { router, useFocusEffect } from "expo-router";
import {Href, router, useFocusEffect} from "expo-router";
import { useAtom } from "jotai";
import { useCallback, useMemo, useRef, useState } from "react";
import { Alert, TouchableOpacity, View, ViewProps } from "react-native";
Expand Down Expand Up @@ -206,7 +206,16 @@ export const DownloadItem: React.FC<DownloadProps> = ({ item, ...props }) => {
) : isDownloaded ? (
<TouchableOpacity
onPress={() => {
router.push("/downloads");
router.push(
item.Type !== "Episode"
? "/downloads"
: {
pathname: `/downloads/${item.SeriesId}`,
params: {
episodeSeasonIndex: item.ParentIndexNumber
}
} as Href
);
}}
>
<Ionicons name="cloud-download" size={26} color="#9333ea" />
Expand Down
69 changes: 42 additions & 27 deletions components/downloads/EpisodeCard.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import * as Haptics from "expo-haptics";
import React, { useCallback, useMemo, useRef } from "react";
import React, { useCallback, useMemo } from "react";
import { TouchableOpacity, View } from "react-native";
import {
ActionSheetProvider,
useActionSheet,
} from "@expo/react-native-action-sheet";

import { useDownloadedFileOpener } from "@/hooks/useDownloadedFileOpener";
import { Text } from "../common/Text";
import { useDownload } from "@/providers/DownloadProvider";
import { storage } from "@/utils/mmkv";
import { Image } from "expo-image";
import { ItemCardText } from "../ItemCardText";
import { Ionicons } from "@expo/vector-icons";
import {Text} from "@/components/common/Text";
import {runtimeTicksToSeconds} from "@/utils/time";

interface EpisodeCardProps {
item: BaseItemDto;
Expand All @@ -31,7 +31,7 @@ export const EpisodeCard: React.FC<EpisodeCardProps> = ({ item }) => {

const base64Image = useMemo(() => {
return storage.getString(item.Id!);
}, []);
}, [item]);

const handleOpenFile = useCallback(() => {
openFile(item);
Expand Down Expand Up @@ -76,32 +76,47 @@ export const EpisodeCard: React.FC<EpisodeCardProps> = ({ item }) => {
<TouchableOpacity
onPress={handleOpenFile}
onLongPress={showActionSheet}
className="flex flex-col w-44 mr-2"
className="flex flex-col mr-2"
>
{base64Image ? (
<View className="w-44 aspect-video rounded-lg overflow-hidden">
<Image
source={{
uri: `data:image/jpeg;base64,${base64Image}`,
}}
style={{
width: "100%",
height: "100%",
resizeMode: "cover",
}}
/>
<View className="flex flex-row items-start mb-2">
<View className="mr-2">
{base64Image ? (
<View className="w-44 aspect-video rounded-lg overflow-hidden">
<Image
source={{
uri: `data:image/jpeg;base64,${base64Image}`,
}}
style={{
width: "100%",
height: "100%",
resizeMode: "cover",
}}
/>
</View>
) : (
<View className="w-44 aspect-video rounded-lg bg-neutral-900 flex items-center justify-center">
<Ionicons
name="image-outline"
size={24}
color="gray"
className="self-center mt-16"
/>
</View>
)}
</View>
) : (
<View className="w-44 aspect-video rounded-lg bg-neutral-900 flex items-center justify-center">
<Ionicons
name="image-outline"
size={24}
color="gray"
className="self-center mt-16"
/>
<View className="w-56 flex flex-col">
<Text numberOfLines={2} className="">
{item.Name}
</Text>
<Text numberOfLines={1} className="text-xs text-neutral-500">
{`S${item.ParentIndexNumber?.toString()}:E${item.IndexNumber?.toString()}`}
</Text>
<Text className="text-xs text-neutral-500">
{runtimeTicksToSeconds(item.RunTimeTicks)}
</Text>
</View>
)}
<ItemCardText item={item} />
</View>
<Text numberOfLines={3} className="text-xs text-neutral-500 shrink">{item.Overview}</Text>
</TouchableOpacity>
);
};
Expand Down
88 changes: 42 additions & 46 deletions components/downloads/SeriesCard.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import { ScrollView, View } from "react-native";
import { EpisodeCard } from "./EpisodeCard";
import {TouchableOpacity, View} from "react-native";
import { Text } from "../common/Text";
import { useMemo } from "react";
import { SeasonPicker } from "../series/SeasonPicker";
import React, {useMemo} from "react";
import {storage} from "@/utils/mmkv";
import {Image} from "expo-image";
import {Ionicons} from "@expo/vector-icons";
import {router} from "expo-router";

export const SeriesCard: React.FC<{ items: BaseItemDto[] }> = ({ items }) => {
const groupBySeason = useMemo(() => {
const seasons: Record<string, BaseItemDto[]> = {};

items.forEach((item) => {
if (!seasons[item.SeasonName!]) {
seasons[item.SeasonName!] = [];
}

seasons[item.SeasonName!].push(item);
});

return Object.values(seasons).sort(
(a, b) => a[0].IndexNumber! - b[0].IndexNumber!
);
}, [items]);

const sortByIndex = (a: BaseItemDto, b: BaseItemDto) => {
return a.IndexNumber! > b.IndexNumber! ? 1 : -1;
};
export const SeriesCard: React.FC<{ items: BaseItemDto[] }> = ({items}) => {
const base64Image = useMemo(() => {
return storage.getString(items[0].SeriesId!);
}, []);

return (
<View>
<View className="flex flex-row items-center justify-between px-4">
<Text className="text-lg font-bold shrink">{items[0].SeriesName}</Text>
<View className="bg-purple-600 rounded-full h-6 w-6 flex items-center justify-center">
<Text className="text-xs font-bold">{items.length}</Text>
<TouchableOpacity onPress={() => router.push(`/downloads/${items[0].SeriesId}`)}>
{base64Image ? (
<View className="w-28 aspect-[10/15] rounded-lg overflow-hidden mr-2 border border-neutral-900">
<Image
source={{
uri: `data:image/jpeg;base64,${base64Image}`,
}}
style={{
width: "100%",
height: "100%",
resizeMode: "cover",
}}
/>
<View
className="bg-purple-600 rounded-full h-6 w-6 flex items-center justify-center absolute bottom-1 right-1">
<Text className="text-xs font-bold">{items.length}</Text>
</View>
</View>
</View>

<Text className="opacity-50 mb-2 px-4">TV-Series</Text>
{groupBySeason.map((seasonItems, seasonIndex) => (
<View key={seasonIndex}>
<Text className="mb-2 font-semibold px-4">
{seasonItems[0].SeasonName}
</Text>
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
<View className="px-4 flex flex-row">
{seasonItems.sort(sortByIndex)?.map((item, index) => (
<EpisodeCard item={item} key={index} />
))}
</View>
</ScrollView>
) : (
<View className="w-28 aspect-[10/15] rounded-lg bg-neutral-900 mr-2 flex items-center justify-center">
<Ionicons
name="image-outline"
size={24}
color="gray"
className="self-center mt-16"
/>
</View>
))}
</View>
)}

<View className="w-28 mt-2 flex flex-col">
<Text numberOfLines={2} className="">{items[0].SeriesName}</Text>
<Text className="text-xs opacity-50">{items[0].ProductionYear}</Text>
</View>
</TouchableOpacity>
);
};
Loading

0 comments on commit 3e45adf

Please sign in to comment.