Add user icon choice fragment and functionality

This commit is contained in:
tsb1995 2020-04-20 19:16:35 -07:00
parent 0316099a0f
commit 35040d34f4
12 changed files with 255 additions and 167 deletions

View File

@ -1 +0,0 @@
# TutorToolkit

View File

@ -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
}
}

View File

@ -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<Boolean?>()
val navigateToStudentList: LiveData<Boolean?>
get() = _navigateToStudentList
private var currentStudent = MutableLiveData<Student?>()
// Setup Livdata to signal when to navigate to chooseIcon
private val _navigateToChooseIcon = MutableLiveData<Boolean?>()
val navigateToChooseIcon: LiveData<Boolean?>
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()
}
}
}

View File

@ -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,15 +49,15 @@ 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
}

View File

@ -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<Boolean?>()
private val _navigateToStudentList = MutableLiveData<Boolean?>()
/**
* When true immediately navigate back to the [SleepTrackerFragment]
*/
val navigateToSleepTracker: LiveData<Boolean?>
get() = _navigateToSleepTracker
val navigateToStudentList: LiveData<Boolean?>
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
}
}

View File

@ -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")

View File

@ -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
)

View File

@ -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<Student>
@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<Student>
@Query("SELECT * FROM student_table ORDER BY studentId DESC LIMIT 1")
fun getCurrentStudent(): Student?
// Grade Table Dao

View File

@ -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
})
}
}

View File

@ -91,29 +91,17 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/address_text" />
<Button
android:id="@+id/add_student_button"
style="@style/AddButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:onClick="@{() -> addStudentViewModel.onAddStudent(studentNameText.getText().toString(), subjectText.getText().toString(), gradeText.getText().toString(), addressText.getText().toString(), emailText.getText().toString())}"
android:text="@string/add_student"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageButton" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:onClick="@{() -> addStudentViewModel.onChooseIcon(studentNameText.getText().toString(), subjectText.getText().toString(), gradeText.getText().toString(), addressText.getText().toString(), emailText.getText().toString())}"
app:srcCompat="@drawable/icon_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email_text"
app:srcCompat="@drawable/icon_1"
android:contentDescription="@string/student_image_icon_choice" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,20 +1,4 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2018, The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!-- Wrapping the layout into /layout to make it available with data binding. -->
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
@ -23,7 +7,6 @@
the whole ViewModel, so that we can access the LiveData,
click handlers, and state variables. -->
<data>
<variable
name="chooseIconViewModel"
type="com.taymath.tutortoolkit.chooseicon.ChooseIconViewModel"/>
@ -45,14 +28,100 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/quality_zero_image"
<ImageButton
android:id="@+id/icon_one_image"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_margin="@dimen/triple_margin"
android:contentDescription="@string/icon_1"
app:layout_constraintEnd_toEndOf="parent"
android:onClick="@{() -> chooseIconViewModel.onChooseIcon(1)}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_text"
app:srcCompat="@drawable/icon_1" />
<ImageButton
android:id="@+id/icon_two_image"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_margin="@dimen/triple_margin"
android:contentDescription="@string/icon_1"
android:onClick="@{() -> chooseIconViewModel.onChooseIcon(2)}"
app:layout_constraintEnd_toStartOf="@+id/icon_three_image"
app:layout_constraintStart_toEndOf="@+id/icon_one_image"
app:layout_constraintTop_toBottomOf="@+id/title_text"
app:srcCompat="@drawable/icon_2" />
<ImageButton
android:id="@+id/icon_three_image"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_margin="@dimen/triple_margin"
android:layout_marginTop="32dp"
android:contentDescription="@string/icon_1"
android:onClick="@{() -> chooseIconViewModel.onChooseIcon(3)}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_text"
app:srcCompat="@drawable/icon_3" />
<ImageButton
android:id="@+id/icon_four_image"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_margin="@dimen/triple_margin"
android:contentDescription="@string/icon_1"
android:onClick="@{() -> chooseIconViewModel.onChooseIcon(4)}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon_two_image"
app:srcCompat="@drawable/icon_4" />
<ImageButton
android:id="@+id/icon_five_image"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_margin="@dimen/triple_margin"
android:layout_marginStart="74dp"
android:layout_marginTop="16dp"
android:contentDescription="@string/icon_1"
android:onClick="@{() -> chooseIconViewModel.onChooseIcon(5)}"
app:layout_constraintEnd_toStartOf="@+id/icon_six_image"
app:layout_constraintStart_toEndOf="@+id/icon_four_image"
app:layout_constraintTop_toBottomOf="@+id/icon_two_image"
app:srcCompat="@drawable/icon_5" />
<ImageButton
android:id="@+id/icon_six_image"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_margin="@dimen/triple_margin"
android:contentDescription="@string/icon_1"
android:onClick="@{() -> chooseIconViewModel.onChooseIcon(6)}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon_two_image"
app:srcCompat="@drawable/icon_6" />
<ImageButton
android:id="@+id/icon_seven_image"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_margin="@dimen/triple_margin"
android:contentDescription="@string/icon_1"
android:onClick="@{() -> chooseIconViewModel.onChooseIcon(7)}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon_five_image"
app:srcCompat="@drawable/icon_7" />
<ImageButton
android:id="@+id/icon_eight_image"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_margin="@dimen/triple_margin"
android:layout_marginTop="105dp"
android:contentDescription="@string/icon_1"
android:onClick="@{() -> chooseIconViewModel.onChooseIcon(8)}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon_five_image"
app:srcCompat="@drawable/icon_8" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View File

