diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/LoginActivity.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/LoginActivity.kt index 4107b45..da250cf 100644 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/LoginActivity.kt +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/LoginActivity.kt @@ -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, response: Response) { + 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", diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/SharedPreferenceHelper.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/SharedPreferenceHelper.kt new file mode 100644 index 0000000..2f150c8 --- /dev/null +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/SharedPreferenceHelper.kt @@ -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() + } +} \ No newline at end of file