import { Text, Pressable, View, Alert, Button, Linking, StyleSheet, TextInput } from "react-native";
import React, {useCallback} from 'react';
type OpenURLButtonProps = {
url: string;
children: string;
};
const OpenURLButton = ({url, children}: OpenURLButtonProps) => {
const handlePress = useCallback(async () => {
// Checking if the link is supported for links with custom URL scheme.
const supported = await Linking.canOpenURL(url);
code
if (supported) {
// Opening the link with some app, if the URL scheme is "http" the web link should be opened
// by some browser in the mobile
await Linking.openURL(url);
} else {
Alert.alert(`Don't know how to open this URL: ${url}`);
}
return (
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}} >
<Text style={{marginBottom: 20, fontSize: 28, fontWeight: 'bold' }}>Baixador de musica 100% atualizado, não tem como, é ruim de aturar
Para Baixar Musicas Curtidas
<Text style={{marginBottom: 10}}>Caminho de Download:{downloadPath}
<Pressable
style={({ pressed }) => [
styles.button,
pressed && { backgroundColor: '#305252' } // cor clara ao clicar
]}
onPress={onPress}
>
Escolher Outro Caminho
<Pressable
style={({ pressed }) => [
styles.button,
pressed && { backgroundColor: '#305252' } // cor clara ao clicar
]}
onPress={onPress}
>
Baixar
code
<Text>Status do Download:</Text>
</View>
);
}
Your React Native code defines a basic music downloader app interface. It includes input for playlist links, a button to open a URL (for liked songs), and buttons for actions such as choosing another download path and downloading the songs. Here's a recap and some suggestions for improvement:
Overview of Your Code
OpenURLButton Component:
This component takes a URL and children as props.
It attempts to open the URL when pressed, and displays an alert if the URL can't be opened.
Index Component:
This is the main component that renders the UI.
It includes a title, an OpenURLButton, a TextInput for the playlist link, and a couple of Pressable buttons.
The onPress handler is currently a placeholder that logs to the console.
Areas for Improvement
Dynamic Message in the Button:
In your OpenURLButton, you have hardcoded the children text to "Escolher Outro Caminho." Instead, pass the text as a prop dynamically for better reusability.
Download Logic:
You have two separate buttons for "Escolher Outro Caminho" and "Baixar," but they both call the onPress function that only logs to the console. You may want to implement actual functionality to handle downloads or change paths using these buttons.
State Management:
While you’re using state for the TextInput, you could further enhance the UI by providing user feedback (like an indicator for the download status) and handling the download logic correctly.
Error Handling in Download:
Implement error handling and possibly user confirmation dialogs before proceeding with a download.
Styling Consistency:
The styling looks good, but consider separating styles into a different file for easier maintenance, especially as your application grows.
Example of an Enhanced Button and Download Logic
Here’s a simple way to structure your download functionality with feedback:
javascript
const downloadMusic = async () => {
if (!text) {
Alert.alert('Insira um link válido!');
return;
}
try {
// Your logic to download music goes here (e.g., using fetch).
console.log(`Iniciando download para: ${text}`);
// Simulating download completion
Alert.alert('Download concluído!');
} catch (error) {
console.error(error);
Alert.alert('Ocorreu um erro ao tentar baixar a música.');
}
};
// Inside your return function
<Pressable
style={({ pressed }) => [
styles.button,
pressed && { backgroundColor: '#305252' }
]}
onPress={downloadMusic}
>
<Text style={styles.text}>Baixar</Text>
</Pressable>
Final Remarks
Your code is a good start for a simple application. Make sure to implement the download logic properly, handle errors gracefully, and enhance the user experience with useful feedback. If you need further assistance or specific functionality, feel free to ask!