From fbeefa0276003314efdca97c60aa6b20cd7732b8 Mon Sep 17 00:00:00 2001 From: soraefir Date: Tue, 9 Apr 2024 01:43:41 +0200 Subject: [PATCH] WIP Stats --- app/build.gradle | 1 - .../net/helcel/beans/activity/StatActivity.kt | 86 +++---------------- .../activity/adapter/StatsListAdapter.kt | 73 ++++++++++++++++ .../java/net/helcel/beans/helper/Visits.kt | 4 + app/src/main/res/layout/activity_stat.xml | 45 +++++++++- 5 files changed, 129 insertions(+), 80 deletions(-) create mode 100644 app/src/main/java/net/helcel/beans/activity/adapter/StatsListAdapter.kt diff --git a/app/build.gradle b/app/build.gradle index 11f0397..a84892c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -87,6 +87,5 @@ dependencies { implementation 'com.caverock:androidsvg-aar:1.4' implementation 'com.github.chrisbanes:PhotoView:2.3.0' - implementation "com.github.AppDevNext:AndroidChart:3.1.0.21" implementation 'com.mikepenz:aboutlibraries:11.1.1' } \ No newline at end of file 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 7e5b90d..82ee38a 100644 --- a/app/src/main/java/net/helcel/beans/activity/StatActivity.kt +++ b/app/src/main/java/net/helcel/beans/activity/StatActivity.kt @@ -1,57 +1,30 @@ package net.helcel.beans.activity -import android.graphics.Color import android.os.Bundle -import android.util.Log import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity -import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry -import com.github.mikephil.charting.formatter.PercentFormatter -import com.github.mikephil.charting.utils.MPPointF +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import net.helcel.beans.R -import net.helcel.beans.countries.World -import net.helcel.beans.helper.Data.groups -import net.helcel.beans.helper.Data.visits -import net.helcel.beans.helper.Theme.colorWrapper +import net.helcel.beans.activity.adapter.StatsListAdapter +import net.helcel.beans.databinding.ActivityStatBinding import net.helcel.beans.helper.Theme.createActionBar class StatActivity : AppCompatActivity() { - private lateinit var chart: PieChart + private lateinit var _binding: ActivityStatBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - - setContentView(R.layout.activity_stat) + _binding = ActivityStatBinding.inflate(layoutInflater) + setContentView(_binding.root) createActionBar(this, getString(R.string.action_stat)) - chart = findViewById(R.id.chart) - chart.setUsePercentValues(true) - chart.description.isEnabled = false - chart.setExtraOffsets(5F, 10F, 5F, 5F) - - chart.centerText = "Country Area" - - chart.setDrawCenterText(true) - chart.isDrawHoleEnabled = true - chart.setTransparentCircleColor(Color.TRANSPARENT) - chart.setHoleColor(Color.TRANSPARENT) - chart.setCenterTextColor(colorWrapper(this, android.R.attr.colorForeground).color) - chart.setTransparentCircleAlpha(0) - chart.holeRadius = 40F - chart.transparentCircleRadius = 40F - chart.rotationAngle = 0F - chart.isRotationEnabled = false - chart.isHighlightPerTapEnabled = false - - chart.legend.isEnabled = false - - bind() + _binding.stats.layoutManager = + LinearLayoutManager(this, RecyclerView.VERTICAL, false) + _binding.stats.adapter = StatsListAdapter() } override fun onOptionsItemSelected(item: MenuItem): Boolean { @@ -61,44 +34,5 @@ class StatActivity : AppCompatActivity() { private fun bind() { - val entries = ArrayList() - val VIS_continents = World.WWW.children.groupBy { visits.getVisited(it) } - .map { Pair(it.key, it.value.map { c -> c.area }.fold(0) { acc, i -> acc + i }) } - val VIS_country = - World.WWW.children.map { it.children }.flatten().groupBy { visits.getVisited(it) } - .map { Pair(it.key, it.value.map { c -> c.area }.fold(0) { acc, i -> acc + i }) } - val vis = VIS_country - Log.d("VIS", vis.toString()) - val max = vis.map { it.second }.fold(0) { acc, i -> acc + i } - vis.forEach { - entries.add( - PieEntry( - it.second.toFloat().div(max.toFloat()), - groups.getGroupFromKey(it.first).name - ) - ) - } - val dataSet = PieDataSet(entries, "GG1") - - dataSet.valueTextColor = Color.BLACK - dataSet.sliceSpace = 3f - dataSet.iconsOffset = MPPointF(0f, 40f) - dataSet.selectionShift = 5f - - - dataSet.setDrawIcons(true) - dataSet.colors = vis.map { groups.getGroupFromKey(it.first).color.color }.toList() - - val data = PieData(dataSet) - data.setDrawValues(false) - data.setValueFormatter(PercentFormatter()) - data.setValueTextSize(11f) - data.setValueTextColor(Color.BLACK) - chart.setEntryLabelColor(Color.BLACK) - chart.data = data - chart.highlightValues(null) - chart.invalidate() } - - } \ 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 new file mode 100644 index 0000000..53fb960 --- /dev/null +++ b/app/src/main/java/net/helcel/beans/activity/adapter/StatsListAdapter.kt @@ -0,0 +1,73 @@ +package net.helcel.beans.activity.adapter + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +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() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StatsViewHolder { + val binding = + ItemListGroupBinding.inflate(LayoutInflater.from(parent.context), parent, false) + + + return StatsViewHolder(binding) + } + + override fun onBindViewHolder(holder: StatsViewHolder, pos: Int) { + holder.bind(Data.groups.getGroupFromPos(pos)) + } + + override fun getItemCount(): Int { + return Data.groups.size() + } + + inner class StatsViewHolder( + private val _binding: ItemListGroupBinding + ) : RecyclerView.ViewHolder(_binding.root) { + + private lateinit var data: Pair + private var countMode: Boolean = true + fun bind(entry: Pair) { + data = entry + _binding.groupColor.text = entry.second.name + + val entryColor = data.second.color.color + val contrastEntryColor = getContrastColor(entryColor) + _binding.groupColor.setBackgroundColor(entryColor) + _binding.groupColor.setTextColor(contrastEntryColor) + _binding.name.setTextColor(contrastEntryColor) + + _binding.groupColor.setOnClickListener { + countMode = !countMode + refresh() + } + refresh() + } + + private fun refresh() { + val visited = Data.visits.getVisitedByValue(data.first) + val wwwCount = World.WWW.children.filter { it.code in visited } + val countryCount = + World.WWW.children.map { it.children.filter { itt -> itt.code in visited } } + .flatten() + val 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 }}" + + } + + } + + } +} \ No newline at end of file diff --git a/app/src/main/java/net/helcel/beans/helper/Visits.kt b/app/src/main/java/net/helcel/beans/helper/Visits.kt index 71c36f1..683b55c 100644 --- a/app/src/main/java/net/helcel/beans/helper/Visits.kt +++ b/app/src/main/java/net/helcel/beans/helper/Visits.kt @@ -42,6 +42,10 @@ class Visits(val id: Int, private val locs: HashMap) { return locs.keys.groupBy { getVisited(it) } } + fun getVisitedByValue(key: Int): List { + return locs.filter { it.value == key }.keys.toList() + } + fun reassignAllVisitedtoGroup(group: Int) { val keys = locs.filter { (_, grp) -> grp !in listOf(NO_GROUP, AUTO_GROUP) diff --git a/app/src/main/res/layout/activity_stat.xml b/app/src/main/res/layout/activity_stat.xml index f529571..cca2830 100644 --- a/app/src/main/res/layout/activity_stat.xml +++ b/app/src/main/res/layout/activity_stat.xml @@ -1,5 +1,6 @@ - + android:layout_height="wrap_content" + android:paddingTop="10dp" + android:paddingBottom="10dp"> + + + + + + + + + \ No newline at end of file