From 3b9451184c60ff9feb1bad8b036f08b6603c85b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 5 Sep 2021 03:15:40 +0200 Subject: [PATCH] Fix preview of second student guardian when first guardian is null (#1473) --- .../modules/studentinfo/StudentInfoAdapter.kt | 4 +- .../studentinfo/StudentInfoFragment.kt | 69 ++++++++----------- .../ui/modules/studentinfo/StudentInfoItem.kt | 3 +- .../studentinfo/StudentInfoPresenter.kt | 25 ++++--- .../ui/modules/studentinfo/StudentInfoView.kt | 4 +- 5 files changed, 48 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoAdapter.kt index 2d838749..60912200 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoAdapter.kt @@ -13,7 +13,7 @@ class StudentInfoAdapter @Inject constructor() : var items = listOf() - var onItemClickListener: (position: Int) -> Unit = {} + var onItemClickListener: (StudentInfoView.Type?) -> Unit = {} var onItemLongClickListener: (text: String) -> Unit = {} @@ -32,7 +32,7 @@ class StudentInfoAdapter @Inject constructor() : studentInfoItemArrow.visibility = if (item.showArrow) VISIBLE else GONE with(root) { - setOnClickListener { onItemClickListener(position) } + setOnClickListener { onItemClickListener(item.viewType) } setOnLongClickListener { onItemLongClickListener(studentInfoItemSubtitle.text.toString()) true diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoFragment.kt index e9ef4137..361a5944 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoFragment.kt @@ -1,6 +1,5 @@ package io.github.wulkanowy.ui.modules.studentinfo -import android.annotation.SuppressLint import android.content.ClipData import android.content.ClipboardManager import android.os.Bundle @@ -130,9 +129,9 @@ class StudentInfoFragment : getString(R.string.student_info_parents_name) to studentInfo.parentsNames ).map { StudentInfoItem( - it.first, - it.second.ifBlank { getString(R.string.all_no_data) }, - false, + title = it.first, + subtitle = it.second.ifBlank { getString(R.string.all_no_data) }, + showArrow = false, ) } ) @@ -146,25 +145,33 @@ class StudentInfoFragment : getString(R.string.student_info_email) to studentInfo.email ).map { StudentInfoItem( - it.first, - it.second.ifBlank { getString(R.string.all_no_data) }, - false, + title = it.first, + subtitle = it.second.ifBlank { getString(R.string.all_no_data) }, + showArrow = false, ) } ) } - @SuppressLint("DefaultLocale") + @OptIn(ExperimentalStdlibApi::class) override fun showFamilyTypeData(studentInfo: StudentInfo) { + val items = buildList { + add(studentInfo.firstGuardian?.let { + Triple(it.kinship.capitalise(), it.fullName, StudentInfoView.Type.FIRST_GUARDIAN) + }) + + add(studentInfo.secondGuardian?.let { + Triple(it.kinship.capitalise(), it.fullName, StudentInfoView.Type.SECOND_GUARDIAN) + }) + }.filterNotNull() + updateData( - listOfNotNull( - studentInfo.firstGuardian?.let { it.kinship.capitalise() to it.fullName }, - studentInfo.secondGuardian?.let { it.kinship.capitalise() to it.fullName }, - ).map { (title, value) -> + items.map { (title, value, type) -> StudentInfoItem( - title.ifBlank { getString(R.string.all_no_data) }, - value.ifBlank { getString(R.string.all_no_data) }, - true, + title = title.ifBlank { getString(R.string.all_no_data) }, + subtitle = value.ifBlank { getString(R.string.all_no_data) }, + showArrow = true, + viewType = type, ) } ) @@ -178,15 +185,15 @@ class StudentInfoFragment : getString(R.string.student_info_correspondence_address) to studentInfo.correspondenceAddress ).map { StudentInfoItem( - it.first, - it.second.ifBlank { getString(R.string.all_no_data) }, - false, + title = it.first, + subtitle = it.second.ifBlank { getString(R.string.all_no_data) }, + showArrow = false, ) } ) } - override fun showFirstGuardianTypeData(studentGuardian: StudentGuardian) { + override fun showGuardianTypeData(studentGuardian: StudentGuardian) { updateData( listOf( getString(R.string.student_info_full_name) to studentGuardian.fullName, @@ -196,27 +203,9 @@ class StudentInfoFragment : getString(R.string.student_info_email) to studentGuardian.email ).map { StudentInfoItem( - it.first, - it.second.ifBlank { getString(R.string.all_no_data) }, - false, - ) - } - ) - } - - override fun showSecondGuardianTypeData(studentGuardian: StudentGuardian) { - updateData( - listOf( - getString(R.string.student_info_full_name) to studentGuardian.fullName, - getString(R.string.student_info_kinship) to studentGuardian.kinship, - getString(R.string.student_info_guardian_address) to studentGuardian.address, - getString(R.string.student_info_phones) to studentGuardian.phones, - getString(R.string.student_info_email) to studentGuardian.email - ).map { - StudentInfoItem( - it.first, - it.second.ifBlank { getString(R.string.all_no_data) }, - false, + title = it.first, + subtitle = it.second.ifBlank { getString(R.string.all_no_data) }, + showArrow = false, ) } ) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoItem.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoItem.kt index bb149b2b..21226539 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoItem.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoItem.kt @@ -3,5 +3,6 @@ package io.github.wulkanowy.ui.modules.studentinfo data class StudentInfoItem( val title: String, val subtitle: String, - val showArrow: Boolean + val showArrow: Boolean, + val viewType: StudentInfoView.Type? = null, ) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoPresenter.kt index 55ac66d0..80798b11 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoPresenter.kt @@ -58,13 +58,12 @@ class StudentInfoPresenter @Inject constructor( view?.showErrorDetailsDialog(lastError) } - fun onItemSelected(position: Int) { - if (infoType != StudentInfoView.Type.FAMILY) return + fun onItemSelected(viewType: StudentInfoView.Type?) { + viewType ?: return view?.openStudentInfoView( - if (position == 0) StudentInfoView.Type.FIRST_GUARDIAN - else StudentInfoView.Type.SECOND_GUARDIAN, - studentWithSemesters + studentWithSemesters = studentWithSemesters, + infoType = viewType, ) } @@ -76,15 +75,19 @@ class StudentInfoPresenter @Inject constructor( flowWithResourceIn { val semester = studentWithSemesters.semesters.getCurrentOrLast() studentInfoRepository.getStudentInfo( - studentWithSemesters.student, - semester, - forceRefresh + student = studentWithSemesters.student, + semester = semester, + forceRefresh = forceRefresh ) }.onEach { when (it.status) { Status.LOADING -> Timber.i("Loading student info $infoType started") Status.SUCCESS -> { - if (it.data != null && !(infoType == StudentInfoView.Type.FAMILY && it.data.firstGuardian == null && it.data.secondGuardian == null)) { + val isFamily = infoType == StudentInfoView.Type.FAMILY + val isFirstGuardianEmpty = it.data?.firstGuardian == null + val isSecondGuardianEmpty = it.data?.secondGuardian == null + + if (it.data != null && !(isFamily && isFirstGuardianEmpty && isSecondGuardianEmpty)) { Timber.i("Loading student info $infoType result: Success") showCorrectData(it.data) view?.run { @@ -122,8 +125,8 @@ class StudentInfoPresenter @Inject constructor( StudentInfoView.Type.CONTACT -> view?.showContactTypeData(studentInfo) StudentInfoView.Type.ADDRESS -> view?.showAddressTypeData(studentInfo) StudentInfoView.Type.FAMILY -> view?.showFamilyTypeData(studentInfo) - StudentInfoView.Type.SECOND_GUARDIAN -> view?.showSecondGuardianTypeData(studentInfo.secondGuardian!!) - StudentInfoView.Type.FIRST_GUARDIAN -> view?.showFirstGuardianTypeData(studentInfo.firstGuardian!!) + StudentInfoView.Type.SECOND_GUARDIAN -> view?.showGuardianTypeData(studentInfo.secondGuardian!!) + StudentInfoView.Type.FIRST_GUARDIAN -> view?.showGuardianTypeData(studentInfo.firstGuardian!!) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoView.kt index 87613e62..c20359df 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/studentinfo/StudentInfoView.kt @@ -25,9 +25,7 @@ interface StudentInfoView : BaseView { fun showFamilyTypeData(studentInfo: StudentInfo) - fun showFirstGuardianTypeData(studentGuardian: StudentGuardian) - - fun showSecondGuardianTypeData(studentGuardian: StudentGuardian) + fun showGuardianTypeData(studentGuardian: StudentGuardian) fun openStudentInfoView(infoType: Type, studentWithSemesters: StudentWithSemesters)