@ -1,59 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/navigation"
app:startDestination="@id/studentListFragment">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation"
app:startDestination="@id/studentListFragment"
tools:ignore="ExtraText">
<fragment
android:id="@+id/studentListFragment"
android:name="com.taymath.tutortoolkit.studentlist.StudentListFragment"
android:label="StudentListFragment" >
android:id="@+id/addGradeFragment"
android:name="com.taymath.tutortoolkit.addgrade.AddGradeFragment"
android:label="AddGradeFragment" >
<action
android:id="@+id/action_studentListFragment_to_addStudentFragment"
app:destination="@id/addStudentFragment" />
<action
android:id="@+id/action_studentListFragment_to_studentDetailFragment"
app:destination="@id/studentDetailFragment" />
android:id="@+id/action_addGradeFragment_to_studentListFragment"
app:destination="@id/studentListFragment" />
<argument
android:name="student_id"
app:argType="long" />
</fragment>
<fragment
android:id="@+id/addStudentFragment"
android:name="com.taymath.tutortoolkit.addstudent.AddStudentFragment"
android:label="AddStudentFragment" >
<action
android:id="@+id/action_addStudentFragment_to_studentListFragment"
app:destination="@id/studentListFragment" />
<action
android:id="@+id/action_addStudentFragment_to_chooseIconFragment"
app:destination="@id/chooseIconFragment" />
<action
android:id="@+id/action_addStudentFragment_to_chooseIconFragment2"
app:destination="@id/chooseIconFragment" />
</fragment>
<fragment
android:id="@+id/studentDetailFragment"
android:name="com.taymath.tutortoolkit.studentdetail.StudentDetailFragment"
android:label="StudentDetailFragment" >
<action
android:id="@+id/action_studentDetailFragment_to_studentListFragment"
app:destination="@id/studentListFragment" />
<action
android:id="@+id/action_studentDetailFragment_to_addGradeFragment"
app:destination="@id/addGradeFragment" />
<argument android:name="student_id"
app:argType="long" />
</fragment>
<fragment
android:id="@+id/addGradeFragment"
android:name="com.taymath.tutortoolkit.addgrade.AddGradeFragment"
android:label="AddGradeFragment" >
<argument
android:name="student_id"
app:argType="long" />
<action
android:id="@+id/action_addGradeFragment_to_studentListFragment"
android:id="@+id/action_studentDetailFragment_to_addGradeFragment"
app:destination="@id/addGradeFragment" />
<action
android:id="@+id/action_studentDetailFragment_to_studentListFragment"
app:destination="@id/studentListFragment" />
</fragment>
<fragment
android:id="@+id/studentListFragment"
android:name="com.taymath.tutortoolkit.studentlist.StudentListFragment"
android:label="StudentListFragment" >
<action
android:id="@+id/action_studentListFragment_to_studentDetailFragment"
app:destination="@id/studentDetailFragment" />
<action
android:id="@+id/action_studentListFragment_to_addStudentFragment"
app:destination="@id/addStudentFragment" />
</fragment>
<fragment
android:id="@+id/chooseIconFragment"
android:name="com.taymath.tutortoolkit.chooseicon.ChooseIconFragment"
android:label="ChooseIconFragment" />
android:label="ChooseIconFragment">
<action
android:id="@+id/action_chooseIconFragment_to_studentListFragment"
app:destination="@id/studentListFragment" />
</fragment>
</navigation>