Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions app/src/main/java/com/ethran/notable/data/PageDataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/ethran/notable/io/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/ethran/notable/io/renderFromFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading