diff --git a/README.md b/README.md deleted file mode 100644 index a569379..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# TutorToolkit \ No newline at end of file diff --git a/app/src/main/java/com/taymath/tutortoolkit/addstudent/AddStudentFragment.kt b/app/src/main/java/com/taymath/tutortoolkit/addstudent/AddStudentFragment.kt index 3532589..f010440 100644 --- a/app/src/main/java/com/taymath/tutortoolkit/addstudent/AddStudentFragment.kt +++ b/app/src/main/java/com/taymath/tutortoolkit/addstudent/AddStudentFragment.kt @@ -46,14 +46,27 @@ class AddStudentFragment : Fragment() { binding.setLifecycleOwner(this) - // Listen for when we navigate to student list - addStudentViewModel.navigateToStudentList.observe(viewLifecycleOwner, Observer { +// // Add an Observer to the state variable for Navigating when student icon is clicked. +// addStudentViewModel.navigateToChooseIcon.observe(viewLifecycleOwner, Observer {student -> +// student?.let { +// this.findNavController().navigate(AddStudentFragmentDirections +// .actionAddStudentFragmentToChooseIconFragment(student.studentId)) +// addStudentViewModel.doneNavigating() +// } +// }) + + // Add an Observer to the state variable for Navigating when a Quality icon is tapped. + addStudentViewModel.navigateToChooseIcon.observe(viewLifecycleOwner, Observer { if (it == true) { // Observed state is true. this.findNavController().navigate( - AddStudentFragmentDirections.actionAddStudentFragmentToStudentListFragment()) + AddStudentFragmentDirections.actionAddStudentFragmentToChooseIconFragment()) + // Reset state to make sure we only navigate once, even if the device + // has a configuration change. addStudentViewModel.doneNavigating() } }) + + return binding.root } } \ No newline at end of file diff --git a/app/src/main/java/com/taymath/tutortoolkit/addstudent/AddStudentViewModel.kt b/app/src/main/java/com/taymath/tutortoolkit/addstudent/AddStudentViewModel.kt index bc40714..41be8cb 100644 --- a/app/src/main/java/com/taymath/tutortoolkit/addstudent/AddStudentViewModel.kt +++ b/app/src/main/java/com/taymath/tutortoolkit/addstudent/AddStudentViewModel.kt @@ -1,42 +1,53 @@ package com.taymath.tutortoolkit.addstudent +import android.util.Log +import android.widget.ImageView +import androidx.databinding.BindingAdapter import androidx.lifecycle.LiveData +import androidx.lifecycle.MediatorLiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import com.taymath.tutortoolkit.R import com.taymath.tutortoolkit.studentdatabase.Student import com.taymath.tutortoolkit.studentdatabase.StudentDatabaseDao +import com.taymath.tutortoolkit.studentdetail.StudentDetailFragmentArgs import kotlinx.coroutines.* + class AddStudentViewModel( val database: StudentDatabaseDao ) : ViewModel() { + val newStudent = Student() + // Initialize viewModelJob private val viewModelJob = Job() // Initialize uiScope private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob) - // Setup Livdata to signal when to navigate to studentList - private val _navigateToStudentList = MutableLiveData() - val navigateToStudentList: LiveData - get() = _navigateToStudentList + private var currentStudent = MutableLiveData() + + // Setup Livdata to signal when to navigate to chooseIcon + private val _navigateToChooseIcon = MutableLiveData() + val navigateToChooseIcon: LiveData + get() = _navigateToChooseIcon // Initialize Student and add to student_table - fun onAddStudent(studentNameString : String, subjectString: String, + fun onChooseIcon(studentNameString : String, subjectString: String, gradeLevelString: String, addressString: String, emailString: String) { + newStudent.studentNameString = studentNameString + newStudent.subjectString = subjectString + newStudent.emailString = emailString + newStudent.gradeLevelString = gradeLevelString + newStudent.addressString = addressString + uiScope.launch { withContext(Dispatchers.IO) { - val newStudent = Student() - newStudent.studentNameString = studentNameString - newStudent.subjectString = subjectString - newStudent.emailString = emailString - newStudent.gradeLevelString = gradeLevelString - newStudent.addressString = addressString insert(newStudent) } } - _navigateToStudentList.value = true + _navigateToChooseIcon.value = true } override fun onCleared() { @@ -45,7 +56,7 @@ class AddStudentViewModel( } fun doneNavigating() { - _navigateToStudentList.value = null + _navigateToChooseIcon.value = null } private suspend fun insert(student: Student) { @@ -54,14 +65,17 @@ class AddStudentViewModel( } } -// fun onSetSleepQuality(quality: Int) { -// uiScope.launch { -// withContext(Dispatchers.IO) { -// val tonight = database.get(sleepNightKey) ?: return@withContext -// tonight.sleepQuality = quality -// database.update(tonight) -// } -// _navigateToSleepTracker.value = true -// } -// } + private suspend fun getCurrentStudentFromDatabase(): Student? { + return withContext(Dispatchers.IO) { + var student = database.getCurrentStudent() + student + } + } + + private fun initializeCurrentStudent() { + uiScope.launch { + currentStudent.value = getCurrentStudentFromDatabase() + } + } + } \ No newline at end of file diff --git a/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconFragment.kt b/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconFragment.kt index f192317..adf583d 100644 --- a/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconFragment.kt +++ b/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconFragment.kt @@ -6,12 +6,19 @@ import android.view.View import android.view.ViewGroup import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment +import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import androidx.navigation.fragment.findNavController import com.taymath.tutortoolkit.R import com.taymath.tutortoolkit.databinding.FragmentChooseIconBinding import com.taymath.tutortoolkit.studentdatabase.StudentDatabase +/** + * Fragment that displays a list of clickable icons, + * each representing a sleep quality rating. + * Once the user taps an icon, the quality is set in the current sleepNight + * and the database is updated. + */ class ChooseIconFragment : Fragment() { /** @@ -28,8 +35,6 @@ class ChooseIconFragment : Fragment() { val application = requireNotNull(this.activity).application -// val arguments = SleepQualityFragmentArgs.fromBundle(arguments!!) - // Create an instance of the ViewModel Factory. val dataSource = StudentDatabase.getInstance(application).studentDatabaseDao val viewModelFactory = ChooseIconViewModelFactory(dataSource) @@ -44,16 +49,16 @@ class ChooseIconFragment : Fragment() { binding.chooseIconViewModel = chooseIconViewModel // Add an Observer to the state variable for Navigating when a Quality icon is tapped. -// chooseIconViewModel.navigateToSleepTracker.observe(this, Observer { -// if (it == true) { // Observed state is true. -// this.findNavController().navigate( -// SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment()) -// // Reset state to make sure we only navigate once, even if the device -// // has a configuration change. -// sleepQualityViewModel.doneNavigating() -// } -// }) + chooseIconViewModel.navigateToStudentList.observe(viewLifecycleOwner, Observer { + if (it == true) { // Observed state is true. + this.findNavController().navigate( + ChooseIconFragmentDirections.actionChooseIconFragmentToStudentListFragment()) + // Reset state to make sure we only navigate once, even if the device + // has a configuration change. + chooseIconViewModel.doneNavigating() + } + }) return binding.root } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconViewModel.kt b/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconViewModel.kt index 83404eb..31d9577 100644 --- a/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconViewModel.kt +++ b/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconViewModel.kt @@ -6,42 +6,25 @@ import androidx.lifecycle.ViewModel import com.taymath.tutortoolkit.studentdatabase.StudentDatabaseDao import kotlinx.coroutines.* +/** + * ViewModel for SleepQualityFragment. + * + * @param sleepNightKey The key of the current night we are working on. + */ class ChooseIconViewModel( val database: StudentDatabaseDao) : ViewModel() { - /** Coroutine setup variables */ - - /** - * viewModelJob allows us to cancel all coroutines started by this ViewModel. - */ private val viewModelJob = Job() - /** - * A [CoroutineScope] keeps track of all coroutines started by this ViewModel. - * - * Because we pass it [viewModelJob], any coroutine started in this scope can be cancelled - * by calling `viewModelJob.cancel()` - * - * By default, all coroutines started in uiScope will launch in [Dispatchers.Main] which is - * the main thread on Android. This is a sensible default because most coroutines started by - * a [ViewModel] update the UI after performing some processing. - */ private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob) - /** - * Variable that tells the fragment whether it should navigate to [SleepTrackerFragment]. - * - * This is `private` because we don't want to expose the ability to set [MutableLiveData] to - * the [Fragment] - */ - private val _navigateToSleepTracker = MutableLiveData() + private val _navigateToStudentList = MutableLiveData() /** * When true immediately navigate back to the [SleepTrackerFragment] */ - val navigateToSleepTracker: LiveData - get() = _navigateToSleepTracker - + val navigateToStudentList: LiveData + get() = _navigateToStudentList /** * Cancels all coroutines when the ViewModel is cleared, to cleanup any pending work. * @@ -53,29 +36,23 @@ class ChooseIconViewModel( } /** - * Call this immediately after navigating to [SleepTrackerFragment] + * Call this immediately after navigating to [StudentListFragment] */ fun doneNavigating() { - _navigateToSleepTracker.value = null + _navigateToStudentList.value = null } - /** - * Sets the sleep quality and updates the database. - * - * Then navigates back to the SleepTrackerFragment. - */ -// fun onSetSleepQuality(quality: Int) { -// uiScope.launch { -// // IO is a thread pool for running operations that access the disk, such as -// // our Room database. -// withContext(Dispatchers.IO) { -// val tonight = database.get(sleepNightKey) ?: return@withContext -// tonight.sleepQuality = quality -// database.update(tonight) -// } -// -// // Setting this state variable to true will alert the observer and trigger navigation. -// _navigateToSleepTracker.value = true -// } -// } + fun onChooseIcon(iconNumber: Int) { + uiScope.launch { + // IO is a thread pool for running operations that access the disk, such as + // our Room database. + withContext(Dispatchers.IO) { + val currentStudent = database.getCurrentStudent() ?: return@withContext + currentStudent.iconNumber = iconNumber + database.updateStudent(currentStudent) + } + } + // Setting this state variable to true will alert the observer and trigger navigation. + _navigateToStudentList.value = true + } } \ No newline at end of file diff --git a/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconViewModelFactory.kt b/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconViewModelFactory.kt index cffed31..4534a61 100644 --- a/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconViewModelFactory.kt +++ b/app/src/main/java/com/taymath/tutortoolkit/chooseicon/ChooseIconViewModelFactory.kt @@ -4,6 +4,11 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import com.taymath.tutortoolkit.studentdatabase.StudentDatabaseDao +/** + * This is pretty much boiler plate code for a ViewModel Factory. + * + * Provides the key for the night and the SleepDatabaseDao to the ViewModel. + */ class ChooseIconViewModelFactory( private val dataSource: StudentDatabaseDao) : ViewModelProvider.Factory { @Suppress("unchecked_cast") diff --git a/app/src/main/java/com/taymath/tutortoolkit/studentdatabase/Student.kt b/app/src/main/java/com/taymath/tutortoolkit/studentdatabase/Student.kt index ff390c5..61b46d9 100644 --- a/app/src/main/java/com/taymath/tutortoolkit/studentdatabase/Student.kt +++ b/app/src/main/java/com/taymath/tutortoolkit/studentdatabase/Student.kt @@ -42,6 +42,9 @@ data class Student ( var addressString: String = "CHECK_Student.KT", @ColumnInfo(name = "email") - var emailString: String = "CHECK_Student.KT" + var emailString: String = "CHECK_Student.KT", + + @ColumnInfo(name="iconNumber") + var iconNumber: Int = 1 ) diff --git a/app/src/main/java/com/taymath/tutortoolkit/studentdatabase/StudentDatabaseDao.kt b/app/src/main/java/com/taymath/tutortoolkit/studentdatabase/StudentDatabaseDao.kt index e191532..067e17f 100644 --- a/app/src/main/java/com/taymath/tutortoolkit/studentdatabase/StudentDatabaseDao.kt +++ b/app/src/main/java/com/taymath/tutortoolkit/studentdatabase/StudentDatabaseDao.kt @@ -17,16 +17,13 @@ package com.taymath.tutortoolkit.studentdatabase import androidx.lifecycle.LiveData -import androidx.room.Dao -import androidx.room.Insert -import androidx.room.Query -import androidx.room.Update +import androidx.room.* @Dao interface StudentDatabaseDao { // Student Table Dao - @Insert + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertStudent(student: Student) @Update @@ -47,12 +44,15 @@ interface StudentDatabaseDao { @Query("SELECT * from student_table WHERE studentId = :key") fun getStudentWithId(key: Long): LiveData - @Query("SELECT * FROM student_table WHERE studentId = :studentId") - fun getStudentClassWithId(studentId: Long): Student + @Query("SELECT * from student_table WHERE studentId = :key") + fun getStudentClassWithId(key: Long): Student? @Query("SELECT * from student_table WHERE student_name = :studentName") fun getStudentWithName(studentName: String): LiveData + @Query("SELECT * FROM student_table ORDER BY studentId DESC LIMIT 1") + fun getCurrentStudent(): Student? + // Grade Table Dao diff --git a/app/src/main/java/com/taymath/tutortoolkit/studentlist/StudentBindingUtils.kt b/app/src/main/java/com/taymath/tutortoolkit/studentlist/StudentBindingUtils.kt index d05eb10..3b41353 100644 --- a/app/src/main/java/com/taymath/tutortoolkit/studentlist/StudentBindingUtils.kt +++ b/app/src/main/java/com/taymath/tutortoolkit/studentlist/StudentBindingUtils.kt @@ -6,13 +6,6 @@ import androidx.databinding.BindingAdapter import com.taymath.tutortoolkit.R import com.taymath.tutortoolkit.studentdatabase.Student -@BindingAdapter("studentImage") -fun ImageView.setStudentImage(item: Student?){ - item?.let { - setImageResource(R.drawable.ic_item_test) - } -} - @BindingAdapter("subjectText") fun TextView.setSubjectText(item: Student?){ item?.let { @@ -33,3 +26,20 @@ fun TextView.setGradeLevelText(item: Student?){ text =item.gradeLevelString } } + +@BindingAdapter("studentImage") +fun ImageView.setStudentImage(item: Student?) { + item?.let { + setImageResource(when (item.iconNumber) { + 1 -> R.drawable.icon_1 + 2 -> R.drawable.icon_2 + 3 -> R.drawable.icon_3 + 4 -> R.drawable.icon_4 + 5 -> R.drawable.icon_5 + 6 -> R.drawable.icon_6 + 7 -> R.drawable.icon_7 + 8 -> R.drawable.icon_8 + else -> R.drawable.icon_1 + }) + } +} diff --git a/app/src/main/res/layout/fragment_add_student.xml b/app/src/main/res/layout/fragment_add_student.xml index 7efc059..cca236f 100644 --- a/app/src/main/res/layout/fragment_add_student.xml +++ b/app/src/main/res/layout/fragment_add_student.xml @@ -91,29 +91,17 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/address_text" /> -