Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed usages of the obsolete coroutines API #2558

Merged
merged 1 commit into from
Sep 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -105,34 +105,33 @@ class StorageDialogBuilder(
// This will be called when user saves a project.
myDocumentUpdater = Consumer { document ->
val killProgress = myDialogUi.toggleProgress(true)
val onFinish = Channel<Boolean>()
documentManager.getProxyDocument(document).also {
it.createContents()
myProject.document = it
}
GlobalScope.launch(Dispatchers.IO) {
try {
if (document.isLocal) {
document.asLocalDocument()?.create()
}
projectUi.saveProject(myProject, onFinish)
if (onFinish.receive()) {

try {
if (document.isLocal) {
document.asLocalDocument()?.create()
}
projectUi.saveProject(myProject).await { success ->
if (success) {
document.asOnlineDocument()?.let {
if (it is GPCloudDocument) {
it.onboard(documentManager, webSocket)
}
}
}
myDialogUi.toggleProgress(false)
myDialogUi.close()
} catch (e: Exception) {
killProgress()
if (e is PaymentRequiredException) {
println(e.message)
}
myDialogUi.error(e.message ?: "")
LOG.error("Failed to save document {}", document.uri, exception = e)
}
myDialogUi.toggleProgress(false)
myDialogUi.close()
} catch (e: Exception) {
killProgress()
if (e is PaymentRequiredException) {
println(e.message)
}
myDialogUi.error(e.message ?: "")
LOG.error("Failed to save document {}", document.uri, exception = e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SaveProjectAction private constructor(
if (calledFromAppleScreenMenu(e)) {
return
}
myProjectUiFacade.saveProject(myProject, null)
myProjectUiFacade.saveProject(myProject)
}

override fun projectModified() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import java.io.IOException
typealias AuthenticationFlow = (()->Unit)->Unit

interface ProjectUIFacade {
fun saveProject(project: IGanttProject, onFinish: Channel<Boolean>?)
fun saveProject(project: IGanttProject): Barrier<Boolean>
fun saveProjectAs(project: IGanttProject)
fun ensureProjectSaved(project: IGanttProject): Barrier<Boolean>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import javafx.scene.layout.BorderPane
import javafx.scene.layout.Pane
import javafx.stage.Window
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import net.sourceforge.ganttproject.GPLogger
import net.sourceforge.ganttproject.GanttProjectImpl
Expand All @@ -54,7 +53,6 @@ import net.sourceforge.ganttproject.importer.importBufferProject
import net.sourceforge.ganttproject.language.GanttLanguage
import net.sourceforge.ganttproject.resource.HumanResourceMerger
import net.sourceforge.ganttproject.resource.HumanResourceMerger.MergeResourcesOption.BY_ID
import net.sourceforge.ganttproject.task.event.*
import net.sourceforge.ganttproject.task.export
import net.sourceforge.ganttproject.task.importFromDatabase
import net.sourceforge.ganttproject.undo.GPUndoManager
Expand All @@ -66,7 +64,6 @@ import javax.swing.JOptionPane
import javax.swing.SwingUtilities


@ExperimentalCoroutinesApi
class ProjectUIFacadeImpl(
private val window: Window,
private val myWorkbenchFacade: UIFacade,
Expand All @@ -79,29 +76,17 @@ class ProjectUIFacadeImpl(
private val myConverterGroup = GPOptionGroup("convert", ProjectOpenStrategy.milestonesOption)
private var isSaving = false

override fun saveProject(project: IGanttProject, onFinish: Channel<Boolean>?) {
override fun saveProject(project: IGanttProject): Barrier<Boolean> {
if (isSaving) {
GPLogger.logToLogger("We're saving the project now. This save request was rejected")
}
val saveBarrier = SimpleBarrier<Boolean>()
isSaving = true
try {
val broadcastWaitScope = CoroutineScope(Executors.newFixedThreadPool(2).asCoroutineDispatcher())
val broadcastChannel = BroadcastChannel<Boolean>(1)
broadcastChannel.openSubscription().let { channel ->
broadcastWaitScope.launch {
if (channel.receive()) {
afterSaveProject(project)
channel.cancel()
}
}
saveBarrier.await {
afterSaveProject(project)
}
onFinish?.let {
broadcastChannel.openSubscription().let { channel -> broadcastWaitScope.launch {
it.send(channel.receive())
channel.cancel()
}}
}
ProjectSaveFlow(project = project, onFinish = broadcastChannel,
ProjectSaveFlow(project = project, onFinish = saveBarrier,
signin = this::signin,
error = this::onError,
saveAs = { saveProjectAs(project) }
Expand All @@ -110,6 +95,7 @@ class ProjectUIFacadeImpl(
isSaving = false
myWorkbenchFacade.activeChart.focus()
}
return saveBarrier
}

fun onError(ex: Exception) {
Expand Down Expand Up @@ -212,17 +198,9 @@ class ProjectUIFacadeImpl(
result.resolve(false)
},
OkAction.create("yes") {
val onFinish = Channel<Boolean>()
CoroutineScope(Executors.newSingleThreadExecutor().asCoroutineDispatcher()).launch {
try {
if (onFinish.receive()) {
result.resolve(true)
}
} catch (e: Exception) {
myWorkbenchFacade.showErrorDialog(e)
}
saveProject(project).await { success ->
result.resolve(success)
}
saveProject(project, onFinish)
},
OkAction.create("no") {
result.resolve(true)
Expand Down Expand Up @@ -350,20 +328,15 @@ class ProjectUIFacadeImpl(
}
}

@ExperimentalCoroutinesApi
class ProjectSaveFlow(
private val project: IGanttProject,
private val onFinish: BroadcastChannel<Boolean>,
private val signin: (()->Unit) -> Unit,
private val error: (Exception) -> Unit,
private val saveAs: () -> Unit) {
private val project: IGanttProject,
private val onFinish: SimpleBarrier<Boolean>,
private val signin: (()->Unit) -> Unit,
private val error: (Exception) -> Unit,
private val saveAs: () -> Unit) {

private val doneScope = CoroutineScope(Executors.newSingleThreadExecutor().asCoroutineDispatcher())
private fun done(success: Boolean) {
doneScope.launch {
onFinish.send(success)
onFinish.close()
}
onFinish.resolve(success)
}

fun run() {
Expand Down
Loading