Move history menu implementation to its own class - crossword.koplugin - Unnamed repository; edit this file 'description' to name the repository.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit ad7659feb2cb4830a2805b80aee7a56b349b2f81
 (DIR) parent ce94ccfcd13c745ed6ca62ab6021174c8964a925
 (HTM) Author: Scarlett <social@scarlettmcallister.com>
       Date:   Mon, 14 Feb 2022 17:23:13 -0400
       
       Move history menu implementation to its own class
       
       Diffstat:
         M history.lua                         |       5 +++++
         A historyview.lua                     |      71 +++++++++++++++++++++++++++++++
         M main.lua                            |      29 +++++++++++++++--------------
       
       3 files changed, 91 insertions(+), 14 deletions(-)
       ---
 (DIR) diff --git a/history.lua b/history.lua
       @@ -68,4 +68,9 @@ function History:save()
        
        end
        
       +function History:clear()
       +   self.lua_settings:saveSetting(History.STACK, {})
       +   self.lua_settings:flush()
       +end
       +
        return History
 (DIR) diff --git a/historyview.lua b/historyview.lua
       @@ -0,0 +1,71 @@
       +local _ = require("gettext")
       +
       +local History = require("history")
       +
       +-- Maybe we should extend the history module?
       +
       +local HistoryView = {
       +   
       +}
       +
       +function HistoryView:new(o)
       +   o = o or {}
       +   setmetatable(o, self)
       +   self.__index = self   
       +   return o
       +end
       +
       +function HistoryView:getContinueButton(load_puzzle_cb)
       +   local history = History:new{}
       +   if #history:get() > 0 then
       +      local history_item = history:get()[1]
       +      return {
       +         text = _(("Continue \"%s\""):format(history_item['puzzle_title'])),
       +         callback = function()
       +            load_puzzle_cb(history_item)
       +         end
       +      }
       +   else
       +      return nil
       +   end
       +end
       +
       +--[[--
       +Return a list that can be used to populate a plugin menu.
       +]]
       +function HistoryView:getMenuItems(load_puzzle_cb, clear_history_cb)
       +   local menu_items = {}
       +   local sub_menu_items = {}
       +   -- If the user has started a puzzle, we'll add a new option to the menu.   
       +   local history = History:new{}   
       +   if #history:get() > 0 then
       +      local history_list = {}
       +      for i, item in ipairs(history:get()) do
       +         table.insert(sub_menu_items, {
       +               text = item['puzzle_title'],
       +               callback = function()
       +                  load_puzzle_cb(item)
       +               end
       +         })
       +      end
       +      -- Add a clear history button
       +      table.insert(sub_menu_items,
       +         {
       +            text = _("Clear history"),
       +            keep_menu_open = false,
       +            callback = function()
       +               history:clear()
       +            end
       +         }
       +      )
       +      table.insert(menu_items, {
       +            text = _("History"),
       +            sub_item_table = sub_menu_items
       +      })
       +      return menu_items
       +   else
       +      return nil
       +   end
       +end
       +
       +return HistoryView
 (DIR) diff --git a/main.lua b/main.lua
       @@ -21,6 +21,7 @@ local GridView = require("gridview")
        local GridInput = require("gridinput")
        local GameView = require("gameview")
        local History = require("history")
       +local HistoryView = require("historyview")
        local Library = require("library")
        local Puzzle = require("puzzle")
        
       @@ -71,20 +72,20 @@ function Crossword:getSubMenuItems()
                 }
              }
           }
       -   -- If the user has started a puzzle, we'll add a new option to the menu.
       -   local history = History:new{}
       -   if #history:get() > 0 then
       -      local history_item = history:get()[1]
       -      table.insert(sub_menu_items, 1,
       -         {
       -            text = _(("Continue \"%s\""):format(history_item['puzzle_title'])),
       -            callback = function()
       -               local puzzle = Puzzle:loadById(history_item['puzzle_id'])            
       -               self:showGameView(puzzle)
       -            end
       -         }
       -      )
       -   end
       +
       +   local history_view = HistoryView:new{}
       +   local continue_puzzle_item = history_view:getContinueButton(function(history_item)
       +         local puzzle = Puzzle:loadById(history_item['puzzle_id'])            
       +         self:showGameView(puzzle)
       +   end)
       +   local history_menu_items = history_view:getMenuItems(function(history_item)
       +         local puzzle = Puzzle:loadById(history_item['puzzle_id'])            
       +         self:showGameView(puzzle)
       +   end)
       +
       +   if continue_puzzle_item then table.insert(sub_menu_items, 1, continue_puzzle_item) end
       +   if history_menu_items then table.insert(sub_menu_items, history_menu_items[1]) end
       +   
           return sub_menu_items
        end