From 42c93a7623c6d04a2001f943ddc5b0b0638e3a24 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Sat, 30 Mar 2024 14:04:43 +0100 Subject: [PATCH] FileChooser collates: use "item.sort_percent" for sorting + extra `on-hold` handling (#11592) re https://github.com/koreader/koreader/pull/11524#issuecomment-1984110990 re https://github.com/koreader/koreader/pull/11542#issuecomment-2018164346 re #11592 --- frontend/ui/widget/filechooser.lua | 31 ++++++++++++++++++------------ frontend/util.lua | 8 ++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/frontend/ui/widget/filechooser.lua b/frontend/ui/widget/filechooser.lua index 7bbfd03d4..592f867f5 100644 --- a/frontend/ui/widget/filechooser.lua +++ b/frontend/ui/widget/filechooser.lua @@ -172,7 +172,9 @@ local FileChooser = Menu:extend{ local doc_settings = DocSettings:open(item.path) percent_finished = doc_settings:readSetting("percent_finished") end - item.percent_finished = percent_finished or 0 + + -- smooth 2 decimal points (0.00) instead of 16 decimal points + item.percent_finished = util.round_decimal(percent_finished or 0, 2) end, mandatory_func = function(item) return item.opened and string.format("%d %%", 100 * item.percent_finished) or "–" @@ -200,14 +202,16 @@ local FileChooser = Menu:extend{ local doc_settings = DocSettings:open(item.path) percent_finished = doc_settings:readSetting("percent_finished") end - item.percent_finished = percent_finished or 0 + + -- smooth 2 decimal points (0.00) instead of 16 decimal points + item.percent_finished = util.round_decimal(percent_finished or 0, 2) end, mandatory_func = function(item) return item.opened and string.format("%d %%", 100 * item.percent_finished) or "–" end, }, percent_natural = { - -- sort 90% > 50% > 0% > unopened > 100% or finished + -- sort 90% > 50% > 0% or on hold > unopened > 100% or finished text = _("percent - unopened - finished last"), menu_order = 90, can_collate_mixed = false, @@ -215,14 +219,14 @@ local FileChooser = Menu:extend{ local natsort natsort, cache = sort.natsort_cmp(cache) local sortfunc = function(a, b) - if a.percent_finished == b.percent_finished then + if a.sort_percent == b.sort_percent then return natsort(a.text, b.text) - elseif a.percent_finished == 1 then + elseif a.sort_percent == 1 then return false - elseif b.percent_finished == 1 then + elseif b.sort_percent == 1 then return true else - return a.percent_finished > b.percent_finished + return a.sort_percent > b.sort_percent end end @@ -230,21 +234,24 @@ local FileChooser = Menu:extend{ end, item_func = function(item) local percent_finished + local sort_percent item.opened = DocSettings:hasSidecarFile(item.path) if item.opened then local doc_settings = DocSettings:open(item.path) local summary = doc_settings:readSetting("summary") - -- books marked as "finished" should be considered the same as 100% + -- books marked as "finished" or "on hold" should be considered the same as 100% and 0% respectively if summary and summary.status == "complete" then - item.percent_finished = 1.0 - return + sort_percent = 1.0 + elseif summary and summary.status == "abandoned" then + sort_percent = 0 end percent_finished = doc_settings:readSetting("percent_finished") end - -- smooth 2 decimal points (0.00) instead of 16 decimal numbers - item.percent_finished = math.floor((percent_finished or -1) * 100) / 100 + -- smooth 2 decimal points (0.00) instead of 16 decimal points + item.sort_percent = sort_percent or util.round_decimal(percent_finished or -1, 2) + item.percent_finished = percent_finished or 0 end, mandatory_func = function(item) return item.opened and string.format("%d %%", 100 * item.percent_finished) or "–" diff --git a/frontend/util.lua b/frontend/util.lua index 817b0b740..62c4cfd83 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -1537,4 +1537,12 @@ function util.wrapMethod(target_table, target_field_name, new_func, before_callb return wrapped end +-- Round a given "num" to the decimal points of "points" +-- (i.e. `round_decimal(0.000000001, 2)` will yield `0.00`) +function util.round_decimal(num, points) + local op = 10 ^ points + + return math.floor(num * op) / op +end + return util