Skip to content

Commit

Permalink
feat: 完成之前的列表删除功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-shop committed May 25, 2023
1 parent de36230 commit d298755
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 100 deletions.
4 changes: 0 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ android {
versionCode = 1
versionName = "1.0"
resourceConfigurations.addAll(listOf("zh", "en"))
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
create("js") {
Expand Down Expand Up @@ -66,7 +65,4 @@ dependencies {
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
implementation("com.google.android.material:material:1.9.0")
implementation("com.squareup.okhttp3:okhttp:4.10.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
121 changes: 98 additions & 23 deletions app/src/main/java/net/imshit/aircraftwar/gui/ScoreboardActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ package net.imshit.aircraftwar.gui
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.ActionMode
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
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.card.MaterialCardView
import net.imshit.aircraftwar.R
import net.imshit.aircraftwar.data.app.AppInfo
import net.imshit.aircraftwar.data.scoreboard.ScoreInfo
import net.imshit.aircraftwar.data.scoreboard.ScoreboardDaoSharedPreferences
import net.imshit.aircraftwar.databinding.ActivityScoreboardBinding
import net.imshit.aircraftwar.logic.game.Difficulty
import java.util.TreeMap

class ScoreboardActivity : AppCompatActivity() {
companion object {
Expand All @@ -27,10 +30,11 @@ class ScoreboardActivity : AppCompatActivity() {
})
}

class ScoreInfoAdapter(private val scoreInfoList: MutableList<ScoreInfo>) :
RecyclerView.Adapter<ScoreInfoAdapter.ScoreInfoViewHolder>() {
class ScoreInfoAdapter(
private val activity: AppCompatActivity,
private val scoreInfoList: MutableList<ScoreInfo>
) : RecyclerView.Adapter<ScoreInfoAdapter.ScoreInfoViewHolder>() {
class ScoreInfoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
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)
Expand All @@ -41,25 +45,101 @@ class ScoreboardActivity : AppCompatActivity() {
}
}

inner class ActionModeCallback : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
mode?.menuInflater?.inflate(R.menu.scoreboard_action, menu)
return true
}

override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true
}

override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
return when (item?.itemId) {
R.id.item_delete -> {
removeSelected()
mode?.finish()
true
}

else -> false
}
}

override fun onDestroyActionMode(mode: ActionMode?) {
onExitActionMode()
}
}

private val callback = ActionModeCallback()
private var isMultiSelect = false
private val selected: MutableMap<Int, MaterialCardView> = TreeMap()
private var actionMode: ActionMode? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ScoreInfoViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.scoreboard_view_item, parent, false)
val scoreInfoViewHolder = ScoreInfoViewHolder(view)
scoreInfoViewHolder.view.setOnLongClickListener {
val position = scoreInfoViewHolder.adapterPosition
scoreInfoList.removeAt(position)
notifyItemRemoved(position)
false
.inflate(R.layout.scoreboard_view_item, parent, false) as MaterialCardView
val holder = ScoreInfoViewHolder(view)
view.setOnLongClickListener {
isMultiSelect = true
updateSelection(holder.adapterPosition, view)
return@setOnLongClickListener true
}
view.setOnClickListener {
if (isMultiSelect) {
updateSelection(holder.adapterPosition, view)
}
}
return holder
}

private fun updateActionMode() {
if (actionMode == null) {
actionMode = activity.startActionMode(callback)
}
if (selected.isEmpty()) {
actionMode?.finish()
} else {
actionMode?.title =
activity.getString(R.string.scoreboard_action_title, selected.size)
}
return scoreInfoViewHolder
}

private fun updateSelection(index: Int, view: MaterialCardView) {
if (view.isChecked) {
selected.remove(index)
view.isChecked = false
} else {
selected[index] = view
view.isChecked = true
}
updateActionMode()
}

override fun getItemCount(): Int = scoreInfoList.size

override fun onBindViewHolder(holder: ScoreInfoViewHolder, position: Int) {
holder.bind(scoreInfoList[position])
}

fun removeSelected() {
selected.keys.sortedDescending().forEach {
scoreInfoList.removeAt(it)
notifyItemRemoved(it)
}
}

