diff --git a/app/src/main/java/net/helcel/beans/activity/StatActivity.kt b/app/src/main/java/net/helcel/beans/activity/StatActivity.kt index 82ee38a..827b3a2 100644 --- a/app/src/main/java/net/helcel/beans/activity/StatActivity.kt +++ b/app/src/main/java/net/helcel/beans/activity/StatActivity.kt @@ -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() { - } } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beans/activity/adapter/StatsListAdapter.kt b/app/src/main/java/net/helcel/beans/activity/adapter/StatsListAdapter.kt index 53fb960..3687dd5 100644 --- a/app/src/main/java/net/helcel/beans/activity/adapter/StatsListAdapter.kt +++ b/app/src/main/java/net/helcel/beans/activity/adapter/StatsListAdapter.kt @@ -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() { +class StatsListAdapter(private val stats: RecyclerView) : + RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StatsViewHolder { val binding = @@ -27,12 +29,25 @@ class StatsListAdapter : RecyclerView.Adapter( 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 private var countMode: Boolean = true + private var locMode: String = "World" + + private lateinit var wwwCount: List + private lateinit var countryCount: List + private lateinit var stateCount: List fun bind(entry: Pair) { data = entry _binding.groupColor.text = entry.second.name @@ -45,26 +60,39 @@ class StatsListAdapter : RecyclerView.Adapter( _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() } } diff --git a/app/src/main/res/layout/activity_stat.xml b/app/src/main/res/layout/activity_stat.xml index cca2830..aabba15 100644 --- a/app/src/main/res/layout/activity_stat.xml +++ b/app/src/main/res/layout/activity_stat.xml @@ -8,27 +8,39 @@ android:theme="@style/Theme.Beans" tools:context=".activity.StatActivity"> + + + + + app:layout_constraintTop_toTopOf="parent" + tools:ignore="RtlSymmetry" /> Delete Cancel Ok + Total \ No newline at end of file