Use sequence on viewmodel instead of list

This commit is contained in:
Lucas Lima 2020-03-28 01:06:12 -03:00
parent 78a7d9384b
commit 77c24f9017
No known key found for this signature in database
GPG key ID: C5EEF4C30BFBF8D7
5 changed files with 16 additions and 13 deletions

View file

@ -12,9 +12,10 @@
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" />
<uses-permission
android:name="android.permission.VIBRATE" />
<uses-permission
android:name="android.permission.WAKE_LOCK" />
<uses-feature
android:name="android.hardware.touchscreen"
@ -57,6 +58,8 @@
<activity
android:name="dev.lucasnlm.antimine.GameActivity"
android:saveEnabled="false"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:configChanges="screenSize|orientation"
android:theme="@style/AppTheme.NoActionBar">

View file

@ -29,7 +29,7 @@ class ShareBuilder(
) {
private val context: Context = context.applicationContext
suspend fun share(minefield: Minefield, field: List<Area>, spentTime: Long?): Boolean {
suspend fun share(minefield: Minefield, field: Sequence<Area>, spentTime: Long?): Boolean {
val rightMines = field.count { it.hasMine && it.mark == Mark.Flag }
val totalMines = field.count { it.hasMine }
@ -42,7 +42,7 @@ class ShareBuilder(
}
}
private suspend fun createImage(minefield: Minefield, field: List<Area>): File? = withContext(Dispatchers.IO) {
private suspend fun createImage(minefield: Minefield, field: Sequence<Area>): File? = withContext(Dispatchers.IO) {
val size = 38f
val padding = 1f
val radius = 2f
@ -75,7 +75,7 @@ class ShareBuilder(
for (x in 0 until minefield.width) {
for (y in 0 until minefield.height) {
val area = field[x + y * minefield.width]
val area = field.first { it.id == (x + y * minefield.width) }
canvas.save()
canvas.translate(x * size + padding, y * size + padding)
area.paintOnCanvas(

View file

@ -13,8 +13,8 @@ class ShareViewModel(
) : AndroidViewModel(application) {
private val context = getApplication<Application>().applicationContext
suspend fun share(minefield: Minefield?, field: List<Area>?, spentTime: Long?) {
val result = if (minefield != null && field != null && field.isNotEmpty()) {
suspend fun share(minefield: Minefield?, field: Sequence<Area>?, spentTime: Long?) {
val result = if (minefield != null && field != null && field.count() != 0) {
ShareBuilder(context).share(minefield, field, spentTime)
} else {
false

View file

@ -40,8 +40,8 @@ class AreaAdapter(
this.clickEnabled = value
}
fun bindField(area: List<Area>) {
this.field = area
fun bindField(field: Sequence<Area>) {
this.field = field.toList()
notifyDataSetChanged()
}

View file

@ -39,7 +39,7 @@ class GameViewModel(
private var currentDifficulty: Difficulty = Difficulty.Standard
private var initialized = false
val field = MutableLiveData<List<Area>>()
val field = MutableLiveData<Sequence<Area>>()
val fieldRefresh = MutableLiveData<Int>()
val elapsedTimeSeconds = MutableLiveData<Long>()
val mineCount = MutableLiveData<Int>()
@ -274,13 +274,13 @@ class GameViewModel(
private fun refreshIndex(targetIndex: Int, changes: Int = 1) {
if (changes > 1) {
field.postValue(levelFacade.field.toList())
field.postValue(levelFacade.field)
} else {
fieldRefresh.postValue(targetIndex)
}
}
private fun refreshAll() {
field.postValue(levelFacade.field.toList())
field.postValue(levelFacade.field)
}
}