Skip to content

Commit

Permalink
feat: 删除成绩
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-shop committed May 12, 2023
1 parent 2430f61 commit 01f00f7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ManageSpaceActivity : AppCompatActivity() {
Difficulty.EASY, Difficulty.MEDIUM, Difficulty.HARD
).forEach { gameMode ->
ScoreboardDaoSharedPreferences(this@ManageSpaceActivity, gameMode).apply {
deleteAll()
clear()
close()
}
}
Expand Down
36 changes: 24 additions & 12 deletions app/src/main/java/net/imshit/aircraftwar/gui/ScoreboardActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import net.imshit.aircraftwar.R
import net.imshit.aircraftwar.databinding.ActivityScoreboardBinding
import net.imshit.aircraftwar.logic.data.Difficulty
import net.imshit.aircraftwar.util.dao.ScoreInfo
import net.imshit.aircraftwar.util.dao.ScoreboardDao
import net.imshit.aircraftwar.util.dao.ScoreboardDaoSharedPreferences

class ScoreboardActivity : AppCompatActivity() {
Expand All @@ -26,23 +27,31 @@ class ScoreboardActivity : AppCompatActivity() {
})
}

class ScoreInfoAdapter(val scoreInfoList: List<ScoreInfo>) :
class ScoreInfoAdapter(val scoreInfoList: MutableList<ScoreInfo>) :
RecyclerView.Adapter<ScoreInfoAdapter.ScoreInfoViewHolder>() {
class ScoreInfoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val scoreInfoNameView: TextView = itemView.findViewById(R.id.sbvi_name)
private val scoreInfoScoreView: TextView = itemView.findViewById(R.id.sbvi_score)
private val scoreInfoTimeView: TextView = itemView.findViewById(R.id.sbvi_time)
val view: CardView = itemView.findViewById(R.id.sbvi_card)
private val nameView: TextView = itemView.findViewById(R.id.sbvi_name)
private val scoreView: TextView = itemView.findViewById(R.id.sbvi_score)
private val timeView: TextView = itemView.findViewById(R.id.sbvi_time)
fun bind(scoreInfo: ScoreInfo) {
scoreInfoNameView.text = scoreInfo.name
scoreInfoScoreView.text = scoreInfo.score.toString()
scoreInfoTimeView.text = scoreInfo.time
this.nameView.text = scoreInfo.name
this.scoreView.text = scoreInfo.score.toString()
this.timeView.text = scoreInfo.time
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ScoreInfoViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.scoreboard_view_item, parent, false)
return ScoreInfoViewHolder(view)
val scoreInfoViewHolder = ScoreInfoViewHolder(view)
scoreInfoViewHolder.view.setOnLongClickListener {
val position = scoreInfoViewHolder.adapterPosition
scoreInfoList.removeAt(position)
notifyItemRemoved(position)
false
}
return scoreInfoViewHolder
}

override fun getItemCount(): Int = scoreInfoList.size
Expand All @@ -53,7 +62,7 @@ class ScoreboardActivity : AppCompatActivity() {
}
}

private lateinit var dao: ScoreboardDao
private lateinit var dao: ScoreboardDaoSharedPreferences

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -78,11 +87,14 @@ class ScoreboardActivity : AppCompatActivity() {
R.string.item_about_long
).setIcon(R.drawable.ic_about_24).setMessage(R.string.app_about)
.setPositiveButton(android.R.string.ok) { _, _ -> }.show()

R.id.item_delete -> Toast.makeText(
this@ScoreboardActivity, "长按删除", Toast.LENGTH_SHORT
).show()// TODO
}
return@setOnMenuItemClickListener true
}

asRv.adapter = ScoreInfoAdapter(dao.getTopK())
asRv.adapter = ScoreInfoAdapter(dao.buffer)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sealed interface ScoreboardDao : Closeable {
*/
fun deleteItem(indices: IntArray)

fun deleteAll()
fun clear()

/**
* 关闭计分板数据模型(通常来说意味着缓存写回)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ScoreboardDaoSharedPreferences(context: Context, gameMode: Difficulty) : S
private val sharedPreferences =
context.getSharedPreferences("record-${gameMode.name}", Context.MODE_PRIVATE)

private val buffer = mutableListOf<ScoreInfo>()
val buffer = mutableListOf<ScoreInfo>()

init {
val size = this.sharedPreferences.getInt("size", 0)
Expand Down Expand Up @@ -42,7 +42,7 @@ class ScoreboardDaoSharedPreferences(context: Context, gameMode: Difficulty) : S
indices.sortedDescending().forEach(this.buffer::removeAt)
}

override fun deleteAll() {
override fun clear() {
this.buffer.clear()
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/layout/scoreboard_view_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sbvi_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="15dp"

>
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="15dp"
app:cardCornerRadius="15dp">

<LinearLayout
android:layout_width="match_parent"
Expand Down

0 comments on commit 01f00f7

Please sign in to comment.