-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSort.lua
58 lines (46 loc) · 1.47 KB
/
Sort.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
local _, ADDON = ...
local cache = {}
local setTimer = false
local function CacheToy(itemId)
if cache[itemId] then
return cache[itemId][1], cache[itemId][2]
end
if not setTimer then
C_Timer.After(0.1, function()
cache = {}
setTimer = false
end)
setTimer = true
end
local _, name = C_ToyBox.GetToyInfo(itemId)
local isFavorite = ADDON.Api:GetIsFavorite(itemId)
cache[itemId] = { name, isFavorite}
return name, isFavorite
end
function ADDON:SortHandler(itemA, itemB)
if itemA == itemB then
return false
end
local result = false
local nameA, isFavoriteA = CacheToy(itemA)
local nameB, isFavoriteB = CacheToy(itemB)
if ADDON.settings.sort.favoritesFirst and isFavoriteA ~= isFavoriteB then
return isFavoriteA and not isFavoriteB
end
if ADDON.settings.sort.unownedAtLast then
local isCollectedA = isFavoriteA or PlayerHasToy(itemA)
local isCollectedB = isFavoriteB or PlayerHasToy(itemB)
if isCollectedA ~= isCollectedB then
return isCollectedA and not isCollectedB
end
end
if ADDON.settings.sort.by == 'name' then
result = (nameA or '') < (nameB or '') -- warning: names can be nil on uninitialised toys
elseif ADDON.settings.sort.by == 'expansion' then
result = itemA < itemB
end
if ADDON.settings.sort.descending then
result = not result
end
return result
end