Clean up, parallel compute,...

This commit is contained in:
soraefir 2024-01-20 01:19:19 +01:00
parent d4f67804a2
commit 84a8a1fe13
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
12 changed files with 48 additions and 61 deletions

View File

@ -27,7 +27,6 @@
android:exported="true" > android:exported="true" >
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>

View File

@ -5,7 +5,6 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import net.helcel.beendroid.R
import net.helcel.beendroid.databinding.FragmentAboutBinding import net.helcel.beendroid.databinding.FragmentAboutBinding
class AboutFragment: Fragment() { class AboutFragment: Fragment() {

View File

@ -4,17 +4,13 @@ import android.content.Context
import android.graphics.Color import android.graphics.Color
import android.graphics.Typeface import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.button.MaterialButton
import com.google.android.material.checkbox.MaterialCheckBox import com.google.android.material.checkbox.MaterialCheckBox
import net.helcel.beendroid.R import net.helcel.beendroid.R
import net.helcel.beendroid.countries.GeoLoc import net.helcel.beendroid.countries.GeoLoc
@ -43,10 +39,7 @@ class FoldingListAdapter(
override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) { override fun onBindViewHolder(holder: FoldingListViewHolder, position: Int) {
val el = cg.toList()[position] val el = cg.toList()[position]
holder.bind(el) { holder.bind(el) { parentLambda() }
notifyItemChanged(position)
parentLambda()
}
holder.addListeners( { holder.addListeners( {
if (!el.first.isEnd) { if (!el.first.isEnd) {
@ -67,25 +60,16 @@ class FoldingListAdapter(
class FoldingListViewHolder(private val ctx: Context, itemView: View, class FoldingListViewHolder(private val ctx: Context, itemView: View,
private val visited: Visited, private val visited: Visited,
) : RecyclerView.ViewHolder(itemView) { ) : RecyclerView.ViewHolder(itemView) {
private val textView: TextView private val textView: TextView = itemView.findViewById(R.id.textView)
//private val expand: MaterialButton private val checkBox: MaterialCheckBox = itemView.findViewById(R.id.checkBox)
private val checkBox: MaterialCheckBox private val subItemView: View = itemView.findViewById(R.id.sub_item)
private val subItemView: View private val list: RecyclerView = itemView.findViewById(R.id.list_list)
private val list: RecyclerView
init { init {
textView = itemView.findViewById(R.id.textView)
//expand = itemView.findViewById(R.id.expand)
checkBox = itemView.findViewById(R.id.checkBox)
subItemView = itemView.findViewById(R.id.sub_item)
list = itemView.findViewById(R.id.list_list)
list.layoutManager = LinearLayoutManager(ctx, RecyclerView.VERTICAL, false) list.layoutManager = LinearLayoutManager(ctx, RecyclerView.VERTICAL, false)
} }
fun bind(el: Pair<GeoLoc, Boolean>, parentLambda: () -> Unit) { fun bind(el: Pair<GeoLoc, Boolean>, parentLambda: () -> Unit) {
//expand.rotation = if(el.second) 90f else 0f
subItemView.visibility = if (el.second) View.VISIBLE else View.GONE subItemView.visibility = if (el.second) View.VISIBLE else View.GONE
//expand.visibility = if(!el.first.isEnd) View.VISIBLE else View.INVISIBLE
textView.text = el.first.fullName textView.text = el.first.fullName
if (el.first.type == LocType.GROUP) { if (el.first.type == LocType.GROUP) {
@ -95,15 +79,9 @@ class FoldingListAdapter(
ctx.theme.resolveAttribute(android.R.attr.panelColorBackground, colorGrayTyped, true) ctx.theme.resolveAttribute(android.R.attr.panelColorBackground, colorGrayTyped, true)
val color = Color.valueOf(colorGrayTyped.data) val color = Color.valueOf(colorGrayTyped.data)
textView.setBackgroundColor(Color.valueOf(color.red(), color.green(), color.blue(), 0.5f).toArgb()) textView.setBackgroundColor(Color.valueOf(color.red(), color.green(), color.blue(), 0.5f).toArgb())
checkBox.visibility = View.INVISIBLE list.adapter = FoldingListAdapter(ctx, el.first.children,visited, parentLambda)
textView.parent.parent.requestChildFocus(textView,textView)
el.first.children.apply {
val nbCountries = this.size
val nbVisited = this.map { if (visited.visited(it)) 1 else 0 }.reduce{ a, b -> (a + b) }
val ratio = nbVisited.toFloat() / nbCountries.toFloat()
val percentage = (ratio * 100).toInt()
textView.text = "${textView.text as String} (${percentage}%)"
}
} else { } else {
val colorBackgroundTyped = TypedValue() val colorBackgroundTyped = TypedValue()
ctx.theme.resolveAttribute(android.R.attr.colorBackground, colorBackgroundTyped, true) ctx.theme.resolveAttribute(android.R.attr.colorBackground, colorBackgroundTyped, true)
@ -114,7 +92,6 @@ class FoldingListAdapter(
val layoutParam = checkBox.layoutParams val layoutParam = checkBox.layoutParams
layoutParam.width = 125 layoutParam.width = 125
checkBox.layoutParams = layoutParam checkBox.layoutParams = layoutParam
checkBox.visibility = View.VISIBLE checkBox.visibility = View.VISIBLE
} }
checkBox.checkedState = checkBox.checkedState =
@ -122,19 +99,13 @@ class FoldingListAdapter(
else if (el.first.children.any { visited.visited(it) }) MaterialCheckBox.STATE_INDETERMINATE else if (el.first.children.any { visited.visited(it) }) MaterialCheckBox.STATE_INDETERMINATE
else MaterialCheckBox.STATE_UNCHECKED else MaterialCheckBox.STATE_UNCHECKED
textView.parent.parent.requestChildFocus(textView,textView)
list.adapter = FoldingListAdapter(ctx, el.first.children,visited, parentLambda)
} }
fun addListeners(expandLambda: ()->Boolean, visitedLambda: (Boolean)->Unit) { fun addListeners(expandLambda: ()->Boolean, visitedLambda: (Boolean)->Unit) {
textView.setOnClickListener { expandLambda() } textView.setOnClickListener { expandLambda() }
checkBox.addOnCheckedStateChangedListener { _, e -> checkBox.addOnCheckedStateChangedListener { _, e ->
visitedLambda(e == MaterialCheckBox.STATE_CHECKED) visitedLambda(e == MaterialCheckBox.STATE_CHECKED)
} }
//textView.setOnLongClickListener{ checkBox.toggle(); true }
//expand.setOnClickListener { expandLambda() }
} }
} }

View File

@ -2,13 +2,9 @@ package net.helcel.beendroid.activity
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.ActionBar
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.mikepenz.aboutlibraries.Libs
import net.helcel.beendroid.R import net.helcel.beendroid.R
import net.helcel.beendroid.databinding.FragmentLicenseBinding import net.helcel.beendroid.databinding.FragmentLicenseBinding
import com.mikepenz.aboutlibraries.LibsBuilder import com.mikepenz.aboutlibraries.LibsBuilder

View File

@ -1,13 +1,12 @@
package net.helcel.beendroid.activity package net.helcel.beendroid.activity
import kotlinx.coroutines.*
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
@ -37,6 +36,8 @@ class MainActivity : AppCompatActivity() {
private lateinit var psvg : PSVGWrapper private lateinit var psvg : PSVGWrapper
private lateinit var css : CSSWrapper private lateinit var css : CSSWrapper
private var processor: ImageProcessor = ImageProcessor({ refreshMapCompute() },{ refreshMapDisplay(it) })
private val bitmap: Bitmap = Bitmap.createBitmap(1200,900, Bitmap.Config.ARGB_8888) private val bitmap: Bitmap = Bitmap.createBitmap(1200,900, Bitmap.Config.ARGB_8888)
private val canvas = Canvas(bitmap) private val canvas = Canvas(bitmap)
@ -64,6 +65,10 @@ class MainActivity : AppCompatActivity() {
// TODO: Enable editing selected countries // TODO: Enable editing selected countries
true true
} }
R.id.action_stats -> {
// TODO: Write stats activity
true
}
R.id.action_settings -> { R.id.action_settings -> {
// Open settings // Open settings
startActivity(Intent(this@MainActivity, SettingsActivity::class.java)) startActivity(Intent(this@MainActivity, SettingsActivity::class.java))
@ -89,24 +94,45 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
map = findViewById(R.id.map) map = findViewById(R.id.map)
map.setImageBitmap(bitmap) map.setImageBitmap(bitmap)
refreshMap() refreshMapDisplay(refreshMapCompute())
// Populate list below the map // Populate list below the map
list = findViewById(R.id.list) list = findViewById(R.id.list)
list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) list.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
list.adapter = FoldingListAdapter(this, World.WWW.children, visited) { refreshMap() } list.adapter = FoldingListAdapter(this, World.WWW.children, visited) { processor.process() }
} }
private fun refreshMap(){ private fun refreshMapDisplay(css_value: String){
// Set or reset background (replaces canvas.drawColor(0, 0, 0)) // Set or reset background (replaces canvas.drawColor(0, 0, 0))
val colorBackgroundTyped = TypedValue() val colorBackgroundTyped = TypedValue()
theme.resolveAttribute(android.R.attr.colorBackground, colorBackgroundTyped, true) theme.resolveAttribute(android.R.attr.colorBackground, colorBackgroundTyped, true)
canvas.drawColor(colorBackgroundTyped.data) canvas.drawColor(colorBackgroundTyped.data)
// Render all countries and visited ones // Render all countries and visited ones
psvg.getFill().renderToCanvas(canvas, RenderOptions.create().css(css.get())) psvg.getFill().renderToCanvas(canvas, RenderOptions.create().css(css_value))
// Render all contours in the same color as the background to make them much clearer // Render all contours in the same color as the background to make them much clearer
psvg.getDraw().renderToCanvas(canvas) psvg.getDraw().renderToCanvas(canvas)
} }
private fun refreshMapCompute() : String {
return css.get()
}
class ImageProcessor(private val refreshMapCompute: ()->String, private val refreshMapDisplay: (String)->Unit) {
private var currentJob : Job? = null
fun process() {
currentJob?.cancel()
currentJob = CoroutineScope(Dispatchers.Main).launch {
try {
refreshMapDisplay(refreshMapCompute())
} catch (_: CancellationException) {
}
}
}
}
} }

View File

@ -1,14 +1,10 @@
package net.helcel.beendroid.activity package net.helcel.beendroid.activity
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.os.PersistableBundle
import android.util.TypedValue import android.util.TypedValue
import android.view.MenuItem import android.view.MenuItem
import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import net.helcel.beendroid.R import net.helcel.beendroid.R
class SettingsActivity: AppCompatActivity() { class SettingsActivity: AppCompatActivity() {

View File

@ -1,9 +1,7 @@
package net.helcel.beendroid.activity package net.helcel.beendroid.activity
import android.content.Context import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import android.util.TypedValue
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference

View File

@ -1,6 +1,5 @@
package net.helcel.beendroid.svg package net.helcel.beendroid.svg
import android.util.TypedValue
import net.helcel.beendroid.countries.Visited import net.helcel.beendroid.countries.Visited
import net.helcel.beendroid.countries.World import net.helcel.beendroid.countries.World

View File

@ -1,12 +1,8 @@
package net.helcel.beendroid.svg package net.helcel.beendroid.svg
import android.content.Context import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import com.caverock.androidsvg.SVG import com.caverock.androidsvg.SVG
import net.helcel.beendroid.R
import net.helcel.beendroid.countries.Country import net.helcel.beendroid.countries.Country
import net.helcel.beendroid.countries.GeoLoc import net.helcel.beendroid.countries.GeoLoc
import net.helcel.beendroid.countries.World import net.helcel.beendroid.countries.World

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">

View File

@ -10,10 +10,17 @@
android:visible="false" android:visible="false"
android:title="@string/action_edit" android:title="@string/action_edit"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />
<item
android:id="@+id/action_stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orderInCategory="100"
android:title="@string/action_stats"
app:showAsAction="never" />
<item <item
android:id="@+id/action_settings" android:id="@+id/action_settings"
android:orderInCategory="100" android:orderInCategory="100"
android:title="@string/action_settings" android:title="@string/action_settings"
app:showAsAction="never" /> app:showAsAction="never" />
</menu> </menu>

View File

@ -3,6 +3,7 @@
<string name="app_name">BeenDroid</string> <string name="app_name">BeenDroid</string>
<string name="app_version">1.0</string> <string name="app_version">1.0</string>
<string name="action_settings">Settings</string> <string name="action_settings">Settings</string>
<string name="action_stats">Stats</string>
<string name="action_edit">Edit</string> <string name="action_edit">Edit</string>
<string name="welcome">Welcome!</string> <string name="welcome">Welcome!</string>
<string name="change_lang">Change language</string> <string name="change_lang">Change language</string>