Skip to content

Commit

Permalink
Handle delete properly (#141)
Browse files Browse the repository at this point in the history
* When a program is deleted, make sure its screens are popped from the
  backstack (as they are re-visited)
  • Loading branch information
jblebrun authored Oct 29, 2024
1 parent a153e03 commit e0e2281
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
11 changes: 5 additions & 6 deletions app/src/main/java/com/emerjbl/ultra8/data/ProgramStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ interface ProgramDao {
@Query("SELECT name, cyclesPerTick from program ORDER BY name")
fun allFlow(): Flow<List<Program>>

@Query("SELECT * from program where name == :name limit 1")
fun nameFlow(name: String): Flow<Program?>

@Query("SELECT * from program where name == :name limit 1")
suspend fun withData(name: String): Program?

Expand All @@ -45,19 +48,15 @@ interface ProgramDao {
suspend fun add(program: Program)
}

sealed interface SelectedProgram {
data object Loading : SelectedProgram
data object None : SelectedProgram
data class Program(val programName: String) : SelectedProgram
}

/** The store of programs that Ultra8 can run. */
class ProgramStore(
private val context: Context,
private val programDao: ProgramDao,
) {
fun programsFlow(): Flow<List<Program>> = programDao.allFlow()

fun nameFlow(name: String): Flow<Program?> = programDao.nameFlow(name)

suspend fun add(program: Program) {
programDao.add(program)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PlayGameViewModel(
private val programStore: ProgramStore,
) : ViewModel() {
private var machine = newMachine(byteArrayOf())
val program = programStore.nameFlow(programName)

val running = MutableStateFlow<Boolean>(false)
val halted = MutableStateFlow<Halt?>(null)
Expand Down Expand Up @@ -83,19 +84,19 @@ class PlayGameViewModel(

suspend fun runMachine() {
machine = withContext(Dispatchers.IO) {
val savedState = chip8StateStore.findState(programName)
val program = programStore.withData(programName)
if (program != null) {
cyclesPerTick.value = program.cyclesPerTick
if (program == null) {
Log.i("Chip8", "Could not find $programName at all")
return@withContext null
}

cyclesPerTick.value = program.cyclesPerTick
val savedState = chip8StateStore.findState(programName)
if (savedState != null) {
Log.i("Chip8", "Restored save state for $programName")
newMachine(savedState)
} else if (program != null) {
newMachine(program.data!!)
} else {
Log.i("Chip8", "Could not find $programName at all")
null
newMachine(program.data!!)
}
} ?: return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import com.emerjbl.ultra8.ui.gameplay.input.onKeyEvent
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first

@Composable
fun PlayScreen(
programName: String,
gameShouldRun: Boolean,
resetEvents: Flow<Unit>,
onDrawerOpen: () -> Unit,
onProgramGone: () -> Unit,
title: String = programName,
) {
val viewModel =
Expand All @@ -36,6 +38,13 @@ fun PlayScreen(
factory = PlayGameViewModel.Factory
)

// Listen for program disappearance
LaunchedEffect(viewModel.program) {
viewModel.program.first { it == null }
println("Program disappeared!")
onProgramGone()
}

// Collect reset events from above
LaunchedEffect(resetEvents) {
resetEvents.collect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ fun InitialLoadScreen(
val programCount = viewModel.programs.collectAsState(null).value
when {
programCount == null -> Unit
programCount.size == 0 -> onCatalog()
else -> onDrawerOpen()
programCount.size == 0 -> {
println("No programs, show catalog")
onCatalog()
}

else -> {
println("Some programs, show drawer")
onDrawerOpen()
}
}

// Just show a blank play screen while loading.
PlayScreen("", false, flowOf(), onDrawerOpen, "<- Choose a game")
PlayScreen("", false, flowOf(), onDrawerOpen, {}, "<- Choose a game")

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ fun Ultra8NavHost(
InitialLoadScreen(
onDrawerOpen,
onCatalog = {
navController.navigate(Catalog) {
// https://issuetracker.google.com/issues/370694831
@SuppressLint("RestrictedApi")
popUpTo(InitialLoad) {
inclusive = true
}
}
navController.navigate(Catalog)
},
)
}
Expand All @@ -44,7 +38,7 @@ fun Ultra8NavHost(
navController.navigate(PlayGame(it)) {
// https://issuetracker.google.com/issues/370694831
@SuppressLint("RestrictedApi")
popUpTo(InitialLoad) {
popUpTo(Catalog) {
inclusive = true
}
}
Expand All @@ -64,6 +58,13 @@ fun Ultra8NavHost(
programName = routeProgram,
gameShouldRun = gameShouldRun,
resetEvents = resetEvents,
onProgramGone = {
println("$routeProgram GONE!")
if (!navController.popBackStack()) {
println("Couldn't pop...")
navController.navigate(InitialLoad)
}
},
onDrawerOpen = onDrawerOpen,
)
}
Expand Down

0 comments on commit e0e2281

Please sign in to comment.