Don't block waiting to initialize geoclue

This commit is contained in:
2022-12-24 00:18:38 -08:00
parent cae4e512f5
commit 52ccf6d080
3 changed files with 26 additions and 10 deletions

View File

@@ -18,8 +18,4 @@ Then you can run the program with
crystal run src/gtktest.cr crystal run src/gtktest.cr
``` ```
You can build for production with You can build for production with `make` and install with `make install`
```
shards build -Dpreview_mt --release --no-debug
```

View File

@@ -1,10 +1,27 @@
module Wince::Location module Wince::Location
extend self extend self
@@client = Geoclue::Simple.new_sync("space.quietfeathers.Wince", Geoclue::AccuracyLevel::Street, nil) @@channel = Channel(Geoclue::Simple).new(1)
@@client : Geoclue::Simple? = nil
@@fiber : Fiber = spawn setup_client
def find_location() def setup_client
location = @@client.location @@channel.send Geoclue::Simple.new_sync("space.quietfeathers.Wince", Geoclue::AccuracyLevel::Street, nil)
[location.latitude, location.longitude] Fiber.yield
end
# returns latlon if available, otherwise empty array
def find_location
if !@@fiber.dead?
return [] of Float64
end
if @@client.nil?
@@client = @@channel.receive
end
location = @@client.try do |c|
[c.location.latitude, c.location.longitude]
end || [] of Float64
end end
end end

View File

@@ -62,7 +62,10 @@ module Wince
def handle_geolocate def handle_geolocate
latlon = Location.find_location() latlon = Location.find_location()
LOCATION_ENTRY.text = "#{latlon[0]}, #{latlon[1]}"
if !latlon.empty?
LOCATION_ENTRY.text = "#{latlon[0]}, #{latlon[1]}"
end
end end
def yelp_response_to_business_ids(response : JSON::Any) def yelp_response_to_business_ids(response : JSON::Any)