Improved Stats

This commit is contained in:
soraefir 2024-04-09 08:29:53 +02:00
parent fbeefa0276
commit 8b2b694a00
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
4 changed files with 85 additions and 25 deletions

View File

@ -3,28 +3,50 @@ package net.helcel.beans.activity
import android.os.Bundle import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import net.helcel.beans.R import net.helcel.beans.R
import net.helcel.beans.activity.adapter.StatsListAdapter import net.helcel.beans.activity.adapter.StatsListAdapter
import net.helcel.beans.databinding.ActivityStatBinding import net.helcel.beans.databinding.ActivityStatBinding
import net.helcel.beans.helper.Theme.createActionBar import net.helcel.beans.helper.Theme.createActionBar
private val MODE_LIST = listOf("World", "Country", "Region")
class StatActivity : AppCompatActivity() { class StatActivity : AppCompatActivity() {
private lateinit var _binding: ActivityStatBinding private lateinit var _binding: ActivityStatBinding
private var activeMode: String = "World"
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
_binding = ActivityStatBinding.inflate(layoutInflater) _binding = ActivityStatBinding.inflate(layoutInflater)
setContentView(_binding.root) setContentView(_binding.root)
createActionBar(this, getString(R.string.action_stat)) createActionBar(this, getString(R.string.action_stat))
_binding.stats.layoutManager = _binding.stats.layoutManager =
LinearLayoutManager(this, RecyclerView.VERTICAL, false) LinearLayoutManager(this, RecyclerView.VERTICAL, false)
_binding.stats.adapter = StatsListAdapter() val adapter = StatsListAdapter(_binding.stats)
_binding.stats.adapter = adapter
_binding.pager.adapter = object : FragmentStateAdapter(supportFragmentManager, lifecycle) {
override fun getItemCount(): Int = 3
override fun createFragment(position: Int): Fragment = Fragment()
}
TabLayoutMediator(_binding.tab, _binding.pager) { tab, position ->
tab.text = MODE_LIST[position]
}.attach()
_binding.pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
activeMode = MODE_LIST[position]
adapter.refreshMode(activeMode)
}
})
adapter.refreshMode(activeMode)
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
@ -32,7 +54,4 @@ class StatActivity : AppCompatActivity() {
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
private fun bind() {
}
} }

View File

