Pendelsession information save in profile in progress new fragment "fragment_start_pendulum" finished last step save Data from Pendulum in Profilmaster
@ -1,4 +1,15 @@ | |||
package com.example.acapendulum20.data | |||
class MeasurementDao { | |||
import androidx.lifecycle.LiveData | |||
import androidx.room.* | |||
import com.example.acapendulum20.model.Measurement | |||
import com.example.acapendulum20.model.User | |||
@Dao | |||
interface MeasurementDao { | |||
@Insert(onConflict = OnConflictStrategy.IGNORE ) | |||
fun addMeasurement(measurement: Measurement) | |||
} |
@ -1,4 +1,35 @@ | |||
package com.example.acapendulum20.data | |||
class MeasurementDatabase { | |||
import android.content.Context | |||
import android.icu.util.Measure | |||
import androidx.room.Database | |||
import androidx.room.Room | |||
import androidx.room.RoomDatabase | |||
import com.example.acapendulum20.model.User | |||
import com.example.acapendulum20.model.Measurement | |||
@Database(entities = [Measurement::class], version = 1, exportSchema = false) | |||
abstract class MeasurementDatabase: RoomDatabase() { | |||
abstract fun measurementDao(): MeasurementDao | |||
companion object { | |||
@Volatile | |||
private var INSTANCE: MeasurementDatabase? = null | |||
fun getDatabase(context: Context): MeasurementDatabase{ | |||
val tempInstance = INSTANCE | |||
if(tempInstance != null){ | |||
return tempInstance | |||
} | |||
synchronized(this){ | |||
val instance = Room.databaseBuilder( | |||
context.applicationContext, | |||
MeasurementDatabase::class.java, | |||
"measurement_database" | |||
).build() | |||
INSTANCE = instance | |||
return instance | |||
} | |||
} | |||
} | |||
} |
@ -0,0 +1,71 @@ | |||
package com.example.acapendulum20.fragments.pendulum | |||
import android.os.Bundle | |||
import android.text.Editable | |||
import android.text.TextUtils | |||
import android.view.LayoutInflater | |||
import android.view.View | |||
import android.view.ViewGroup | |||
import android.widget.Toast | |||
import androidx.fragment.app.Fragment | |||
import androidx.lifecycle.ViewModelProvider | |||
import androidx.navigation.fragment.findNavController | |||
import androidx.navigation.fragment.navArgs | |||
import androidx.room.ForeignKey | |||
import com.example.acapendulum20.R | |||
import com.example.acapendulum20.StartPendulum | |||
import com.example.acapendulum20.fragments.update.UpdateFragmentArgs | |||
import com.example.acapendulum20.model.Measurement | |||
import com.example.acapendulum20.model.User | |||
import com.example.acapendulum20.viewmodel.MeasurementViewmodel | |||
import com.example.acapendulum20.viewmodel.UserViewmodel | |||
import kotlinx.android.synthetic.main.fragment_start_pendulum.* | |||
import kotlinx.android.synthetic.main.fragment_start_pendulum.view.* | |||
import java.time.LocalDateTime | |||
import java.util.* | |||
class StartPendulumFragment: Fragment() { | |||
private val args by navArgs<StartPendulumFragmentArgs>() | |||
private lateinit var mMeasurementViewmodel: MeasurementViewmodel | |||
override fun onCreateView( | |||
inflater: LayoutInflater, container: ViewGroup?, | |||
savedInstanceState: Bundle? | |||
): View? { | |||
// Inflate the layout for this fragment | |||
val view = inflater.inflate(R.layout.fragment_start_pendulum, container, false) | |||
mMeasurementViewmodel = ViewModelProvider(this).get(MeasurementViewmodel::class.java) | |||
//navigate to pendulumFragment | |||
view.startPendulum_btn.setOnClickListener{ | |||
insertDataToDatabase() | |||
} | |||
return view | |||
} | |||
private fun insertDataToDatabase() { | |||
val targetVelocity = (targetVelocity_et.text).toString() | |||
val maxAttemptTime = (maxAttemptTime_et.text).toString() | |||
val owner = args.owner | |||
if(inputCheck(targetVelocity, maxAttemptTime)){ | |||
//Create Measurement Object | |||
val measurement = Measurement(UUID.randomUUID().toString(), targetVelocity.toFloat(),maxAttemptTime.toFloat(),0.0f,0.00f,0.00f,owner) | |||
//Add Data to Database | |||
mMeasurementViewmodel.addMeasurement(measurement) | |||
Toast.makeText(requireContext(),"Successfully added!", Toast.LENGTH_LONG).show() | |||
//Navigate Back | |||
val action = StartPendulumFragmentDirections.actionStartPendulumFragmentToPendulumFragment(targetVelocity.toFloat(),maxAttemptTime.toFloat(),owner) | |||
findNavController().navigate(action) | |||
}else{ | |||
Toast.makeText(requireContext(),"Please fill out all fields.", Toast.LENGTH_LONG).show() | |||
} | |||
} | |||
private fun inputCheck(targetVelocity: String, maxAttemptTime: String): Boolean{ | |||
val validated = !TextUtils.isEmpty(targetVelocity) && !TextUtils.isEmpty(maxAttemptTime) | |||
return validated | |||
} | |||
} | |||
@ -1,4 +1,15 @@ | |||
package com.example.acapendulum20.repository | |||
class MeasurementRepository { | |||
import androidx.lifecycle.LiveData | |||
import com.example.acapendulum20.data.MeasurementDao | |||
import com.example.acapendulum20.data.UserDao | |||
import com.example.acapendulum20.model.Measurement | |||
import com.example.acapendulum20.model.User | |||
class MeasurementRepository(private val measurementDao: MeasurementDao) { | |||
suspend fun addMeasurement(measurement: Measurement){ | |||
measurementDao.addMeasurement(measurement) | |||
} | |||
} |
@ -1,4 +1,34 @@ | |||
package com.example.acapendulum20.viewmodel | |||
class MeasurementViewmodel { | |||
import android.app.Application | |||
import androidx.lifecycle.AndroidViewModel | |||
import androidx.lifecycle.LiveData | |||
import androidx.lifecycle.viewModelScope | |||
import com.example.acapendulum20.data.MeasurementDao | |||
import com.example.acapendulum20.data.MeasurementDatabase | |||
import com.example.acapendulum20.data.UserDatabase | |||
import com.example.acapendulum20.data.UserDatabase.Companion.getDatabase | |||
import com.example.acapendulum20.model.Measurement | |||
import com.example.acapendulum20.model.User | |||
import com.example.acapendulum20.repository.MeasurementRepository | |||
import com.example.acapendulum20.repository.UserRepository | |||
import kotlinx.coroutines.Dispatchers | |||
import kotlinx.coroutines.launch | |||
class MeasurementViewmodel(application: Application): AndroidViewModel(application) { | |||
private val repository: MeasurementRepository | |||
init{ | |||
val measurementDao = MeasurementDatabase.getDatabase(application).measurementDao() | |||
repository = MeasurementRepository(measurementDao) | |||
} | |||
fun addMeasurement(measurement: Measurement){ | |||
viewModelScope.launch(Dispatchers.IO){ | |||
repository.addMeasurement(measurement) | |||
} | |||
} | |||
} |