|
|
@ -6,23 +6,26 @@ import android.os.CountDownTimer |
|
|
|
import android.view.LayoutInflater |
|
|
|
import android.view.MotionEvent |
|
|
|
import android.view.View |
|
|
|
import android.view.View.OnTouchListener |
|
|
|
import android.view.ViewGroup |
|
|
|
import android.widget.Button |
|
|
|
import android.widget.TextView |
|
|
|
import androidx.fragment.app.Fragment |
|
|
|
import androidx.lifecycle.ViewModelProvider |
|
|
|
import androidx.navigation.fragment.findNavController |
|
|
|
import androidx.navigation.fragment.navArgs |
|
|
|
import com.example.acapendulum20.R |
|
|
|
import com.example.acapendulum20.canvas.CanvasPendulum |
|
|
|
import com.example.acapendulum20.model.Measurement |
|
|
|
import com.example.acapendulum20.viewmodel.MeasurementViewmodel |
|
|
|
import kotlinx.android.synthetic.main.fragment_pendulum.* |
|
|
|
import java.time.LocalDateTime |
|
|
|
import java.text.DateFormat |
|
|
|
import java.text.SimpleDateFormat |
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
class PendulumFragment : Fragment() { |
|
|
|
|
|
|
|
private val args by navArgs<PendulumFragmentArgs>() |
|
|
|
|
|
|
|
private lateinit var mMeasurementViewmodel: MeasurementViewmodel |
|
|
|
|
|
|
|
@SuppressLint("ClickableViewAccessibility") |
|
|
@ -33,35 +36,41 @@ class PendulumFragment : Fragment() { |
|
|
|
// Inflate the layout for this fragment |
|
|
|
val view = inflater.inflate(R.layout.fragment_pendulum, container, false) |
|
|
|
val mCanvasPendulum: CanvasPendulum = view.findViewById(R.id.canvasContainer)!! |
|
|
|
mMeasurementViewmodel = ViewModelProvider(this).get(MeasurementViewmodel::class.java) |
|
|
|
|
|
|
|
|
|
|
|
//activate the magnet |
|
|
|
val button = view.findViewById<Button>(R.id.activate_btn) |
|
|
|
val magnetButton = view.findViewById<Button>(R.id.activate_btn) |
|
|
|
|
|
|
|
//Save results |
|
|
|
val saveButton = view.findViewById<Button>(R.id.speichern_btn) |
|
|
|
|
|
|
|
saveButton.setOnClickListener(){ |
|
|
|
finishMeasurement() |
|
|
|
} |
|
|
|
|
|
|
|
//Show targetVelocity and maxAttemptTime |
|
|
|
val targetVelocity = view.findViewById<TextView>(R.id.targetVelocity_view) |
|
|
|
val maxAttemptTime = view.findViewById<TextView>(R.id.maxAttemptTime_view) |
|
|
|
val timer = view.findViewById<TextView>(R.id.timer_view) |
|
|
|
val currentVelocity = view.findViewById<TextView>(R.id.actV_view) |
|
|
|
|
|
|
|
targetVelocity.text = args.targetVelocity.toString() |
|
|
|
maxAttemptTime.text = args.maxAttemptTime.toString() |
|
|
|
//timer.text = |
|
|
|
targetVelocity.text = args.measurement.targetVelocity.toString() |
|
|
|
maxAttemptTime.text = args.measurement.maxAttemptTime.toString() |
|
|
|
|
|
|
|
//time count down for 30 seconds |
|
|
|
//time count down for "maxAttemptTime" seconds |
|
|
|
//with 1 second as countDown interval |
|
|
|
object : CountDownTimer(args.maxAttemptTime.toLong()*1000, 1000){ |
|
|
|
|
|
|
|
object : CountDownTimer(args.measurement.maxAttemptTime*1000, 1000){ |
|
|
|
override fun onTick(millisUntilFinished: Long) { |
|
|
|
timer.setText("sec.: " + millisUntilFinished / 1000) |
|
|
|
currentVelocity.setText((mCanvasPendulum.getVelocity().toString()) + "cm/s") |
|
|
|
} |
|
|
|
|
|
|
|
override fun onFinish() { |
|
|
|
timer.setText("done!") |
|
|
|
} |
|
|
|
}.start() |
|
|
|
|
|
|
|
button.setOnTouchListener(object : View.OnTouchListener{ |
|
|
|
magnetButton.setOnTouchListener(object : View.OnTouchListener{ |
|
|
|
override fun onTouch(v: View?, event: MotionEvent?): Boolean { |
|
|
|
if (event?.action == MotionEvent.ACTION_DOWN){ |
|
|
|
println("button action DOWN") |
|
|
@ -77,4 +86,13 @@ class PendulumFragment : Fragment() { |
|
|
|
}) |
|
|
|
return view |
|
|
|
} |
|
|
|
|
|
|
|
private fun finishMeasurement(){ |
|
|
|
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) |
|
|
|
mMeasurementViewmodel.finishMeasurement(measurement) |
|
|
|
val action = PendulumFragmentDirections.actionPendulumFragmentToUpdateFragment(args.currentUser) |
|
|
|
findNavController().navigate(action) |
|
|
|
} |
|
|
|
} |