Previews in AppMarket

This commit is contained in:
Smok1e 2024-07-02 14:51:58 +03:00
parent 318c96d948
commit 57446a0bd8

View File

@ -9,6 +9,7 @@ local color = require("Color")
local number = require("Number") local number = require("Number")
local text = require("Text") local text = require("Text")
local event = require("Event") local event = require("Event")
local SHA = require("SHA-256")
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -23,7 +24,7 @@ local overviewMaximumTouchAcceleration = 5
local appMarketPath = paths.user.applicationData .. "App Market/" local appMarketPath = paths.user.applicationData .. "App Market/"
local configPath = appMarketPath .. "Config.cfg" local configPath = appMarketPath .. "Config.cfg"
local userPath = appMarketPath .. "User.cfg" local userPath = appMarketPath .. "User.cfg"
local iconCachePath = appMarketPath .. "Cache/" local cachePath = appMarketPath .. "Cache/"
local currentScriptDirectory = filesystem.path(system.getCurrentScript()) local currentScriptDirectory = filesystem.path(system.getCurrentScript())
local localization = system.getLocalization(currentScriptDirectory .. "Localizations/") local localization = system.getLocalization(currentScriptDirectory .. "Localizations/")
@ -35,6 +36,14 @@ local categories = {
{ icon = "📷", name = localization.categoryWallpapers }, { icon = "📷", name = localization.categoryWallpapers },
} }
local filesTypes = {
main = 1,
resource = 2,
icon = 3,
localization = 4,
preview = 5
}
local orderDirections = { local orderDirections = {
"desc", "desc",
"asc", "asc",
@ -76,7 +85,7 @@ if not component.isAvailable("internet") then
return return
end end
filesystem.makeDirectory(iconCachePath) filesystem.makeDirectory(cachePath)
local iconsCache = {} local iconsCache = {}
@ -261,61 +270,60 @@ local function checkContentLength(url)
end end
end end
local function checkImage(url, mneTolkoSprosit) local function checkImage(url, mneTolkoSprosit, sizeFilter)
local handle, reason = checkContentLength(url) local handle, reason = checkContentLength(url)
if handle then if not handle then
return false, reason
end
local needCheck, data, chunk, reason = true, "" local needCheck, data, chunk, reason = true, ""
while true do while true do
chunk, reason = handle.read(math.huge) chunk, reason = handle.read(math.huge)
if chunk then if not chunk then
handle:close()
if reason then
return false, reason
end
return data
end
data = data .. chunk data = data .. chunk
progressIndicator:roll() progressIndicator:roll()
workspace:draw() workspace:draw()
if needCheck and #data > 8 then if needCheck and #data > 8 then
if data:sub(1, 4) == "OCIF" then if data:sub(1, 4) ~= "OCIF" then
local encodingMethod = string.byte(data:sub(5, 5)) handle:close()
return false, "Wrong image file signature"
end
local encodingMethod = string.byte(data:sub(5, 5))
if not (6 <= encodingMethod and encodingMethod <= 8) then
handle:close()
return false, "Image method is not supported"
end
local is8 = encodingMethod == 8 and 1 or 0
if sizeFilter then
local result, reason = sizeFilter(string.byte(data:sub(6, 6)) + is8, string.byte(data:sub(7, 7)) + is8)
if not result then
handle:close()
return false, reason
end
end
if encodingMethod >= 6 or encodingMethod <= 8 then
if string.byte(data:sub(6, 6)) <= 8 and string.byte(data:sub(7, 7)) <= 4 then
if mneTolkoSprosit then if mneTolkoSprosit then
handle:close() handle:close()
return true return true
end end
needCheck = false needCheck = false
else
handle:close()
return false, "Image size is larger than 8x4"
end end
else
handle:close()
return false, "Image encoding method is not supported"
end
else
handle:close()
return false, "Wrong image file signature"
end
end
else
handle:close()
if reason then
return false, reason
else
return data
end
end
end
else
return false, reason
end end
end end
@ -519,7 +527,11 @@ local function download(publication)
if publication.dependencies then if publication.dependencies then
for i = 1, #publication.all_dependencies do for i = 1, #publication.all_dependencies do
table.insert(treeData, publication.dependencies_data[publication.all_dependencies[i]]) local dependency = publication.dependencies_data[publication.all_dependencies[i]]
if dependency.type_id ~= filesTypes.preview then
table.insert(treeData, dependency)
end
end end
end end
@ -586,6 +598,7 @@ local function download(publication)
if publication.dependencies then if publication.dependencies then
for i = 1, #publication.all_dependencies do for i = 1, #publication.all_dependencies do
local dependency = publication.dependencies_data[publication.all_dependencies[i]] local dependency = publication.dependencies_data[publication.all_dependencies[i]]
if dependency.type_id ~= filesTypes.preview then
local dependencyPath = getDependencyPath(mainFilePath, dependency) local dependencyPath = getDependencyPath(mainFilePath, dependency)
govnoed(dependency, i + 1) govnoed(dependency, i + 1)
@ -601,6 +614,7 @@ local function download(publication)
end end
end end
end end
end
container:remove() container:remove()
callLastMethod() callLastMethod()
@ -631,12 +645,8 @@ local function loadImage(path)
end end
end end
local function getPublicationIcon(publication) local function getImage(publication, url, sizeFilter)
if publication.icon_url then local path = cachePath .. publication.file_id .. "@" .. publication.version .. "_" .. SHA.hash(url) .. ".pic"
if config.hideApplicationIcons then
return loadIcon("Application")
else
local path = iconCachePath .. publication.file_id .. "@" .. publication.version .. ".pic"
if filesystem.exists(path) then if filesystem.exists(path) then
return loadImage(path) return loadImage(path)
@ -644,7 +654,7 @@ local function getPublicationIcon(publication)
progressIndicator.active = true progressIndicator.active = true
workspace:draw() workspace:draw()
local data, reason = checkImage(publication.icon_url) local data, reason = checkImage(url, false, sizeFilter)
progressIndicator.active = false progressIndicator.active = false
workspace:draw() workspace:draw()
@ -654,9 +664,33 @@ local function getPublicationIcon(publication)
return loadImage(path) return loadImage(path)
else else
return nil, reason
end
end
end
local function getPublicationIcon(publication)
if publication.icon_url then
if config.hideApplicationIcons then
return loadIcon("Application")
else
local image, reason = getImage(
publication,
publication.icon_url,
function(width, height)
if width > 8 or height > 4 then
return false, "Image size is larger than 8x4"
end
return true
end
)
if not image then
return loadIcon("Application") return loadIcon("Application")
end end
end
return image
end end
elseif publication.category_id == 2 then elseif publication.category_id == 2 then
return loadIcon("Lua") return loadIcon("Lua")
@ -1026,9 +1060,9 @@ local function settings()
end end
buttonsLayout:addChild(GUI.adaptiveRoundedButton(1, 1, 2, 0, 0xC3C3C3, 0xFFFFFF, 0x2D2D2D, 0xFFFFFF, localization.clearCache)).onTouch = function() buttonsLayout:addChild(GUI.adaptiveRoundedButton(1, 1, 2, 0, 0xC3C3C3, 0xFFFFFF, 0x2D2D2D, 0xFFFFFF, localization.clearCache)).onTouch = function()
local list = filesystem.list(iconCachePath) local list = filesystem.list(cachePath)
for i = 1, #list do for i = 1, #list do
filesystem.remove(iconCachePath .. list[i]) filesystem.remove(cachePath .. list[i])
end end
end end
@ -1243,6 +1277,73 @@ local function dialogs()
end end
end end
--------------------------------------------------------------------------------
local function previewContainerUpdate(container)
container.height = 0
container.contentWidth = 0
local child
for i = 1, #container.children do
child = container.children[i]
child.localX = container.contentWidth + 1 + container.contentOffset
container.contentWidth = container.contentWidth + child.width + container.horizontalSpacing
if child.height > container.height then
container.height = child.height
end
end
end
local function previewContainerFocusItem(container, index)
if index < 1 or index > #container.children then
return
end
container.focusedItem = index
local newContentOffset = 0
for i = 1, index - 1 do
newContentOffset = newContentOffset - container.children[i].width - container.horizontalSpacing
end
local startContentOffset = container.contentOffset
container:addAnimation(
function(animation)
container.contentOffset = math.floor(startContentOffset + (newContentOffset - startContentOffset) * animation.position^(1/2))
container:update()
end,
function(animation)
animation:remove()
if container.onItemFocused then
container.onItemFocused()
end
end
):start(container.animationDuration)
end
-- Хитрый контейнер, который будет демонстрировать элементы в виде горизонтальной прокручиваемой ленты
local function previewContainerNew(x, y, width, height)
local container = GUI.container(x, y, width, height)
container.contentOffset = 0
container.focusedItem = 1
container.contentWidth = 0
container.horizontalSpacing = 1
container.animationDuration = .2
container.update = previewContainerUpdate
container.focusItem = previewContainerFocusItem
return container
end
--------------------------------------------------------------------------------
newPublicationInfo = function(file_id) newPublicationInfo = function(file_id)
lastMethod, lastArguments = newPublicationInfo, {file_id} lastMethod, lastArguments = newPublicationInfo, {file_id}
@ -1453,6 +1554,19 @@ newPublicationInfo = function(file_id)
end end
end end
-- Превьюхи
local previewContainer = textDetailsContainer:addChild(previewContainerNew(3, y, textDetailsContainer.width - 4, 1))
if publication.dependencies_data then
for file_id, dependency in pairs(publication.dependencies_data) do
if dependency.type_id == filesTypes.preview then
previewContainer:addChild(GUI.image(1, 1, getImage(publication, dependency.source_url)))
end
end
end
previewContainer:update()
-- Подсчитываем результирующие размеры -- Подсчитываем результирующие размеры
textDetailsContainer.height = math.max( textDetailsContainer.height = math.max(
textDetailsContainer.children[#textDetailsContainer.children].localY + textDetailsContainer.children[#textDetailsContainer.children].height, textDetailsContainer.children[#textDetailsContainer.children].localY + textDetailsContainer.children[#textDetailsContainer.children].height,
@ -1463,6 +1577,33 @@ newPublicationInfo = function(file_id)
ratingsContainer.panel.height = textDetailsContainer.height ratingsContainer.panel.height = textDetailsContainer.height
detailsContainer.height = textDetailsContainer.height detailsContainer.height = textDetailsContainer.height
local function addButton(text, width, height, alignRight)
return textDetailsContainer:addChild(GUI.roundedButton(
previewContainer.x + (alignRight and (previewContainer.width - width - 1) or 1),
previewContainer.y + math.floor(previewContainer.height / 2 - height / 2),
width, height,
0xC3C3C3, 0xFFFFFF, 0x2D2D2D, 0xFFFFFF,
text
))
end
local leftButton = addButton("<", 3, 3, false)
leftButton.onTouch = function()
previewContainer:focusItem(previewContainer.focusedItem - 1)
end
local rightButton = addButton(">", 3, 3, true)
rightButton.onTouch = function()
previewContainer:focusItem(previewContainer.focusedItem + 1)
end
previewContainer.onItemFocused = function()
leftButton.hidden = previewContainer.contentOffset == 0
rightButton.hidden = previewContainer.contentOffset + previewContainer.contentWidth < previewContainer.width
end
previewContainer:onItemFocused()
if #reviews > 0 then if #reviews > 0 then
-- Отображаем все оценки -- Отображаем все оценки
layout:addChild(GUI.text(1, 1, 0x696969, localization.reviewsOfUsers)) layout:addChild(GUI.text(1, 1, 0x696969, localization.reviewsOfUsers))
@ -1674,6 +1815,7 @@ editPublication = function(initialPublication, initialCategoryID)
dependencyTypeComboBox:addItem(localization.fileByURL) dependencyTypeComboBox:addItem(localization.fileByURL)
dependencyTypeComboBox:addItem(localization.localizationDependency) dependencyTypeComboBox:addItem(localization.localizationDependency)
dependencyTypeComboBox:addItem(localization.existingPublication) dependencyTypeComboBox:addItem(localization.existingPublication)
dependencyTypeComboBox:addItem(localization.previewDepencency)
dependencyTypeComboBox.selectedItem = lastDependencyType dependencyTypeComboBox.selectedItem = lastDependencyType
local publicationNameInput = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xFFFFFF, 0x696969, 0xB4B4B4, 0xFFFFFF, 0x2D2D2D, "", "MineOS")) local publicationNameInput = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xFFFFFF, 0x696969, 0xB4B4B4, 0xFFFFFF, 0x2D2D2D, "", "MineOS"))
@ -1683,6 +1825,28 @@ editPublication = function(initialPublication, initialCategoryID)
local button = container.layout:addChild(GUI.button(1, 1, 36, 3, 0x696969, 0xFFFFFF, 0x0, 0xFFFFFF, localization.add)) local button = container.layout:addChild(GUI.button(1, 1, 36, 3, 0x696969, 0xFFFFFF, 0x0, 0xFFFFFF, localization.add))
button.onTouch = function() button.onTouch = function()
if lastDependencyType == 4 then
local result, reason = checkImage(
urlInput.text,
true,
function(width, height)
if width > 64 or height ~= 16 then
return false, "Preview image width shold be <= 64 and height should be equal to 16"
end
return true
end
)
if not result then
GUI.alert(reason)
container:remove()
workspace:draw()
return
end
end
addDependency({ addDependency({
publication_name = lastDependencyType == 3 and publicationNameInput.text or nil, publication_name = lastDependencyType == 3 and publicationNameInput.text or nil,
path = path =
@ -1694,8 +1858,13 @@ editPublication = function(initialPublication, initialCategoryID)
( (
"Localizations/" .. filesystem.name(urlInput.text) "Localizations/" .. filesystem.name(urlInput.text)
) or ) or
lastDependencyType == 4 and
(
filesystem.name(urlInput.text)
) or
nil, nil,
source_url = lastDependencyType ~= 3 and urlInput.text or nil, source_url = lastDependencyType ~= 3 and urlInput.text or nil,
preview = lastDependencyType == 4
}) })
container:remove() container:remove()
@ -1707,6 +1876,8 @@ editPublication = function(initialPublication, initialCategoryID)
button.disabled = #pathInput.text == 0 or #urlInput.text == 0 button.disabled = #pathInput.text == 0 or #urlInput.text == 0
elseif lastDependencyType == 2 then elseif lastDependencyType == 2 then
button.disabled = #urlInput.text == 0 or filesystem.extension(urlInput.text) ~= ".lang" button.disabled = #urlInput.text == 0 or filesystem.extension(urlInput.text) ~= ".lang"
elseif lastDependencyType == 4 then
button.disabled = #urlInput.text == 0
else else
button.disabled = #publicationNameInput.text == 0 button.disabled = #publicationNameInput.text == 0
end end
@ -1714,7 +1885,11 @@ editPublication = function(initialPublication, initialCategoryID)
pathInput.onInputFinished, urlInput.onInputFinished = publicationNameInput.onInputFinished, publicationNameInput.onInputFinished pathInput.onInputFinished, urlInput.onInputFinished = publicationNameInput.onInputFinished, publicationNameInput.onInputFinished
local function updatePlaceholder() local function updatePlaceholder()
urlInput.placeholderText = lastDependencyType == 1 and "http://example.com/Main.lua" or "http://example.com/English.lang" urlInput.placeholderText =
lastDependencyType == 1 and "http://example.com/Main.lua"
or lastDependencyType == 2 and "http://example.com/English.lang"
or "http://example.com/Preview.pic"
pathInput.placeholderText = pathType.switch.state and "Resources/Main.lua" or "Users/Scripts/Main.lua" pathInput.placeholderText = pathType.switch.state and "Resources/Main.lua" or "Users/Scripts/Main.lua"
end end
@ -1835,7 +2010,10 @@ updateFileList = function(category_id, updates)
file_ids = file_ids, file_ids = file_ids,
}) })
if result then if not result then
return
end
contentContainer:removeChildren() contentContainer:removeChildren()
if updates then if updates then
@ -1977,6 +2155,7 @@ updateFileList = function(category_id, updates)
local xStart = math.floor(1 + contentContainer.width / 2 - (appsPerWidth * (appWidth + appHSpacing) - appHSpacing) / 2) local xStart = math.floor(1 + contentContainer.width / 2 - (appsPerWidth * (appWidth + appHSpacing) - appHSpacing) / 2)
local x, counter = xStart, 1 local x, counter = xStart, 1
for i = 1, #result do for i = 1, #result do
contentContainer:addChild(newApplicationPreview(x, y, result[i])) contentContainer:addChild(newApplicationPreview(x, y, result[i]))
@ -1994,7 +2173,6 @@ updateFileList = function(category_id, updates)
else else
showLabelAsContent(contentContainer, localization.noUpdates) showLabelAsContent(contentContainer, localization.noUpdates)
end end
end
end end
local function loadCategory(...) local function loadCategory(...)