Starting on the details page

This commit is contained in:
2022-12-19 00:55:39 -08:00
parent 9aa5741e1a
commit 976562e8bd
4 changed files with 59 additions and 16 deletions

View File

@@ -3,7 +3,7 @@ using Adw 1;
Adw.ApplicationWindow mainWindow { Adw.ApplicationWindow mainWindow {
title: "Wince"; title: "Wince";
default-width: 600; default-width: 700;
default-height: 500; default-height: 500;
Gtk.Box { Gtk.Box {
@@ -32,19 +32,30 @@ Adw.ApplicationWindow mainWindow {
} }
} }
Gtk.ScrolledWindow scrollWindow {
vexpand: true;
visible: false;
Gtk.ListBox searchResults {}
}
Gtk.Label poweredByText { Gtk.Label poweredByText {
halign: center; halign: center;
valign: center; valign: center;
vexpand: true; vexpand: true;
use-markup: true; label: "Wince is powered by Yelp";
label: "<span foreground=\"gray\" style=\"italic\">Wince is powered by Yelp</span>"; }
Adw.Leaflet leaflet {
visible: false;
Gtk.ScrolledWindow scrollWindow {
vexpand: true;
width-request: 300;
Gtk.ListBox searchResults {}
}
Gtk.Box {
Gtk.Label seeDetailsText {
label: "Select a result to see details";
hexpand: true;
halign: center;
}
}
} }
} }
} }

View File

@@ -22,6 +22,19 @@ module Wince::Yelp
headers = HTTP::Headers{ "Authorization" => "Bearer " + @@token } headers = HTTP::Headers{ "Authorization" => "Bearer " + @@token }
response = HTTP::Client.get(uri, headers) response = HTTP::Client.get(uri, headers)
end end
def get_business_info(id : String)
uri = URI.new(
scheme: "https",
host: "api.yelp.com",
path: "/v3/businesses/#{id}",
query: params
)
headers = HTTP::Headers{ "Authorization" => "Bearer " + @@token }
response = HTTP::Client.get(uri, headers)
end
end end
#response = Wince::Yelp.search_businesses("miss saigon", "94610") #response = Wince::Yelp.search_businesses("miss saigon", "94610")

View File

@@ -6,6 +6,7 @@ require "../api/yelp.cr"
module Wince module Wince
@@main_window_id = 0_u32 @@main_window_id = 0_u32
@@business_rows = [] of BusinessRow @@business_rows = [] of BusinessRow
@@business_ids = [] of String
def activate(app : Adw::Application) def activate(app : Adw::Application)
main_window = APP.window_by_id(@@main_window_id) main_window = APP.window_by_id(@@main_window_id)
@@ -20,13 +21,19 @@ module Wince
handle_search handle_search
end end
BUSINESS_LIST.row_selected_signal.connect do
handle_business_select
end
window.present window.present
end end
def yelp_response_to_business_rows(response : String) def yelp_response_to_business_ids(response : JSON::Any)
businesses = JSON.parse(response)["businesses"].as_a response["businesses"].as_a.map { |b| b["id"].as_s }
end
businesses.map do |business| def yelp_response_to_business_rows(response : JSON::Any)
response["businesses"].as_a.map do |business|
name = business["name"].as_s? || "" name = business["name"].as_s? || ""
rating = business["rating"].as_f32 rating = business["rating"].as_f32
open = business["is_closed"].as_bool open = business["is_closed"].as_bool
@@ -45,12 +52,12 @@ module Wince
location = LOCATION_ENTRY.buffer.text location = LOCATION_ENTRY.buffer.text
if search.blank? || location.blank? if search.blank? || location.blank?
SCROLL_VIEW.visible = false LEAFLET.visible = false
POWERD_BY_TEXT.visible = true POWERD_BY_TEXT.visible = true
return return
end end
SCROLL_VIEW.visible = true LEAFLET.visible = true
POWERD_BY_TEXT.visible = false POWERD_BY_TEXT.visible = false
response = Yelp.search_businesses(search, location) response = Yelp.search_businesses(search, location)
@@ -61,13 +68,23 @@ module Wince
return #TODO: show a toast here return #TODO: show a toast here
end end
response_json = JSON.parse(response.body)
clear_business_rows() clear_business_rows()
@@business_rows = yelp_response_to_business_rows(response.body) @@business_ids = yelp_response_to_business_ids(response_json)
@@business_rows = yelp_response_to_business_rows(response_json)
@@business_rows.each do |row| @@business_rows.each do |row|
BUSINESS_LIST.append(row) BUSINESS_LIST.append(row)
end end
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)) APP.activate_signal.connect(->activate(Adw::Application))
exit(APP.run(ARGV)) exit(APP.run(ARGV))
end end

View File

@@ -12,6 +12,8 @@ module Wince
BUSINESS_LIST = Gtk::ListBox.cast(B_UI["searchResults"]) BUSINESS_LIST = Gtk::ListBox.cast(B_UI["searchResults"])
POWERD_BY_TEXT = Gtk::Label.cast(B_UI["poweredByText"]) POWERD_BY_TEXT = Gtk::Label.cast(B_UI["poweredByText"])
SCROLL_VIEW = Gtk::ScrolledWindow.cast(B_UI["scrollWindow"]) SCROLL_VIEW = Gtk::ScrolledWindow.cast(B_UI["scrollWindow"])
LEAFLET = Adw::Leaflet.cast(B_UI["leaflet"])
SEE_DETAILS_TEXT = Gtk::Label.cast(B_UI["seeDetailsText"])
APP = Adw::Application.new("dev.wince", Gio::ApplicationFlags::None) APP = Adw::Application.new("dev.wince", Gio::ApplicationFlags::None)
end end