From ba9551e3aa56ff55c816cd03b2f6c503dc2c67db Mon Sep 17 00:00:00 2001 From: YXWang <19281171@bjtu.edu.cn> Date: Mon, 2 Aug 2021 14:57:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20.gitignore=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6,=20=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=87=A0=E4=B8=AA?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E7=9A=84=E6=97=A0=E8=A7=86=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + .../compose_tetris/TetrisGameWindow.kt | 89 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 app/src/main/java/github/leavesc/compose_tetris/TetrisGameWindow.kt diff --git a/.gitignore b/.gitignore index d89174d..9bec4d0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ *.bin *.bak *.lock +/.tmp +/kotlin-profile +/native /caches /wrapper /daemon diff --git a/app/src/main/java/github/leavesc/compose_tetris/TetrisGameWindow.kt b/app/src/main/java/github/leavesc/compose_tetris/TetrisGameWindow.kt new file mode 100644 index 0000000..b85c837 --- /dev/null +++ b/app/src/main/java/github/leavesc/compose_tetris/TetrisGameWindow.kt @@ -0,0 +1,89 @@ +package github.leavesc.compose_tetris + +import androidx.compose.material.Surface +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.platform.LocalLifecycleOwner +import androidx.compose.ui.tooling.preview.Preview +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.viewmodel.compose.viewModel +import androidx.navigation.compose.rememberNavController +import github.leavesc.compose_tetris.logic.Action +import github.leavesc.compose_tetris.logic.TetrisViewModel +import github.leavesc.compose_tetris.logic.combinedPlayListener +import github.leavesc.compose_tetris.logic.previewTetrisState +import github.leavesc.compose_tetris.ui.TetrisBody +import github.leavesc.compose_tetris.ui.TetrisButton +import github.leavesc.compose_tetris.ui.TetrisScreen +import github.leavesc.compose_tetris.ui.theme.ComposeTetrisTheme + +@Composable +@Preview(widthDp = 420, heightDp = 760) +fun PreviewTetrisGameWindow() { + TetrisBody( + tetrisScreen = { + TetrisScreen(tetrisState = previewTetrisState) + }, + tetrisButton = { + TetrisButton() + } + ) +} + +@Composable +fun tetrisGameWindow() { + ComposeTetrisTheme { + val tetrisViewModel = viewModel() + val lifecycle = LocalLifecycleOwner.current.lifecycle + + DisposableEffect(key1 = Unit) { + val observer = object : DefaultLifecycleObserver { + override fun onResume(owner: LifecycleOwner) { + tetrisViewModel.dispatch(Action.Resume) + } + + override fun onPause(owner: LifecycleOwner) { + tetrisViewModel.dispatch(Action.Background) + } + } + lifecycle.addObserver(observer) + onDispose { + lifecycle.removeObserver(observer) + } + } + Surface { + val tetrisState by tetrisViewModel.tetrisStateLD.collectAsState() + TetrisBody( + tetrisScreen = { + TetrisScreen(tetrisState = tetrisState) + }, + tetrisButton = { + TetrisButton( + playListener = combinedPlayListener( + onStart = { + tetrisViewModel.dispatch(Action.Start) + }, + onPause = { + tetrisViewModel.dispatch(Action.Pause) + }, + onReset = { + tetrisViewModel.dispatch(Action.Reset) + }, + onTransformation = { + tetrisViewModel.dispatch(Action.Transformation(it)) + }, + onSound = { + tetrisViewModel.dispatch(Action.Sound) + }, + ) + ) + } + ) + } + tetrisViewModel.dispatch(action = Action.Welcome) + } +} + -- Gitee From 516c2275ad6d639db11b3372bbe548aa222b11ca Mon Sep 17 00:00:00 2001 From: YXWang <19281171@bjtu.edu.cn> Date: Mon, 2 Aug 2021 15:03:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=89=80=E4=BD=9C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../compose_tetris/TetrisGameWindow.kt | 89 ------------------- .../leavesc/compose_tetris/ui/TetrisBody.kt | 1 - 2 files changed, 90 deletions(-) delete mode 100644 app/src/main/java/github/leavesc/compose_tetris/TetrisGameWindow.kt diff --git a/app/src/main/java/github/leavesc/compose_tetris/TetrisGameWindow.kt b/app/src/main/java/github/leavesc/compose_tetris/TetrisGameWindow.kt deleted file mode 100644 index b85c837..0000000 --- a/app/src/main/java/github/leavesc/compose_tetris/TetrisGameWindow.kt +++ /dev/null @@ -1,89 +0,0 @@ -package github.leavesc.compose_tetris - -import androidx.compose.material.Surface -import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.collectAsState -import androidx.compose.runtime.getValue -import androidx.compose.ui.platform.LocalLifecycleOwner -import androidx.compose.ui.tooling.preview.Preview -import androidx.lifecycle.DefaultLifecycleObserver -import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.viewmodel.compose.viewModel -import androidx.navigation.compose.rememberNavController -import github.leavesc.compose_tetris.logic.Action -import github.leavesc.compose_tetris.logic.TetrisViewModel -import github.leavesc.compose_tetris.logic.combinedPlayListener -import github.leavesc.compose_tetris.logic.previewTetrisState -import github.leavesc.compose_tetris.ui.TetrisBody -import github.leavesc.compose_tetris.ui.TetrisButton -import github.leavesc.compose_tetris.ui.TetrisScreen -import github.leavesc.compose_tetris.ui.theme.ComposeTetrisTheme - -@Composable -@Preview(widthDp = 420, heightDp = 760) -fun PreviewTetrisGameWindow() { - TetrisBody( - tetrisScreen = { - TetrisScreen(tetrisState = previewTetrisState) - }, - tetrisButton = { - TetrisButton() - } - ) -} - -@Composable -fun tetrisGameWindow() { - ComposeTetrisTheme { - val tetrisViewModel = viewModel() - val lifecycle = LocalLifecycleOwner.current.lifecycle - - DisposableEffect(key1 = Unit) { - val observer = object : DefaultLifecycleObserver { - override fun onResume(owner: LifecycleOwner) { - tetrisViewModel.dispatch(Action.Resume) - } - - override fun onPause(owner: LifecycleOwner) { - tetrisViewModel.dispatch(Action.Background) - } - } - lifecycle.addObserver(observer) - onDispose { - lifecycle.removeObserver(observer) - } - } - Surface { - val tetrisState by tetrisViewModel.tetrisStateLD.collectAsState() - TetrisBody( - tetrisScreen = { - TetrisScreen(tetrisState = tetrisState) - }, - tetrisButton = { - TetrisButton( - playListener = combinedPlayListener( - onStart = { - tetrisViewModel.dispatch(Action.Start) - }, - onPause = { - tetrisViewModel.dispatch(Action.Pause) - }, - onReset = { - tetrisViewModel.dispatch(Action.Reset) - }, - onTransformation = { - tetrisViewModel.dispatch(Action.Transformation(it)) - }, - onSound = { - tetrisViewModel.dispatch(Action.Sound) - }, - ) - ) - } - ) - } - tetrisViewModel.dispatch(action = Action.Welcome) - } -} - diff --git a/app/src/main/java/github/leavesc/compose_tetris/ui/TetrisBody.kt b/app/src/main/java/github/leavesc/compose_tetris/ui/TetrisBody.kt index a7691e2..c27fdc2 100644 --- a/app/src/main/java/github/leavesc/compose_tetris/ui/TetrisBody.kt +++ b/app/src/main/java/github/leavesc/compose_tetris/ui/TetrisBody.kt @@ -25,7 +25,6 @@ import github.leavesc.compose_tetris.ui.theme.BodyBackground @Composable fun PreviewTetrisBody() { TetrisBody(tetrisScreen = { - }, tetrisButton = { Box( modifier = Modifier -- Gitee