-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
562 additions
and
102 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
app/src/main/java/moe/fuqiuluo/portal/android/coro/CoroutineController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package moe.fuqiuluo.portal.android.coro | ||
|
||
import kotlinx.coroutines.channels.Channel | ||
|
||
class CoroutineController { | ||
private val controlChannel = Channel<ControlCommand>(Channel.UNLIMITED) | ||
var isPaused = false | ||
|
||
suspend fun controlledCoroutine() { | ||
checkControl() | ||
} | ||
|
||
private suspend fun checkControl() { | ||
controlChannel.tryReceive().getOrNull()?.let { | ||
when (it) { | ||
ControlCommand.Pause -> { | ||
isPaused = true | ||
while (controlChannel.receive() != ControlCommand.Resume) { | ||
// do nothing | ||
} | ||
isPaused = false | ||
} | ||
ControlCommand.Resume -> {} | ||
} | ||
} | ||
} | ||
|
||
fun pause() { | ||
controlChannel.trySend(ControlCommand.Pause) | ||
} | ||
|
||
fun resume() { | ||
controlChannel.trySend(ControlCommand.Resume) | ||
} | ||
} | ||
|
||
enum class ControlCommand { | ||
Pause, | ||
Resume | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.