vinceglb / FileKit

Pick and save Files, Medias and Folder for Kotlin Multiplatform / KMP and Compose Multiplatform / CMP
https://vinceglb.github.io/FileKit/
MIT License
249 stars 8 forks source link

Encountered an issue while using the Save File Picker #45

Closed willflier closed 2 weeks ago

willflier commented 2 weeks ago
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Already resumed, but proposed with update PlatformFile(file=C:\Users\Acker\Desktop\myTextFile.txt)
    at kotlinx.coroutines.CancellableContinuationImpl.alreadyResumedError(CancellableContinuationImpl.kt:551)
    at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:516)
    at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:489)
    at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:364)
    at io.github.vinceglb.filekit.core.platform.awt.AwtFileSaver.saveFile$lambda$2$handleResult(AwtFileSaver.kt:27)
    at io.github.vinceglb.filekit.core.platform.awt.AwtFileSaver.access$saveFile$lambda$2$handleResult(AwtFileSaver.kt:12)
    at io.github.vinceglb.filekit.core.platform.awt.AwtFileSaver$saveFile$2$dialog$2.setVisible(AwtFileSaver.kt:45)
    at io.github.vinceglb.filekit.core.platform.awt.AwtFileSaver.saveFile(AwtFileSaver.kt:57)
    at io.github.vinceglb.filekit.core.FileKit$saveFile$2.invokeSuspend(FileKit.jvm.kt:76)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
    at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:111)
    at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:99)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:811)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:715)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:702)
    Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.ui.scene.ComposeContainer$DesktopCoroutineExceptionHandler@6f20c911, androidx.compose.runtime.BroadcastFrameClock@703f84f, StandaloneCoroutine{Cancelling}@6c45d059, FlushCoroutineDispatcher@1780e81a]
       fun handleResult(value: Boolean, files: Array<File>?) {
          if (value) {
              val file = files?.firstOrNull()?.let { file ->
                  // Write bytes to file, or create a new file
                  bytes?.let { file.writeBytes(bytes) } ?: file.createNewFile()
                  PlatformFile(file)
              }
              continuation.resume(file)
          } else {
              continuation.resume(null)
          }
      }

      // Handle parentWindow: Dialog, Frame, or null
      val dialog = when (parentWindow) {
          is Dialog -> object : FileDialog(parentWindow, "Save dialog", SAVE) {
              override fun setVisible(value: Boolean) {
                  super.setVisible(value)
                  handleResult(value, files)
              }
          }

          else -> object : FileDialog(parentWindow as? Frame, "Save dialog", SAVE) {
              override fun setVisible(value: Boolean) {
                  super.setVisible(value)
                  handleResult(value, files)  // <-- the function was called twice, with the value `false` the first time and `true` the second time.
              }
          }
      }
vinceglb commented 2 weeks ago

Thanks for reporting this! I'm preparing a fix 👌

vinceglb commented 2 weeks ago

I'm publishing a version 0.6.1 with a fix 🚀

Do not hesitate to tell me if you still encounter the bug!

willflier commented 2 weeks ago

It works fine, thanks a lot!