2016-11-18 17:09:30 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local InputDialog = require("ui/widget/inputdialog")
|
|
|
|
local DoubleKeyValuePage = require("doublekeyvaluepage")
|
|
|
|
local MultiInputDialog = require("ui/widget/multiinputdialog")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local Screen = require("device").screen
|
|
|
|
local _ = require("gettext")
|
2017-02-27 17:29:52 +00:00
|
|
|
local NetworkMgr = require("ui/network/manager")
|
2016-11-18 17:09:30 +00:00
|
|
|
|
2016-11-19 11:36:39 +00:00
|
|
|
local Goodreads = InputContainer:new {
|
2018-08-17 18:54:11 +00:00
|
|
|
name = "goodreads",
|
2016-11-19 11:36:39 +00:00
|
|
|
goodreads_key = "",
|
|
|
|
goodreads_secret = "",
|
2016-11-18 17:09:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 11:36:39 +00:00
|
|
|
function Goodreads:init()
|
2016-11-18 17:09:30 +00:00
|
|
|
local gr_sett = DoubleKeyValuePage:readGRSettings().data
|
|
|
|
if gr_sett.goodreads then
|
2016-11-19 11:36:39 +00:00
|
|
|
self.goodreads_key = gr_sett.goodreads.key
|
|
|
|
self.goodreads_secret = gr_sett.goodreads.secret
|
2016-11-18 17:09:30 +00:00
|
|
|
end
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end
|
|
|
|
|
2017-03-04 13:46:38 +00:00
|
|
|
function Goodreads:addToMainMenu(menu_items)
|
|
|
|
menu_items.goodreads = {
|
2016-11-19 11:36:39 +00:00
|
|
|
text = _("Goodreads"),
|
2016-11-18 17:09:30 +00:00
|
|
|
sub_item_table = {
|
|
|
|
{
|
|
|
|
text = _("Settings"),
|
2018-09-04 21:55:58 +00:00
|
|
|
keep_menu_open = true,
|
2016-11-18 17:09:30 +00:00
|
|
|
callback = function() self:updateSettings() end,
|
|
|
|
},
|
|
|
|
{
|
2016-11-19 11:36:39 +00:00
|
|
|
text = _("Search all books"),
|
2018-09-04 21:55:58 +00:00
|
|
|
keep_menu_open = true,
|
|
|
|
callback = function(touchmenu_instance)
|
2016-11-19 11:36:39 +00:00
|
|
|
if self.goodreads_key ~= "" then
|
2018-09-04 21:55:58 +00:00
|
|
|
touchmenu_instance:closeMenu()
|
2016-11-18 17:09:30 +00:00
|
|
|
self:search("all")
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2016-11-19 11:36:39 +00:00
|
|
|
text = _("Please set up your Goodreads key in the settings dialog"),
|
2016-11-18 17:09:30 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
2016-11-19 11:36:39 +00:00
|
|
|
text = _("Search for book by title"),
|
2018-09-04 21:55:58 +00:00
|
|
|
keep_menu_open = true,
|
|
|
|
callback = function(touchmenu_instance)
|
2016-11-19 11:36:39 +00:00
|
|
|
if self.goodreads_key ~= "" then
|
2018-09-04 21:55:58 +00:00
|
|
|
touchmenu_instance:closeMenu()
|
2016-11-18 17:09:30 +00:00
|
|
|
self:search("title")
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2016-11-19 11:36:39 +00:00
|
|
|
text = _("Please set up your Goodreads key in the settings dialog"),
|
2016-11-18 17:09:30 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
2016-11-19 11:36:39 +00:00
|
|
|
text = _("Search for book by author"),
|
2018-09-04 21:55:58 +00:00
|
|
|
keep_menu_open = true,
|
|
|
|
callback = function(touchmenu_instance)
|
2016-11-19 11:36:39 +00:00
|
|
|
if self.goodreads_key ~= "" then
|
2018-09-04 21:55:58 +00:00
|
|
|
touchmenu_instance:closeMenu()
|
2016-11-18 17:09:30 +00:00
|
|
|
self:search("author")
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2016-11-19 11:36:39 +00:00
|
|
|
text = _("Please set up your Goodreads key in the settings dialog"),
|
2016-11-18 17:09:30 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
2017-02-28 21:46:32 +00:00
|
|
|
}
|
2016-11-18 17:09:30 +00:00
|
|
|
end
|
|
|
|
|
2016-11-19 11:36:39 +00:00
|
|
|
function Goodreads:updateSettings()
|
2016-11-18 17:09:30 +00:00
|
|
|
local hint_top
|
|
|
|
local text_top
|
|
|
|
local hint_bottom
|
|
|
|
local text_bottom
|
2019-08-22 15:11:47 +00:00
|
|
|
local text_info = _([[
|
|
|
|
How to generate a key and a secret key:
|
2018-03-14 14:50:35 +00:00
|
|
|
|
|
|
|
1. Go to https://www.goodreads.com/user/sign_up and create an account
|
|
|
|
2. Register for a development key on the following page: https://www.goodreads.com/user/sign_in?rd=true
|
|
|
|
3. Your key and secret key will now be available on https://www.goodreads.com/api/key
|
|
|
|
4. Enter your generated key and your secret key in the settings dialog (Login to Goodreads window)
|
|
|
|
]])
|
|
|
|
|
2016-11-19 11:36:39 +00:00
|
|
|
if self.goodreads_key == "" then
|
2016-12-16 13:45:48 +00:00
|
|
|
hint_top = _("Goodreads key left empty")
|
2016-11-18 17:09:30 +00:00
|
|
|
text_top = ""
|
|
|
|
else
|
|
|
|
hint_top = ""
|
2016-11-19 11:36:39 +00:00
|
|
|
text_top = self.goodreads_key
|
2016-11-18 17:09:30 +00:00
|
|
|
end
|
|
|
|
|
2016-11-19 11:36:39 +00:00
|
|
|
if self.goodreads_secret == "" then
|
2016-12-16 13:45:48 +00:00
|
|
|
hint_bottom = _("Goodreads secret left empty (optional)")
|
2016-11-18 17:09:30 +00:00
|
|
|
text_bottom = ""
|
|
|
|
else
|
|
|
|
hint_bottom = ""
|
2016-11-19 11:36:39 +00:00
|
|
|
text_bottom = self.goodreads_key
|
2016-11-18 17:09:30 +00:00
|
|
|
end
|
|
|
|
self.settings_dialog = MultiInputDialog:new {
|
2016-11-19 11:36:39 +00:00
|
|
|
title = _("Login to Goodreads"),
|
2016-11-18 17:09:30 +00:00
|
|
|
fields = {
|
|
|
|
{
|
|
|
|
text = text_top,
|
|
|
|
input_type = "string",
|
|
|
|
hint = hint_top ,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = text_bottom,
|
|
|
|
input_type = "string",
|
|
|
|
hint = hint_bottom,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
self.settings_dialog:onClose()
|
|
|
|
UIManager:close(self.settings_dialog)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Info"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(InfoMessage:new{text = text_info })
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Apply"),
|
|
|
|
callback = function()
|
|
|
|
self:saveSettings(MultiInputDialog:getFields())
|
|
|
|
self.settings_dialog:onClose()
|
|
|
|
UIManager:close(self.settings_dialog)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
width = Screen:getWidth() * 0.95,
|
|
|
|
height = Screen:getHeight() * 0.2,
|
|
|
|
input_type = "text",
|
|
|
|
}
|
|
|
|
UIManager:show(self.settings_dialog)
|
2018-03-30 10:46:36 +00:00
|
|
|
self.settings_dialog:onShowKeyboard()
|
2016-11-18 17:09:30 +00:00
|
|
|
end
|
|
|
|
|
2016-11-19 11:36:39 +00:00
|
|
|
function Goodreads:saveSettings(fields)
|
2016-11-18 17:09:30 +00:00
|
|
|
if fields then
|
2016-11-19 11:36:39 +00:00
|
|
|
self.goodreads_key = fields[1]
|
|
|
|
self.goodreads_secret = fields[2]
|
2016-11-18 17:09:30 +00:00
|
|
|
end
|
|
|
|
local settings = {
|
2016-11-19 11:36:39 +00:00
|
|
|
key = self.goodreads_key,
|
|
|
|
secret = self.goodreads_secret,
|
2016-11-18 17:09:30 +00:00
|
|
|
}
|
|
|
|
DoubleKeyValuePage:saveGRSettings(settings)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- search_type = all - search all
|
|
|
|
-- search_type = author - serch book by author
|
|
|
|
-- search_type = title - search book by title
|
2016-11-19 11:36:39 +00:00
|
|
|
function Goodreads:search(search_type)
|
2016-11-18 17:09:30 +00:00
|
|
|
local title_header
|
|
|
|
local hint
|
|
|
|
local search_input
|
|
|
|
local text_input
|
|
|
|
local info
|
|
|
|
local result
|
2017-02-27 17:29:52 +00:00
|
|
|
if not NetworkMgr:isOnline() then
|
|
|
|
NetworkMgr:promptWifiOn()
|
|
|
|
return
|
|
|
|
end
|
2016-11-18 17:09:30 +00:00
|
|
|
if search_type == "all" then
|
2016-11-19 11:36:39 +00:00
|
|
|
title_header = _("Search all books in Goodreads")
|
2016-11-18 17:09:30 +00:00
|
|
|
hint = _("Title, author or ISBN")
|
|
|
|
elseif search_type == "author" then
|
2016-11-19 11:36:39 +00:00
|
|
|
title_header = _("Search for book by author in Goodreads")
|
2016-11-18 17:09:30 +00:00
|
|
|
hint = _("Author")
|
|
|
|
elseif search_type == "title" then
|
2016-11-19 11:36:39 +00:00
|
|
|
title_header = _("Search for book by title in Goodreads")
|
2016-11-18 17:09:30 +00:00
|
|
|
hint = _("Title")
|
|
|
|
end
|
|
|
|
|
|
|
|
search_input = InputDialog:new{
|
|
|
|
title = title_header,
|
|
|
|
input = "",
|
|
|
|
input_hint = hint,
|
|
|
|
input_type = "string",
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(search_input)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Find"),
|
|
|
|
is_enter_default = true,
|
|
|
|
callback = function()
|
|
|
|
text_input = search_input:getInputText()
|
|
|
|
if text_input ~= nil and text_input ~= "" then
|
2017-10-14 15:55:26 +00:00
|
|
|
info = InfoMessage:new{text = _("Please wait…")}
|
2016-11-18 17:09:30 +00:00
|
|
|
UIManager:close(search_input)
|
2017-10-14 15:55:26 +00:00
|
|
|
UIManager:show(info)
|
|
|
|
UIManager:forceRePaint()
|
|
|
|
result = DoubleKeyValuePage:new{
|
|
|
|
title = _("Select book"),
|
|
|
|
text_input = text_input,
|
|
|
|
search_type = search_type,
|
|
|
|
}
|
|
|
|
if #result.kv_pairs > 0 then
|
|
|
|
UIManager:show(result)
|
|
|
|
end
|
|
|
|
UIManager:close(info)
|
|
|
|
|
2016-11-18 17:09:30 +00:00
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2016-11-19 11:36:39 +00:00
|
|
|
text =_("Please enter text"),
|
2016-11-18 17:09:30 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
UIManager:show(search_input)
|
2018-03-30 10:46:36 +00:00
|
|
|
search_input:onShowKeyboard()
|
2016-11-18 17:09:30 +00:00
|
|
|
end
|
|
|
|
|
2016-11-19 11:36:39 +00:00
|
|
|
return Goodreads
|