diff --git a/app/src/main/java/com/ethran/notable/data/PageDataManager.kt b/app/src/main/java/com/ethran/notable/data/PageDataManager.kt index b7df1bfe..7e635613 100644 --- a/app/src/main/java/com/ethran/notable/data/PageDataManager.kt +++ b/app/src/main/java/com/ethran/notable/data/PageDataManager.kt @@ -36,6 +36,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.launch @@ -665,16 +666,6 @@ class PageDataManager @Inject constructor( } fun setBackground(pageId: String, background: CachedBackground) { - if (background.bitmap == null) { - val msg = "setBackground: skipping cache write, bitmap is null (id=${background.id})" - log.w(msg) - appEventBus.tryEmit( - AppEvent.GenericError( - msg - ) - ) - return - } dataScope.launch { // we assume that the pageId is in current notebook. val observeBg = appRepository.isObservable(pageFromDb?.notebookId) @@ -758,38 +749,26 @@ class PageDataManager @Inject constructor( val eventString = fileObserverEventNames(event) log.d("Background file changed: $filePath [event=$eventString]") - - // HANDLE DELETION / RE-REGISTRATION if (event == DELETE || event == DELETE_SELF) { - log.d("Background file deleted. Waiting for recreation...") + log.d("Background file deleted.") synchronized(fileObservers) { fileObservers.remove(filePath)?.stopWatching() } - if (!waitForFileAvailable(filePath)) { - log.w("File disappeared and did not return: $filePath") + log.w("File changed, but does not exist: $filePath") appEventBus.tryEmit( AppEvent.ActionHint( "Background does not exist", 3000 ) ) - } else { - // RE-REGISTER: This starts a new observer on the new file handle + return@launch + } else observeBackgroundFile(pageId, filePath) - } - - // IMPORTANT: Return here. Do NOT emit invalidateFileFlow. - // We wait for the CLOSE_WRITE event from the NEW observer. - return@launch } - // HANDLE ACTUAL DATA UPDATES - // Only emit when the file is ready to be read (CLOSE_WRITE or MOVED_TO) - if (event == CLOSE_WRITE || event == MOVED_TO) { - log.d("File is stable. Invalidating flow for: $filePath") - invalidateFileFlow.emit(filePath) - } + + invalidateFileFlow.emit(filePath) } } } diff --git a/app/src/main/java/com/ethran/notable/io/FileUtils.kt b/app/src/main/java/com/ethran/notable/io/FileUtils.kt index 03cc0dcb..d9f4f1e6 100644 --- a/app/src/main/java/com/ethran/notable/io/FileUtils.kt +++ b/app/src/main/java/com/ethran/notable/io/FileUtils.kt @@ -18,7 +18,6 @@ import com.ethran.notable.SCREEN_HEIGHT import com.ethran.notable.SCREEN_WIDTH import com.ethran.notable.utils.logCallStack import com.onyx.android.sdk.utils.UriUtils.getDataColumn -import io.shipbook.shipbooksdk.Log import io.shipbook.shipbooksdk.ShipBook import kotlinx.coroutines.delay import java.io.File @@ -257,14 +256,12 @@ suspend fun waitForFileAvailable( var count = 1 while (System.currentTimeMillis() - start < timeoutMs) { if (file.exists() && file.length() > 0) { - Log.d("waitForFileAvailable", "File available: $filePath, size=${file.length()}") return true } delay(intervalMs) intervalMs += count * count // Quadratic growth count++ } - Log.d("waitForFileAvailable", "File not available: $filePath") return false } diff --git a/app/src/main/java/com/ethran/notable/io/renderFromFile.kt b/app/src/main/java/com/ethran/notable/io/renderFromFile.kt index 842c3a85..eeef9dd7 100644 --- a/app/src/main/java/com/ethran/notable/io/renderFromFile.kt +++ b/app/src/main/java/com/ethran/notable/io/renderFromFile.kt @@ -83,6 +83,6 @@ fun loadBackgroundBitmap(filePath: String, pageNumber: Int, scale: Float): Bitma renderPdfPageMuPdf(filePath, pageNumber, targetWidth.toInt(), resolutionModifier = 1.5f) else renderPdfPageAndroid(file, pageNumber, targetWidth.toInt(), resolutionModifier = 1.2f) - timer.end("loaded background, newBitmap: $newBitmap") + timer.end("loaded background") return newBitmap } \ No newline at end of file