@ -3,13 +3,15 @@ package net.helcel.beans.activity.adapter
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import net.helcel.beans.countries.GeoLoc
import net.helcel.beans.countries.World import net.helcel.beans.countries.World
import net.helcel.beans.databinding.ItemListGroupBinding import net.helcel.beans.databinding.ItemListGroupBinding
import net.helcel.beans.helper.Data import net.helcel.beans.helper.Data
import net.helcel.beans.helper.Groups import net.helcel.beans.helper.Groups
import net.helcel.beans.helper.Theme.getContrastColor import net.helcel.beans.helper.Theme.getContrastColor
class StatsListAdapter : RecyclerView.Adapter<StatsListAdapter.StatsViewHolder>() { class StatsListAdapter(private val stats: RecyclerView) :
RecyclerView.Adapter<StatsListAdapter.StatsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StatsViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StatsViewHolder {
val binding = val binding =
@ -27,12 +29,25 @@ class StatsListAdapter : RecyclerView.Adapter<StatsListAdapter.StatsViewHolder>(
return Data.groups.size() return Data.groups.size()
} }
fun refreshMode(mode: String) {
for (i in 0 until itemCount) {
val viewHolder = stats.findViewHolderForAdapterPosition(i) as? StatsViewHolder
viewHolder?.refresh(mode)
}
}
inner class StatsViewHolder( inner class StatsViewHolder(
private val _binding: ItemListGroupBinding private val _binding: ItemListGroupBinding
) : RecyclerView.ViewHolder(_binding.root) { ) : RecyclerView.ViewHolder(_binding.root) {
private lateinit var data: Pair<Int, Groups.Group> private lateinit var data: Pair<Int, Groups.Group>
private var countMode: Boolean = true private var countMode: Boolean = true
private var locMode: String = "World"
private lateinit var wwwCount: List<GeoLoc>
private lateinit var countryCount: List<GeoLoc>
private lateinit var stateCount: List<GeoLoc>
fun bind(entry: Pair<Int, Groups.Group>) { fun bind(entry: Pair<Int, Groups.Group>) {
data = entry data = entry
_binding.groupColor.text = entry.second.name _binding.groupColor.text = entry.second.name
@ -45,26 +60,39 @@ class StatsListAdapter : RecyclerView.Adapter<StatsListAdapter.StatsViewHolder>(
_binding.groupColor.setOnClickListener { _binding.groupColor.setOnClickListener {
countMode = !countMode countMode = !countMode
refresh() refresh(locMode)
} }
refresh() compute()
refresh(locMode)
} }
private fun refresh() { private fun compute() {
val visited = Data.visits.getVisitedByValue(data.first) val visited = Data.visits.getVisitedByValue(data.first)
val wwwCount = World.WWW.children.filter { it.code in visited } wwwCount = World.WWW.children.filter { it.code in visited }
val countryCount = countryCount =
World.WWW.children.map { it.children.filter { itt -> itt.code in visited } } World.WWW.children.map { it.children.filter { itt -> itt.code in visited } }
.flatten() .flatten()
val stateCount = stateCount =
World.WWW.children.map { it.children.map { itt -> itt.children.filter { ittt -> ittt.code in visited } } } World.WWW.children.map { it.children.map { itt -> itt.children.filter { ittt -> ittt.code in visited } } }
.flatten().flatten() .flatten().flatten()
if (countMode) { }
_binding.name.text = "${wwwCount.size} | ${countryCount.size} | ${stateCount.size}"
} else {
_binding.name.text =
"${wwwCount.sumOf { it.area }} | ${countryCount.sumOf { it.area }} | ${stateCount.sumOf { it.area }}"
fun refresh(mode: String) {
locMode = mode
if (countMode) {
_binding.name.text = when (locMode) {
"World" -> wwwCount.size
"Country" -> countryCount.size
"Region" -> stateCount.size
else -> "?"
}.toString()
} else {
_binding.name.text = when (locMode) {
"World" -> wwwCount.sumOf { it.area }
"Country" -> countryCount.sumOf { it.area }
"Region" -> stateCount.sumOf { it.area }
else -> "?"
}.toString()
} }
} }

View File

@ -8,27 +8,39 @@
android:theme="@style/Theme.Beans" android:theme="@style/Theme.Beans"
tools:context=".activity.StatActivity"> tools:context=".activity.StatActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="gone" />
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"> android:paddingBottom="10dp">
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/group_color" android:id="@+id/group_color"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="2dp" android:layout_marginTop="2dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="2dp" android:layout_marginBottom="2dp"
android:text="Total" android:paddingStart="56dp"
android:text="@string/total"
android:textAlignment="textStart" android:textAlignment="textStart"
android:textColor="?attr/colorOnPrimary" android:textColor="?attr/colorOnPrimary"
app:cornerRadius="0dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlSymmetry" />
<com.google.android.material.textview.MaterialTextView <com.google.android.material.textview.MaterialTextView
android:id="@+id/name" android:id="@+id/name"
@ -36,8 +48,8 @@
android:layout_height="50dp" android:layout_height="50dp"
android:gravity="start|center_vertical" android:gravity="start|center_vertical"
android:paddingStart="20dp" android:paddingStart="20dp"
android:paddingEnd="20dp" android:paddingEnd="52dp"
android:text="0 | 0 | 0" android:text="TODO"
android:textColor="?attr/colorOnPrimary" android:textColor="?attr/colorOnPrimary"
app:layout_constraintBottom_toBottomOf="@id/group_color" app:layout_constraintBottom_toBottomOf="@id/group_color"
app:layout_constraintEnd_toEndOf="@id/group_color" app:layout_constraintEnd_toEndOf="@id/group_color"

View File

@ -35,4 +35,5 @@
<string name="delete">Delete</string> <string name="delete">Delete</string>
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>
<string name="ok">Ok</string> <string name="ok">Ok</string>
<string name="total">Total</string>
</resources> </resources>