fun onExitActionMode() {
selected.values.forEach {
it.isChecked = false
}
selected.clear()
isMultiSelect = false
actionMode = null
}
}

}

private lateinit var dao: ScoreboardDaoSharedPreferences
Expand All @@ -74,33 +154,28 @@ class ScoreboardActivity : AppCompatActivity() {
scoreInfo = getParcelableExtra("scoreInfo", ScoreInfo::class.java)
}
this.dao = ScoreboardDaoSharedPreferences(this, gameMode)
scoreInfo?.let {
this.dao.addItem(scoreInfo)
}
scoreInfo?.let(this.dao::addItem)
// draw
with(ActivityScoreboardBinding.inflate(layoutInflater)) {
setContentView(root)

asTb.setNavigationOnClickListener {
this@ScoreboardActivity.finish()
}

asTb.setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.item_about -> AppInfo.showAboutDialog(this@ScoreboardActivity)

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

asRv.adapter = ScoreInfoAdapter(this@ScoreboardActivity, dao.buffer)

}
}

override fun onDestroy() {
super.onDestroy()
this.dao.close()
super.onDestroy()
}
}
2 changes: 0 additions & 2 deletions app/src/main/java/net/imshit/aircraftwar/logic/game/Games.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.graphics.Paint
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.ViewTreeObserver
Expand Down Expand Up @@ -59,7 +58,6 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) :
game.update()
game.draw()
val elapseTime = System.currentTimeMillis() % REFRESH_INTERVAL
Log.e(">>>", elapseTime.toString())
sleep(REFRESH_INTERVAL - elapseTime)
}
} catch (_: InterruptedException) {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-night/ic_close_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_close_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_scoreboard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:menu="@menu/scoreboard"
app:menu="@menu/main"
app:navigationContentDescription="@string/navback"
app:navigationIcon="@drawable/ic_arrow_back_24"
app:title="@string/scoreboard_label"
Expand Down
104 changes: 47 additions & 57 deletions app/src/main/res/layout/scoreboard_view_item.xml
Original file line number Diff line number Diff line change
@@ -1,67 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
<com.google.android.material.card.MaterialCardView 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_marginHorizontal="20dp"
android:layout_marginVertical="15dp"
app:cardCornerRadius="15dp">

<LinearLayout
style="?attr/materialCardViewFilledStyle"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="8dp"
android:checkable="true"
android:clickable="true"
android:focusable="true">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:paddingHorizontal="10dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
<TextView
android:id="@+id/sbvi_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray"
android:padding="10dp">

<TextView
android:id="@+id/sbvi_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textFontWeight="100"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="NAME" />

<TextView
android:id="@+id/sbvi_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textFontWeight="950"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="SCORE" />

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/sbvi_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:textAlignment="center"
android:textColor="@color/gray"
android:textFontWeight="100"
android:textSize="14sp"
app:layout_constraintTop_toTopOf="parent"
tools:text="TIME" />
</androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>
</androidx.cardview.widget.CardView>
android:fontFamily="sans-serif"
android:textFontWeight="450"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@id/sbvi_time"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:text="哈小深" />

<TextView
android:id="@+id/sbvi_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:textFontWeight="950"
android:textSize="64sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="114514" />

<TextView
android:id="@+id/sbvi_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textFontWeight="100"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="2023-5-25 23:01:49" />
</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,4 @@
android:tooltipText="@string/item_delete_long"
android:visible="true"
app:showAsAction="ifRoom|withText|collapseActionView" />
<item
android:id="@+id/item_about"
android:contentDescription="@null"
android:enabled="true"
android:icon="@drawable/ic_about_24"
android:menuCategory="secondary"
android:orderInCategory="0"
android:title="@string/item_about"
android:titleCondensed="@string/item_about"
android:tooltipText="@string/item_about_long"
android:visible="true"
app:showAsAction="ifRoom|withText|collapseActionView" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
<string name="dialog_login_button_register">Register</string>
<string name="account_label">User Info</string>
<string name="navback">Return</string>
<string name="scoreboard_action_title">%d item(s) selected.</string>
</resources>
Loading

0 comments on commit d298755

Please sign in to comment.