diff --git a/app/src/main/java/net/imshit/aircraftwar/gui/ManageSpaceActivity.kt b/app/src/main/java/net/imshit/aircraftwar/gui/ManageSpaceActivity.kt index 4424aa7..9e5a851 100644 --- a/app/src/main/java/net/imshit/aircraftwar/gui/ManageSpaceActivity.kt +++ b/app/src/main/java/net/imshit/aircraftwar/gui/ManageSpaceActivity.kt @@ -31,7 +31,7 @@ class ManageSpaceActivity : AppCompatActivity() { Difficulty.EASY, Difficulty.MEDIUM, Difficulty.HARD ).forEach { gameMode -> ScoreboardDaoSharedPreferences(this@ManageSpaceActivity, gameMode).apply { - deleteAll() + clear() close() } } diff --git a/app/src/main/java/net/imshit/aircraftwar/gui/ScoreboardActivity.kt b/app/src/main/java/net/imshit/aircraftwar/gui/ScoreboardActivity.kt index 8736bcf..3661492 100644 --- a/app/src/main/java/net/imshit/aircraftwar/gui/ScoreboardActivity.kt +++ b/app/src/main/java/net/imshit/aircraftwar/gui/ScoreboardActivity.kt @@ -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() { @@ -26,23 +27,31 @@ class ScoreboardActivity : AppCompatActivity() { }) } - class ScoreInfoAdapter(val scoreInfoList: List) : + class ScoreInfoAdapter(val scoreInfoList: MutableList) : RecyclerView.Adapter() { 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 @@ -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) @@ -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) } } diff --git a/app/src/main/java/net/imshit/aircraftwar/util/dao/ScoreboardDao.kt b/app/src/main/java/net/imshit/aircraftwar/util/dao/ScoreboardDao.kt index c69ae0f..4b8db29 100644 --- a/app/src/main/java/net/imshit/aircraftwar/util/dao/ScoreboardDao.kt +++ b/app/src/main/java/net/imshit/aircraftwar/util/dao/ScoreboardDao.kt @@ -32,7 +32,7 @@ sealed interface ScoreboardDao : Closeable { */ fun deleteItem(indices: IntArray) - fun deleteAll() + fun clear() /** * 关闭计分板数据模型(通常来说意味着缓存写回) diff --git a/app/src/main/java/net/imshit/aircraftwar/util/dao/ScoreboardDaoSharedPreferences.kt b/app/src/main/java/net/imshit/aircraftwar/util/dao/ScoreboardDaoSharedPreferences.kt index 0dccc8e..9e6d4a7 100644 --- a/app/src/main/java/net/imshit/aircraftwar/util/dao/ScoreboardDaoSharedPreferences.kt +++ b/app/src/main/java/net/imshit/aircraftwar/util/dao/ScoreboardDaoSharedPreferences.kt @@ -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() + val buffer = mutableListOf() init { val size = this.sharedPreferences.getInt("size", 0) @@ -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() } diff --git a/app/src/main/res/layout/scoreboard_view_item.xml b/app/src/main/res/layout/scoreboard_view_item.xml index e24a7ee..e0f99ba 100644 --- a/app/src/main/res/layout/scoreboard_view_item.xml +++ b/app/src/main/res/layout/scoreboard_view_item.xml @@ -2,12 +2,12 @@ + android:layout_marginHorizontal="20dp" + android:layout_marginVertical="15dp" + app:cardCornerRadius="15dp">