1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-09-20 08:39:08 -05:00

Disable signed in students in login (#677)

This commit is contained in:
Dominik Korsa 2020-02-04 09:43:24 +01:00 committed by GitHub
parent 2f56f7e4a4
commit 720a530a6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 15 deletions

View File

@ -2,6 +2,8 @@ package io.github.wulkanowy.ui.modules.login.studentselect
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.view.View import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
import eu.davidea.flexibleadapter.items.IFlexible import eu.davidea.flexibleadapter.items.IFlexible
@ -10,12 +12,16 @@ import io.github.wulkanowy.R
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import kotlinx.android.extensions.LayoutContainer import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.item_login_student_select.* import kotlinx.android.synthetic.main.item_login_student_select.*
import timber.log.Timber
class LoginStudentSelectItem(val student: Student) : AbstractFlexibleItem<LoginStudentSelectItem.ItemViewHolder>() { class LoginStudentSelectItem(val student: Student, val alreadySaved: Boolean) :
AbstractFlexibleItem<LoginStudentSelectItem.ItemViewHolder>() {
override fun getLayoutRes() = R.layout.item_login_student_select override fun getLayoutRes() = R.layout.item_login_student_select
override fun createViewHolder(view: View, adapter: FlexibleAdapter<IFlexible<*>>): ItemViewHolder { override fun createViewHolder(view: View, adapter: FlexibleAdapter<IFlexible<*>>): ItemViewHolder {
Timber.i("createViewHolder()")
Timber.i(alreadySaved.toString())
return ItemViewHolder(view, adapter) return ItemViewHolder(view, adapter)
} }
@ -24,6 +30,10 @@ class LoginStudentSelectItem(val student: Student) : AbstractFlexibleItem<LoginS
holder.apply { holder.apply {
loginItemName.text = "${student.studentName} ${student.className}" loginItemName.text = "${student.studentName} ${student.className}"
loginItemSchool.text = student.schoolName loginItemSchool.text = student.schoolName
loginItemName.isEnabled = !alreadySaved
loginItemSchool.isEnabled = !alreadySaved
loginItemCheck.isEnabled = !alreadySaved
loginItemSignedIn.visibility = if (alreadySaved) VISIBLE else GONE
} }
} }
@ -34,6 +44,7 @@ class LoginStudentSelectItem(val student: Student) : AbstractFlexibleItem<LoginS
other as LoginStudentSelectItem other as LoginStudentSelectItem
if (student != other.student) return false if (student != other.student) return false
if (alreadySaved != other.alreadySaved) return false
return true return true
} }
@ -42,7 +53,8 @@ class LoginStudentSelectItem(val student: Student) : AbstractFlexibleItem<LoginS
return student.hashCode() return student.hashCode()
} }
class ItemViewHolder(view: View, adapter: FlexibleAdapter<*>) : FlexibleViewHolder(view, adapter), LayoutContainer { class ItemViewHolder(view: View, val adapter: FlexibleAdapter<*>) :
FlexibleViewHolder(view, adapter), LayoutContainer {
override val containerView: View override val containerView: View
get() = itemView get() = itemView
@ -53,7 +65,10 @@ class LoginStudentSelectItem(val student: Student) : AbstractFlexibleItem<LoginS
override fun onClick(view: View?) { override fun onClick(view: View?) {
super.onClick(view) super.onClick(view)
if (loginItemCheck.isEnabled) {
loginItemCheck.apply { isChecked = !isChecked } loginItemCheck.apply { isChecked = !isChecked }
} }
} }
} }
}

View File

