Implemented null check for cookies to prevent app crash, tweaked HelperFunctions#assignRanking to prepare for future years

This commit is contained in:
Deniz Düzgören
2019-09-27 21:49:39 +02:00
parent 2e4f423586
commit 54acd982dc
7 changed files with 38 additions and 14 deletions
@@ -18,11 +18,13 @@ import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
import com.denizd.substitutionplan.R
import com.denizd.substitutionplan.data.HelperFunctions
import com.denizd.substitutionplan.data.LoginWebViewClient
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
import com.google.android.material.snackbar.Snackbar
import kotlin.math.hypot
/**
@@ -34,11 +36,13 @@ internal class Login : AppCompatActivity(R.layout.activity_login_webview), Login
private lateinit var prefs: SharedPreferences
private lateinit var parentLayout: ConstraintLayout
private lateinit var logInButton: ExtendedFloatingActionButton
private lateinit var snackBarLayout: CoordinatorLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTheme(R.style.AppTheme0)
parentLayout = findViewById(R.id.constraintLayout)
snackBarLayout = findViewById(R.id.snackBarLayout)
parentLayout.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
val title = findViewById<TextView>(R.id.txtWelcome)
prefs = PreferenceManager.getDefaultSharedPreferences(this)
@@ -65,6 +69,8 @@ internal class Login : AppCompatActivity(R.layout.activity_login_webview), Login
override fun onLoginSucceeded(success: Boolean) {
if (success) {
success()
} else {
Snackbar.make(snackBarLayout, getString(R.string.error_please_try_again), Snackbar.LENGTH_LONG).show()
}
}
@@ -323,15 +323,15 @@ internal object HelperFunctions {
* @return the ranking as an integer
*/
fun assignRanking(group: String, isPSA: Boolean): Int {
if (isPSA) return -31
if (isPSA) return -102
return try {
when {
checkStringForArray(group.substring(0, 1), juniors) -> -30
group.substring(0, 1) == "1" || group.substring(0, 1) == "2" -> {
val a = group.substring(1, 2).toInt()
a - (a * 2)
group.substring(0, 2).toIntOrNull() != null -> {
val a = group.substring(0, 2).toInt()
-a
}
else -> -29
checkStringForArray(group.substring(0, 1), juniors) -> -101
else -> -100
}
} catch (e: Exception) {
0
@@ -15,6 +15,10 @@ import java.lang.IllegalStateException
*/
internal class LoginWebViewClient(private val successListener: OnLoginSuccessListener) : WebViewClient() {
private val schoolUrls = arrayOf("https://307.joomla.schule.bremen.de/index.php/component/users/#top",
"https://307.joomla.schule.bremen.de/index.php/component/users/?task=user.login&Itemid=171",
"https://307.joomla.schule.bremen.de/index.php/component/users/profile?Itemid=171")
/**
* onPageStarted has been overridden to prevent users from reaching a domain different from
* the school's login page
@@ -22,9 +26,7 @@ internal class LoginWebViewClient(private val successListener: OnLoginSuccessLis
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
if (url != "https://307.joomla.schule.bremen.de/index.php/component/users/profile?Itemid=171"
&& url != "https://307.joomla.schule.bremen.de/index.php/component/users/#top"
&& url != "https://307.joomla.schule.bremen.de/index.php/component/users/?task=user.login&Itemid=171") {
if (!HelperFunctions.checkStringForArray(url.toString(), schoolUrls)) {
reloadLoginPage(webView = view)
}
}
@@ -39,7 +41,10 @@ internal class LoginWebViewClient(private val successListener: OnLoginSuccessLis
val cookies = CookieManager.getInstance().getCookie(url)
if (cookies.contains("joomla_user_state=logged_in")) {
if (cookies == null) {
reloadLoginPage(webView = view)
successListener.onLoginSucceeded(false)
} else if (cookies.contains("joomla_user_state=logged_in")) {
successListener.onLoginSucceeded(true)
}
}
@@ -54,10 +59,10 @@ internal class LoginWebViewClient(private val successListener: OnLoginSuccessLis
internal interface OnLoginSuccessListener {
/**
* This function can be overridden to execute code when a login has been successful
* This function returns a boolean value depending on the success status of the login process
*
* @param success boolean value that is set to true if the cookie could be found,
* false otherwise
* false if an error occurred
*/
fun onLoginSucceeded(success: Boolean)
}