/
alexm
/
Notes
Обзор
Документация
Войти
/
alexm
/
Notes
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/src/main/java/com/alex/notes/data/NotesRepositoryImpl.kt
96 строк
3 KB
bolschoy
5.15
11 дек 2025, 21:11
11 дек 2025, 21:11
5264678
Код
Авторство
О чём код?
package com.alex.notes.data import com.alex.notes.domain.ContentItem import com.alex.notes.domain.Note import com.alex.notes.domain.NotesRepository import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import javax.inject.Inject class NotesRepositoryImpl @Inject constructor( private val notesDao: NotesDao, private val imageFileManager: ImageFileManager ) : NotesRepository { override suspend fun addNote( title: String, content: List<ContentItem>, isPinned: Boolean, updatedAt: Long ) { val processedContent = content.processForStorage() val noteDbModel = NoteDbModel(0, title, updatedAt, isPinned) notesDao.addNoteWithContent(noteDbModel, processedContent) } override suspend fun deleteNote(noteId: Int) { val note = notesDao.getNote(noteId).toEntity() notesDao.deleteNote(noteId) note.content .filterIsInstance<ContentItem.Image>() .map {it.url} .forEach { imageFileManager.deleteImage(it) } } override suspend fun editNote(note: Note) { val oldNote = notesDao.getNote(note.id).toEntity() val oldUrls = oldNote.content.filterIsInstance<ContentItem.Image>().map {it.url} val newUrls = note.content.filterIsInstance<ContentItem.Image>().map {it.url} val removedUrls = oldUrls - newUrls removedUrls.forEach { imageFileManager.deleteImage(it) } val processedContent = note.content.processForStorage() val processedNote = note.copy(content = processedContent) notesDao.updateNote( noteDbModel = processedNote.toDbModel(), content = processedContent.toContentItemDbModels(note.id) ) } override fun getAllNotes(): Flow<List<Note>> { return notesDao.getAllNotes().map {it.toEntities() } } override suspend fun getNote(noteId: Int): Note { return notesDao.getNote(noteId).toEntity() } override fun searchNotes(query: String): Flow<List<Note>> { return notesDao.searchNotes(query).map {it.toEntities() } } override suspend fun switchPinnedStatus(noteId: Int) { notesDao.switchPinnedStatus(noteId) } private suspend fun List<ContentItem>.processForStorage(): List<ContentItem> { return map {contentItem -> when(contentItem) { is ContentItem.Image -> { if (imageFileManager.isInternal(contentItem.url)){ contentItem } else{ val internalPath = imageFileManager.copyImageToInternalStorage(contentItem.url) ContentItem.Image(internalPath) } } is ContentItem.Text -> contentItem } } } }