Skip to content

Commit f4bea2c

Browse files
committed
refactor
move config
1 parent b90300f commit f4bea2c

27 files changed

Lines changed: 564 additions & 474 deletions

anki/ankiconnect.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--[[
2-
Copyright: Ren Tatsumoto and contributors
2+
Copyright: Ajatt-Tools and contributors; https://github.com/Ajatt-Tools
33
License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html
44
55
AnkiConnect requests
@@ -8,6 +8,7 @@ AnkiConnect requests
88
local utils = require('mp.utils')
99
local msg = require('mp.msg')
1010
local h = require('helpers')
11+
local platform = require('platform.init')
1112
local self = {}
1213

1314
self.execute = function(request, completion_fn)
@@ -22,7 +23,7 @@ self.execute = function(request, completion_fn)
2223
if error ~= nil or request_json == "null" then
2324
return completion_fn and completion_fn()
2425
else
25-
return self.platform.curl_request(self.config.ankiconnect_url, request_json, completion_fn)
26+
return platform.curl_request(self.config.ankiconnect_url, request_json, completion_fn)
2627
end
2728
end
2829

@@ -255,12 +256,9 @@ self.append_media = function(note_id, fields, tag, on_finish_fn)
255256
self.execute(args, on_finish_wrap)
256257
end
257258

258-
self.init = function(config, platform)
259-
self.config = config
260-
self.platform = platform
261-
if not self.config.init_done then
262-
error("config not loaded")
263-
end
259+
self.init = function(cfg_mgr)
260+
cfg_mgr.fail_if_not_ready()
261+
self.config = cfg_mgr.config()
264262
end
265263

266264
return self

anki/new_note_checker.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ local function make_anki_new_note_checker()
4747
end
4848
end
4949

50+
local function has_no_media(note_fields)
51+
-- Mpvacious will try to update every new note, including the ones added by Mpvacious itself.
52+
-- To avoid updating notes added by mpvacious, check if the note already has media.
53+
return h.is_empty(note_fields[self.config.audio_field]) and h.is_empty(note_fields[self.config.image_field])
54+
end
55+
5056
local function check_for_new_notes()
5157
local note_ids = find_notes_added_today()
5258
if h.is_empty(note_ids) then
@@ -59,7 +65,7 @@ local function make_anki_new_note_checker()
5965
-- Get note info to check if it matches the user's config
6066
local note_fields = self.ankiconnect.get_note_fields(note_id)
6167
-- Check if the note has the configured sentence field.
62-
if not h.is_empty(note_fields) and note_fields[self.config.sentence_field] ~= nil and is_note_recent(note_id) then
68+
if not h.is_empty(note_fields) and note_fields[self.config.sentence_field] ~= nil and is_note_recent(note_id) and has_no_media(note_fields) then
6369
-- Note matches our criteria, update it (just like pressing Ctrl+M does).
6470
table.insert(to_update, note_id)
6571
end
@@ -98,13 +104,11 @@ local function make_anki_new_note_checker()
98104
msg.info("new note checker stopped.")
99105
end
100106

101-
local function init(ankiconnect, update_notes_fn, config)
107+
local function init(ankiconnect, update_notes_fn, cfg_mgr)
108+
cfg_mgr.fail_if_not_ready()
102109
self.ankiconnect = ankiconnect
103110
self.update_notes_fn = update_notes_fn
104-
self.config = config
105-
if not self.config.init_done then
106-
error("config not loaded")
107-
end
111+
self.config = cfg_mgr.config()
108112
end
109113

110114
return {

anki/note_exporter.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,14 @@ local function make_exporter()
192192
return string.format("%s%s%s", old_text, separator, new_text)
193193
end
194194

195+
local function fail_if_not_ready()
196+
if h.is_empty(self.config) then
197+
error("config not assigned")
198+
end
199+
end
200+
195201
local function join_fields(new_data, stored_data)
202+
fail_if_not_ready()
196203
for _, field in pairs { self.config.audio_field, self.config.image_field, self.config.miscinfo_field, self.config.sentence_field, self.config.secondary_field } do
197204
if not h.is_empty(field) then
198205
new_data[field] = join_field_content(h.table_get(new_data, field, ""), h.table_get(stored_data, field, ""))
@@ -348,16 +355,14 @@ local function make_exporter()
348355
end
349356

350357
local function init(ankiconnect, quick_creation_opts, subs_observer, encoder, forvo, cfg_mgr)
351-
self.config = cfg_mgr.get_config()
358+
cfg_mgr.fail_if_not_ready()
359+
self.config = cfg_mgr.config()
352360
self.cfg_mgr = cfg_mgr
353361
self.ankiconnect = ankiconnect
354362
self.quick_creation_opts = quick_creation_opts
355363
self.subs_observer = subs_observer
356364
self.encoder = encoder
357365
self.forvo = forvo
358-
if not self.config.init_done then
359-
error("config not loaded")
360-
end
361366
end
362367

363368
return {

cfg_mgr.lua

Lines changed: 0 additions & 249 deletions
This file was deleted.

0 commit comments

Comments
 (0)