@@ -7,17 +7,30 @@ import {
7
7
StyleSheet ,
8
8
Image ,
9
9
} from 'react-native' ;
10
- import { globalStyles } from './Styles' ;
11
10
import { getTracksAsync } from '@nodefinity/react-native-music-library' ;
12
11
import { useState } from 'react' ;
13
12
import type { Track } from '../../src/NativeMusicLibrary' ;
13
+ import { usePermission } from './usePermission' ;
14
+ import { pickDirectory } from '@react-native-documents/picker' ;
14
15
15
16
export default function TrackList ( ) {
16
17
const [ tracks , setTracks ] = useState < Track [ ] > ( [ ] ) ;
17
18
const [ loading , setLoading ] = useState ( false ) ;
19
+ const { permissionStatus, requestPermissions } = usePermission ( ) ;
18
20
19
21
const getAllTracks = async ( ) => {
20
22
try {
23
+ if ( ! permissionStatus ?. granted ) {
24
+ const result = await requestPermissions ( ) ;
25
+ if ( ! result . granted ) {
26
+ Alert . alert (
27
+ 'Error' ,
28
+ 'Can not request permission again, please grant audio permission manually'
29
+ ) ;
30
+ return ;
31
+ }
32
+ }
33
+
21
34
setLoading ( true ) ;
22
35
const startTime = Date . now ( ) ;
23
36
const results = await loadAllTracks ( ) ;
@@ -76,15 +89,46 @@ export default function TrackList() {
76
89
</ View >
77
90
) ;
78
91
92
+ const getTracksFromPickedDirectory = async ( ) => {
93
+ try {
94
+ const { uri } = await pickDirectory ( {
95
+ requestLongTermAccess : false ,
96
+ } ) ;
97
+
98
+ console . log ( 'uri' , uri ) ;
99
+
100
+ if ( ! uri ) return ;
101
+
102
+ const results = await getTracksAsync ( {
103
+ first : 100 ,
104
+ directory : uri ,
105
+ } ) ;
106
+
107
+ Alert . alert (
108
+ 'Success' ,
109
+ `Picked ${ results . items . length } tracks from:\n${ uri } `
110
+ ) ;
111
+ } catch ( err ) {
112
+ Alert . alert ( 'Error' , err as string ) ;
113
+ } finally {
114
+ setLoading ( false ) ;
115
+ }
116
+ } ;
117
+
79
118
return (
80
119
< >
81
- < View style = { globalStyles . buttonContainer } >
82
- < View style = { globalStyles . buttonSpacer } />
120
+ < View style = { styles . buttonContainer } >
83
121
< Button
84
122
title = { `${ loading ? 'loading...' : '' } get all tracks` }
85
123
onPress = { getAllTracks }
86
124
disabled = { loading }
87
125
/>
126
+
127
+ < Button
128
+ title = { `${ loading ? 'loading...' : '' } get tracks from directory` }
129
+ onPress = { getTracksFromPickedDirectory }
130
+ disabled = { loading }
131
+ />
88
132
</ View >
89
133
90
134
{ tracks . length > 0 && (
@@ -100,6 +144,11 @@ export default function TrackList() {
100
144
}
101
145
102
146
const styles = StyleSheet . create ( {
147
+ buttonContainer : {
148
+ width : '100%' ,
149
+ maxWidth : 300 ,
150
+ gap : 10 ,
151
+ } ,
103
152
list : {
104
153
flex : 1 ,
105
154
width : '100%' ,
0 commit comments