Skip to content

Instantly share code, notes, and snippets.

@Nataanthoni
Last active June 21, 2020 07:48
Show Gist options
  • Save Nataanthoni/7c62fcb5e814bf58be6bb4ac68b653ae to your computer and use it in GitHub Desktop.
Save Nataanthoni/7c62fcb5e814bf58be6bb4ac68b653ae to your computer and use it in GitHub Desktop.
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)
}
}
}
package com.kweracodes.nyumba.storage
import android.content.Context
import com.kweracodes.nyumba.models.User
class SharedPrefManager constructor(private val mCtx: Context) {
val isLoggedIn: Boolean
get() {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
return sharedPreferences.getInt("id", -1) != -1
}
val user: User
get() {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
return User(
sharedPreferences.getString("about", null),
sharedPreferences.getString("address", null),
sharedPreferences.getString("city", null),
sharedPreferences.getString("company", null),
sharedPreferences.getString("createdAt", null),
sharedPreferences.getString("email", null),
sharedPreferences.getString("emailVerifiedAt", ""),
sharedPreferences.getString("fname", ""),
sharedPreferences.getInt("id", -1),
sharedPreferences.getString("lname", null),
sharedPreferences.getString("locality", null),
sharedPreferences.getString("phone", null),
sharedPreferences.getString("preferredComm", null),
sharedPreferences.getString("profilePic", null),
sharedPreferences.getString("updatedAt", null)
)
}
fun saveUser(user: User) {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putInt("id", user.id)
editor.putString("email", user.email)
editor.putString("lname", user.lname)
editor.putString("lname", user.fname)
editor.putString("lname", user.about)
editor.putString("lname", user.address)
editor.putString("lname", user.emailVerifiedAt.toString())
editor.putString("lname", user.city)
editor.putString("lname", user.locality)
editor.putString("lname", user.company)
editor.putString("lname", user.createdAt)
editor.putString("lname", user.phone)
editor.putString("lname", user.preferredComm)
editor.putString("school", user.updatedAt)
editor.apply()
}
fun saveAuthToken(token: String?) {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString(AUTH_TOKEN, token)
editor.apply()
}
fun fetchAuthToken(): String? {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
return sharedPreferences.getString(SharedPrefManager.AUTH_TOKEN, null)
}
fun clear() {
val sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.clear()
editor.apply()
}
companion object {
private val SHARED_PREF_NAME = "my_shared_preff"
private var mInstance: SharedPrefManager? = null
private val AUTH_TOKEN = ""
@Synchronized
fun getInstance(mCtx: Context): SharedPrefManager {
if (mInstance == null) {
mInstance = SharedPrefManager(mCtx)
}
return mInstance as SharedPrefManager
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment