From a1810e3f2ce7de503c526e0fcef499b90f1ae0f8 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Tue, 12 Mar 2024 11:24:44 -0500 Subject: [PATCH] Cache application data for offline use --- lib/api/applications.rb | 2 ++ lib/pages/login.rb | 2 ++ lib/settings.rb | 10 ++++++++++ lib/states/boot.rb | 23 +++++++++++++++++++---- w3d_hub_linux_launcher.rb | 1 + 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/api/applications.rb b/lib/api/applications.rb index 1a40f18..8e4f553 100644 --- a/lib/api/applications.rb +++ b/lib/api/applications.rb @@ -1,6 +1,8 @@ class W3DHub class Api class Applications + attr_reader :data + def initialize(response) @data = JSON.parse(response, symbolize_names: true) diff --git a/lib/pages/login.rb b/lib/pages/login.rb index 8d40d68..e35730c 100644 --- a/lib/pages/login.rb +++ b/lib/pages/login.rb @@ -102,6 +102,8 @@ class W3DHub end def populate_account_info + return if Store.offline_mode + @host.instance_variable_get(:"@account_container").clear do flow(fill: true, height: 1.0) do avatar_image = begin diff --git a/lib/settings.rb b/lib/settings.rb index 45d4def..35abeac 100644 --- a/lib/settings.rb +++ b/lib/settings.rb @@ -71,5 +71,15 @@ class W3DHub def save_settings File.write(SETTINGS_FILE_PATH, @settings.to_json) end + + def save_application_cache(json) + File.write(APPLICATIONS_CACHE_FILE_PATH, json) + end + + def load_application_cache + JSON.parse(File.read(APPLICATIONS_CACHE_FILE_PATH), symbolize_names: true) + rescue + nil + end end end diff --git a/lib/states/boot.rb b/lib/states/boot.rb index 31a1370..7dda073 100644 --- a/lib/states/boot.rb +++ b/lib/states/boot.rb @@ -29,8 +29,8 @@ class W3DHub stack(width: 1.0, height: 60) do flow(width: 1.0, height: 26, margin_left: 16, margin_right: 16, margin_bottom: 8, margin_top: 8) do - @status_label = caption "Starting #{I18n.t(:app_name_simple)}...", width: 0.5 - para "#{I18n.t(:app_name)} #{W3DHub::VERSION}", width: 0.5, text_align: :right + @status_label = caption "Starting #{I18n.t(:app_name_simple)}...", fill: true + para "#{I18n.t(:app_name)} #{W3DHub::VERSION}", text_align: :right end @progressbar = progress height: 4, width: 1.0, margin_left: 16, margin_right: 16, margin_bottom: 8 @@ -52,7 +52,16 @@ class W3DHub @progressbar.value = @fraction - load_offline_applications_list if @offline_mode + if @offline_mode + load_offline_applications_list + + unless Store.applications + @progressbar.value = 0.0 + @status_label.value = "Unable to connect to W3D Hub API. No application data cached, unable to continue." + + return + end + end if @offline_mode || (@progressbar.value >= 1.0 && @task_index == @tasks.size) pop_state @@ -156,7 +165,7 @@ class W3DHub Api.on_thread(:applications) do |applications| if applications Store.applications = applications - + Store.settings.save_application_cache(applications.data.to_json) @tasks[:applications][:complete] = true else # FIXME: Failed to retreive! @@ -227,6 +236,12 @@ class W3DHub end def load_offline_applications_list + if (application_cache = Store.settings.load_application_cache) + Store.applications = Api::Applications.new(application_cache.to_json) + + return + end + hash = { applications: [] } diff --git a/w3d_hub_linux_launcher.rb b/w3d_hub_linux_launcher.rb index d4dc03a..9af7df7 100644 --- a/w3d_hub_linux_launcher.rb +++ b/w3d_hub_linux_launcher.rb @@ -23,6 +23,7 @@ class W3DHub GAME_ROOT_PATH = File.expand_path(".", __dir__) CACHE_PATH = "#{GAME_ROOT_PATH}/data/cache" SETTINGS_FILE_PATH = "#{GAME_ROOT_PATH}/data/settings.json" + APPLICATIONS_CACHE_FILE_PATH = "#{GAME_ROOT_PATH}/data/applications_cache.json" LOGGER = Logger.new("#{GAME_ROOT_PATH}/data/logs/w3d_hub_linux_launcher.log", "daily") LOGGER.level = Logger::Severity::DEBUG # W3DHUB_DEBUG ? Logger::Severity::DEBUG : Logger::Severity::WARN