2019-10-08 21:20:04 +02:00
|
|
|
package info.nightscout.androidaps.activities
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.widget.ArrayAdapter
|
|
|
|
import com.google.firebase.auth.FirebaseAuth
|
|
|
|
import com.google.firebase.database.FirebaseDatabase
|
|
|
|
import info.nightscout.androidaps.R
|
|
|
|
import info.nightscout.androidaps.data.defaultProfile.DefaultProfile
|
2019-12-30 00:53:44 +01:00
|
|
|
import info.nightscout.androidaps.dialogs.ProfileViewerDialog
|
|
|
|
import info.nightscout.androidaps.logging.AAPSLogger
|
|
|
|
import info.nightscout.androidaps.logging.LTag
|
2019-10-08 21:20:04 +02:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
|
2019-12-31 00:37:36 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
|
2019-10-08 21:20:04 +02:00
|
|
|
import info.nightscout.androidaps.utils.*
|
2019-12-30 00:53:44 +01:00
|
|
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
2019-12-20 22:22:58 +01:00
|
|
|
import kotlinx.android.synthetic.main.survey_activity.*
|
2019-12-28 22:51:04 +01:00
|
|
|
import javax.inject.Inject
|
2019-10-08 21:20:04 +02:00
|
|
|
|
|
|
|
class SurveyActivity : NoSplashAppCompatActivity() {
|
2019-12-30 00:53:44 +01:00
|
|
|
@Inject lateinit var aapsLogger: AAPSLogger
|
|
|
|
@Inject lateinit var resourceHelper: ResourceHelper
|
|
|
|
@Inject lateinit var configBuilderPlugin: ConfigBuilderPlugin
|
|
|
|
@Inject lateinit var tddCalculator: TddCalculator
|
2019-12-31 00:37:36 +01:00
|
|
|
@Inject lateinit var profileFunction: ProfileFunction
|
2019-12-28 22:51:04 +01:00
|
|
|
|
2019-10-08 21:20:04 +02:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2019-12-20 22:22:58 +01:00
|
|
|
setContentView(R.layout.survey_activity)
|
2019-10-08 21:20:04 +02:00
|
|
|
|
|
|
|
survey_id.text = InstanceId.instanceId()
|
|
|
|
|
2019-12-30 00:53:44 +01:00
|
|
|
val profileStore = configBuilderPlugin.activeProfileInterface.profile
|
2019-12-08 21:55:49 +01:00
|
|
|
val profileList = profileStore?.getProfileList() ?: return
|
|
|
|
survey_spinner.adapter = ArrayAdapter(this, R.layout.spinner_centered, profileList)
|
2019-10-08 21:20:04 +02:00
|
|
|
|
2019-12-28 22:51:04 +01:00
|
|
|
survey_tdds.text = tddCalculator.stats()
|
2019-12-09 19:03:26 +01:00
|
|
|
survey_tir.text = TirCalculator.stats()
|
|
|
|
survey_activity.text = ActivityMonitor.stats()
|
2019-12-08 23:14:46 +01:00
|
|
|
|
2019-10-08 21:20:04 +02:00
|
|
|
survey_profile.setOnClickListener {
|
|
|
|
val age = SafeParse.stringToDouble(survey_age.text.toString())
|
|
|
|
val weight = SafeParse.stringToDouble(survey_weight.text.toString())
|
|
|
|
val tdd = SafeParse.stringToDouble(survey_tdd.text.toString())
|
|
|
|
if (age < 1 || age > 120) {
|
|
|
|
ToastUtils.showToastInUiThread(this, R.string.invalidage)
|
|
|
|
return@setOnClickListener
|
|
|
|
}
|
|
|
|
if ((weight < 5 || weight > 150) && tdd == 0.0) {
|
|
|
|
ToastUtils.showToastInUiThread(this, R.string.invalidweight)
|
|
|
|
return@setOnClickListener
|
|
|
|
}
|
|
|
|
if ((tdd < 5 || tdd > 150) && weight == 0.0) {
|
|
|
|
ToastUtils.showToastInUiThread(this, R.string.invalidweight)
|
|
|
|
return@setOnClickListener
|
|
|
|
}
|
2019-12-31 00:37:36 +01:00
|
|
|
val profile = DefaultProfile().profile(age, tdd, weight, profileFunction.getUnits())
|
2019-10-08 21:20:04 +02:00
|
|
|
val args = Bundle()
|
|
|
|
args.putLong("time", DateUtil.now())
|
|
|
|
args.putInt("mode", ProfileViewerDialog.Mode.CUSTOM_PROFILE.ordinal)
|
|
|
|
args.putString("customProfile", profile.data.toString())
|
|
|
|
args.putString("customProfileUnits", profile.units)
|
|
|
|
args.putString("customProfileName", "Age: $age TDD: $tdd Weight: $weight")
|
|
|
|
val pvd = ProfileViewerDialog()
|
|
|
|
pvd.arguments = args
|
|
|
|
pvd.show(supportFragmentManager, "ProfileViewDialog")
|
|
|
|
}
|
|
|
|
|
|
|
|
survey_submit.setOnClickListener {
|
|
|
|
val r = FirebaseRecord()
|
|
|
|
r.id = InstanceId.instanceId()
|
|
|
|
r.age = SafeParse.stringToInt(survey_age.text.toString())
|
|
|
|
r.weight = SafeParse.stringToInt(survey_weight.text.toString())
|
|
|
|
if (r.age < 1 || r.age > 120) {
|
|
|
|
ToastUtils.showToastInUiThread(this, R.string.invalidage)
|
|
|
|
return@setOnClickListener
|
|
|
|
}
|
|
|
|
if (r.weight < 5 || r.weight > 150) {
|
|
|
|
ToastUtils.showToastInUiThread(this, R.string.invalidweight)
|
|
|
|
return@setOnClickListener
|
|
|
|
}
|
|
|
|
|
|
|
|
if (survey_spinner.selectedItem == null)
|
|
|
|
return@setOnClickListener
|
|
|
|
val profileName = survey_spinner.selectedItem.toString()
|
|
|
|
val specificProfile = profileStore.getSpecificProfile(profileName)
|
|
|
|
|
|
|
|
r.profileJson = specificProfile.toString()
|
|
|
|
|
|
|
|
val auth = FirebaseAuth.getInstance()
|
|
|
|
auth.signInAnonymously()
|
2019-12-20 22:22:58 +01:00
|
|
|
.addOnCompleteListener(this) { task ->
|
|
|
|
if (task.isSuccessful) {
|
2019-12-30 00:53:44 +01:00
|
|
|
aapsLogger.debug(LTag.CORE, "signInAnonymously:success")
|
2019-12-31 00:37:36 +01:00
|
|
|
//val user = auth.currentUser // TODO: do we need this, seems unused?
|
2019-10-08 21:20:04 +02:00
|
|
|
|
2019-12-20 22:22:58 +01:00
|
|
|
val database = FirebaseDatabase.getInstance().reference
|
|
|
|
database.child("survey").child(r.id).setValue(r)
|
|
|
|
} else {
|
2019-12-30 00:53:44 +01:00
|
|
|
aapsLogger.error("signInAnonymously:failure", task.exception!!)
|
2019-12-20 22:22:58 +01:00
|
|
|
ToastUtils.showToastInUiThread(this, "Authentication failed.")
|
|
|
|
//updateUI(null)
|
2019-10-08 21:20:04 +02:00
|
|
|
}
|
2019-12-20 22:22:58 +01:00
|
|
|
|
|
|
|
// ...
|
|
|
|
}
|
2019-10-08 21:20:04 +02:00
|
|
|
finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner class FirebaseRecord {
|
|
|
|
var id = ""
|
|
|
|
var age: Int = 0
|
|
|
|
var weight: Int = 0
|
|
|
|
var profileJson = "ghfg"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|