diff --git a/data/ui/main.blp b/data/ui/main.blp index 075baf0..6239a18 100644 --- a/data/ui/main.blp +++ b/data/ui/main.blp @@ -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; + } } } } diff --git a/data/ui/templates.blp b/data/ui/templates.blp index e7f6ebf..429325c 100644 --- a/data/ui/templates.blp +++ b/data/ui/templates.blp @@ -18,8 +18,6 @@ template Wince-BusinessRow : Gtk.ListBoxRow { halign: start; spacing: 12; - Gtk.Label businessOpen { - } Gtk.Label businessRating { } Gtk.Label businessDistance { diff --git a/src/modules/api/yelp.cr b/src/modules/api/yelp.cr index 5e90bdd..7dd6712 100644 --- a/src/modules/api/yelp.cr +++ b/src/modules/api/yelp.cr @@ -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) diff --git a/src/modules/templates/businessrow.cr b/src/modules/templates/businessrow.cr index ff26894..bb9a47f 100644 --- a/src/modules/templates/businessrow.cr +++ b/src/modules/templates/businessrow.cr @@ -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 = "open" - else - @open.markup = "closed" - end distance_miles = distance / 1609.344 @distance.text = distance_miles.round(2).to_s + "mi" end diff --git a/src/modules/utils/utils.cr b/src/modules/utils/utils.cr new file mode 100644 index 0000000..111173c --- /dev/null +++ b/src/modules/utils/utils.cr @@ -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 \ No newline at end of file diff --git a/src/modules/views/main.cr b/src/modules/views/main.cr index 43cf717..cfebeb5 100644 --- a/src/modules/views/main.cr +++ b/src/modules/views/main.cr @@ -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 = "open" + else + DETAILS_IS_OPEN.markup = "closed" + 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)) diff --git a/src/wince.cr b/src/wince.cr index 3330382..1df3867 100644 --- a/src/wince.cr +++ b/src/wince.cr @@ -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