WIP Stats

This commit is contained in:
soraefir 2024-04-09 01:43:41 +02:00
parent cd999c2a6e
commit fbeefa0276
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
5 changed files with 129 additions and 80 deletions

View File

@ -87,6 +87,5 @@ dependencies {
implementation 'com.caverock:androidsvg-aar:1.4' implementation 'com.caverock:androidsvg-aar:1.4'
implementation 'com.github.chrisbanes:PhotoView:2.3.0' implementation 'com.github.chrisbanes:PhotoView:2.3.0'
implementation "com.github.AppDevNext:AndroidChart:3.1.0.21"
implementation 'com.mikepenz:aboutlibraries:11.1.1' implementation 'com.mikepenz:aboutlibraries:11.1.1'
} }

View File

@ -1,57 +1,30 @@
package net.helcel.beans.activity package net.helcel.beans.activity
import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.github.mikephil.charting.charts.PieChart import androidx.recyclerview.widget.LinearLayoutManager
import com.github.mikephil.charting.data.PieData import androidx.recyclerview.widget.RecyclerView
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 net.helcel.beans.R import net.helcel.beans.R
import net.helcel.beans.countries.World import net.helcel.beans.activity.adapter.StatsListAdapter
import net.helcel.beans.helper.Data.groups import net.helcel.beans.databinding.ActivityStatBinding
import net.helcel.beans.helper.Data.visits
import net.helcel.beans.helper.Theme.colorWrapper
import net.helcel.beans.helper.Theme.createActionBar import net.helcel.beans.helper.Theme.createActionBar
class StatActivity : AppCompatActivity() { class StatActivity : AppCompatActivity() {
private lateinit var chart: PieChart private lateinit var _binding: ActivityStatBinding
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
_binding = ActivityStatBinding.inflate(layoutInflater)
setContentView(R.layout.activity_stat) setContentView(_binding.root)
createActionBar(this, getString(R.string.action_stat)) createActionBar(this, getString(R.string.action_stat))
chart = findViewById(R.id.chart) _binding.stats.layoutManager =
chart.setUsePercentValues(true) LinearLayoutManager(this, RecyclerView.VERTICAL, false)
chart.description.isEnabled = false _binding.stats.adapter = StatsListAdapter()
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()
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
@ -61,44 +34,5 @@ class StatActivity : AppCompatActivity() {
private fun bind() { private fun bind() {
val entries = ArrayList<PieEntry>()
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()
}
} }

View File

@ -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<StatsListAdapter.StatsViewHolder>() {
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<Int, Groups.Group>
private var countMode: Boolean = true
fun bind(entry: Pair<Int, Groups.Group>) {
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 }}"
}
}
}
}

View File

@ -42,6 +42,10 @@ class Visits(val id: Int, private val locs: HashMap<String, Int>) {
return locs.keys.groupBy { getVisited(it) } return locs.keys.groupBy { getVisited(it) }
} }
fun getVisitedByValue(key: Int): List<String> {
return locs.filter { it.value == key }.keys.toList()
}
fun reassignAllVisitedtoGroup(group: Int) { fun reassignAllVisitedtoGroup(group: Int) {
val keys = locs.filter { (_, grp) -> val keys = locs.filter { (_, grp) ->
grp !in listOf(NO_GROUP, AUTO_GROUP) grp !in listOf(NO_GROUP, AUTO_GROUP)

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout 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" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -7,9 +8,47 @@
android:theme="@style/Theme.Beans" android:theme="@style/Theme.Beans"
tools:context=".activity.StatActivity"> tools:context=".activity.StatActivity">
<com.github.mikephil.charting.charts.PieChart <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/chart"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<com.google.android.material.button.MaterialButton
android:id="@+id/group_color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="2dp"
android:text="Total"
android:textAlignment="textStart"
android:textColor="?attr/colorOnPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="start|center_vertical"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="0 | 0 | 0"
android:textColor="?attr/colorOnPrimary"
app:layout_constraintBottom_toBottomOf="@id/group_color"
app:layout_constraintEnd_toEndOf="@id/group_color"
app:layout_constraintTop_toTopOf="@id/group_color" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/stats"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout> </LinearLayout>