-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add download list in the example app #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| import React, { useEffect, useState } from 'react'; | ||
| import { | ||
| View, | ||
| Text, | ||
| FlatList, | ||
| TouchableOpacity, | ||
| StyleSheet, | ||
| Alert, | ||
| } from 'react-native'; | ||
| import { TPStreamsDownload } from 'react-native-tpstreams'; | ||
| import type { DownloadItem } from 'react-native-tpstreams'; | ||
|
|
||
| interface DownloadExampleProps { | ||
| onBack?: () => void; | ||
| } | ||
|
|
||
| export default function DownloadExample({ onBack }: DownloadExampleProps) { | ||
| const [downloads, setDownloads] = useState<DownloadItem[]>([]); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| loadDownloads(); | ||
| }, []); | ||
|
|
||
| const loadDownloads = async () => { | ||
| try { | ||
| setLoading(true); | ||
| const downloadItems = await TPStreamsDownload.getDownloads(); | ||
| setDownloads(downloadItems); | ||
| } catch (error) { | ||
| Alert.alert('Error', 'Failed to load downloads'); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| const handlePauseResume = async (item: DownloadItem) => { | ||
| try { | ||
| if (item.state === 1) { | ||
| // DOWNLOADING | ||
| await TPStreamsDownload.pauseDownload(item.videoId); | ||
| } else if (item.state === 2) { | ||
| // PAUSED | ||
| await TPStreamsDownload.resumeDownload(item.videoId); | ||
| } | ||
| loadDownloads(); | ||
|
Comment on lines
+37
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Replace magic numbers with typed enum / constants Using raw // ideally exported by SDK, otherwise create locally
enum DownloadState {
Idle = 0,
Downloading = 1,
Paused = 2,
Completed = 3,
Failed = 4,
}
if (item.state === DownloadState.Downloading) {
await TPStreamsDownload.pauseDownload(item.videoId);
} else if (item.state === DownloadState.Paused) {
await TPStreamsDownload.resumeDownload(item.videoId);
}This also simplifies 🤖 Prompt for AI Agents |
||
| } catch (error) { | ||
| Alert.alert('Error', `Failed to pause/resume download`); | ||
| } | ||
| }; | ||
|
|
||
| const handleCancel = async (videoId: string) => { | ||
| try { | ||
| await TPStreamsDownload.cancelDownload(videoId); | ||
| loadDownloads(); | ||
| } catch (error) { | ||
| Alert.alert('Error', `Failed to cancel download`); | ||
| } | ||
| }; | ||
|
|
||
| const getStateText = (state: number): string => { | ||
| switch (state) { | ||
| case 0: | ||
| return 'Idle'; | ||
| case 1: | ||
| return 'Downloading'; | ||
| case 2: | ||
| return 'Paused'; | ||
| case 3: | ||
| return 'Completed'; | ||
| case 4: | ||
| return 'Failed'; | ||
| default: | ||
| return 'Unknown'; | ||
| } | ||
| }; | ||
|
|
||
| const renderDownloadItem = ({ item }: { item: DownloadItem }) => { | ||
| const isPaused = item.state === 2; | ||
| const isDownloading = item.state === 1; | ||
|
|
||
| return ( | ||
| <View style={styles.downloadItem}> | ||
| <View style={styles.downloadInfo}> | ||
| <Text style={styles.title}>{item.title || 'Untitled'}</Text> | ||
| <Text style={styles.status}>Status: {getStateText(item.state)}</Text> | ||
| <Text style={styles.progress}> | ||
| Progress: {Math.round(item.progressPercentage)}% | ||
| </Text> | ||
| </View> | ||
|
|
||
| <View style={styles.actions}> | ||
| {(isDownloading || isPaused) && ( | ||
| <TouchableOpacity | ||
| style={styles.button} | ||
| onPress={() => handlePauseResume(item)} | ||
| > | ||
| <Text style={styles.buttonText}> | ||
| {isPaused ? 'Resume' : 'Pause'} | ||
| </Text> | ||
| </TouchableOpacity> | ||
| )} | ||
|
|
||
| <TouchableOpacity | ||
| style={[styles.button, styles.cancelButton]} | ||
| onPress={() => handleCancel(item.videoId)} | ||
| > | ||
| <Text style={styles.buttonText}>Delete</Text> | ||
| </TouchableOpacity> | ||
| </View> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <View style={styles.container}> | ||
| <View style={styles.header}> | ||
| {onBack && ( | ||
| <TouchableOpacity style={styles.backButton} onPress={onBack}> | ||
| <Text style={styles.backButtonText}>← Back</Text> | ||
| </TouchableOpacity> | ||
| )} | ||
| <Text style={styles.headerTitle}>Downloads</Text> | ||
| </View> | ||
|
|
||
| {loading ? ( | ||
| <Text style={styles.loading}>Loading...</Text> | ||
| ) : downloads.length === 0 ? ( | ||
| <Text style={styles.noDownloads}>No downloads found</Text> | ||
| ) : ( | ||
| <FlatList | ||
| data={downloads} | ||
| renderItem={renderDownloadItem} | ||
| keyExtractor={(item) => item.videoId} | ||
| style={styles.list} | ||
| /> | ||
| )} | ||
|
|
||
| <TouchableOpacity style={styles.refreshButton} onPress={loadDownloads}> | ||
| <Text style={styles.buttonText}>Refresh</Text> | ||
| </TouchableOpacity> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| flex: 1, | ||
| padding: 16, | ||
| backgroundColor: '#f5f5f5', | ||
| }, | ||
| header: { | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| marginBottom: 16, | ||
| }, | ||
| backButton: { | ||
| padding: 8, | ||
| marginRight: 8, | ||
| }, | ||
| backButtonText: { | ||
| fontSize: 16, | ||
| color: '#2196F3', | ||
| }, | ||
| headerTitle: { | ||
| fontSize: 20, | ||
| fontWeight: 'bold', | ||
| }, | ||
| loading: { | ||
| textAlign: 'center', | ||
| marginTop: 20, | ||
| }, | ||
| noDownloads: { | ||
| textAlign: 'center', | ||
| marginTop: 20, | ||
| color: '#666', | ||
| }, | ||
| list: { | ||
| flex: 1, | ||
| }, | ||
| downloadItem: { | ||
| backgroundColor: 'white', | ||
| borderRadius: 8, | ||
| padding: 16, | ||
| marginBottom: 12, | ||
| flexDirection: 'row', | ||
| justifyContent: 'space-between', | ||
| alignItems: 'center', | ||
| shadowColor: '#000', | ||
| shadowOffset: { width: 0, height: 1 }, | ||
| shadowOpacity: 0.2, | ||
| shadowRadius: 1, | ||
| elevation: 2, | ||
| }, | ||
| downloadInfo: { | ||
| flex: 1, | ||
| }, | ||
| title: { | ||
| fontSize: 16, | ||
| fontWeight: 'bold', | ||
| marginBottom: 4, | ||
| }, | ||
| status: { | ||
| fontSize: 14, | ||
| color: '#666', | ||
| marginBottom: 2, | ||
| }, | ||
| progress: { | ||
| fontSize: 14, | ||
| color: '#666', | ||
| }, | ||
| actions: { | ||
| flexDirection: 'row', | ||
| }, | ||
| button: { | ||
| backgroundColor: '#2196F3', | ||
| borderRadius: 4, | ||
| paddingVertical: 6, | ||
| paddingHorizontal: 12, | ||
| marginLeft: 8, | ||
| }, | ||
| cancelButton: { | ||
| backgroundColor: '#F44336', | ||
| }, | ||
| buttonText: { | ||
| color: 'white', | ||
| fontSize: 14, | ||
| fontWeight: '500', | ||
| }, | ||
| refreshButton: { | ||
| backgroundColor: '#4CAF50', | ||
| borderRadius: 4, | ||
| paddingVertical: 12, | ||
| alignItems: 'center', | ||
| marginTop: 16, | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard against state updates after unmount
loadDownloadsis an async function that sets state after awaiting a promise.If the component unmounts before the promise resolves (e.g., user quickly navigates away), the
setDownloads/setLoadingcalls will try to update an unmounted component, triggering a memory-leak warning.Add a simple
isMountedref or anAbortControllerto ignore the result once unmounted.📝 Committable suggestion
🤖 Prompt for AI Agents