Improved Stats
This commit is contained in:
@@ -3,28 +3,50 @@ package net.helcel.beans.activity
|
||||
import android.os.Bundle
|
||||
import android.view.MenuItem
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
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.activity.adapter.StatsListAdapter
|
||||
import net.helcel.beans.databinding.ActivityStatBinding
|
||||
import net.helcel.beans.helper.Theme.createActionBar
|
||||
|
||||
private val MODE_LIST = listOf("World", "Country", "Region")
|
||||
|
||||
class StatActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var _binding: ActivityStatBinding
|
||||
private var activeMode: String = "World"
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
_binding = ActivityStatBinding.inflate(layoutInflater)
|
||||
setContentView(_binding.root)
|
||||
createActionBar(this, getString(R.string.action_stat))
|
||||
|
||||
|
||||
|
||||
_binding.stats.layoutManager =
|
||||
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 {
|
||||
@@ -32,7 +54,4 @@ class StatActivity : AppCompatActivity() {
|
||||
return super.onOptionsItemSelected(item)
|
||||
}
|
||||
|
||||
|
||||
private fun bind() {
|
||||
}
|
||||
}
|
@@ -3,13 +3,15 @@ package net.helcel.beans.activity.adapter
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import net.helcel.beans.countries.GeoLoc
|
||||
import net.helcel.beans.countries.World
|
||||
import net.helcel.beans.databinding.ItemListGroupBinding
|
||||
import net.helcel.beans.helper.Data
|
||||
import net.helcel.beans.helper.Groups
|
||||
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 {
|
||||
val binding =
|
||||
@@ -27,12 +29,25 @@ class StatsListAdapter : RecyclerView.Adapter<StatsListAdapter.StatsViewHolder>(
|
||||
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(
|
||||
private val _binding: ItemListGroupBinding
|
||||
) : RecyclerView.ViewHolder(_binding.root) {
|
||||
|
||||
private lateinit var data: Pair<Int, Groups.Group>
|
||||
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>) {
|
||||
data = entry
|
||||
_binding.groupColor.text = entry.second.name
|
||||
@@ -45,26 +60,39 @@ class StatsListAdapter : RecyclerView.Adapter<StatsListAdapter.StatsViewHolder>(
|
||||
|
||||
_binding.groupColor.setOnClickListener {
|
||||
countMode = !countMode
|
||||
refresh()
|
||||
refresh(locMode)
|
||||
}
|
||||
refresh()
|
||||
compute()
|
||||
refresh(locMode)
|
||||
}
|
||||
|
||||
private fun refresh() {
|
||||
private fun compute() {
|
||||
val visited = Data.visits.getVisitedByValue(data.first)
|
||||
val wwwCount = World.WWW.children.filter { it.code in visited }
|
||||
val countryCount =
|
||||
wwwCount = World.WWW.children.filter { it.code in visited }
|
||||
countryCount =
|
||||
World.WWW.children.map { it.children.filter { itt -> itt.code in visited } }
|
||||
.flatten()
|
||||
val stateCount =
|
||||
stateCount =
|
||||
World.WWW.children.map { it.children.map { itt -> itt.children.filter { ittt -> ittt.code in visited } } }
|
||||
.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()
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user