override suspend fun collect()

in intellij-plugin/educational-core/src/com/jetbrains/edu/learning/update/SectionUpdater.kt [16:58]


  override suspend fun collect(localItems: List<Section>, remoteItems: List<Section>): List<SectionUpdate> {
    val updates = mutableListOf<SectionUpdate>()

    val localSections = localItems.toMutableSet()
    val remoteSections = remoteItems.toMutableSet()

    while (localSections.isNotEmpty() || remoteSections.isNotEmpty()) {
      if (localSections.isEmpty()) {
        // new sections
        remoteSections.forEach { remoteSection ->
          updates.add(SectionCreationInfo(course, remoteSection))
        }
        remoteSections.clear()
      }
      if (remoteSections.isEmpty()) {
        // sections to be deleted
        localSections.forEach { localSection ->
          updates.add(SectionDeletionInfo(localSection))
        }
        localSections.clear()
      }

      // sections to be updated
      val localSection = localSections.firstOrNull() ?: continue
      val remoteSection = remoteSections.find { it.id == localSection.id }
      if (remoteSection == null) {
        updates.add(SectionDeletionInfo(localSection))
        localSections.remove(localSection)
      }
      else {
        val lessonUpdater = createLessonUpdater(localSection)
        val lessonUpdates = lessonUpdater.collect(remoteSection)
        if (lessonUpdates.isNotEmpty() || localSection.isOutdated(remoteSection) || localSection.isChanged(remoteSection)) {
          updates.add(SectionUpdateInfo(localSection, remoteSection, lessonUpdates))
        }

        localSections.remove(localSection)
        remoteSections.remove(remoteSection)
      }
    }

    return updates
  }