From 4e3864f26ff01cc6d2f3289d737869a58f2f02f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sat, 30 Mar 2019 09:31:30 +0100 Subject: [PATCH] Add opening login view on no current student (#312) --- .../wulkanowy/data/exceptions/NoCurrentStudent.kt | 3 +++ .../data/repositories/student/StudentRepository.kt | 3 ++- .../wulkanowy/ui/base/session/BaseSessionFragment.kt | 10 ++++++++++ .../wulkanowy/ui/base/session/BaseSessionPresenter.kt | 5 ++++- .../wulkanowy/ui/base/session/BaseSessionView.kt | 2 ++ .../wulkanowy/ui/base/session/SessionErrorHandler.kt | 5 +++++ 6 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/data/exceptions/NoCurrentStudent.kt diff --git a/app/src/main/java/io/github/wulkanowy/data/exceptions/NoCurrentStudent.kt b/app/src/main/java/io/github/wulkanowy/data/exceptions/NoCurrentStudent.kt new file mode 100644 index 00000000..58a2396e --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/data/exceptions/NoCurrentStudent.kt @@ -0,0 +1,3 @@ +package io.github.wulkanowy.data.exceptions + +class NoCurrentStudentException : Exception("There no set current student in database") diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt index 7ddff0aa..b4b7c828 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt @@ -4,6 +4,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import io.github.wulkanowy.data.ApiHelper import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.data.exceptions.NoCurrentStudentException import io.reactivex.Completable import io.reactivex.Maybe import io.reactivex.Single @@ -36,7 +37,7 @@ class StudentRepository @Inject constructor( fun getCurrentStudent(decryptPass: Boolean = true): Single { return local.getCurrentStudent(decryptPass) - .switchIfEmpty(Maybe.error(NoSuchElementException("No current student"))) + .switchIfEmpty(Maybe.error(NoCurrentStudentException())) .toSingle() } diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionFragment.kt index 42622b50..110729d4 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionFragment.kt @@ -1,6 +1,9 @@ package io.github.wulkanowy.ui.base.session +import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK +import android.content.Intent.FLAG_ACTIVITY_NEW_TASK import io.github.wulkanowy.ui.base.BaseFragment +import io.github.wulkanowy.ui.modules.login.LoginActivity import io.github.wulkanowy.ui.modules.main.MainActivity open class BaseSessionFragment : BaseFragment(), BaseSessionView { @@ -8,4 +11,11 @@ open class BaseSessionFragment : BaseFragment(), BaseSessionView { override fun showExpiredDialog() { (activity as? MainActivity)?.showExpiredDialog() } + + override fun openLoginView() { + activity?.also { + startActivity(LoginActivity.getStartIntent(it) + .apply { addFlags(FLAG_ACTIVITY_CLEAR_TASK or FLAG_ACTIVITY_NEW_TASK) }) + } + } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionPresenter.kt index 17ed6537..2c9f50ea 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionPresenter.kt @@ -7,6 +7,9 @@ open class BaseSessionPresenter(private val errorHandler: S override fun onAttachView(view: T) { super.onAttachView(view) - errorHandler.onDecryptionFail = { view.showExpiredDialog() } + errorHandler.apply { + onDecryptionFail = { view.showExpiredDialog() } + onNoCurrentStudent = { view.openLoginView() } + } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionView.kt b/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionView.kt index 81ce4d8f..619c2863 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/session/BaseSessionView.kt @@ -5,4 +5,6 @@ import io.github.wulkanowy.ui.base.BaseView interface BaseSessionView : BaseView { fun showExpiredDialog() + + fun openLoginView() } diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/session/SessionErrorHandler.kt b/app/src/main/java/io/github/wulkanowy/ui/base/session/SessionErrorHandler.kt index 1d81e932..017eabba 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/session/SessionErrorHandler.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/session/SessionErrorHandler.kt @@ -2,6 +2,7 @@ package io.github.wulkanowy.ui.base.session import android.content.res.Resources import com.readystatesoftware.chuck.api.ChuckCollector +import io.github.wulkanowy.data.exceptions.NoCurrentStudentException import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.security.ScramblerException import javax.inject.Inject @@ -13,9 +14,12 @@ open class SessionErrorHandler @Inject constructor( var onDecryptionFail: () -> Unit = {} + var onNoCurrentStudent: () -> Unit = {} + override fun proceed(error: Throwable) { when (error) { is ScramblerException -> onDecryptionFail() + is NoCurrentStudentException -> onNoCurrentStudent() else -> super.proceed(error) } } @@ -23,5 +27,6 @@ open class SessionErrorHandler @Inject constructor( override fun clear() { super.clear() onDecryptionFail = {} + onNoCurrentStudent = {} } }