Skip to content

Commit

Permalink
Merge pull request #459 from lucasnlm/save-as-file
Browse files Browse the repository at this point in the history
Save as file
  • Loading branch information
lucasnlm committed Oct 4, 2023
2 parents 3a305aa + e643734 commit 21ecce8
Show file tree
Hide file tree
Showing 127 changed files with 3,426 additions and 3,452 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,3 @@ Feel free to contribute with [issues](https://github.com/lucasnlm/antimine-andro
- [Lifecycle](https://developer.android.com/topic/libraries/architecture/lifecycle)
- [Coroutines](https://kotlinlang.org/docs/reference/coroutines-overview.html)
- [Koin](https://github.com/InsertKoinIO/koin)
- [Room](https://developer.android.com/training/data-storage/room)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class LicenseViewModel : IntentViewModel<Void, LicenseState>() {
"Koin" to "https://github.com/InsertKoinIO/koin/blob/main/LICENSE",
"LibGDX" to "https://github.com/libgdx/libgdx/blob/master/LICENSE",
"Material Design" to "https://github.com/material-components/material-components-android/",
"Moshi" to "https://github.com/square/moshi/blob/master/LICENSE.txt",
"Mockk" to "https://github.com/mockk/mockk/blob/master/LICENSE",
"Noto Emoji" to "https://github.com/googlefonts/noto-emoji/blob/main/fonts/LICENSE",
"kotlin" to "https://github.com/JetBrains/kotlin/blob/master/license/LICENSE.txt",
Expand Down
9 changes: 3 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {

defaultConfig {
// versionCode and versionName must be hardcoded to support F-droid
versionCode 1704001
versionName '17.4.0'
versionCode 1705001
versionName '17.5.0'
minSdk 21
targetSdk 34
compileSdk 34
Expand Down Expand Up @@ -138,9 +138,6 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.2'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.6.2'

// Room
implementation 'androidx.room:room-ktx:2.5.2'

// Constraint
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

Expand All @@ -157,7 +154,7 @@ dependencies {
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'

// Konfetti
implementation 'nl.dionsegijn:konfetti-xml:2.0.2'
implementation 'nl.dionsegijn:konfetti-xml:2.0.3'

// Tests
testImplementation 'junit:junit:4.13.2'
Expand Down
474 changes: 234 additions & 240 deletions app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion app/src/main/java/dev/lucasnlm/antimine/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.lucasnlm.antimine
import androidx.multidex.MultiDexApplication
import com.badlogic.gdx.utils.GdxNativesLoader
import com.google.android.material.color.DynamicColors
import dev.lucasnlm.antimine.common.io.di.CommonIoModule
import dev.lucasnlm.antimine.common.level.di.LevelModule
import dev.lucasnlm.antimine.core.di.CommonModule
import dev.lucasnlm.antimine.core.models.Analytics
Expand Down Expand Up @@ -37,7 +38,7 @@ open class MainApplication : MultiDexApplication() {
stopKoin()
startKoin {
androidContext(applicationContext)
modules(AppModule, CommonModule, ExternalModule, LevelModule, ViewModelModule)
modules(AppModule, CommonModule, CommonIoModule, ExternalModule, LevelModule, ViewModelModule)
}

appScope.launch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.lucasnlm.antimine.cloud

import dev.lucasnlm.antimine.common.level.database.models.toHashMap
import dev.lucasnlm.antimine.common.level.repository.StatsRepository
import dev.lucasnlm.antimine.core.cloud.CloudSaveManager
import dev.lucasnlm.antimine.preferences.PreferencesRepository
Expand Down Expand Up @@ -29,7 +28,6 @@ class CloudSaveManagerImpl(
private suspend fun getCloudSave(): CloudSave? {
return runCatching {
val prefs = preferencesRepository
val minId = prefs.getStatsBase()
playGamesManager.playerId()?.let { playerId ->
CloudSave(
playId = playerId,
Expand All @@ -44,7 +42,7 @@ class CloudSaveManagerImpl(
hapticFeedbackLevel = prefs.getHapticFeedbackLevel(),
soundEffects = prefs.isSoundEffectsEnabled().toInt(),
music = prefs.isMusicEnabled().toInt(),
stats = statsRepository.getAllStats(minId).map { it.toHashMap() },
stats = statsRepository.getAllStats().map { it.toHashMap() },
premiumFeatures = prefs.isPremiumEnabled().toInt(),
controlStyle = prefs.controlStyle().ordinal,
openDirectly = prefs.openGameDirectly().toInt(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class GameOverDialogFragment : CommonGameDialogFragment() {
dialogViewModel.sendEvent(
EndGameDialogEvent.BuildCustomEndGame(
gameResult =
if (totalMines > 0) {
gameResult
} else {
GameResult.GameOver
},
if (totalMines > 0) {
gameResult
} else {
GameResult.GameOver
},
showContinueButton = showContinueButton,
time = time,
rightMines = rightMines,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class WinGameDialogFragment : CommonGameDialogFragment() {
dialogViewModel.sendEvent(
EndGameDialogEvent.BuildCustomEndGame(
gameResult =
if (totalMines > 0) {
gameResult
} else {
GameResult.GameOver
},
if (totalMines > 0) {
gameResult
} else {
GameResult.GameOver
},
showContinueButton = showContinueButton,
time = time,
rightMines = rightMines,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.lucasnlm.antimine.history.viewmodel
sealed class HistoryEvent {
data object LoadAllSaves : HistoryEvent()

data class ReplaySave(val id: Int) : HistoryEvent()
data class ReplaySave(val id: String) : HistoryEvent()

data class LoadSave(val id: Int) : HistoryEvent()
data class LoadSave(val id: String) : HistoryEvent()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.lucasnlm.antimine.history.viewmodel

import dev.lucasnlm.antimine.common.level.database.models.Save
import dev.lucasnlm.antimine.common.io.models.Save

data class HistoryState(
val loading: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,39 @@ class HistoryViewModel(
when (event) {
is HistoryEvent.LoadAllSaves -> {
emit(state.copy(loading = true))
val newSaveList = savesRepository.getAllSaves().sortedByDescending { it.uid }
emit(state.copy(saveList = newSaveList, loading = false))
val newSaveList = savesRepository.getAllSaves().sortedByDescending { it.startDate }
emit(
state.copy(
saveList = newSaveList,
loading = false,
),
)
}
else -> {
}
}
}

private fun replayGame(uid: Int) {
private fun replayGame(saveId: String) {
audioManager.playClickSound()

val context = application.applicationContext
val intent =
Intent(context, GameActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
putExtra(GameActivity.RETRY_GAME, uid)
putExtra(GameActivity.RETRY_GAME, saveId)
}
context.startActivity(intent)
}

private fun loadGame(uid: Int) {
private fun loadGame(saveId: String) {
audioManager.playClickSound()

val context = application.applicationContext
val intent =
Intent(context, GameActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
putExtra(GameActivity.START_GAME, uid)
putExtra(GameActivity.START_GAME, saveId)
}
context.startActivity(intent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.color.MaterialColors
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.common.level.database.models.Save
import dev.lucasnlm.antimine.common.level.database.models.SaveStatus
import dev.lucasnlm.antimine.common.io.models.Save
import dev.lucasnlm.antimine.common.io.models.SaveStatus
import dev.lucasnlm.antimine.core.models.Difficulty
import dev.lucasnlm.antimine.core.viewmodel.StatelessViewModel
import dev.lucasnlm.antimine.databinding.ViewHistoryItemBinding
Expand All @@ -34,7 +34,7 @@ class HistoryAdapter(
}

override fun getItemId(position: Int): Long {
return saveHistory[position].uid.toLong()
return saveHistory[position].id.hashCode().toLong()
}

override fun getItemCount(): Int {
Expand All @@ -46,10 +46,10 @@ class HistoryAdapter(
position: Int,
) = with(saveHistory[position]) {
val context = holder.itemView.context
val buttonBackgroundColor =
val buttonIconColor =
MaterialColors.getColorStateListOrNull(
context,
GR.attr.colorOnBackground,
GR.attr.colorTertiary,
)?.withAlpha(BUTTON_BACKGROUND_ALPHA)

val difficultyText =
Expand All @@ -66,10 +66,10 @@ class HistoryAdapter(
},
)

val gameNameText = "$difficultyText #$uid"
val saveId = id.orEmpty()

holder.binding.run {
difficulty.text = gameNameText
difficulty.text = difficultyText
badge.alpha = if (status == SaveStatus.VICTORY) BADGE_VICTORY_ALPHA else BADGE_DEFEAT_ALPHA

minefieldSize.text = context.getString(i18n.string.minefield_size, minefield.width, minefield.height)
Expand All @@ -79,23 +79,23 @@ class HistoryAdapter(
if (status != SaveStatus.VICTORY) {
icon = ContextCompat.getDrawable(context, R.drawable.replay)
setOnClickListener {
statelessViewModel.sendEvent(HistoryEvent.ReplaySave(uid))
statelessViewModel.sendEvent(HistoryEvent.ReplaySave(saveId))
}
backgroundTintList = buttonBackgroundColor
backgroundTintList = buttonIconColor
} else {
icon = ContextCompat.getDrawable(context, R.drawable.play)
setOnClickListener {
statelessViewModel.sendEvent(HistoryEvent.ReplaySave(uid))
statelessViewModel.sendEvent(HistoryEvent.ReplaySave(saveId))
}
backgroundTintList = buttonBackgroundColor
backgroundTintList = buttonIconColor
}
}

open.run {
setOnClickListener {
statelessViewModel.sendEvent(HistoryEvent.LoadSave(uid))
statelessViewModel.sendEvent(HistoryEvent.LoadSave(saveId))
}
backgroundTintList = buttonBackgroundColor
backgroundTintList = buttonIconColor
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import androidx.viewpager2.widget.ViewPager2
import dev.lucasnlm.antimine.GameActivity
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.about.AboutActivity
import dev.lucasnlm.antimine.common.level.database.models.SaveStatus
import dev.lucasnlm.antimine.common.io.models.SaveStatus
import dev.lucasnlm.antimine.common.level.repository.MinefieldRepository
import dev.lucasnlm.antimine.common.level.repository.SavesRepository
import dev.lucasnlm.antimine.control.ControlActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.os.Bundle
import android.util.Log
import androidx.lifecycle.viewModelScope
import dev.lucasnlm.antimine.GameActivity
import dev.lucasnlm.antimine.common.level.database.models.Stats
import dev.lucasnlm.antimine.common.io.models.Stats
import dev.lucasnlm.antimine.common.level.repository.StatsRepository
import dev.lucasnlm.antimine.core.models.Difficulty
import dev.lucasnlm.antimine.core.viewmodel.StatelessViewModel
Expand Down Expand Up @@ -100,7 +100,6 @@ class MainViewModel(
cloudSave.stats.mapNotNull {
runCatching {
Stats(
uid = it["uid"]!!.toInt(),
duration = it["duration"]!!.toLong(),
mines = it["mines"]!!.toInt(),
victory = it["victory"]!!.toInt(),
Expand All @@ -109,8 +108,6 @@ class MainViewModel(
openArea = it["openArea"]!!.toInt(),
)
}.getOrNull()
}.distinctBy {
it.uid
}.also {
runCatching {
statsRepository.addAllStats(it)
Expand Down
Loading

0 comments on commit 21ecce8

Please sign in to comment.