Last active
June 21, 2020 07:48
-
-
Save Nataanthoni/7c62fcb5e814bf58be6bb4ac68b653ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kweracodes.nyumba | |
import android.content.Intent | |
import android.net.Uri | |
import android.os.Bundle | |
import android.view.View | |
import android.widget.Toast | |
import androidx.appcompat.app.AppCompatActivity | |
import com.kweracodes.nyumba.network.LoginResponse | |
import com.kweracodes.nyumba.services.AuthApiClient | |
import com.kweracodes.nyumba.storage.SharedPrefManager | |
import kotlinx.android.synthetic.main.activity_login.* | |
import retrofit2.Call | |
import retrofit2.Callback | |
import retrofit2.Response | |
class LoginActivity : AppCompatActivity() { | |
private lateinit var apiClient: AuthApiClient | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_login) | |
val sharedPrefManager = SharedPrefManager(this) | |
buttonLogin.setOnClickListener { | |
val email = emailaddress.text.toString().trim() | |
val password = clientpassword.text.toString().trim() | |
if (email.isEmpty()) { | |
emailaddress.error = "Email required" | |
emailaddress.requestFocus() | |
return@setOnClickListener | |
} | |
if (password.isEmpty()) { | |
clientpassword.error = "Password required" | |
clientpassword.requestFocus() | |
return@setOnClickListener | |
} | |
progressbarlogin.visibility = View.VISIBLE | |
buttonLogin.visibility = View.GONE | |
apiClient = AuthApiClient | |
apiClient.instance.userLogin(email, password) | |
.enqueue(object : Callback<LoginResponse> { | |
override fun onFailure(call: Call<LoginResponse>, t: Throwable) { | |
Toast.makeText(applicationContext, "Error connecting to the server", Toast.LENGTH_LONG).show() | |
progressbarlogin.visibility = View.GONE | |
buttonLogin.visibility = View.VISIBLE | |
} | |
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) { | |
if (response.isSuccessful) { | |
//Store user session | |
sharedPrefManager.saveUser(response.body()?.user!!) | |
//Save token if login is successful | |
sharedPrefManager.saveAuthToken(response.body()?.token!!) | |
Toast.makeText(this@LoginActivity, "Done saving", Toast.LENGTH_LONG).show() | |
progressbarlogin.visibility = View.GONE | |
Toast.makeText(applicationContext, response.body()?.message, Toast.LENGTH_SHORT).show() | |
val intent = Intent(applicationContext, NyumbaHome::class.java) | |
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | |
startActivity(intent) | |
} else { | |
Toast.makeText(applicationContext, "Sorry, login Unsuccessful. Please check credentials and try again", Toast.LENGTH_LONG).show() | |
progressbarlogin.visibility = View.GONE | |
buttonLogin.visibility = View.VISIBLE | |
} | |
} | |
}) | |
} | |
//start forgot Password | |
forgotpassword.setOnClickListener { | |
val url = "https://www.usenyumba.com/password/reset" | |
val reset = Intent(Intent.ACTION_VIEW) | |
reset.data = Uri.parse(url) | |
startActivity(reset) | |
} | |
signup.setOnClickListener { | |
val intent = Intent(this, RegisterActivity::class.java) | |
startActivity(intent) | |
} | |
} | |
override fun onStart() { | |
super.onStart() | |
val sharedPrefManager = SharedPrefManager.getInstance(this) | |
if (sharedPrefManager.isLoggedIn) { | |
val intent = Intent(applicationContext, NyumbaHome::class.java) | |
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | |
startActivity(intent) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment