@@ -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
131128end
@@ -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 )
149146end
150147
151148function 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
163160end
@@ -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
176173end
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
218213end
219214
@@ -234,18 +229,13 @@ local function sanitize_filename(str)
234229 return str :gsub (" [^%w%.%-%_]" , " _" )
235230end
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+
282270end
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