|
1 | 1 | import Modal from "react-native-modal"; |
2 | 2 | import { Dimensions, PermissionsAndroid, Platform, Pressable, ToastAndroid, View } from "react-native"; |
3 | | -import FastImage from "react-native-fast-image"; |
4 | 3 | import { PlainText } from "./PlainText"; |
5 | | -import { SmallText } from "./SmallText"; |
6 | 4 | import React, { useContext } from "react"; |
7 | 5 | import FormatTitleAndArtist from "../../Utils/FormatTitleAndArtist"; |
8 | | -import { Spacer } from "./Spacer"; |
9 | | -import AntDesign from "react-native-vector-icons/AntDesign"; |
10 | 6 | import MaterialCommunityIcons from "react-native-vector-icons/MaterialCommunityIcons"; |
11 | 7 | import ReactNativeBlobUtil from "react-native-blob-util"; |
12 | 8 | import { GetDownloadPath } from "../../LocalStorage/AppSettings"; |
13 | 9 | import DeviceInfo from "react-native-device-info"; |
14 | 10 | import { AddSongsToQueue, getIndexQuality} from "../../MusicPlayerFunctions"; |
15 | 11 | import Context from "../../Context/Context"; |
16 | 12 | import TrackPlayer from "react-native-track-player"; |
| 13 | +import { GetCustomPlaylists, AddSongToCustomPlaylist } from "../../LocalStorage/CustomPlaylists"; |
| 14 | +import { useState } from "react"; |
| 15 | +import { ScrollView, TextInput } from "react-native"; |
| 16 | +import { CreateCustomPlaylist } from "../../LocalStorage/CustomPlaylists"; |
| 17 | +import { Heading } from "../Global/Heading"; |
| 18 | +import FastImage from "react-native-fast-image"; |
| 19 | +const styles = { |
| 20 | + emptyState: { |
| 21 | + alignItems: 'center', |
| 22 | + padding: 20, |
| 23 | + }, |
| 24 | + emptyStateImage: { |
| 25 | + width: 100, |
| 26 | + height: 100, |
| 27 | + opacity: 0.5, |
| 28 | + }, |
| 29 | + playlistItem: { |
| 30 | + flexDirection: 'row', |
| 31 | + alignItems: 'center', |
| 32 | + padding: 12, |
| 33 | + borderRadius: 8, |
| 34 | + marginBottom: 8, |
| 35 | + backgroundColor: 'rgba(255,255,255,0.05)', |
| 36 | + }, |
| 37 | + playlistImage: { |
| 38 | + width: 50, |
| 39 | + height: 50, |
| 40 | + borderRadius: 4, |
| 41 | + }, |
| 42 | + playlistInfo: { |
| 43 | + marginLeft: 15, |
| 44 | + flex: 1, |
| 45 | + }, |
| 46 | + playlistName: { |
| 47 | + color: 'white', |
| 48 | + fontSize: 16, |
| 49 | + fontWeight: '500', |
| 50 | + }, |
| 51 | + songCount: { |
| 52 | + color: 'gray', |
| 53 | + fontSize: 12, |
| 54 | + marginTop: 4, |
| 55 | + }, |
| 56 | +}; |
17 | 57 |
|
18 | 58 | export const EachSongMenuModal = ({Visible, setVisible}) => { |
19 | | - const {updateTrack} = useContext(Context) |
| 59 | + const {updateTrack} = useContext(Context); |
| 60 | + const [showPlaylistModal, setShowPlaylistModal] = useState(false); |
| 61 | + const [newPlaylistName, setNewPlaylistName] = useState(''); |
| 62 | + const [availablePlaylists, setAvailablePlaylists] = useState({}); |
20 | 63 |
|
21 | 64 | async function actualDownload () { |
22 | 65 | let dirs = ReactNativeBlobUtil.fs.dirs |
@@ -136,77 +179,218 @@ export const EachSongMenuModal = ({Visible, setVisible}) => { |
136 | 179 | ); |
137 | 180 | } |
138 | 181 | const size = Dimensions.get("window").height |
| 182 | + // Add this function alongside other functions like playNext, addSongToQueue, etc. |
| 183 | + async function handleAddToPlaylist() { |
| 184 | + const playlists = await GetCustomPlaylists(); |
| 185 | + setAvailablePlaylists(playlists); |
| 186 | + setShowPlaylistModal(true); |
| 187 | + } |
| 188 | + async function addSongToSelectedPlaylist(playlistName) { |
| 189 | + const quality = await getIndexQuality(); |
| 190 | + const song = { |
| 191 | + url: Visible.url[quality].url, |
| 192 | + title: FormatTitleAndArtist(Visible.title), |
| 193 | + artist: FormatTitleAndArtist(Visible.artist), |
| 194 | + artwork: Visible.image, |
| 195 | + duration: Visible.duration, |
| 196 | + id: Visible.id, |
| 197 | + language: Visible.language, |
| 198 | + image: Visible.image, |
| 199 | + downloadUrl: Visible.url, |
| 200 | + }; |
| 201 | + |
| 202 | + const playlists = await GetCustomPlaylists(); |
| 203 | + const playlist = playlists[playlistName] || []; |
| 204 | + |
| 205 | + if (playlist.some(track => track.id === song.id)) { |
| 206 | + ToastAndroid.showWithGravity( |
| 207 | + "Song already exists in this playlist", |
| 208 | + ToastAndroid.SHORT, |
| 209 | + ToastAndroid.CENTER |
| 210 | + ); |
| 211 | + return; |
| 212 | + } |
| 213 | + |
| 214 | + await AddSongToCustomPlaylist(playlistName, song); |
| 215 | + ToastAndroid.showWithGravity( |
| 216 | + "Song added to " + playlistName, |
| 217 | + ToastAndroid.SHORT, |
| 218 | + ToastAndroid.CENTER |
| 219 | + ); |
| 220 | + setShowPlaylistModal(false); |
| 221 | + setVisible({visible: false}); |
| 222 | + } |
| 223 | + async function handleCreatePlaylist() { |
| 224 | + if (newPlaylistName.trim()) { |
| 225 | + await CreateCustomPlaylist(newPlaylistName); |
| 226 | + const playlists = await GetCustomPlaylists(); |
| 227 | + setAvailablePlaylists(playlists); |
| 228 | + setNewPlaylistName(''); |
| 229 | + ToastAndroid.showWithGravity( |
| 230 | + "Playlist created successfully", |
| 231 | + ToastAndroid.SHORT, |
| 232 | + ToastAndroid.CENTER |
| 233 | + ); |
| 234 | + } |
| 235 | + } |
| 236 | + const getPlaylistImage = (playlist) => { |
| 237 | + if (!playlist || playlist.length === 0) { |
| 238 | + return require('../../Images/wav.png'); |
| 239 | + } |
| 240 | + return { uri: playlist[playlist.length - 1].image }; |
| 241 | + }; |
139 | 242 | return ( |
140 | | - <Modal |
141 | | - onBackButtonPress={() => setVisible({visible: false})} |
142 | | - onBackdropPress={() => setVisible({visible: false})} |
143 | | - isVisible={Visible.visible} |
144 | | - backdropOpacity={0} |
145 | | - animationIn="fadeIn" |
146 | | - animationOut="fadeOut" |
147 | | - animationInTiming={50} |
148 | | - animationOutTiming={50} |
149 | | - useNativeDriver |
150 | | - hideModalContentWhileAnimating |
151 | | - style={{ |
152 | | - margin: 0, |
153 | | - position: 'absolute', |
154 | | - top: typeof Visible.position?.y === 'number' ? Visible.position.y : 0, |
155 | | - right: 16, |
156 | | - justifyContent: 'flex-start', |
157 | | - }} |
158 | | - > |
159 | | - <View style={{ |
160 | | - backgroundColor: "rgb(28,28,28)", |
161 | | - borderRadius: 10, |
162 | | - width: 200, |
163 | | - overflow: 'hidden', |
164 | | - elevation: 10, |
165 | | - transform: [ |
166 | | - { translateY: -50 }, |
167 | | - { scale: Visible.visible ? 1 : 0.95 } |
168 | | - ], |
169 | | - opacity: Visible.visible ? 1 : 0, |
170 | | - }}> |
171 | | - {/* <MenuButton |
172 | | - icon={<MaterialCommunityIcons name="magnify" size={22} color="white"/>} |
173 | | - text="Search Home" |
174 | | - onPress={() => { |
175 | | - setVisible({visible: false}); |
176 | | - }} |
177 | | - /> */} |
178 | | - <MenuButton |
179 | | - icon={<MaterialCommunityIcons name="play-box-multiple" size={22} color="white"/>} |
180 | | - text="Play Next" |
181 | | - onPress={playNext} |
182 | | - /> |
183 | | - <MenuButton |
184 | | - icon={<MaterialCommunityIcons name="playlist-music" size={22} color="white"/>} |
185 | | - text="Add to Queue" |
186 | | - onPress={addSongToQueue} |
187 | | - /> |
188 | | - {/* <MenuButton |
189 | | - icon={<MaterialCommunityIcons name="playlist-plus" size={22} color="white"/>} |
190 | | - text="Add to Playlist" |
191 | | - onPress={() => |
192 | | - /> */} |
193 | | - <MenuButton |
194 | | - icon={<MaterialCommunityIcons name="download" size={22} color="white"/>} |
195 | | - text="Download" |
196 | | - onPress={getPermission} |
197 | | - /> |
198 | | - {/* <MenuButton |
199 | | - icon={<MaterialCommunityIcons name="youtube" size={22} color="white"/>} |
200 | | - text="Watch Video" |
201 | | - onPress={() => {}} |
202 | | - /> |
203 | | - <MenuButton |
204 | | - icon={<MaterialCommunityIcons name="share-variant" size={22} color="white"/>} |
205 | | - text="Share" |
206 | | - onPress={() => {}} |
207 | | - /> */} |
208 | | - </View> |
209 | | - </Modal> |
| 243 | + <> |
| 244 | + <Modal |
| 245 | + onBackButtonPress={() => setVisible({visible: false})} |
| 246 | + onBackdropPress={() => setVisible({visible: false})} |
| 247 | + isVisible={Visible.visible} |
| 248 | + backdropOpacity={0} |
| 249 | + animationIn="fadeIn" |
| 250 | + animationOut="fadeOut" |
| 251 | + animationInTiming={50} |
| 252 | + animationOutTiming={50} |
| 253 | + useNativeDriver |
| 254 | + hideModalContentWhileAnimating |
| 255 | + style={{ |
| 256 | + margin: 0, |
| 257 | + position: 'absolute', |
| 258 | + top: typeof Visible.position?.y === 'number' ? Visible.position.y : 0, |
| 259 | + right: 16, |
| 260 | + justifyContent: 'flex-start', |
| 261 | + }} |
| 262 | + > |
| 263 | + <View style={{ |
| 264 | + backgroundColor: "rgb(28,28,28)", |
| 265 | + borderRadius: 10, |
| 266 | + width: 200, |
| 267 | + overflow: 'hidden', |
| 268 | + elevation: 10, |
| 269 | + transform: [ |
| 270 | + { translateY: -50 }, |
| 271 | + { scale: Visible.visible ? 1 : 0.95 } |
| 272 | + ], |
| 273 | + opacity: Visible.visible ? 1 : 0, |
| 274 | + }}> |
| 275 | + {/* <MenuButton |
| 276 | + icon={<MaterialCommunityIcons name="magnify" size={22} color="white"/>} |
| 277 | + text="Search Home" |
| 278 | + onPress={() => { |
| 279 | + setVisible({visible: false}); |
| 280 | + }} |
| 281 | + /> */} |
| 282 | + <MenuButton |
| 283 | + icon={<MaterialCommunityIcons name="play-box-multiple" size={22} color="white"/>} |
| 284 | + text="Play Next" |
| 285 | + onPress={playNext} |
| 286 | + /> |
| 287 | + <MenuButton |
| 288 | + icon={<MaterialCommunityIcons name="playlist-music" size={22} color="white"/>} |
| 289 | + text="Add to Queue" |
| 290 | + onPress={addSongToQueue} |
| 291 | + /> |
| 292 | + <MenuButton |
| 293 | + icon={<MaterialCommunityIcons name="playlist-plus" size={22} color="white"/>} |
| 294 | + text="Add to Playlist" |
| 295 | + onPress={handleAddToPlaylist} |
| 296 | + /> |
| 297 | + <MenuButton |
| 298 | + icon={<MaterialCommunityIcons name="download" size={22} color="white"/>} |
| 299 | + text="Download" |
| 300 | + onPress={getPermission} |
| 301 | + /> |
| 302 | + {/* <MenuButton |
| 303 | + icon={<MaterialCommunityIcons name="youtube" size={22} color="white"/>} |
| 304 | + text="Watch Video" |
| 305 | + onPress={() => {}} |
| 306 | + /> |
| 307 | + <MenuButton |
| 308 | + icon={<MaterialCommunityIcons name="share-variant" size={22} color="white"/>} |
| 309 | + text="Share" |
| 310 | + onPress={() => {}} |
| 311 | + /> */} |
| 312 | + </View> |
| 313 | + </Modal> |
| 314 | + <Modal |
| 315 | + isVisible={showPlaylistModal} |
| 316 | + onBackdropPress={() => setShowPlaylistModal(false)} |
| 317 | + onBackButtonPress={() => setShowPlaylistModal(false)} |
| 318 | + style={{ |
| 319 | + margin: 20, |
| 320 | + justifyContent: 'center', |
| 321 | + }} |
| 322 | + > |
| 323 | + <View style={{ |
| 324 | + backgroundColor: "rgb(28,28,28)", |
| 325 | + borderRadius: 10, |
| 326 | + padding: 20, |
| 327 | + maxHeight: '80%', |
| 328 | + }}> |
| 329 | + <Heading text="Add to Playlist" /> |
| 330 | + <View style={{ marginVertical: 15 }}> |
| 331 | + <TextInput |
| 332 | + placeholder="Create new playlist..." |
| 333 | + placeholderTextColor="gray" |
| 334 | + value={newPlaylistName} |
| 335 | + onChangeText={setNewPlaylistName} |
| 336 | + style={{ |
| 337 | + borderWidth: 1, |
| 338 | + borderColor: 'gray', |
| 339 | + borderRadius: 5, |
| 340 | + padding: 10, |
| 341 | + color: 'white', |
| 342 | + marginBottom: 10, |
| 343 | + }} |
| 344 | + /> |
| 345 | + <Pressable |
| 346 | + onPress={handleCreatePlaylist} |
| 347 | + style={{ |
| 348 | + backgroundColor: '#1DB954', |
| 349 | + padding: 10, |
| 350 | + borderRadius: 5, |
| 351 | + alignItems: 'center', |
| 352 | + }} |
| 353 | + > |
| 354 | + <PlainText text="Create New Playlist" style={{ color: 'white' }} /> |
| 355 | + </Pressable> |
| 356 | + </View> |
| 357 | + <ScrollView style={{ maxHeight: 300 }}> |
| 358 | + {Object.keys(availablePlaylists).length === 0 ? ( |
| 359 | + <View style={styles.emptyState}> |
| 360 | + <FastImage |
| 361 | + source={require('../../Images/wav.png')} |
| 362 | + style={styles.emptyStateImage} |
| 363 | + /> |
| 364 | + <PlainText |
| 365 | + text="No playlists available. Create one!" |
| 366 | + style={{ color: 'gray', textAlign: 'center', marginTop: 10 }} |
| 367 | + /> |
| 368 | + </View> |
| 369 | + ) : ( |
| 370 | + Object.keys(availablePlaylists).map((name) => ( |
| 371 | + <Pressable |
| 372 | + key={name} |
| 373 | + onPress={() => addSongToSelectedPlaylist(name)} |
| 374 | + style={styles.playlistItem} |
| 375 | + > |
| 376 | + <FastImage |
| 377 | + source={getPlaylistImage(availablePlaylists[name])} |
| 378 | + style={styles.playlistImage} |
| 379 | + /> |
| 380 | + <View style={styles.playlistInfo}> |
| 381 | + <PlainText text={name} style={styles.playlistName} /> |
| 382 | + <PlainText |
| 383 | + text={`${availablePlaylists[name].length} songs`} |
| 384 | + style={styles.songCount} |
| 385 | + /> |
| 386 | + </View> |
| 387 | + </Pressable> |
| 388 | + )) |
| 389 | + )} |
| 390 | + </ScrollView> |
| 391 | + </View> |
| 392 | + </Modal> |
| 393 | + </> |
210 | 394 | ); |
211 | 395 | }; |
212 | 396 |
|
|
0 commit comments