Skip to content

Commit

Permalink
Fixed: When a note is deleted, the corresponding file remained in sto…
Browse files Browse the repository at this point in the history
…rage.

* The note file is now deleted from storage when the user selects multiple notes and deletes them from the "Notes" screen.
* Improved note deletion in the "Note Dialog." Previously, when deleting a note from the `AddNoteDialog` on the Notes screen, the file was removed from storage, but its entry remained in the database, causing the note to still appear on the "Notes" screen after deletion. This issue has now been fixed.
  • Loading branch information
MohitMaliDeveloper committed Jan 29, 2025
1 parent 7346383 commit b7e7a3e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions core/src/main/java/org/kiwix/kiwixmobile/core/dao/NotesRoomDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import io.reactivex.Flowable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.kiwix.kiwixmobile.core.dao.entities.NotesRoomEntity
import org.kiwix.kiwixmobile.core.extensions.deleteFile
import org.kiwix.kiwixmobile.core.extensions.isFileExist
import org.kiwix.kiwixmobile.core.page.adapter.Page
import org.kiwix.kiwixmobile.core.page.notes.adapter.NoteListItem
import org.kiwix.kiwixmobile.core.reader.ZimReaderSource.Companion.fromDatabaseValue
import java.io.File

@Dao
abstract class NotesRoomDao : PageDao {
Expand Down Expand Up @@ -71,6 +77,22 @@ abstract class NotesRoomDao : PageDao {
notesList.forEachIndexed { _, note ->
val notesRoomEntity = NotesRoomEntity(note)
deleteNote(noteTitle = notesRoomEntity.noteTitle)
removeNoteFileFromStorage(notesRoomEntity.noteFilePath)
}
}

/**
* Deletes the saved file from storage.
* When the user deletes a note from the "Notes" screen,
* the associated file should also be removed from storage,
* as it is no longer needed.
*/
private fun removeNoteFileFromStorage(noteFilePath: String) {
CoroutineScope(Dispatchers.IO).launch {
val noteFile = File(noteFilePath)
if (noteFile.isFileExist()) {
noteFile.deleteFile()

Check warning on line 94 in core/src/main/java/org/kiwix/kiwixmobile/core/dao/NotesRoomDao.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/dao/NotesRoomDao.kt#L94

Added line #L94 was not covered by tests
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ class AddNoteDialog : DialogFragment() {
val noteText = dialogNoteAddNoteBinding?.addNoteEditText?.text.toString()
if (noteDeleted) {
dialogNoteAddNoteBinding?.addNoteEditText?.text?.clear()
mainRepositoryActions.deleteNote(articleNoteFileName)
mainRepositoryActions.deleteNote(getNoteTitle())

Check warning on line 439 in core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt#L439

Added line #L439 was not covered by tests
disableMenuItems()
view?.snack(
stringId = R.string.note_delete_successful,
Expand Down

0 comments on commit b7e7a3e

Please sign in to comment.