Cache application data for offline use

This commit is contained in:
2024-03-12 11:24:44 -05:00
parent 75b9e3e14a
commit a1810e3f2c
5 changed files with 34 additions and 4 deletions

View File

@@ -1,6 +1,8 @@
class W3DHub
class Api
class Applications
attr_reader :data
def initialize(response)
@data = JSON.parse(response, symbolize_names: true)

View File

@@ -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

View File

@@ -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

View File

@@ -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 = "<c=f80>Unable to connect to W3D Hub API. No application data cached, unable to continue.</c>"
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: []
}

View File

@@ -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