require "json" require "../templates/businessrow.cr" require "../api/yelp.cr" module Wince @@main_window_id = 0_u32 @@business_rows = [] of BusinessRow @@business_ids = [] of String def activate(app : Adw::Application) main_window = APP.window_by_id(@@main_window_id) return main_window.present if main_window window = Adw::ApplicationWindow.cast(B_UI["mainWindow"]) window.application = app @@main_window_id = window.id SEARCH_BUTTON.clicked_signal.connect do handle_search end BUSINESS_LIST.row_selected_signal.connect do handle_business_select end window.present end def yelp_response_to_business_ids(response : JSON::Any) response["businesses"].as_a.map { |b| b["id"].as_s } end def yelp_response_to_business_rows(response : JSON::Any) response["businesses"].as_a.map do |business| name = business["name"].as_s? || "" rating = business["rating"].as_f32 open = business["is_closed"].as_bool distance = business["distance"].as_f32 BusinessRow.new(name, rating, open, distance) end end def clear_business_rows @@business_rows.each { |row| BUSINESS_LIST.remove(row) } end def handle_search search = SEARCH_ENTRY.buffer.text location = LOCATION_ENTRY.buffer.text if search.blank? || location.blank? LEAFLET.visible = false POWERD_BY_TEXT.visible = true return end LEAFLET.visible = true POWERD_BY_TEXT.visible = false response = Yelp.search_businesses(search, location) if response.status_code != 200 puts "api call error" puts response.body return #TODO: show a toast here end response_json = JSON.parse(response.body) clear_business_rows() @@business_ids = yelp_response_to_business_ids(response_json) @@business_rows = yelp_response_to_business_rows(response_json) @@business_rows.each do |row| BUSINESS_LIST.append(row) end end def handle_business_select index = @@business_rows.index(BUSINESS_LIST.selected_row) || 0 id = @@business_ids[index] puts id # TODO call yelp and show the results end APP.activate_signal.connect(->activate(Adw::Application)) exit(APP.run(ARGV)) end