Skip to content

Commit

Permalink
Feat: 졸업요건/성적 사항 총 평균 계산 구현 #21
Browse files Browse the repository at this point in the history
  • Loading branch information
dkyuuum committed Feb 18, 2024
1 parent 6c775e2 commit 35f9049
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import okhttp3.internal.notify
import umc.com.mobile.project.R
import umc.com.mobile.project.data.model.gradInfo.GradesResponse
import umc.com.mobile.project.data.model.gradInfo.GradesTotalDto
import umc.com.mobile.project.databinding.FragmentGradeBinding
import umc.com.mobile.project.ui.gradInfo.adapter.AverageRVAdapter
import umc.com.mobile.project.ui.gradInfo.adapter.GradeRVAdapter
Expand All @@ -23,6 +22,7 @@ import umc.com.mobile.project.ui.gradInfo.viewmodel.GradeViewModel
class GradeFragment : Fragment() {
private var _binding: FragmentGradeBinding? = null
private val viewModel: GradeViewModel by viewModels()
private var selectedSemesterPosition: String? = null
private val binding get() = _binding!!

override fun onCreateView(
Expand All @@ -43,9 +43,19 @@ class GradeFragment : Fragment() {
})

viewModel.totalAverage.observe(viewLifecycleOwner, Observer { totalAverageGrade ->
binding.tvAverageTotal.text = totalAverageGrade.toString()
binding.tvAverageContent.text = totalAverageGrade.toString()
})

/**
* 총 학점 체크 후 데이터 넣기
*/
viewModel.grades.observe(viewLifecycleOwner, Observer { gradesMap ->
(binding.recyclerView2.adapter as? AverageRVAdapter)?.setData(gradesMap.values.toList())
})

/**
* 총 학점 체크 후 화면 띄우기
*/
viewModel.selectedSemesterGradeAndGrades.observe(viewLifecycleOwner) { pair ->
val selectedGrade = pair.first
val gradesMap = pair.second
Expand All @@ -61,12 +71,13 @@ class GradeFragment : Fragment() {
binding.tvAverageGrade.text = selectedGradeInfo?.averageGrade
binding.tvAverageTotal.text = selectedGradeInfo?.totalGrade
binding.tvPercent.text = selectedGradeInfo?.percentile
}

viewModel.grades.observe(viewLifecycleOwner, Observer { gradesMap ->
(binding.recyclerView2.adapter as? AverageRVAdapter)?.setData(gradesMap.values.toList())
})
selectedSemesterPosition = selectedGrade
}

/**
* 이수 안 한 학기 체크
*/
viewModel.isNullCheckGrade.observe(viewLifecycleOwner) {
if (it) {
showDialog()
Expand All @@ -79,6 +90,7 @@ class GradeFragment : Fragment() {
override fun onDestroyView() {
super.onDestroyView()
_binding = null
selectedSemesterPosition = null
}

private fun initRecyclerView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class GradeViewModel : ViewModel() {
private val selectedSemesterGrade: LiveData<String>
get() = _selectedSemesterGrade

private val _totalAverage: MutableLiveData<Double> = MutableLiveData()
val totalAverage: LiveData<Double>
private val _totalAverage: MutableLiveData<String> = MutableLiveData()
val totalAverage: LiveData<String>
get() = _totalAverage

private val _isNullCheckGrade: MutableLiveData<Boolean> = MutableLiveData()
Expand All @@ -67,6 +67,7 @@ class GradeViewModel : ViewModel() {
val gradesTotalMap = mutableMapOf<String, GradesTotalDto>()
val semestersDto = gradesResponse.result.semesters
var count = 1
var total = 0.0

semestersDto.let {
for ((semester, semesterClasses) in it) {
Expand All @@ -88,15 +89,14 @@ class GradeViewModel : ViewModel() {
val newGradeKey = "$count 학기 성적"

gradesTotalMap[newGradeKey] = gradeTotalDto

total += gradeTotalDto.averageGrade.toDouble()
count++
}
Log.d("Grade: gradeTotalList ", "$gradesTotalMap")

count = 0
// Log.d("Grade: gradeTotalList ", "$gradesTotalMap")
}
_semesters?.postValue(semestersMap)
_grades.postValue(gradesTotalMap)
_totalAverage.postValue(String.format("%.2f", total / (count - 1)))
}

fun getGradeInfo() {
Expand All @@ -110,7 +110,7 @@ class GradeViewModel : ViewModel() {
if (userResponse != null) {
_gradesInfo.postValue(userResponse)
processRequiredBasicCourses(userResponse)
Log.d("gradInfo", "${response.body()}")
// Log.d("gradInfo", "${response.body()}")
} else {
_error.postValue("서버 응답이 올바르지 않습니다.")
}
Expand Down Expand Up @@ -141,10 +141,6 @@ class GradeViewModel : ViewModel() {
_selectedSemesterGrade.postValue(grade)
}

fun onSetTotalAverageGrade(totalAverageGrade: Double, totalNumber: Int) {
_totalAverage.postValue((totalAverageGrade/totalNumber))
}

fun onSetNullCheckGrade(flag: Boolean) {
_isNullCheckGrade.postValue(flag)
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/layout/fragment_grade.xml
Original file line number Diff line number Diff line change
Expand Up @@ -349,20 +349,20 @@
app:layout_constraintTop_toBottomOf="@+id/tv_grade_average_title">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_average_content"
android:id="@+id/tv_average_title"
style="@style/MediumFont.13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="13dp"
android:text="총 평균"
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/appCompatTextView9"
app:layout_constraintBottom_toTopOf="@+id/tv_average_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/appCompatTextView9"
android:id="@+id/tv_average_content"
style="@style/BoldFont.20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -371,7 +371,7 @@
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_average_content" />
app:layout_constraintTop_toBottomOf="@+id/tv_average_title" />

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down

0 comments on commit 35f9049

Please sign in to comment.