Common base class for HighlightEvent, SelectEvent,

I started a refactoring today, where I wanted to eliminate some duplicates.
Here is a part of our chart synchronization code:

    chart.onSelect { event ->
      // some specific code

      // common code
      val selectedSample = event.data.firstOrNull()
      if (selectedSample?.domain is SampleRaw) {
        event.preventDefault = true
        chart.select(selectedSample.domain.s.listOfSampleRaw)
      }
      for (ct in otherCharts) {
        charts[ct]?.also { otherChart ->
          val entries = mutableListOf<ISample>()
          for (s in event.data) {
            val entry = otherChart.dataset.data.firstOrNull {
              it.domain.s.idx == s.domain.s.idx
            }
            if (entry != null) {
              entries.add(entry.domain)
            }
          }
          otherChart.select(entries)
        }
      }
    }

for the chart.onSelectionAdd() the code is almost the same but:

  • the event class is different
  • instead of select() method the addToSelection() method should be called

I cannot extract this code because of the various Event classes. I checked those classes, and it turned out, that they have exactly the same source code.
Would it possible to create a base class (with preventDefault and data/selectedData properties) and the specific classes extend this base class?

I found a difference: in the SelectionRemoveEvent class

  • the selectedData attribute is a Collection, not a list
  • the secondary constructor is commented out

I’ll have a look at this :slight_smile:

Can we have a meeting on this topic?
I would like to show you the code and ask you for tips

Is tomorrow morning OK ? I let you schedule something at your convenience.

Tomorrow morning at 10?

1 Like

Perfect!

We can meet here: meet.google.com/thx-xdhs-wau