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

Don't save last program state #140

Merged
merged 1 commit into from
Oct 29, 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
6 changes: 0 additions & 6 deletions app/src/main/java/com/emerjbl/ultra8/Ultra8Application.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.emerjbl.ultra8

import android.app.Application
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
import androidx.room.RoomDatabase
import com.emerjbl.ultra8.data.CatalogDatabase
import com.emerjbl.ultra8.data.CatalogStore
Expand All @@ -20,8 +16,6 @@ interface Provider {
val catalogDb: RoomDatabase
}

val Context.preferences: DataStore<Preferences> by preferencesDataStore(name = "settings")

class Ultra8Application : Application() {
val provider = object : Provider {
override val chip8StateStore by lazy { Chip8StateStore(userDb.chip8StateDao()) }
Expand Down
21 changes: 0 additions & 21 deletions app/src/main/java/com/emerjbl/ultra8/data/ProgramStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ package com.emerjbl.ultra8.data
import android.content.Context
import android.net.Uri
import android.provider.MediaStore.MediaColumns
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Upsert
import com.emerjbl.ultra8.preferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext

/** A single program entry. */
Expand Down Expand Up @@ -62,18 +58,6 @@ class ProgramStore(
) {
fun programsFlow(): Flow<List<Program>> = programDao.allFlow()

val selectedProgram: Flow<SelectedProgram> = context.preferences.data.map {
it[SELECTED_PROGRAM_KEY]
?.let { SelectedProgram.Program(it) }
?: SelectedProgram.None
}

suspend fun setSelectedProgram(name: String) {
context.preferences.edit {
it[SELECTED_PROGRAM_KEY] = name
}
}

suspend fun add(program: Program) {
programDao.add(program)
}
Expand Down Expand Up @@ -109,9 +93,4 @@ class ProgramStore(
programDao.updateCyclesPerTick(name, cyclesPerTick)

suspend fun remove(name: String) = programDao.remove(Program(name, 0))

companion object {
private val SELECTED_PROGRAM_KEY = stringPreferencesKey("lastProgram")
private const val DEFAULT_PROGRAM_NAME = "breakout"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ class PlayGameViewModel(
// TODO: See if we can improve this.
frames.value = machine.nextFrame(null)

programStore.setSelectedProgram(programName)
while (isActive) {
if (!running.value || machine.stateView.halted != null) {
Log.i("Chip8", "$programName paused at ${machine.stateView.pc.toHexString()}")
Expand All @@ -117,7 +116,6 @@ class PlayGameViewModel(
// TODO: See if we can improve this.
frames.value = machine.nextFrame(null)
Log.i("Chip8", "$programName resumed at ${machine.stateView.pc.toHexString()}")
programStore.setSelectedProgram(programName)
}
val instructionTime = measureTime {
val result = machine.tick(cyclesPerTick.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@ package com.emerjbl.ultra8.ui.loading
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.lifecycle.viewmodel.compose.viewModel
import com.emerjbl.ultra8.data.SelectedProgram
import com.emerjbl.ultra8.ui.gameplay.PlayScreen
import kotlinx.coroutines.flow.flowOf

@Composable
fun InitialLoadScreen(
onDrawerOpen: () -> Unit,
onCatalog: () -> Unit,
onReady: (String) -> Unit,
) {
val viewModel = viewModel<InitialLoadViewModel>(factory = InitialLoadViewModel.Factory)
val selectedProgram = viewModel.selectedProgram.collectAsState(SelectedProgram.Loading).value
println("SELECTED PROGRAM: $selectedProgram")
when (selectedProgram) {
is SelectedProgram.None -> onCatalog()
is SelectedProgram.Program -> onReady(selectedProgram.programName)
else -> Unit
}
val title = when (selectedProgram) {
is SelectedProgram.Loading -> "Loading..."
is SelectedProgram.None -> "<- Choose a game"
is SelectedProgram.Program -> selectedProgram.programName
val programCount = viewModel.programs.collectAsState(null).value
when {
programCount == null -> Unit
programCount.size == 0 -> onCatalog()
else -> onDrawerOpen()
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package com.emerjbl.ultra8.ui.loading
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.emerjbl.ultra8.data.ProgramStore
import com.emerjbl.ultra8.data.SelectedProgram
import com.emerjbl.ultra8.util.viewModelFactory
import kotlinx.coroutines.flow.Flow

class InitialLoadViewModel(
programStore: ProgramStore,
) : ViewModel() {
val selectedProgram: Flow<SelectedProgram> = programStore.selectedProgram
val programs = programStore.programsFlow()

companion object {
val Factory: ViewModelProvider.Factory = viewModelFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import com.emerjbl.ultra8.data.SelectedProgram
import com.emerjbl.ultra8.ui.theme.Ultra8Theme
import com.emerjbl.ultra8.util.matchesRoute
import kotlinx.coroutines.launch
Expand All @@ -27,8 +26,8 @@ fun AppEntryPoint() {

val topLevelViewModel = viewModel<TopLevelViewModel>(factory = TopLevelViewModel.Factory)
val programs = topLevelViewModel.programs.collectAsState(emptyList())
val selectedProgram =
topLevelViewModel.selectedProgram.collectAsState(SelectedProgram.Loading).value

val selectedProgram = remember { mutableStateOf("") }

val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)

Expand All @@ -52,7 +51,7 @@ fun AppEntryPoint() {
SideDrawer(
drawerState,
programs.value,
selectedProgram,
selectedProgram.value,
onProgramSelected = { program ->
scope.launch { drawerState.close() }
val route = PlayGame(program.name)
Expand Down Expand Up @@ -80,6 +79,7 @@ fun AppEntryPoint() {
gameShouldRun = gameShouldRun.value,
resetEvents = topLevelViewModel.resetEvents,
onDrawerOpen = { scope.launch { drawerState.open() } },
onActiveProgram = { selectedProgram.value = it }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.emerjbl.ultra8.data.Program
import com.emerjbl.ultra8.data.SelectedProgram

@Composable
fun SideDrawer(
drawerState: DrawerState,
programs: List<Program>,
selectedProgram: SelectedProgram,
selectedProgram: String,
onProgramSelected: (Program) -> Unit,
onReset: () -> Unit,
onCatalog: () -> Unit,
Expand Down Expand Up @@ -93,7 +92,7 @@ fun SideDrawer(
}
}
},
selected = SelectedProgram.Program(program.name) == selectedProgram
selected = program.name == selectedProgram
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class TopLevelViewModel(
// But I'll probably find a better approach eventually.
val resetEvents = MutableSharedFlow<Unit>(extraBufferCapacity = 1)

val selectedProgram = programStore.selectedProgram

fun removeProgram(name: String) {
viewModelScope.launch {
programStore.remove(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fun Ultra8NavHost(
gameShouldRun: Boolean,
resetEvents: Flow<Unit>,
onDrawerOpen: () -> Unit,
onActiveProgram: (String) -> Unit,
) {
NavHost(navController, startDestination = InitialLoad) {
composable<InitialLoad> {
Expand All @@ -35,15 +36,7 @@ fun Ultra8NavHost(
}
}
},
onReady = {
navController.navigate(PlayGame(it)) {
// https://issuetracker.google.com/issues/370694831
@SuppressLint("RestrictedApi")
popUpTo(InitialLoad) {
inclusive = true
}
}
})
)
}
composable<Catalog> {
CatalogScreen(
Expand All @@ -66,6 +59,7 @@ fun Ultra8NavHost(
// Prevent game running when view is animating away.
val gameShouldRun =
gameShouldRun && entry.lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)
onActiveProgram(routeProgram)
PlayScreen(
programName = routeProgram,
gameShouldRun = gameShouldRun,
Expand Down
Loading