-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Account for the max sql number when migrating the bookmarks.
- Loading branch information
1 parent
8c8e56d
commit d399946
Showing
4 changed files
with
65 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
scanner/src/main/kotlin/voice/app/scanner/BookmarkMigrator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package voice.app.scanner | ||
|
||
import voice.common.BookId | ||
import voice.data.Bookmark | ||
import voice.data.Chapter | ||
import voice.data.legacy.LegacyBookMetaData | ||
import voice.data.repo.internals.dao.BookmarkDao | ||
import voice.data.repo.internals.dao.LegacyBookDao | ||
import voice.data.runForMaxSqlVariableNumber | ||
import voice.data.toUri | ||
import javax.inject.Inject | ||
|
||
class BookmarkMigrator | ||
@Inject constructor( | ||
private val legacyBookDao: LegacyBookDao, | ||
private val bookmarkDao: BookmarkDao, | ||
) { | ||
|
||
suspend fun migrate(migrationMetaData: LegacyBookMetaData, chapters: List<Chapter>, id: BookId) { | ||
legacyBookDao.chapters() | ||
.filter { | ||
it.bookId == migrationMetaData.id | ||
} | ||
.map { it.file } | ||
.runForMaxSqlVariableNumber { legacyBookDao.bookmarksByFiles(it) } | ||
.forEach { legacyBookmark -> | ||
val legacyChapter = legacyBookDao.chapters() | ||
.filter { | ||
it.bookId == migrationMetaData.id | ||
} | ||
.find { it.file == legacyBookmark.mediaFile } | ||
|
||
if (legacyChapter != null) { | ||
val matchingChapter = chapters.find { | ||
val chapterFilePath = it.id.toUri().filePath() ?: return@find false | ||
legacyChapter.file.absolutePath.endsWith(chapterFilePath) | ||
} | ||
if (matchingChapter != null) { | ||
bookmarkDao.addBookmark( | ||
Bookmark( | ||
bookId = id, | ||
addedAt = legacyBookmark.addedAt, | ||
chapterId = matchingChapter.id, | ||
id = Bookmark.Id.random(), | ||
setBySleepTimer = legacyBookmark.setBySleepTimer, | ||
time = legacyBookmark.time, | ||
title = legacyBookmark.title, | ||
), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters