PasswordDataSourceImpl.kt 3.06 KB
Newer Older
1 2 3
package de.akamu.tudarmstadt.data.password

import android.os.AsyncTask
Niklas Fix's avatar
Niklas Fix committed
4
import de.akamu.tudarmstadt.api.V2APIForgotPassword
5 6 7 8
import de.akamu.tudarmstadt.api.V2APILogin
import de.akamu.tudarmstadt.exceptions.AkamuAPIException
import de.akamu.tudarmstadt.model.Login
import de.akamu.tudarmstadt.model.User
Niklas Fix's avatar
Niklas Fix committed
9
import java.lang.Exception
10 11 12

class PasswordDataSourceImpl : PasswordDataSource {

Niklas Fix's avatar
Niklas Fix committed
13 14 15 16 17 18
    override fun verifyPassword(
        user: User,
        password: String,
        firebaseToken: String,
        callback: PasswordDataSource.VerifyPasswordCallback
    ) {
Niklas Fix's avatar
Niklas Fix committed
19
        val task = VerifyPasswordTask(user, password, firebaseToken, callback)
20
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
21 22 23

    }

Niklas Fix's avatar
Niklas Fix committed
24 25 26 27 28 29
    class VerifyPasswordTask(
        private val user: User,
        private val password: String,
        private val firebaseToken: String,
        private val callback: PasswordDataSource.VerifyPasswordCallback
    ) : AsyncTask<Void, Void, Boolean>() {
30 31 32 33 34 35

        private var errorCode = 0

        override fun doInBackground(vararg params: Void?): Boolean {

            try {
Niklas Fix's avatar
Niklas Fix committed
36
                V2APILogin.login(Login(user.username, password, firebaseToken))
37 38 39
            } catch (e: AkamuAPIException) {
                errorCode = e.code
                return false
Niklas Fix's avatar
Niklas Fix committed
40 41
            } catch (e: Exception) {
                return false
42 43 44 45 46 47
            }

            return true
        }

        override fun onPostExecute(result: Boolean?) {
NiklasFix's avatar
NiklasFix committed
48 49 50 51
            when {
                result!! -> callback.passwordCorrect()
                errorCode == 401 -> callback.passwordInCorrect()
                else -> callback.passwordError("Failed.")
52 53 54 55 56
            }

        }

        override fun onCancelled() {
NiklasFix's avatar
NiklasFix committed
57
            callback.passwordError("Cancelled.")
58 59 60 61
        }

    }

Niklas Fix's avatar
Niklas Fix committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    override fun requestNewPassword(
        usernameOrEmail: String,
        callback: PasswordDataSource.RequestNewPasswordCallback
    ) {
        val task = RequestNewPasswordTask(usernameOrEmail, callback)
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
    }

    class RequestNewPasswordTask(
        private val usernameOrEmail: String,
        private val callback: PasswordDataSource.RequestNewPasswordCallback
    ) : AsyncTask<Void, Void, Boolean>() {

        private var errorCode = 0
        private var errorMsg = "Oops. Something went wrong..."

        override fun doInBackground(vararg params: Void?): Boolean {

            try {
                V2APIForgotPassword.requestNewPassword(usernameOrEmail)
            } catch (e: AkamuAPIException) {
                errorCode = e.code
                errorMsg = e.message!!
                return false
            } catch (e: Exception) {
                errorMsg = e.message!!
                return false
            }

            return true
        }

        override fun onPostExecute(result: Boolean?) {
            when {
                result!! -> callback.onRequestNewPasswordSuccess()
                errorCode == 404 -> callback.onNoSuchUserError()
                else -> callback.onRequestNewPasswordFailed(errorMsg)
            }

        }

    }

106
}