add shared preference
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
noemi3 2019-11-29 09:51:46 +01:00
parent e935380e32
commit d1636ac9d9
2 changed files with 51 additions and 4 deletions

View File

@ -1,10 +1,13 @@
package it.unisannio.ding.ids.wedroid.app
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import it.unisannio.ding.ids.wedroid.app.util.SharedPreferenceHelper
import it.unisannio.ding.ids.wedroid.wrapper.api.LoginService
import it.unisannio.ding.ids.wedroid.wrapper.entity.UserPrototype
import kotlinx.android.synthetic.main.activity_login.*
@ -15,24 +18,27 @@ import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class LoginActivity : AppCompatActivity() {
lateinit var sph: SharedPreferenceHelper
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
sph = SharedPreferenceHelper(this)
val id = sph.userId
val token = sph.token
val url = sph.baseUrl
Log.d("sharedPreference", "userId: $id\n token: $token\n url: $url")
}
fun loginButton(v: View) {
if (username.text.isBlank() || istanceServer.text.isBlank() || password.text.isBlank()) {
if (username.text.isBlank() || istanceServer.text.isBlank() || password.text.isBlank()) {
Toast.makeText(this, "Riempire tutti i campi", Toast.LENGTH_LONG)
.show()
return
}
val userNameText = username.text.toString()
val passwordText = password.text.toString()
@ -54,6 +60,7 @@ class LoginActivity : AppCompatActivity() {
}
override fun onResponse(call: Call<UserPrototype>, response: Response<UserPrototype>) {
if (response.code() != 200) {
Toast.makeText(
applicationContext,
@ -62,6 +69,11 @@ class LoginActivity : AppCompatActivity() {
).show()
return
}
val users = response.body()
sph.baseUrl = istanceServer.text.toString()
sph.token = users?.token
sph.userId = users?.id
Toast.makeText(
applicationContext,
"Login effettuato con successo",

View File

@ -0,0 +1,35 @@
package it.unisannio.ding.ids.wedroid.app.util
import android.content.Context
class SharedPreferenceHelper(context : Context) : PreferenceReader, PreferenceWriter {
private val sp = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE)
override fun getBaseUrl(): String? {
return sp.getString("url", "")
}
override fun getUserId(): String? {
return sp.getString("id", "")
}
override fun getToken(): String? {
return sp.getString("token", "")
}
override fun setBaseUrl(baseUrl: String?) {
val editor = sp.edit()
editor.putString("url", baseUrl).apply()
}
override fun setUserId(userId: String?) {
val editor = sp.edit()
editor.putString("id", userId).apply()
}
override fun setToken(token: String?) {
val editor = sp.edit()
editor.putString("token", token).apply()
}
}