@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.modules.login.LoginErrorHandler
import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.FirebaseAnalyticsHelper
import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.ifNullOrBlank import io.github.wulkanowy.utils.ifNullOrBlank
import io.reactivex.Single
import timber.log.Timber import timber.log.Timber
import java.io.Serializable import java.io.Serializable
import javax.inject.Inject import javax.inject.Inject
@ -50,7 +51,7 @@ class LoginStudentSelectPresenter @Inject constructor(
} }
fun onItemSelected(item: AbstractFlexibleItem<*>?) { fun onItemSelected(item: AbstractFlexibleItem<*>?) {
if (item is LoginStudentSelectItem) { if (item is LoginStudentSelectItem && !item.alreadySaved) {
selectedStudents.removeAll { it == item.student } selectedStudents.removeAll { it == item.student }
.let { if (!it) selectedStudents.add(item.student) } .let { if (!it) selectedStudents.add(item.student) }
@ -58,12 +59,34 @@ class LoginStudentSelectPresenter @Inject constructor(
} }
} }
private fun compareStudents(a: Student, b: Student): Boolean {
return a.email == b.email
&& a.symbol == b.symbol
&& a.studentId == b.studentId
&& a.schoolSymbol == b.schoolSymbol
&& a.classId == b.classId
}
private fun loadData(students: List<Student>) { private fun loadData(students: List<Student>) {
this.students = students this.students = students
view?.apply { disposable.add(studentRepository.getSavedStudents()
updateData(students.map { LoginStudentSelectItem(it) }) .map { savedStudents ->
students.map { student ->
Pair(student, savedStudents.any { compareStudents(student, it) })
} }
} }
.subscribeOn(schedulers.backgroundThread)
.observeOn(schedulers.mainThread)
.subscribe({
view?.updateData(it.map { studentPair ->
LoginStudentSelectItem(studentPair.first, studentPair.second)
})
}, {
errorHandler.dispatch(it)
view?.updateData(students.map { student -> LoginStudentSelectItem(student, false) })
})
)
}
private fun registerStudents(students: List<Student>) { private fun registerStudents(students: List<Student>) {
disposable.add(studentRepository.saveStudents(students) disposable.add(studentRepository.saveStudents(students)

View File

@ -2,8 +2,11 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/loginItemContainer" android:id="@+id/loginItemContainer"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="72dp" android:layout_height="wrap_content"
android:background="?selectableItemBackground"> android:minHeight="72dp"
android:background="?selectableItemBackground"
android:paddingBottom="8dp"
android:paddingTop="8dp">
<com.google.android.material.checkbox.MaterialCheckBox <com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/loginItemCheck" android:id="@+id/loginItemCheck"
@ -11,16 +14,16 @@
android:layout_height="24dp" android:layout_height="24dp"
android:layout_marginStart="12dp" android:layout_marginStart="12dp"
android:layout_marginLeft="12dp" android:layout_marginLeft="12dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="28dp" android:layout_marginEnd="28dp"
android:layout_marginRight="28dp" android:layout_marginRight="28dp"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:layout_centerVertical="true"
tools:text=" " /> tools:text=" " />
<TextView <TextView
android:id="@+id/loginItemName" android:id="@+id/loginItemName"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="32dp" android:layout_height="wrap_content"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
android:layout_toEndOf="@id/loginItemCheck" android:layout_toEndOf="@id/loginItemCheck"
@ -34,7 +37,7 @@
<TextView <TextView
android:id="@+id/loginItemSchool" android:id="@+id/loginItemSchool"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="20dp" android:layout_height="wrap_content"
android:layout_below="@id/loginItemName" android:layout_below="@id/loginItemName"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
@ -42,8 +45,27 @@
android:layout_toRightOf="@id/loginItemCheck" android:layout_toRightOf="@id/loginItemCheck"
android:ellipsize="end" android:ellipsize="end"
android:gravity="bottom" android:gravity="bottom"
android:maxLines="2"
android:textColor="?android:textColorSecondary"
android:textSize="14sp"
tools:text="@tools:sample/lorem/random" />
<TextView
android:id="@+id/loginItemSignedIn"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="@id/loginItemSchool"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_toEndOf="@id/loginItemCheck"
android:layout_toRightOf="@id/loginItemCheck"
android:ellipsize="end"
android:gravity="bottom"
android:maxLines="1" android:maxLines="1"
android:textColor="?android:textColorSecondary" android:textColor="?android:textColorSecondary"
android:textSize="14sp" android:textSize="14sp"
tools:text="@tools:sample/lorem/random" /> android:text="@string/login_signed_in"
android:enabled="false"
android:visibility="gone"
tools:visibility="visible" />
</RelativeLayout> </RelativeLayout>

View File

@ -52,6 +52,7 @@
<string name="login_contact_email">Email</string> <string name="login_contact_email">Email</string>
<string name="login_contact_discord">Discord</string> <string name="login_contact_discord">Discord</string>
<string name="login_email_intent_title">Wyślij email</string> <string name="login_email_intent_title">Wyślij email</string>
<string name="login_signed_in">Uczeń jest już zalogowany</string>
<!--Main--> <!--Main-->

View File

@ -53,6 +53,7 @@
<string name="login_contact_email">Электронная почта</string> <string name="login_contact_email">Электронная почта</string>
<string name="login_contact_discord">Discord</string> <string name="login_contact_discord">Discord</string>
<string name="login_email_intent_title">Отправить письмо</string> <string name="login_email_intent_title">Отправить письмо</string>
<string name="login_signed_in">Студент уже вошел в систему</string>
<!--Main--> <!--Main-->

View File

@ -54,6 +54,7 @@
<string name="login_email_intent_title">Send email</string> <string name="login_email_intent_title">Send email</string>
<string name="login_email_subject" translatable="false">Zgłoszenie: Problemy z logowaniem</string> <string name="login_email_subject" translatable="false">Zgłoszenie: Problemy z logowaniem</string>
<string name="login_email_text" translatable="false">Informacje o aplikacji:\n\nUrządzenie: %1$s\nWersja SDK: %2$s\nWersja aplikacji: %3$s\n\nOpis problemu:</string> <string name="login_email_text" translatable="false">Informacje o aplikacji:\n\nUrządzenie: %1$s\nWersja SDK: %2$s\nWersja aplikacji: %3$s\n\nOpis problemu:</string>
<string name="login_signed_in">Student is already signed in</string>
<!--Main--> <!--Main-->

View File

@ -53,7 +53,7 @@ class LoginStudentSelectPresenterTest {
fun onSelectedStudentTest() { fun onSelectedStudentTest() {
doReturn(Single.just(listOf(1L))).`when`(studentRepository).saveStudents(listOf(testStudent)) doReturn(Single.just(listOf(1L))).`when`(studentRepository).saveStudents(listOf(testStudent))
doReturn(Completable.complete()).`when`(studentRepository).switchStudent(testStudent) doReturn(Completable.complete()).`when`(studentRepository).switchStudent(testStudent)
presenter.onItemSelected(LoginStudentSelectItem(testStudent)) presenter.onItemSelected(LoginStudentSelectItem(testStudent, false))
presenter.onSignIn() presenter.onSignIn()
verify(loginStudentSelectView).showContent(false) verify(loginStudentSelectView).showContent(false)
verify(loginStudentSelectView).showProgress(true) verify(loginStudentSelectView).showProgress(true)
@ -64,7 +64,7 @@ class LoginStudentSelectPresenterTest {
fun onSelectedStudentErrorTest() { fun onSelectedStudentErrorTest() {
doReturn(Single.error<Student>(testException)).`when`(studentRepository).saveStudents(listOf(testStudent)) doReturn(Single.error<Student>(testException)).`when`(studentRepository).saveStudents(listOf(testStudent))
doReturn(Completable.complete()).`when`(studentRepository).logoutStudent(testStudent) doReturn(Completable.complete()).`when`(studentRepository).logoutStudent(testStudent)
presenter.onItemSelected(LoginStudentSelectItem(testStudent)) presenter.onItemSelected(LoginStudentSelectItem(testStudent, false))
presenter.onSignIn() presenter.onSignIn()
verify(loginStudentSelectView).showContent(false) verify(loginStudentSelectView).showContent(false)
verify(loginStudentSelectView).showProgress(true) verify(loginStudentSelectView).showProgress(true)