Skip to content

Instantly share code, notes, and snippets.

@gold24park
Created October 7, 2023 09:10
Show Gist options
  • Save gold24park/2c44e85e4aafc7efba08e350c4724d16 to your computer and use it in GitHub Desktop.
Save gold24park/2c44e85e4aafc7efba08e350c4724d16 to your computer and use it in GitHub Desktop.
DialogQueue-4
class DialogHandlerImpl(
private val queue: DialogQueue,
): DialogHandler {
override fun <Req, Res> showDialog(
builder: DialogBuilder<Req, Res>,
req: Req,
priority: DialogQueue.Priority
) {
show(builder = builder, req = req, priority = priority)
}
override suspend fun <Req, Res> showDialogForResult(
builder: DialogBuilder<Req, Res>,
req: Req,
priority: DialogQueue.Priority
): Result<Res> {
return runCatching {
show(builder = builder, req = req, priority = priority).await()
}
}
override suspend fun <Res> showDialogForResult(
builder: DialogBuilder<Unit, Res>,
priority: DialogQueue.Priority
): Result<Res> {
return showDialogForResult(builder, Unit)
}
private fun <Req, Res> show(
builder: DialogBuilder<Req, Res>,
req: Req,
priority: DialogQueue.Priority
): CompletableDeferred<Res> {
var deferred = CompletableDeferred<Res>()
val tag = "DialogHandler::${builder::class.java.simpleName}::${req}"
queue.add(
DialogQueueElement(
priority = priority,
tag = tag,
dialogBuilder = { context, dismiss ->
// 다이얼로그를 onPause에서 닫고 다시 꺼내서 보여줄때는
// CompletableDeferred가 이미 Completed된 상태라 닫기를 눌러도 먹통이 될 것이다.
// 그래서 다이얼로그를 빌드할때 CompletableDeferred가 isCompleted 라면
// 새로운 deferred를 생성해서 넘겨준다.
if (deferred.isCompleted) {
deferred = CompletableDeferred()
}
deferred.invokeOnCompletion { dismiss() }
builder.build(
context = context,
req = req,
deferred = deferred,
)
}
)
)
return deferred
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment