@@ -154,6 +154,10 @@ class NominatimViewController: UIViewController, UISearchBarDelegate, UITableVie
154154
155155 let result = resultsArray [ indexPath. row]
156156
157+ if let text = searchBar. text, !text. isEmpty {
158+ updateHistory ( with: text)
159+ }
160+
157161 // if nominatim returned an OSM object directly then use it
158162 if let osmType = result. osm_type,
159163 let osmId = result. osm_id,
@@ -184,12 +188,11 @@ class NominatimViewController: UIViewController, UISearchBarDelegate, UITableVie
184188 let data = try await OsmDownloader . osmData ( forUrl: url)
185189 await MainActor . run {
186190 self . activityIndicator. stopAnimating ( )
187- if let node = data. nodes. first {
188- self . updateHistory ( with: " \( objType. string) \( objIdent) " )
189- self . jumpTo ( lat: node. latLon. lat, lon: node. latLon. lon, zoom: nil )
190- } else {
191+ guard let node = data. nodes. first else {
191192 self . presentErrorMessage ( )
193+ return
192194 }
195+ self . jumpTo ( lat: node. latLon. lat, lon: node. latLon. lon, zoom: nil )
193196 }
194197 } catch {
195198 await MainActor . run {
@@ -306,14 +309,19 @@ class NominatimViewController: UIViewController, UISearchBarDelegate, UITableVie
306309 showingHistory = true
307310 resultsArray = [ ]
308311 tableView. reloadData ( )
312+ activityIndicator. stopAnimating ( )
309313 return
310314 }
311315
312316 showingHistory = false
313317 activityIndicator. startAnimating ( )
314318 searchTask = Task {
315319 defer {
316- Task { await MainActor . run { self . activityIndicator. stopAnimating ( ) } }
320+ // Only stop the indicator if this task completed or failed;
321+ // a cancelled task leaves the indicator running for the next task.
322+ if !Task. isCancelled {
323+ activityIndicator. stopAnimating ( )
324+ }
317325 }
318326 // debounce before submitting the searching
319327 try ? await Task . sleep ( nanoseconds: 300_000000 )
@@ -331,68 +339,31 @@ class NominatimViewController: UIViewController, UISearchBarDelegate, UITableVie
331339
332340 // display the results
333341 let results = ( try ? JSONDecoder ( ) . decode ( [ NominatimResult ] . self, from: data) ) ?? [ ]
334- await MainActor . run {
335- resultsArray = sortedByDistance ( results)
336- tableView. reloadData ( )
337- }
342+ resultsArray = sortedByDistance ( results)
343+ tableView. reloadData ( )
338344 }
339345 }
340346
341347 func searchBarSearchButtonClicked( _ searchBar: UISearchBar ) {
342348 searchBar. resignFirstResponder ( )
343349
344- resultsArray = [ ]
345350 guard let string = searchBar. text,
346351 !string. isEmpty
347352 else {
348- // no search
349- searchBar. perform ( #selector( UIResponder . resignFirstResponder) , with: nil , afterDelay: 0.1 )
350353 return
351354 }
352355
353356 // try parsing it as a special case before doing Nominatim lookup
354357 if parsedAsOsmNote ( string) ||
355- parsedAsOsmObjectRef ( string) ||
356358 parsedAsGoogleDynamicLink ( string) ||
357359 parsedAsLatLon ( string)
358360 {
359361 return
360362 }
361-
362- guard
363- let url = nominatimSearchURL ( query: string,
364- lang: PresetLanguages . preferredLanguageCode ( ) ,
365- viewBox: AppDelegate . shared. mainView. viewPort. boundingLatLonForScreen ( ) )
366- else {
363+ if parsedAsOsmObjectRef ( string) {
364+ updateHistory ( with: string)
367365 return
368366 }
369- activityIndicator. startAnimating ( )
370-
371- Task {
372- defer {
373- activityIndicator. stopAnimating ( )
374- }
375- do {
376- let data = try await URLSession . shared. data ( with: url)
377- await MainActor . run {
378- resultsArray = sortedByDistance ( ( try ? JSONDecoder ( ) . decode ( [ NominatimResult ] . self, from: data) ) ?? [ ] )
379- tableView. reloadData ( )
380-
381- if resultsArray. count > 0 {
382- updateHistory ( with: string)
383- // flag that we're no longer showing history and remove all items
384- showingHistory = false
385- tableView. reloadData ( )
386- } else {
387- presentErrorMessage ( )
388- }
389- }
390- } catch {
391- await MainActor . run {
392- presentErrorMessage ( error)
393- }
394- }
395- }
396367 }
397368
398369 func presentErrorMessage( _ error: Error ? = nil ) {
0 commit comments