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 ab88443 commit b6c4651
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,91 @@ package net.imshit.aircraftwar.gui
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
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() {
companion object {
fun actionStart(context: Context, gameMode: Difficulty) {
fun actionStart(context: Context, gameMode: Difficulty, scoreInfo: ScoreInfo?) {
context.startActivity(Intent(context, ScoreboardActivity::class.java).apply {
putExtra("gameMode", gameMode)
putExtra("scoreInfo", scoreInfo)
})
}

class ScoreInfoAdapter(val scoreInfoList: List<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)
fun bind(scoreInfo: ScoreInfo) {
scoreInfoNameView.text = scoreInfo.name
scoreInfoScoreView.text = scoreInfo.score.toString()
scoreInfoTimeView.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)
}

override fun getItemCount(): Int = scoreInfoList.size

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

private lateinit var dao: ScoreboardDao

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// read info
val gameMode: Difficulty
val scoreInfo: ScoreInfo?
intent.apply {
gameMode = getSerializableExtra("gameMode", Difficulty::class.java) ?: Difficulty.EASY
scoreInfo = getParcelableExtra("scoreInfo", ScoreInfo::class.java)
}
this.dao = ScoreboardDaoSharedPreferences(this, gameMode)
scoreInfo?.let {
this.dao.addItem(scoreInfo)
}
// draw
with(ActivityScoreboardBinding.inflate(layoutInflater)) {
setContentView(root)

asTb.setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.item_about -> MaterialAlertDialogBuilder(this@ScoreboardActivity).setTitle(
R.string.item_about_long
).setIcon(R.drawable.ic_about_24).setMessage(R.string.app_about)
.setPositiveButton(android.R.string.ok) { _, _ -> }.show()
}
return@setOnMenuItemClickListener true
}

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

override fun onDestroy() {
super.onDestroy()
this.dao.close()
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-night/ic_delete_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="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_delete_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="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z" />
</vector>
31 changes: 27 additions & 4 deletions app/src/main/res/layout/activity_scoreboard.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".gui.GameActivity">
android:fitsSystemWindows="true">

</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/as_abl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/as_tb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:menu="@menu/scoreboard"
app:title="@string/scoreboard_label"
app:titleCentered="true" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/as_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
67 changes: 67 additions & 0 deletions app/src/main/res/layout/scoreboard_view_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="15dp"

>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
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>
28 changes: 28 additions & 0 deletions app/src/main/res/menu/scoreboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item_delete"
android:contentDescription="@null"
android:enabled="true"
android:icon="@drawable/ic_delete_24"
android:menuCategory="secondary"
android:orderInCategory="0"
android:title="@string/item_delete"
android:titleCondensed="@string/item_delete"
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>

0 comments on commit b6c4651

Please sign in to comment.