-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFavorites.lua
More file actions
135 lines (117 loc) · 4.6 KB
/
Favorites.lua
File metadata and controls
135 lines (117 loc) · 4.6 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
local _, ADDON = ...
local function collectFavoredByApi()
local c = 0
local mountIds = C_MountJournal.GetMountIDs()
local favoredMounts = {}
for _, mountId in ipairs(mountIds) do
local _, _, _, _, _, _, isFavorite = C_MountJournal.GetMountInfoByID(mountId)
if isFavorite then
c = c + 1
favoredMounts[c] = mountId
end
end
return favoredMounts
end
local backgroundTimer
local function cancelBackgroundTask()
if backgroundTimer then
backgroundTimer:Cancel()
end
ADDON.Events:UnregisterFrameEventAndCallback("MOUNT_JOURNAL_SEARCH_UPDATED", 'cleanup-favorite-background')
end
local function UpdateFavoritesInBackground()
cancelBackgroundTask()
local _, _ , favorites = ADDON.Api:GetFavoriteProfile()
favorites = tFilter(favorites, function(mountId)
local _, _, _, _, _, _, _, _, _, shouldHideOnChar = C_MountJournal.GetMountInfoByID(mountId)
return not shouldHideOnChar
end, true)
local flippedFavorites = {}
for _, v in pairs(favorites) do -- CopyValuesAsKeys() somehow created faulty list
flippedFavorites[v] = true
end
local operationCount = #favorites
favorites = nil
local favoredByApi = collectFavoredByApi()
for _, mountId in ipairs(favoredByApi) do
if flippedFavorites[mountId] then
flippedFavorites[mountId] = nil
operationCount = operationCount - 1
else
flippedFavorites[mountId] = false
operationCount = operationCount + 1
end
end
if operationCount > 0 then
local lastTickMount, tickTryMount = 0, 0
local tickHandler = function()
local mountId, shouldBeFavored = next(flippedFavorites)
if mountId then
if lastTickMount == mountId and tickTryMount >= 3 then
flippedFavorites[mountId] = nil
else
if lastTickMount == mountId then
tickTryMount = tickTryMount + 1
else
lastTickMount = mountId
tickTryMount = 1
end
local index = ADDON.Api:MountIdToOriginalIndex(mountId) -- costly call
if index then
local isFavorite = C_MountJournal.GetIsFavorite(index)
if isFavorite == shouldBeFavored then
flippedFavorites[mountId] = nil
else
C_MountJournal.SetIsFavorite(index, shouldBeFavored)
end
end
end
end
if TableIsEmpty(flippedFavorites) then
cancelBackgroundTask()
end
end
ADDON.Events:RegisterFrameEventAndCallback("MOUNT_JOURNAL_SEARCH_UPDATED", function()
local mountId, shouldBeFavored = next(flippedFavorites)
if mountId then
local _, _, _, _, _, _, isFavorite = C_MountJournal.GetMountInfoByID(mountId)
if isFavorite == shouldBeFavored then
flippedFavorites[mountId] = nil
end
end
if TableIsEmpty(flippedFavorites) then
cancelBackgroundTask()
end
end, 'cleanup-favorite-background')
backgroundTimer = C_Timer.NewTicker(0.4, tickHandler)
end
end
ADDON.Events:RegisterCallback("OnNewMount", function(_, mountId)
local currentProfile = ADDON.Api:GetFavoriteProfile()
local tInsert = table.insert
for index, profileData in pairs(ADDON.settings.favorites.profiles) do
if profileData and profileData.autoFavor then
if index == currentProfile then
ADDON.Api:SetIsFavoriteByID(mountId, true)
elseif not tContains(profileData.mounts, mountId) then
tInsert(profileData.mounts, mountId)
end
end
end
end, "autoFavor")
ADDON.Events:RegisterCallback("OnLogin", function()
local profileIndex, _ , favorites = ADDON.Api:GetFavoriteProfile()
-- initial scan for account profile
if 1 == profileIndex and not ADDON.settings.favorites.profiles[1].initialScan then
ADDON.settings.favorites.profiles[1].initialScan = true
if 0 == #favorites then
ADDON.Api:SetBulkIsFavorites(collectFavoredByApi())
end
else
UpdateFavoritesInBackground()
end
end, "favorite hooks")
ADDON.Events:RegisterCallback("OnFavoritesChanged", function()
ADDON.Api:GetDataProvider():Sort()
UpdateFavoritesInBackground()
end, "sort dataprovider")