From ffde1b2978680315402d4cb596deefa3a90ed2f3 Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Wed, 9 Oct 2024 03:57:16 +0200 Subject: [PATCH] Abstract page state --- itemcontroller.lua | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/itemcontroller.lua b/itemcontroller.lua index b5af9fc..b982015 100644 --- a/itemcontroller.lua +++ b/itemcontroller.lua @@ -232,17 +232,21 @@ end local PAGES = { MAIN = function(state) - local found = ItemGroup.collectStacks(state.controller:find(function(stack) - return not stack:isEmpty() - end)) + local pageState = state:currentPageState({}) - table.sort(found, function(a, b) return a:getItemCount() > b:getItemCount() end) + if pageState.stacks == nil then + pageState.stacks = ItemGroup.collectStacks(state.controller:find(function(stack) + return not stack:isEmpty() + end)) + + table.sort(pageState.stacks, function(a, b) return a:getItemCount() > b:getItemCount() end) + end - local listResult, pageState = itemList( - found, + local listResult, listState = itemList( + pageState.stacks, state.width, state.height, - state.pageState[state.currentPage], + pageState.listState, function(element, x, y, source, group) print("Clicked: "..group:getSimpleName()) return true @@ -261,10 +265,12 @@ local PAGES = { end end end) + -- Force reload stacks + state:currentPageState({}).stacks = nil state.nextPage = state.currentPage end ) - state.pageState[state.currentPage] = pageState + pageState.listState = listState listResult:setParent(state.monitor) return function() @@ -306,6 +312,11 @@ function CONTROLLER_STATE:itemTransaction(transact) unlock() end +function CONTROLLER_STATE:currentPageState(default) + self.pageState[self.currentPage] = self.pageState[self.currentPage] or default + return self.pageState[self.currentPage] +end + while not CONTROLLER_STATE.exit do updatePageRender(CONTROLLER_STATE, PAGES) CONTROLLER_STATE._pageRender()