Skip to content

Commit

Permalink
Refactor RenderViewModel into specific View
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Aug 14, 2024
1 parent b18de43 commit f92a31d
Show file tree
Hide file tree
Showing 15 changed files with 660 additions and 745 deletions.
62 changes: 35 additions & 27 deletions app/src/main/java/app/jerboa/spp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import app.jerboa.spp.viewmodel.MUSIC
import app.jerboa.spp.viewmodel.REVIEW_RATE_LIMIT_MILLIS
import app.jerboa.spp.viewmodel.RenderViewModel
import app.jerboa.spp.viewmodel.SPPViewModel
import app.jerboa.spp.viewmodel.SOCIAL
import app.jerboa.spp.composable.renderScreen
import app.jerboa.spp.composable.screen
import app.jerboa.spp.ui.theme.SPPTheme
import app.jerboa.spp.viewmodel.AboutViewModel
import app.jerboa.spp.viewmodel.MainMenuViewModel
import app.jerboa.spp.viewmodel.MenuPromptViewModel
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.games.AuthenticationResult
Expand Down Expand Up @@ -84,7 +87,10 @@ val achievementStates = mutableMapOf(

class MainActivity : AppCompatActivity() {

private val renderViewModel by viewModels<RenderViewModel>()
private val sppViewModel by viewModels<SPPViewModel>()
private val aboutViewModel by viewModels<AboutViewModel>()
private val menuPromptViewModel by viewModels<MenuPromptViewModel>()
private val mainMenuViewModel by viewModels<MainMenuViewModel>()

private var mediaPlayer = MediaPlayer()

Expand Down Expand Up @@ -141,7 +147,7 @@ class MainActivity : AppCompatActivity() {

private fun showAchievements() {
if (!isGooglePlayGamesServicesInstalled(this)) {
renderViewModel.onRequestPGSAndNotInstalled()
sppViewModel.onRequestPGSAndNotInstalled()
return
}
PlayGames.getAchievementsClient(this)
Expand Down Expand Up @@ -173,7 +179,7 @@ class MainActivity : AppCompatActivity() {
isAuthenticatedTask.isSuccessful && isAuthenticatedTask.result.isAuthenticated
if (isAuthenticated) {
// Continue with Play Games Services
renderViewModel.onPlaySuccess(true)
sppViewModel.onPlaySuccess(true)
if (DEBUG) {
Log.d("playGames", "success")
}
Expand All @@ -185,7 +191,7 @@ class MainActivity : AppCompatActivity() {
if (DEBUG) {
Log.d("playGames", "failure ${isAuthenticatedTask.result.toString()}")
}
renderViewModel.onPlaySuccess(false)
sppViewModel.onPlaySuccess(false)
}
}
}
Expand Down Expand Up @@ -323,7 +329,7 @@ class MainActivity : AppCompatActivity() {
val flow = reviewManager.launchReviewFlow(this, reviewInfo)
//Log.d("reviewManager", "flow")
flow.addOnCompleteListener {
renderViewModel.onInAppReviewShown()
sppViewModel.onInAppReviewShown()
//Log.d("reviewManager", "complete: $reviewInfo")
}
} else {
Expand All @@ -335,7 +341,7 @@ class MainActivity : AppCompatActivity() {
}
}

renderViewModel.onInAppReviewShown()
sppViewModel.onInAppReviewShown()
seenReview = true

if (lastReviewTries < 5) {
Expand Down Expand Up @@ -368,7 +374,7 @@ class MainActivity : AppCompatActivity() {

reviewManager = ReviewManagerFactory.create(this)

renderViewModel.requestingInAppReview.observe(
sppViewModel.requestingInAppReview.observe(
this, androidx.lifecycle.Observer {
if (it) {
requestUserReviewPrompt()
Expand All @@ -381,13 +387,13 @@ class MainActivity : AppCompatActivity() {
Log.d("playServices", "$status ${status == ConnectionResult.SUCCESS}")
}

renderViewModel.requestingPlayServices.observe(
sppViewModel.requestingPlayServices.observe(
this, androidx.lifecycle.Observer { request ->
if (request) {
if (DEBUG) {
Log.d("playGames", "${renderViewModel.playSuccess.value!!}")
Log.d("playGames", "${sppViewModel.playSuccess.value!!}")
}
if (!renderViewModel.playSuccess.value!! && isGooglePlayGamesServicesInstalled(this)) {
if (!sppViewModel.playSuccess.value!! && isGooglePlayGamesServicesInstalled(this)) {
if (DEBUG) {
Log.d("playGames", "login")
}
Expand All @@ -409,24 +415,24 @@ class MainActivity : AppCompatActivity() {
}
)

renderViewModel.requestingInstallPGS.observe(
sppViewModel.requestingInstallPGS.observe(
this, androidx.lifecycle.Observer { request ->
if (request) {
installPGS()
renderViewModel.onInstallPGSInitiated()
sppViewModel.onInstallPGSInitiated()
}
}
)

renderViewModel.requestingLicenses.observe(
aboutViewModel.requestingLicenses.observe(
this, androidx.lifecycle.Observer { request ->
if (request) {
showLicenses()
}
}
)

renderViewModel.requestingSocial.observe(
aboutViewModel.requestingSocial.observe(
this, androidx.lifecycle.Observer { request ->
when (request) {
SOCIAL.WEB -> web()
Expand All @@ -438,7 +444,7 @@ class MainActivity : AppCompatActivity() {
}
)

renderViewModel.showToys.observe(
mainMenuViewModel.showToys.observe(
this, androidx.lifecycle.Observer { show ->
val prefs = getSharedPreferences("jerboa.app.spp.prefs", MODE_PRIVATE)
val prefsEdit = prefs.edit()
Expand All @@ -447,7 +453,7 @@ class MainActivity : AppCompatActivity() {
}
)

renderViewModel.playingMusic.observe(
menuPromptViewModel.playingMusic.observe(
this, androidx.lifecycle.Observer { playingMusic ->
when (playingMusic) {
MUSIC.FORREST -> {
Expand All @@ -469,7 +475,7 @@ class MainActivity : AppCompatActivity() {
}
)

renderViewModel.playTime.observe(
sppViewModel.playTime.observe(
this, androidx.lifecycle.Observer { time ->
run {
totalTime = if (seenReview) {
Expand Down Expand Up @@ -536,7 +542,7 @@ class MainActivity : AppCompatActivity() {
prefsEdit.apply()
}

renderViewModel.onShowToysChanged(prefs.getBoolean("showToys", false))
mainMenuViewModel.onShowToysChanged(prefs.getBoolean("showToys", false))

// if (BuildConfig.DEBUG){
// prefs.edit().clear().apply()
Expand All @@ -552,11 +558,11 @@ class MainActivity : AppCompatActivity() {

syncAchievementsState()

renderViewModel.setAchievementState(achievementStates)
sppViewModel.setAchievementState(achievementStates)

}

renderViewModel.achievementStates.observe(
sppViewModel.achievementStates.observe(
this, androidx.lifecycle.Observer { states ->
updatePlayGamesAchievements(states)
}
Expand Down Expand Up @@ -590,9 +596,11 @@ class MainActivity : AppCompatActivity() {
val width = displayMetrics.widthPixels
setContent {
SPPTheme {
// A surface container using the 'background' color from the theme
renderScreen(
renderViewModel,
screen(
sppViewModel,
aboutViewModel,
menuPromptViewModel,
mainMenuViewModel,
Pair(width, height),
imageResources,
appInfo,
Expand All @@ -604,7 +612,7 @@ class MainActivity : AppCompatActivity() {

public override fun onStart() {
super.onStart()
renderViewModel.startClock()
sppViewModel.startClock()
}

public override fun onStop()
Expand All @@ -614,6 +622,6 @@ class MainActivity : AppCompatActivity() {
prefsEdit.putLong("playTime", totalTime)
prefsEdit.apply()
super.onStop()
renderViewModel.stopClock()
sppViewModel.stopClock()
}
}
16 changes: 9 additions & 7 deletions app/src/main/java/app/jerboa/spp/composable/about.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -21,19 +23,19 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import app.jerboa.spp.AppInfo
import app.jerboa.spp.R
import app.jerboa.spp.viewmodel.AboutViewModel
import app.jerboa.spp.viewmodel.SOCIAL

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun about(
displayingAbout: Boolean,
aboutViewModel: AboutViewModel,
width75Percent: Double,
images: Map<String,Int>,
info: AppInfo,
onRequestingLicenses: () -> Unit,
onRequestingSocial: (SOCIAL) -> Unit,
onResetTutorial: () -> Unit,
){
val displayingAbout: Boolean by aboutViewModel.displayingAbout.observeAsState(initial = false)

AnimatedVisibility(
visible = displayingAbout,
enter = fadeIn(),
Expand Down Expand Up @@ -74,7 +76,7 @@ fun about(
fontSize = MaterialTheme.typography.body1.fontSize * info.density,
colour = Color.Black
)
TextButton(onClick = { onRequestingLicenses() }) {
TextButton(onClick = { aboutViewModel.onRequestingLicenses() }) {
Text(
stringResource(id = R.string.OSSprompt),
textAlign = TextAlign.Center,
Expand All @@ -96,7 +98,7 @@ fun about(
textAlign = TextAlign.Center,
color = Color.Black
)
TextButton(onClick = { onResetTutorial() }) {
TextButton(onClick = { aboutViewModel.onResetTutorial() }) {
Text(
stringResource(id = R.string.resetTutorial),
textAlign = TextAlign.Center,
Expand All @@ -108,7 +110,7 @@ fun about(
}
}
Spacer(modifier = Modifier.size(8.dp))
socials(images, info, onRequestingSocial)
socials(images, info) { aboutViewModel.onRequestingSocial(it) }
}
}

Expand Down
Loading

0 comments on commit f92a31d

Please sign in to comment.