gud commit
This commit is contained in:
@@ -19,6 +19,9 @@ android {
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
debug {
|
||||
isDebuggable = true
|
||||
}
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
proguardFiles(
|
||||
@@ -87,4 +90,7 @@ dependencies {
|
||||
implementation("androidx.room:room-runtime:$roomVersion")
|
||||
implementation("androidx.room:room-ktx:$roomVersion")
|
||||
ksp("androidx.room:room-compiler:$roomVersion")
|
||||
|
||||
// Add Timber for logging
|
||||
implementation("com.jakewharton.timber:timber:5.0.1")
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:name=".EpookApplication"
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
||||
@@ -7,31 +7,32 @@ import androidx.datastore.preferences.preferencesDataStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import java.io.File
|
||||
import inhale.rip.epook.data.AppDatabase
|
||||
import android.util.Log
|
||||
|
||||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "books")
|
||||
|
||||
class BookStore(private val context: Context) {
|
||||
private val TAG = "BookStore" // Tag for logging
|
||||
private val bookIdsKey = stringSetPreferencesKey("book_ids")
|
||||
private val bookDao = AppDatabase.getDatabase(context).bookDao()
|
||||
|
||||
fun getAllBooks(): Flow<List<Book>> = context.dataStore.data.map { preferences ->
|
||||
preferences[bookIdsKey]?.mapNotNull { id ->
|
||||
fun getAllBooks(): Flow<List<Book>> = bookDao.getAllBooks().map { entities ->
|
||||
entities.mapNotNull { entity ->
|
||||
try {
|
||||
val bookFile = File(context.filesDir, "book_$id.epub")
|
||||
val coverFile = File(context.filesDir, "cover_$id.jpg")
|
||||
val bookFile = File(entity.filePath)
|
||||
if (bookFile.exists()) {
|
||||
Book(
|
||||
id = id,
|
||||
title = preferences[stringPreferencesKey("title_$id")] ?: "Unknown Title",
|
||||
coverImageFile = if (coverFile.exists()) coverFile else null,
|
||||
filePath = bookFile.absolutePath
|
||||
)
|
||||
} else null
|
||||
entity.toBook()
|
||||
} else {
|
||||
// If the file doesn't exist, delete the database entry
|
||||
Log.d(TAG, "Book file missing for ${entity.id}, cleaning up database entry")
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("Error loading book $id: ${e.message}")
|
||||
Log.e(TAG, "Error loading book ${entity.id}", e)
|
||||
null
|
||||
}
|
||||
} ?: emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addBook(book: Book) {
|
||||
@@ -54,59 +55,76 @@ class BookStore(private val context: Context) {
|
||||
}
|
||||
|
||||
suspend fun deleteBook(bookId: String) {
|
||||
Log.e(TAG, "🔥 DELETE BOOK STARTED 🔥") // Very visible error log
|
||||
Log.e(TAG, "Attempting to delete book with ID: $bookId")
|
||||
|
||||
try {
|
||||
// Debug logging at start
|
||||
println("=== Starting book deletion process ===")
|
||||
println("BookID: $bookId")
|
||||
// Check if book exists in Room database
|
||||
val bookEntity = bookDao.getBook(bookId)
|
||||
Log.e(TAG, if (bookEntity != null) {
|
||||
"Found book in database: ${bookEntity.title} (ID: ${bookEntity.id})"
|
||||
} else {
|
||||
"Book not found in database with ID: $bookId"
|
||||
})
|
||||
|
||||
// Delete the book files
|
||||
if (bookEntity != null) {
|
||||
// Try to delete from Room database
|
||||
try {
|
||||
bookDao.deleteBook(bookEntity)
|
||||
Log.e(TAG, "Successfully deleted from database: ${bookEntity.title}")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to delete from database: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
|
||||
// File deletion
|
||||
val bookFile = File(context.filesDir, "book_$bookId.epub")
|
||||
val coverFile = File(context.filesDir, "cover_$bookId.jpg")
|
||||
|
||||
println("Book file path: ${bookFile.absolutePath}")
|
||||
println("Cover file path: ${coverFile.absolutePath}")
|
||||
println("Book file exists: ${bookFile.exists()}")
|
||||
println("Cover file exists: ${coverFile.exists()}")
|
||||
Log.e(TAG, "Book file path: ${bookFile.absolutePath}")
|
||||
|
||||
if (bookFile.exists()) {
|
||||
val canWrite = bookFile.canWrite()
|
||||
Log.e(TAG, "Book file exists, size: ${bookFile.length()} bytes")
|
||||
val deleted = bookFile.delete()
|
||||
println("Book file writable: $canWrite")
|
||||
println("Book file deleted: $deleted")
|
||||
Log.e(TAG, "Book file deleted: $deleted")
|
||||
} else {
|
||||
Log.e(TAG, "Book file does not exist")
|
||||
}
|
||||
|
||||
val coverFile = File(context.filesDir, "cover_$bookId.jpg")
|
||||
Log.e(TAG, "Cover file path: ${coverFile.absolutePath}")
|
||||
|
||||
if (coverFile.exists()) {
|
||||
val canWrite = coverFile.canWrite()
|
||||
Log.e(TAG, "Cover file exists, size: ${coverFile.length()} bytes")
|
||||
val deleted = coverFile.delete()
|
||||
println("Cover file writable: $canWrite")
|
||||
println("Cover file deleted: $deleted")
|
||||
Log.e(TAG, "Cover file deleted: $deleted")
|
||||
} else {
|
||||
Log.e(TAG, "Cover file does not exist")
|
||||
}
|
||||
|
||||
// Get current state before deletion
|
||||
context.dataStore.data.collect { preferences ->
|
||||
val currentIds = preferences[bookIdsKey]
|
||||
println("Current book IDs before deletion: $currentIds")
|
||||
// DataStore cleanup
|
||||
Log.e(TAG, "Starting DataStore cleanup")
|
||||
try {
|
||||
context.dataStore.edit { preferences ->
|
||||
val currentIds = preferences[bookIdsKey]?.toMutableSet() ?: mutableSetOf()
|
||||
Log.e(TAG, "Current IDs in DataStore: $currentIds")
|
||||
val removed = currentIds.remove(bookId)
|
||||
Log.e(TAG, "ID removed from DataStore: $removed")
|
||||
preferences[bookIdsKey] = currentIds
|
||||
Log.e(TAG, "Updated IDs in DataStore: $currentIds")
|
||||
|
||||
preferences.remove(stringPreferencesKey("title_$bookId"))
|
||||
preferences.remove(stringPreferencesKey("position_$bookId"))
|
||||
Log.e(TAG, "Removed related preferences for book ID: $bookId")
|
||||
}
|
||||
Log.e(TAG, "DataStore cleanup completed successfully")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error during DataStore cleanup", e)
|
||||
}
|
||||
|
||||
// Remove from preferences
|
||||
context.dataStore.edit { preferences ->
|
||||
val currentIds = preferences[bookIdsKey]?.toMutableSet() ?: mutableSetOf()
|
||||
val removed = currentIds.remove(bookId)
|
||||
preferences[bookIdsKey] = currentIds
|
||||
|
||||
// Clean up related preferences
|
||||
preferences.remove(stringPreferencesKey("title_$bookId"))
|
||||
preferences.remove(stringPreferencesKey("position_$bookId"))
|
||||
|
||||
println("Book ID removed from set: $removed")
|
||||
println("Remaining book IDs: $currentIds")
|
||||
}
|
||||
|
||||
println("=== Book deletion process completed ===")
|
||||
Log.e(TAG, "🔥 DELETE BOOK COMPLETED SUCCESSFULLY 🔥")
|
||||
} catch (e: Exception) {
|
||||
println("=== Error during book deletion ===")
|
||||
println("Error message: ${e.message}")
|
||||
println("Stack trace:")
|
||||
Log.e(TAG, "🔥 DELETE BOOK FAILED 🔥")
|
||||
Log.e(TAG, "Error type: ${e.javaClass.simpleName}")
|
||||
Log.e(TAG, "Error message: ${e.message}")
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user