From 52ccf6d080b9f1b07fcf240df8bcfaa5394e5f41 Mon Sep 17 00:00:00 2001 From: Zoe Moore Date: Sat, 24 Dec 2022 00:18:38 -0800 Subject: [PATCH] Don't block waiting to initialize geoclue --- README.md | 6 +----- src/modules/api/location.cr | 25 +++++++++++++++++++++---- src/modules/views/main.cr | 5 ++++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a7358f2..52c66bd 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,4 @@ Then you can run the program with crystal run src/gtktest.cr ``` -You can build for production with - -``` -shards build -Dpreview_mt --release --no-debug -``` +You can build for production with `make` and install with `make install` diff --git a/src/modules/api/location.cr b/src/modules/api/location.cr index 8956e93..48bb1ea 100644 --- a/src/modules/api/location.cr +++ b/src/modules/api/location.cr @@ -1,10 +1,27 @@ module Wince::Location 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() - location = @@client.location - [location.latitude, location.longitude] + def setup_client + @@channel.send Geoclue::Simple.new_sync("space.quietfeathers.Wince", Geoclue::AccuracyLevel::Street, nil) + 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 diff --git a/src/modules/views/main.cr b/src/modules/views/main.cr index 7670b52..33e9af2 100644 --- a/src/modules/views/main.cr +++ b/src/modules/views/main.cr @@ -62,7 +62,10 @@ module Wince def handle_geolocate latlon = Location.find_location() - LOCATION_ENTRY.text = "#{latlon[0]}, #{latlon[1]}" + + if !latlon.empty? + LOCATION_ENTRY.text = "#{latlon[0]}, #{latlon[1]}" + end end def yelp_response_to_business_ids(response : JSON::Any)