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