Very basic details (name and hours) work now
This commit is contained in:
@@ -49,11 +49,32 @@ Adw.ApplicationWindow mainWindow {
|
||||
Gtk.ListBox searchResults {}
|
||||
}
|
||||
|
||||
Gtk.Box {
|
||||
Gtk.Label seeDetailsText {
|
||||
label: "Select a result to see details";
|
||||
hexpand: true;
|
||||
halign: center;
|
||||
Gtk.Label seeDetailsText {
|
||||
label: "Select a result to see details";
|
||||
hexpand: true;
|
||||
halign: center;
|
||||
}
|
||||
|
||||
Gtk.Box detailsBox {
|
||||
orientation: vertical;
|
||||
margin-start: 14;
|
||||
|
||||
Gtk.Label detailsTitle {
|
||||
halign: start;
|
||||
styles ["title-1"]
|
||||
}
|
||||
|
||||
Gtk.Box {
|
||||
orientation: horizontal;
|
||||
spacing: 12;
|
||||
|
||||
Gtk.Label detailsIsOpen {
|
||||
halign: start;
|
||||
}
|
||||
|
||||
Gtk.Label detailsCurrentHours {
|
||||
halign: start;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,6 @@ template Wince-BusinessRow : Gtk.ListBoxRow {
|
||||
halign: start;
|
||||
spacing: 12;
|
||||
|
||||
Gtk.Label businessOpen {
|
||||
}
|
||||
Gtk.Label businessRating {
|
||||
}
|
||||
Gtk.Label businessDistance {
|
||||
|
||||
@@ -29,7 +29,6 @@ module Wince::Yelp
|
||||
scheme: "https",
|
||||
host: "api.yelp.com",
|
||||
path: "/v3/businesses/#{id}",
|
||||
query: params
|
||||
)
|
||||
headers = HTTP::Headers{ "Authorization" => "Bearer " + @@token }
|
||||
response = HTTP::Client.get(uri, headers)
|
||||
|
||||
@@ -1,28 +1,21 @@
|
||||
module Wince
|
||||
@[Gtk::UiTemplate(resource: "/wince/ui/compiled/templates.ui", children: %w(businessName businessRating businessOpen businessDistance))]
|
||||
@[Gtk::UiTemplate(resource: "/wince/ui/compiled/templates.ui", children: %w(businessName businessRating businessDistance))]
|
||||
class BusinessRow < Gtk::ListBoxRow
|
||||
include Gtk::WidgetTemplate
|
||||
|
||||
@name : Gtk::Label
|
||||
@rating : Gtk::Label
|
||||
@open : Gtk::Label
|
||||
@distance : Gtk::Label
|
||||
|
||||
def initialize(name : String, rating : Float32, open : Bool, distance : Float32)
|
||||
def initialize(name : String, rating : Float32, distance : Float32)
|
||||
super()
|
||||
|
||||
@name = Gtk::Label.cast(template_child("businessName"))
|
||||
@rating = Gtk::Label.cast(template_child("businessRating"))
|
||||
@open = Gtk::Label.cast(template_child("businessOpen"))
|
||||
@distance = Gtk::Label.cast(template_child("businessDistance"))
|
||||
|
||||
@name.text = name
|
||||
@rating.text = rating.round(2).to_s
|
||||
if open
|
||||
@open.markup = "<span foreground=\"green\">open</span>"
|
||||
else
|
||||
@open.markup = "<span foreground=\"red\">closed</span>"
|
||||
end
|
||||
distance_miles = distance / 1609.344
|
||||
@distance.text = distance_miles.round(2).to_s + "mi"
|
||||
end
|
||||
|
||||
36
src/modules/utils/utils.cr
Normal file
36
src/modules/utils/utils.cr
Normal file
@@ -0,0 +1,36 @@
|
||||
require "time" # yeah me too
|
||||
|
||||
module Wince::Utils
|
||||
extend self
|
||||
|
||||
def hours_for_day(hours_json : JSON::Any, day : Time::DayOfWeek)
|
||||
open = hours_json[0]["open"].as_a
|
||||
day_number = day_of_week_to_int(day)
|
||||
|
||||
formatted_hours = open.select { |hour| hour["day"].as_i == day_number }.map { |hour|
|
||||
start_hour = hour["start"].as_s
|
||||
end_hour = hour["end"].as_s
|
||||
|
||||
"#{start_hour.insert(2, ":")}-#{end_hour.insert(2, ":")}"
|
||||
}.join(", ")
|
||||
end
|
||||
|
||||
def day_of_week_to_int(day : Time::DayOfWeek)
|
||||
case day
|
||||
when Time::DayOfWeek::Monday
|
||||
0
|
||||
when Time::DayOfWeek::Tuesday
|
||||
1
|
||||
when Time::DayOfWeek::Wednesday
|
||||
2
|
||||
when Time::DayOfWeek::Thursday
|
||||
3
|
||||
when Time::DayOfWeek::Friday
|
||||
4
|
||||
when Time::DayOfWeek::Saturday
|
||||
5
|
||||
when Time::DayOfWeek::Sunday
|
||||
6
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,7 +1,9 @@
|
||||
require "json"
|
||||
require "time"
|
||||
|
||||
require "../templates/businessrow.cr"
|
||||
require "../api/yelp.cr"
|
||||
require "../utils/utils.cr"
|
||||
|
||||
module Wince
|
||||
@@main_window_id = 0_u32
|
||||
@@ -36,10 +38,9 @@ module Wince
|
||||
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)
|
||||
BusinessRow.new(name, rating, distance)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -81,8 +82,30 @@ module Wince
|
||||
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
|
||||
|
||||
response = Yelp.get_business_info(id)
|
||||
|
||||
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)
|
||||
|
||||
SEE_DETAILS_TEXT.visible = false
|
||||
DETAILS_TITLE.text = response_json["name"].as_s? || ""
|
||||
|
||||
is_open = response_json["hours"][0]["is_open_now"].as_bool? || false
|
||||
|
||||
if is_open
|
||||
DETAILS_IS_OPEN.markup = "<span foreground=\"green\">open</span>"
|
||||
else
|
||||
DETAILS_IS_OPEN.markup = "<span foreground=\"red\">closed</span>"
|
||||
end
|
||||
|
||||
DETAILS_CURRENT_HOURS.text = Utils.hours_for_day(response_json["hours"], Time.local.day_of_week)
|
||||
DETAILS_BOX.visible = true
|
||||
end
|
||||
|
||||
APP.activate_signal.connect(->activate(Adw::Application))
|
||||
|
||||
@@ -14,6 +14,10 @@ module Wince
|
||||
SCROLL_VIEW = Gtk::ScrolledWindow.cast(B_UI["scrollWindow"])
|
||||
LEAFLET = Adw::Leaflet.cast(B_UI["leaflet"])
|
||||
SEE_DETAILS_TEXT = Gtk::Label.cast(B_UI["seeDetailsText"])
|
||||
DETAILS_BOX = Gtk::Box.cast(B_UI["detailsBox"])
|
||||
DETAILS_TITLE = Gtk::Label.cast(B_UI["detailsTitle"])
|
||||
DETAILS_IS_OPEN = Gtk::Label.cast(B_UI["detailsIsOpen"])
|
||||
DETAILS_CURRENT_HOURS = Gtk::Label.cast(B_UI["detailsCurrentHours"])
|
||||
|
||||
APP = Adw::Application.new("dev.wince", Gio::ApplicationFlags::None)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user