Add step-by-step tutorial

This commit is contained in:
Evgeny
2025-02-20 07:53:57 +08:00
parent b5b701ce1c
commit adfa6fad13
7 changed files with 299 additions and 0 deletions

23
samples/cat_facts.lua Normal file
View File

@@ -0,0 +1,23 @@
-- name = "Cat Facts"
-- type = "widget"
-- on_alarm is called automatically, up to once every 30 minutes, to refresh the widget.
function on_alarm()
ui:show_text("Loading a random cat fact...")
http:get("https://catfact.ninja/fact")
end
function on_network_result(body, code)
if code == 200 then
local json = require "json"
local data = json.decode(body) -- The json module converts the JSON string into a Lua table.
if data and data.fact then
ui:show_text("Random Cat Fact:\n" .. data.fact)
else
ui:show_text("Failed to retrieve a cat fact.")
end
else
ui:show_text("Error loading data. Code: " .. code)
end
end

View File

@@ -0,0 +1,20 @@
-- name = "Custom Drawer Menu"
-- type = "drawer"
function on_drawer_open()
local menuItems = {"Open Website", "Update list", "Launch App"}
drawer:show_list(menuItems)
end
function on_click(index)
if index == 1 then
system:open_browser("https://www.example.com")
elseif index == 2 then
local menuItems = {"Open Website", "List updated", "Launch App"}
drawer:show_list(menuItems)
elseif index == 3 then
apps:launch("com.android.contacts")
end
return true
end

View File

@@ -0,0 +1,25 @@
-- name = "Interactive Counter"
-- type = "widget"
-- Global variable to store the current count.
local counter = 0
-- Function to update the display: shows two buttons with the counter embedded in the first button.
function update_display()
ui:show_buttons({ "Increase (" .. counter .. ")", "Reset" })
end
function on_load()
update_display()
end
-- Function to handle button clicks.
function on_click(index)
if index == 1 then
counter = counter + 1
elseif index == 2 then
counter = 0
end
update_display()
end

View File

@@ -0,0 +1,4 @@
function on_load()
aio:remove_widget("sale")
ui:show_text("Sale widget not removed :)")
end

View File

@@ -0,0 +1,25 @@
-- name = "Search Engine Selector"
-- type = "search"
local lastQuery = ""
function on_search(query)
lastQuery = query
local results = {}
if #query > 0 then
table.insert(results, "Search Google for: " .. query)
table.insert(results, "Search Bing for: " .. query)
end
search:show_lines(results)
end
function on_click(index)
if index == 1 then
system:open_browser("https://www.google.com/search?q=" .. lastQuery)
return true
elseif index == 2 then
system:open_browser("https://www.bing.com/search?q=" .. lastQuery)
return true
end
return false
end