diff --git a/.gitignore b/.gitignore index ab45be8..38206c4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /.shards/ *.dwarf /data/ui/compiled +api_key \ No newline at end of file diff --git a/README.md b/README.md index 0f2d279..ecc473b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,9 @@ You gotta have blueprint installed pacman -S blueprint-compiler crystal shards libadwaita ``` -Then you can test run with +Then you gotta get an API key from YELP and put it in the file `api_key` at the root of the project + +Then you can run the program with ``` crystal run src/gtktest.cr diff --git a/data/ui/main.blp b/data/ui/main.blp index 7397016..812999e 100644 --- a/data/ui/main.blp +++ b/data/ui/main.blp @@ -29,5 +29,15 @@ Adw.ApplicationWindow mainWindow { icon-name: "edit-find"; } } + + Gtk.ScrolledWindow { + vexpand: true; + + Gtk.ListBox searchResults { + margin-top: 14; + margin-start: 14; + margin-end: 14; + } + } } -} \ No newline at end of file +} diff --git a/data/ui/templates.blp b/data/ui/templates.blp new file mode 100644 index 0000000..e584633 --- /dev/null +++ b/data/ui/templates.blp @@ -0,0 +1,27 @@ +using Gtk 4.0; +using Adw 1; + +template Wince-BusinessRow : Gtk.ListBoxRow { + styles ["card"] + margin-bottom: 14; + Gtk.Box { + orientation: vertical; + margin-start: 8; + margin-top: 8; + + Gtk.Label businessName { + styles ["heading"] + xalign: 0; + } + + Gtk.Label businessRating { + xalign: 0; + } + Gtk.Label businessHours { + xalign: 0; + } + Gtk.LinkButton businessWebsite { + halign: start; + } + } +} \ No newline at end of file diff --git a/data/wince.gresource.xml b/data/wince.gresource.xml index 4224ddd..1bfd975 100644 --- a/data/wince.gresource.xml +++ b/data/wince.gresource.xml @@ -2,5 +2,6 @@ ui/compiled/main.ui + ui/compiled/templates.ui diff --git a/shard.lock b/shard.lock index 0f39dc6..48c675c 100644 --- a/shard.lock +++ b/shard.lock @@ -1,12 +1,13 @@ +# NOTICE: This lockfile contains some overrides from shard.override.yml version: 2.0 shards: gi-crystal: git: https://github.com/hugopl/gi-crystal.git - version: 0.14.0 + version: 0.14.0+git.commit.569f8d4b77effe312c642fc60929000e3e9514e2 - gtk4: + gtk4: # Overridden git: https://github.com/hugopl/gtk4.cr.git - version: 0.12.0 + version: 0.12.0+git.commit.ef2eb03b38b25ea2780e83cf50af3a6807ac6293 libadwaita: git: https://github.com/geopjr/libadwaita.cr.git diff --git a/shard.override.yml b/shard.override.yml new file mode 100644 index 0000000..14873e3 --- /dev/null +++ b/shard.override.yml @@ -0,0 +1,4 @@ +dependencies: + gtk4: + github: hugopl/gtk4.cr + branch: master diff --git a/src/modules/api/yelp.cr b/src/modules/api/yelp.cr new file mode 100644 index 0000000..38a62e3 --- /dev/null +++ b/src/modules/api/yelp.cr @@ -0,0 +1,29 @@ +require "http/client" +require "io" + +module Wince::Yelp + extend self + + @@token : String = {{ read_file("./api_key") }} + + def search_businesses(search : String, location : String) + + params = URI::Params.encode({ + location: location, + term: search, + sort_by: "best_match" + }) + uri = URI.new( + scheme: "https", + host: "api.yelp.com", + path: "/v3/businesses/search", + query: params + ) + headers = HTTP::Headers{ "Authorization" => "Bearer " + @@token } + response = HTTP::Client.get(uri, headers) + end +end + +#response = Wince::Yelp.search_businesses("miss saigon", "94610") +#puts response.status_code +#puts response.body diff --git a/src/modules/templates/businessrow.cr b/src/modules/templates/businessrow.cr new file mode 100644 index 0000000..0f16c09 --- /dev/null +++ b/src/modules/templates/businessrow.cr @@ -0,0 +1,26 @@ +module Wince + @[Gtk::UiTemplate(resource: "/wince/ui/compiled/templates.ui", children: %w(businessName businessRating businessHours businessWebsite))] + class BusinessRow < Gtk::ListBoxRow + include Gtk::WidgetTemplate + + @name : Gtk::Label + @rating : Gtk::Label + @hours : Gtk::Label + @website : Gtk::LinkButton + + def initialize(name : String, rating : String, hours : String, website : String) + super() + + @name = Gtk::Label.cast(template_child("businessName")) + @rating = Gtk::Label.cast(template_child("businessRating")) + @hours = Gtk::Label.cast(template_child("businessHours")) + @website = Gtk::LinkButton.cast(template_child("businessWebsite")) + + @name.text = name + @rating.text = rating + @hours.text = hours + @website.uri = website + @website.label = "view on yelp" + end + end +end \ No newline at end of file diff --git a/src/modules/views/main.cr b/src/modules/views/main.cr index 71a4033..25474e7 100644 --- a/src/modules/views/main.cr +++ b/src/modules/views/main.cr @@ -1,5 +1,11 @@ +require "json" + +require "../templates/businessrow.cr" +require "../api/yelp.cr" + module Wince @@main_window_id = 0_u32 + @@business_rows = [] of BusinessRow def activate(app : Adw::Application) main_window = APP.window_by_id(@@main_window_id) @@ -12,14 +18,49 @@ module Wince SEARCH_BUTTON.clicked_signal.connect do handle_search - end + end window.present end + def yelp_response_to_business_rows(response : String) + businesses = JSON.parse(response)["businesses"].as_a + + businesses.map do |business| + name = business["name"].as_s? || "" + rating = business["rating"].as_f.to_s + hours = business["is_closed"].as_bool.to_s + website = business["url"].as_s? || "" + + BusinessRow.new(name, rating, hours, website) + end + end + + def clear_business_rows + @@business_rows.each { |row| BUSINESS_LIST.remove(row) } + end + def handle_search - puts "searched for " + SEARCH_ENTRY.buffer.text - puts "location for " + LOCATION_ENTRY.buffer.text + search = SEARCH_ENTRY.buffer.text + location = LOCATION_ENTRY.buffer.text + + if !search || !location + return + end + + 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 + + clear_business_rows() + @@business_rows = yelp_response_to_business_rows(response.body) + @@business_rows.each do |row| + BUSINESS_LIST.append(row) + end end APP.activate_signal.connect(->activate(Adw::Application)) diff --git a/src/wince.cr b/src/wince.cr index f08dd96..38589d4 100644 --- a/src/wince.cr +++ b/src/wince.cr @@ -9,6 +9,7 @@ module Wince SEARCH_ENTRY = Gtk::Entry.cast(B_UI["searchEntry"]) LOCATION_ENTRY = Gtk::Entry.cast(B_UI["locationEntry"]) SEARCH_BUTTON = Gtk::Button.cast(B_UI["searchButton"]) + BUSINESS_LIST = Gtk::ListBox.cast(B_UI["searchResults"]) APP = Adw::Application.new("dev.wince", Gio::ApplicationFlags::None) end