diff --git a/app/src/main/java/com/shaarit/data/export/BookmarkImporter.kt b/app/src/main/java/com/shaarit/data/export/BookmarkImporter.kt index 104b864..f07c80e 100644 --- a/app/src/main/java/com/shaarit/data/export/BookmarkImporter.kt +++ b/app/src/main/java/com/shaarit/data/export/BookmarkImporter.kt @@ -50,7 +50,17 @@ class BookmarkImporter @Inject constructor( var imported = 0 var skipped = 0 val errors = mutableListOf() - + + // BUG CORRIGÉ : LinkEntity.id est une @PrimaryKey sans autoGenerate et + // insertLink utilise OnConflictStrategy.REPLACE. Donner id = 0 à chaque + // lien faisait donc écraser la ligne précédente à chaque insertion : + // seul le dernier lien du fichier survivait (le compteur, lui, était + // incrémenté avant l'insertion, d'où un dialogue « 1785 importés » + // pour 1 seul lien visible). On alloue donc un id temporaire négatif + // unique par lien, comme le fait déjà LinkRepositoryImpl.addLink, + // en vérifiant qu'il n'existe pas déjà en base. + var nextTempId = -1 + links.forEach { bookmark -> try { // Vérifier si le lien existe déjà @@ -59,9 +69,14 @@ class BookmarkImporter @Inject constructor( skipped++ return@forEach } - + + while (linkDao.getLinkById(nextTempId) != null) { + nextTempId-- + } + val tempId = nextTempId-- + val entity = LinkEntity( - id = 0, + id = tempId, url = bookmark.url, title = bookmark.title, description = bookmark.description ?: "", @@ -104,7 +119,14 @@ class BookmarkImporter @Inject constructor( var imported = 0 var skipped = 0 val errors = mutableListOf() - + + // Même correctif que pour importFromHtml : id = 0 pour tous les liens + // + insert REPLACE écrasait chaque ligne par la suivante, ne laissant + // qu'un seul lien en base par import (le dernier non encore présent + // après la synchro, qui libérait l'id 0). Chaque lien importé reçoit + // donc un id temporaire négatif unique, vérifié contre la base. + var nextTempId = -1 + exportData.links.forEach { link -> try { val existing = linkDao.getLinkByUrl(link.url) @@ -112,7 +134,12 @@ class BookmarkImporter @Inject constructor( skipped++ return@forEach } - + + while (linkDao.getLinkById(nextTempId) != null) { + nextTempId-- + } + val tempId = nextTempId-- + val contentType = try { ContentType.valueOf(link.contentType) } catch (e: Exception) { @@ -120,7 +147,7 @@ class BookmarkImporter @Inject constructor( } val entity = LinkEntity( - id = 0, + id = tempId, url = link.url, title = link.title, description = link.description,