Skip to content

Commit bfaf711

Browse files
author
Alex Campos
committed
Refactor HighlightSync logic for better performance
1 parent b66ef37 commit bfaf711

2 files changed

Lines changed: 39 additions & 51 deletions

File tree

_meta.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ return {
22
name = "HighlightSync",
33
fullname = "HighlightSync",
44
description = "HighlightSync is a plugin for KOReader that synchronizes and merges your highlights, notes, and bookmarks across multiple devices or cloud backup locations.",
5-
version = "0.7.1", -- ← Add this line!
5+
version = "0.7.2", -- ← Add this line!
66
}

main.lua

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ function Highlightsync:onDispatcherRegisterActions()
122122
event = "SyncBookHighlights",
123123
title = _("Sync Highlights Now"),
124124
help = _("Synchronize highlights with the cloud."),
125-
reader = true,
126-
callback = function()
127-
self:onSyncBookHighlights(false, true)
128-
end
125+
reader = true
129126
})
130127

131128
end
@@ -144,8 +141,8 @@ function Highlightsync:init()
144141
self.is_syncing = false
145142

146143
Highlightsync.settings = G_reader_settings:readSetting("highlight_sync", self.default_settings)
147-
self:onDispatcherRegisterActions(
148-
self.ui.menu:registerToMainMenu(self))
144+
self:onDispatcherRegisterActions()
145+
self.ui.menu:registerToMainMenu(self)
149146
end
150147

151148
function Highlightsync:onReaderReady()
@@ -157,7 +154,7 @@ function Highlightsync:onReaderReady()
157154

158155
if self.settings.sync_on_open and self:canSync() then
159156
UIManager:nextTick(function()
160-
self:onSyncBookHighlights(false, true)
157+
self:SyncBookHighlights(false, true)
161158
end)
162159
end
163160
end
@@ -171,7 +168,7 @@ function Highlightsync:onCloseDocument()
171168

172169
if self.settings.sync_on_close and self:canSync() then
173170
-- Sincroniza e sai (sem reload, pois estamos saindo)
174-
self:onSyncBookHighlights(false, false)
171+
self:SyncBookHighlights(false, false)
175172
end
176173
end
177174

@@ -180,7 +177,7 @@ function Highlightsync:onResume()
180177
if self.settings.sync_on_resume then
181178
UIManager:nextTick(function()
182179
if NetworkMgr:isWifiOn() then
183-
self:onSyncBookHighlights(false, true)
180+
self:SyncBookHighlights(false, true)
184181
self.settings.pending_sync = false
185182
G_reader_settings:saveSetting("highlight_sync", self.settings)
186183
end
@@ -202,18 +199,16 @@ function Highlightsync:onSync(local_path, cached_path, income_path, reload)
202199
write_json_file(SidecarDir .. "/" .. FileName .. ".json", annotations) -- Save annotations local
203200
DataAnnotations = annotations
204201

205-
206-
UIManager:nextTick(function()
207-
if self.ui and self.ui.annotation then
208-
self.ui.annotation.annotations = DataAnnotations
209-
if reload then
210-
is_reloading_due_to_sync = true
211-
self.ui:reloadDocument()
212-
end
213-
end
214-
end)
215-
216-
202+
if self.ui and self.ui.annotation then
203+
self.ui.annotation.annotations = DataAnnotations
204+
if reload then
205+
is_reloading_due_to_sync = true
206+
UIManager:tickAfterNext(function()
207+
self.ui:reloadDocument()
208+
end)
209+
end
210+
end
211+
217212
return true
218213
end
219214

@@ -234,18 +229,13 @@ local function sanitize_filename(str)
234229
return str:gsub("[^%w%.%-%_]", "_")
235230
end
236231

232+
function Highlightsync:onSyncBookHighlights()
233+
self:SyncBookHighlights(false, true)
234+
end
237235

238-
239-
function Highlightsync:onSyncBookHighlights(silent, reload)
236+
function Highlightsync:SyncBookHighlights(silent, reload)
240237
if not self:canSync() then return end
241238

242-
if not silent then
243-
UIManager:show(InfoMessage:new {
244-
text = _("Syncing book highlights. This may take a while."),
245-
timeout = 1,
246-
})
247-
end
248-
249239
if self.is_syncing then
250240
logger.warn("Highlightsync: Duplicate sync attempt ignored.")
251241
return
@@ -254,31 +244,29 @@ function Highlightsync:onSyncBookHighlights(silent, reload)
254244
-- enable lock
255245
self.is_syncing = true
256246

257-
UIManager:nextTick(function()
247+
local doc_path = self.document and self.document.file
248+
local doc_settings = self.ui and self.ui.doc_settings
249+
SidecarDir = doc_settings:getSidecarDir(doc_path)
250+
ensure_dir_exists(SidecarDir)
251+
DataAnnotations = self.ui.annotation.annotations -- self.ui.doc_settings.data.annotations
258252

259-
local doc_path = self.document and self.document.file
260-
local doc_settings = self.ui and self.ui.doc_settings
261-
SidecarDir = doc_settings:getSidecarDir(doc_path)
262-
ensure_dir_exists(SidecarDir)
263-
DataAnnotations = self.ui.annotation.annotations -- self.ui.doc_settings.data.annotations
253+
Raw_name = SidecarDir:match("([^/]+)/*$")
254+
FileName = sanitize_filename(Raw_name)
264255

265-
Raw_name = SidecarDir:match("([^/]+)/*$")
266-
FileName = sanitize_filename(Raw_name)
256+
write_json_file(SidecarDir .. "/" .. FileName .. ".json", self.ui.annotation.annotations) -- Save annotations local
267257

268-
write_json_file(SidecarDir .. "/" .. FileName .. ".json", self.ui.annotation.annotations) -- Save annotations local
269-
270-
SyncService.sync(self.settings.sync_server, SidecarDir .. "/" .. FileName .. ".json",
271-
function(local_path, cached_path, income_path)
272-
local success = self:onSync(local_path, cached_path, income_path, reload)
273-
self.is_syncing = false
274-
return success
275-
end,
276-
silent
277-
)
258+
SyncService.sync(self.settings.sync_server, SidecarDir .. "/" .. FileName .. ".json",
259+
function(local_path, cached_path, income_path)
260+
local success = self:onSync(local_path, cached_path, income_path, reload)
261+
self.is_syncing = false
262+
return success
263+
end,
264+
silent
265+
)
278266

279267

280268

281-
end)
269+
282270
end
283271

284272

@@ -353,7 +341,7 @@ function Highlightsync:addToMainMenu(menu_items)
353341
{
354342
text = _("Sync Highlights"),
355343
callback = function()
356-
self:onSyncBookHighlights(false, true)
344+
self:SyncBookHighlights(false, true)
357345
end,
358346
enabled_func = function() return self.canSync(self) end
359347
},

0 commit comments

Comments
 (0)