Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package net.morpheu5.bleedingscorecalculator
import android.content.SharedPreferences
import android.os.Bundle
import android.support.v4.app.FragmentActivity
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.support.v7.preference.PreferenceFragmentCompat
import android.support.v4.app.Fragment
import android.support.v7.preference.PreferenceManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import java.util.TreeMap
class CalculatorScreen : Fragment(), SharedPreferences.OnSharedPreferenceChangeListener {
var tableName: String? = null
var scoreToPercentage: TreeMap<Int, Double>? = null
var scoreToRisk: TreeMap<Int, String>? = null
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val fm = childFragmentManager
var prefsFragment: PrefsFragment? = fm.findFragmentByTag(tableName) as PrefsFragment?
if (prefsFragment == null) {
prefsFragment = PrefsFragment()
val ft = fm.beginTransaction()
ft.add(R.id.parameters_fragment_container, prefsFragment, tableName)
ft.commit()
fm.executePendingTransactions()
}
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater!!.inflate(R.layout.calculator_screen, container, false)
(rootView.findViewById(R.id.risk) as TextView).text = "VERY LOW RISK"
(rootView.findViewById(R.id.percentage) as TextView).text = "0.00 %"
return rootView
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (savedInstanceState is Bundle) {
tableName = savedInstanceState?.getString("tableName")
scoreToPercentage = savedInstanceState?.getSerializable("scoreToPercentage") as TreeMap<Int, Double>
scoreToRisk = savedInstanceState?.getSerializable("scoreToRisk") as TreeMap<Int, String>
} // Otherwise no biggie, right?
assert(tableName != null)
assert(scoreToPercentage != null)
assert(scoreToRisk != null)
// Force a calculation to get default values on screen.
onSharedPreferenceChanged(PreferenceManager.getDefaultSharedPreferences(context), "")
}
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
outState?.putString("tableName", tableName)
outState?.putSerializable("scoreToPercentage", scoreToPercentage)
outState?.putSerializable("scoreToRisk", scoreToRisk)
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, s: String) {
// OK, let's just agree on the fact that this is awful. But hey, Android is awful.
val activity = activity ?: return
var crusadeTotal = 0
var mehranTotal = 0
if (tableName == "crusade") {
for (key in activity.resources.getStringArray(R.array.crusade_preference_keys)) {
if (key == "crusade_cfh") {
crusadeTotal += if (sharedPreferences.getBoolean(key, false)) 7 else 0
} else if (key == "crusade_prior_vascular_disease") {
crusadeTotal += if (sharedPreferences.getBoolean(key, false)) 6 else 0
} else if (key == "crusade_diabetes") {
crusadeTotal += if (sharedPreferences.getBoolean(key, false)) 6 else 0
} else {
crusadeTotal += Integer.parseInt(sharedPreferences.getString(key, "0"))
}
}
} else if (tableName == "mehran") {
for (key in activity.resources.getStringArray(R.array.mehran_preference_keys)) {
if (key == "mehran_anemia") {
mehranTotal += if (sharedPreferences.getBoolean(key, false)) 6 else 0
} else {
mehranTotal += Integer.parseInt(sharedPreferences.getString(key, "0"))
}
}
}
val view = view ?: return
val rootView = view.rootView
if (null != rootView) {
if (tableName == "crusade") {
val risk = scoreToRisk!!.higherEntry(crusadeTotal).value
val percent = scoreToPercentage!!.higherEntry(crusadeTotal).value
(view.findViewById(R.id.risk) as TextView).text = risk + " RISK"
(view.findViewById(R.id.percentage) as TextView).setText(String.format("%.1f%%", percent!! * 100.0))
} else if (tableName == "mehran") {
val risk = scoreToRisk!!.higherEntry(mehranTotal).value
val percent = scoreToPercentage!!.higherEntry(mehranTotal).value
(view.findViewById(R.id.risk) as TextView).text = risk + " RISK"
(view.findViewById(R.id.percentage) as TextView).setText(String.format("%.1f%%", percent!! * 100.0))
} else {
// MEH!
}
}
}
class PrefsFragment : PreferenceFragmentCompat() {
override fun onResume() {
super.onResume()
val parentFragment = this.parentFragment as CalculatorScreen
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(parentFragment)
}
override fun onCreatePreferences(savedInstanceState: Bundle, rootKey: String) {
// Load the preferences from an XML resource
val calc = this.parentFragment as CalculatorScreen
var resourceName = "@xml/"
if (tag != null) {
resourceName += tag
} else if (calc.tableName != null) {
resourceName += calc.tableName
} else {
// We're screwed!
// But, somehow, OK.
}
if (resourceName.compareTo("@xml/") != 0) {
val resourceId = calc.context.resources.getIdentifier(resourceName, "id", context.packageName)
addPreferencesFromResource(resourceId)
}
}
}
companion object {
/// New fragment instance
fun newInstance(table: String): CalculatorScreen {
val fragment = CalculatorScreen()
fragment.tableName = table
fragment.scoreToPercentage = TreeMap<Int, Double>()
fragment.scoreToRisk = TreeMap<Int, String>()
if (table == "crusade") {
fragment.scoreToPercentage!!.put(0, 0.031)
fragment.scoreToPercentage!!.put(20, 0.031)
fragment.scoreToPercentage!!.put(30, 0.055)
fragment.scoreToPercentage!!.put(40, 0.086)
fragment.scoreToPercentage!!.put(50, 0.119)
fragment.scoreToPercentage!!.put(100, 0.195)
fragment.scoreToRisk!!.put(19, "VERY LOW")
fragment.scoreToRisk!!.put(29, "LOW")
fragment.scoreToRisk!!.put(39, "MODERATE")
fragment.scoreToRisk!!.put(49, "HIGH")
fragment.scoreToRisk!!.put(99, "VERY HIGH")
} else if (table == "mehran") {
fragment.scoreToPercentage!!.put(0, 0.009)
fragment.scoreToPercentage!!.put(5, 0.016)
fragment.scoreToPercentage!!.put(10, 0.028)
fragment.scoreToPercentage!!.put(15, 0.047)
fragment.scoreToPercentage!!.put(20, 0.079)
fragment.scoreToPercentage!!.put(25, 0.129)
fragment.scoreToPercentage!!.put(30, 0.204)
fragment.scoreToPercentage!!.put(35, 0.307)
fragment.scoreToPercentage!!.put(40, 0.435)
fragment.scoreToPercentage!!.put(100, 0.435)
fragment.scoreToRisk!!.put(9, "LOW")
fragment.scoreToRisk!!.put(14, "MODERATE")
fragment.scoreToRisk!!.put(19, "HIGH")
fragment.scoreToRisk!!.put(99, "VERY HIGH")
}
return fragment
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment