This is a lot of code for someone who’s unfamiliar with your project to try to parse through — I think you’ll have more success getting help with your problem if you can make a smaller, runnable-by-anyone example that demonstrates just the core thing you are trying to figure out how to do.
For some more advice about asking shiny questions, see: