@ -46,7 +46,8 @@ class PendulumFragment : Fragment() {
val saveButton = view . findViewById < Button > ( R . id . speichern_btn )
saveButton . setOnClickListener ( ) {
finishMeasurement ( )
saveMeasurement ( false , 0.00f )
route ( )
}
//Show targetVelocity and maxAttemptTime
@ -55,44 +56,68 @@ class PendulumFragment : Fragment() {
val timer = view . findViewById < TextView > ( R . id . timer_view )
val currentVelocity = view . findViewById < TextView > ( R . id . actV_view )
targetVelocity . text = args . measurement . targetVelocity . toString ( )
maxAttemptTime . text = args . measurement . maxAttemptTime . toString ( )
// Value endTime
//val endTIme: Long
targetVelocity . text = args . measurement . targetVelocity . toString ( ) + "cm/s"
maxAttemptTime . text = args . measurement . maxAttemptTime . toString ( ) + "sec"
//time count down for "maxAttemptTime" seconds
//with 1 second as countDown interval
object : CountDownTimer ( args . measurement . maxAttemptTime * 1000 , 1000 ) {
object : CountDownTimer ( args . measurement . maxAttemptTime * 1000 , 100 ) {
override fun onTick ( millisUntilFinished : Long ) {
timer . setText ( "sec.: " + millisUntilFinished / 1000 )
currentVelocity . setText ( ( mCanvasPendulum . getVelocity ( ) . toString ( ) ) + "cm/s" )
val velocity = mCanvasPendulum . getVelocity ( )
val time = millisUntilFinished / 1000
timer . setText ( "sec.: " + time )
currentVelocity . setText ( ( velocity . toString ( ) ) + "cm/s" )
if ( velocity > = args . measurement . targetVelocity ) {
saveMeasurement ( true , velocity . toFloat ( ) )
cancel ( )
}
}
override fun onFinish ( ) {
timer . setText ( "done!" )
saveMeasurement ( true , 0.00f )
}
} . start ( )
magnetButton . setOnTouchListener ( object : View . OnTouchListener {
override fun onTouch ( v : View ? , event : MotionEvent ? ) : Boolean {
if ( event ?. action = = MotionEvent . ACTION_DOWN ) {
println ( "button action DOWN" )
//println("button action DOWN" )
mCanvasPendulum . startMagnet ( true )
}
if ( event ?. action = = MotionEvent . ACTION_UP ) {
println ( "button action UP" )
//println("button action UP" )
mCanvasPendulum . stopMagnet ( false )
}
return true
}
} )
return view
}
private fun finishMeasurement ( ) {
private fun saveMeasurement ( save : Boolean , velocity : Float ) {
val endTime = System . currentTimeMillis ( )
val measuredTime = endTime - args . measurement . startTime
val measurement = Measurement ( args . measurement . id , args . measurement . targetVelocity , args . measurement . maxAttemptTime , args . measurement . measuredVelocity , measuredTime , args . measurement . startTime , endTime , args . measurement . owner )
val maxAttemptTime = args . measurement . maxAttemptTime
val measuredTime = ( ( endTime - args . measurement . startTime ) . toDouble ( ) ) / 1000
//val measuredVelocity = args.measurement.measuredVelocity
val targetVelocity = args . measurement . targetVelocity
//println(measuredTime)
if ( save = = = true ) {
val measurement = Measurement ( args . measurement . id , targetVelocity , maxAttemptTime , velocity , measuredTime , args . measurement . startTime , endTime , true , args . measurement . owner )
mMeasurementViewmodel . finishMeasurement ( measurement )
}
}
private fun route ( ) {
val action = PendulumFragmentDirections . actionPendulumFragmentToUpdateFragment ( args . currentUser )
findNavController ( ) . navigate ( action )
}
}