@@ -16,9 +16,6 @@ import {
1616} from "react-native" ;
1717import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1818
19- import Transit from "@/services/transit" ;
20- import { PlaceSuggestion } from "@/services/transit/models/PlaceSuggestion" ;
21- import { Stop } from "@/services/transit/models/Stop" ;
2219import { TransportAddress } from "@/stores/account/types" ;
2320import Button from "@/ui/components/Button" ;
2421import Item , { Leading , Trailing } from "@/ui/components/Item" ;
@@ -91,27 +88,23 @@ export const AddressModal = ({
9188 const theme = useTheme ( ) ;
9289 const { t } = useTranslation ( ) ;
9390 const insets = useSafeAreaInsets ( ) ;
94- const transit = new Transit ( ) ;
9591
9692 const [ status , requestPermission ] = Location . useForegroundPermissions ( ) ;
9793 const [ searchTerm , setSearchTerm ] = useState ( "" ) ;
98- const [ searchPlaceResults , setSearchPlaceResults ] = useState <
99- PlaceSuggestion [ ]
100- > ( [ ] ) ;
101- const [ searchStopResults , setSearchStopResults ] = useState < Stop [ ] > ( [ ] ) ;
94+ const [ searchResults , setSearchResults ] = useState < TransportAddress [ ] > ( [ ] ) ;
10295
103- let timeout : number | null = null ;
96+ let timeout : ReturnType < typeof setTimeout > | null = null ;
10497
10598 const search = async ( ) => {
106- const location = await Location . getCurrentPositionAsync ( ) ;
107- const res = await transit . suggestions (
108- location . coords . latitude ,
109- location . coords . longitude ,
110- searchTerm
111- ) ;
112-
113- setSearchPlaceResults ( res . suggestions . places ) ;
114- setSearchStopResults ( res . suggestions . stops ) ;
99+ const geocodes = await Location . geocodeAsync ( searchTerm ) ;
100+ const results = geocodes . slice ( 0 , 5 ) . map ( ( item ) => ( {
101+ firstTitle : searchTerm ,
102+ secondTitle : ` ${ item . latitude . toFixed ( 5 ) } , ${ item . longitude . toFixed ( 5 ) } ` ,
103+ address : searchTerm ,
104+ latitude : item . latitude ,
105+ longitude : item . longitude ,
106+ } ) ) ;
107+ setSearchResults ( results ) ;
115108 } ;
116109
117110 const currentLocationToTransportAddress =
@@ -127,41 +120,12 @@ export const AddressModal = ({
127120 } ) ;
128121 } ;
129122
130- const placeToTransportAddress = async (
131- place : PlaceSuggestion
132- ) : Promise < TransportAddress > => {
133- const details = await transit . locationDetails ( place . place_id ) ;
134-
135- return {
136- firstTitle : place . structured_formatting . main_text ,
137- secondTitle : place . structured_formatting . secondary_text ,
138- address : details . placeDetails . result . formatted_address ,
139- latitude : details . placeDetails . result . geometry . location . lat ,
140- longitude : details . placeDetails . result . geometry . location . lng ,
141- } ;
142- } ;
143-
144- const stopToTransportAddress = async (
145- stop : Stop
146- ) : Promise < TransportAddress > => {
147- return new Promise ( resolve => {
148- resolve ( {
149- firstTitle : stop . stop_name ,
150- secondTitle : stop . city_name ,
151- address : `${ stop . stop_name } , ${ stop . city_name } ` ,
152- latitude : stop . stop_lat ,
153- longitude : stop . stop_lon ,
154- } ) ;
155- } ) ;
156- } ;
157-
158123 useEffect ( ( ) => {
159124 if ( timeout !== null ) {
160125 clearTimeout ( timeout ) ;
161126 }
162127 if ( searchTerm . length === 0 ) {
163- setSearchPlaceResults ( [ ] ) ;
164- setSearchStopResults ( [ ] ) ;
128+ setSearchResults ( [ ] ) ;
165129 return ;
166130 }
167131 timeout = setTimeout ( ( ) => search ( ) , 200 ) ;
@@ -251,39 +215,19 @@ export const AddressModal = ({
251215 </ List >
252216 ) }
253217
254- { searchPlaceResults . length > 0 && (
218+ { searchResults . length > 0 && (
255219 < >
256220 < Typography variant = { "h6" } color = { "secondary" } >
257221 { t ( "Settings_Transport_Place" ) }
258222 </ Typography >
259223 < List >
260- { searchPlaceResults . map ( ( item : PlaceSuggestion ) => (
224+ { searchResults . map ( ( item : TransportAddress , index : number ) => (
261225 < AddressItem
262- key = { item . place_id }
226+ key = { ` ${ item . latitude } - ${ item . longitude } - ${ index } ` }
263227 icon = { "MapPin" }
264- firstLine = { item . structured_formatting . main_text }
265- secondLine = { item . structured_formatting . secondary_text }
266- convertFunction = { ( ) => placeToTransportAddress ( item ) }
267- save = { onConfirm }
268- />
269- ) ) }
270- </ List >
271- </ >
272- ) }
273-
274- { searchStopResults . length > 0 && (
275- < >
276- < Typography variant = { "h6" } color = { "secondary" } >
277- { t ( "Settings_Transport_Stops" ) }
278- </ Typography >
279- < List >
280- { searchStopResults . map ( ( item : Stop ) => (
281- < AddressItem
282- key = { item . raw_stop_id }
283- icon = { "Bus" }
284- firstLine = { item . stop_name }
285- secondLine = { item . city_name }
286- convertFunction = { ( ) => stopToTransportAddress ( item ) }
228+ firstLine = { item . firstTitle }
229+ secondLine = { item . secondTitle }
230+ convertFunction = { ( ) => Promise . resolve ( item ) }
287231 save = { onConfirm }
288232 />
289233 ) ) }
@@ -349,4 +293,4 @@ export const AddressModal = ({
349293 ) }
350294 </ View >
351295 ) ;
352- } ;
296+ } ;
0 commit comments