Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into CU04
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/src/main/java/es/unex/giis/asee/gepeto/model/Receta.kt
#	app/src/main/java/es/unex/giis/asee/gepeto/utils/CredentialCheck.kt
#	app/src/main/java/es/unex/giis/asee/gepeto/view/JoinActivity.kt
#	app/src/main/java/es/unex/giis/asee/gepeto/view/LoginActivity.kt
#	app/src/main/java/es/unex/giis/asee/gepeto/view/home/HomeActivity.kt
#	app/src/main/java/es/unex/giis/asee/gepeto/view/home/SettingsFragment.kt
  • Loading branch information
BaderAbb committed Nov 23, 2023
2 parents f19cb4e + 1eedf5f commit 0cb64dc
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package es.unex.giis.asee.gepeto.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import es.unex.giis.asee.gepeto.database.dao.UserDao
import es.unex.giis.asee.gepeto.model.User

@Database(entities = [User::class], version = 2)
abstract class GepetoDatabase : RoomDatabase() {
abstract fun userDao(): UserDao

companion object {
private var INSTANCE: GepetoDatabase? = null

fun getInstance(context: Context): GepetoDatabase? {
if (INSTANCE == null) {
synchronized(GepetoDatabase::class) {
INSTANCE = Room.databaseBuilder(
context,
GepetoDatabase::class.java, "gepeto.db"
).build()
}
}
return INSTANCE
}

fun destroyInstance() {
INSTANCE = null
}
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/es/unex/giis/asee/gepeto/database/dao/UserDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package es.unex.giis.asee.gepeto.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import es.unex.giis.asee.gepeto.model.User

@Dao
interface UserDao {

@Query("SELECT * FROM user WHERE name LIKE :first LIMIT 1")
suspend fun findByName(first: String): User

@Update( onConflict = OnConflictStrategy.REPLACE )
suspend fun update(user: User)

@Insert
suspend fun insert(user: User): Long

}
34 changes: 29 additions & 5 deletions app/src/main/java/es/unex/giis/asee/gepeto/view/JoinActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.Intent
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.lifecycle.lifecycleScope
import es.unex.giis.asee.gepeto.database.GepetoDatabase

import es.unex.giis.asee.gepeto.databinding.ActivityJoinBinding
import es.unex.giis.asee.gepeto.model.User
Expand All @@ -16,6 +17,7 @@ import kotlinx.coroutines.launch

class JoinActivity : AppCompatActivity() {

private lateinit var db: GepetoDatabase

private lateinit var binding: ActivityJoinBinding

Expand All @@ -37,16 +39,12 @@ class JoinActivity : AppCompatActivity() {
binding = ActivityJoinBinding.inflate(layoutInflater)
setContentView(binding.root)

db = GepetoDatabase.getInstance(applicationContext)!!

//views initialization and listeners
setUpUI()
setUpListeners()
}

private fun setUpUI() {
//get attributes from xml using binding
}

private fun setUpListeners() {
with(binding) {
btRegister.setOnClickListener {
Expand All @@ -56,7 +54,33 @@ class JoinActivity : AppCompatActivity() {
}

private fun join() {
with(binding) {
val check = CredentialCheck.join(
etUsername.text.toString(),
etPassword.text.toString(),
etRepassword.text.toString()
)
if (check.fail) notifyInvalidCredentials(check.msg)
else {
lifecycleScope.launch{
val user = User(
null,
etUsername.text.toString(),
etPassword.text.toString()
)
val id = db?.userDao()?.insert(user)

navigateBackWithResult(
User(
id,
etUsername.text.toString(),
etPassword.text.toString()
)
)
}

}
}
}

private fun navigateBackWithResult(user: User) {
Expand Down
44 changes: 34 additions & 10 deletions app/src/main/java/es/unex/giis/asee/gepeto/view/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import es.unex.giis.asee.gepeto.R
import es.unex.giis.asee.gepeto.data.Session
import es.unex.giis.asee.gepeto.database.GepetoDatabase

import es.unex.giis.asee.gepeto.databinding.ActivityLoginBinding
import es.unex.giis.asee.gepeto.model.User
Expand All @@ -22,6 +23,8 @@ class LoginActivity : AppCompatActivity() {

private lateinit var binding: ActivityLoginBinding

private lateinit var db: GepetoDatabase

private val responseLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
Expand Down Expand Up @@ -50,6 +53,8 @@ class LoginActivity : AppCompatActivity() {
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)

db = GepetoDatabase.getInstance(applicationContext)!!

//views initialization and listeners
setUpUI()
setUpListeners()
Expand Down Expand Up @@ -81,29 +86,48 @@ class LoginActivity : AppCompatActivity() {
correctLogin()
}

btRegister.setOnClickListener {
navigateToJoin()
}

btRestore.setOnClickListener {
navigateToRestore()
}

}
}

private fun correctLogin(){
val credentialCheck = CredentialCheck.login(binding.etUsername.text.toString(), binding.etPassword.text.toString())

//crea un nuevo usuario prueba
val user = User(1, binding.etUsername.text.toString(), "")
if (credentialCheck.fail) {
notifyInvalidCredentials(credentialCheck.msg)
return
}

lifecycleScope.launch{
val user = db?.userDao()?.findByName(binding.etUsername.text.toString()) //?: User(-1, etUsername.text.toString(), etPassword.text.toString())
if (user != null) {
// db.userDao().insert(User(-1, etUsername.text.toString(), etPassword.text.toString()))
val passwordCheck = CredentialCheck.passwordOk(binding.etPassword.text.toString(), user.password)
if (passwordCheck.fail) notifyInvalidCredentials(passwordCheck.msg)
else Toast.makeText(this@LoginActivity, "Login successful", Toast.LENGTH_SHORT).show()
}
else notifyInvalidCredentials("Invalid username")
}
}

navigateToHomeActivity(user)
private fun navigateToJoin() {
JoinActivity.start(this, responseLauncher)
}

private fun navigateToHomeActivity(user: User) {
Toast.makeText(this, "Welcome ${user.name}!!", Toast.LENGTH_SHORT).show()
Session.setValue("user", user)
HomeActivity.start(this, user)
private fun navigateToRestore() {
val showPopUp = PopUpFragment()
showPopUp.show(supportFragmentManager, "showPopUp")
}


private fun notifyInvalidCredentials(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package es.unex.giis.asee.gepeto.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import es.unex.giis.asee.gepeto.database.GepetoDatabase
import es.unex.giis.asee.gepeto.databinding.FragmentRestorePopUpBinding
import es.unex.giis.asee.gepeto.model.User
import es.unex.giis.asee.gepeto.utils.CredentialCheck
import kotlinx.coroutines.launch

class PopUpFragment : DialogFragment() {

//binding
private lateinit var binding: FragmentRestorePopUpBinding

private lateinit var db: GepetoDatabase

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentRestorePopUpBinding.inflate(inflater, container, false)

db = GepetoDatabase.getInstance(requireContext())!!

return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

with(binding) {
//boton que llama a la funcion que cambia la contraseña
restorebtn.setOnClickListener {
restorePassword()
dismiss()
}
}
}

//funcion que cambia la contraseña del usuario que se la ha olvidado
private fun restorePassword() {
with(binding) {
val username = restUsername.text.toString()
val newPassword = newPassword.text.toString()

//dado el nombre de usuario, se busca en la base de datos y se actualiza la contraseña antigua por la nueva
lifecycleScope.launch {
//comprueba si la contraseña es correcta
val check = CredentialCheck.newPasswordOk(newPassword)
if (check.fail) {
Toast.makeText(context, check.msg, Toast.LENGTH_SHORT).show()
return@launch
}
else{
val user = db?.userDao()?.findByName(restUsername.text.toString()) //?: User(-1, etUsername.text.toString(), etPassword.text.toString())
if (user != null) {
Toast.makeText(context, "Contraseña cambiada", Toast.LENGTH_SHORT).show()
db?.userDao()?.update(User(user.userId, username, newPassword))
} else {
Toast.makeText(context, "Usuario no encontrado", Toast.LENGTH_SHORT).show()
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)



// Obtener la referencia a la preferencia de edición de sugerencias
val editSuggestionPreference: Preference? = findPreference("edit_suggestion")

// Configurar el click listener para la preferencia de edición de sugerencias
editSuggestionPreference?.setOnPreferenceClickListener {
// Mostrar un cuadro de diálogo de edición de sugerencias
showSuggestionDialog()
true
}
}

private fun setDefaultValues() {
Expand All @@ -40,4 +51,30 @@ class SettingsFragment : PreferenceFragmentCompat() {

editor.apply()
}

// Función para mostrar un cuadro de diálogo de edición de sugerencias
private fun showSuggestionDialog() {
val builder = AlertDialog.Builder(requireContext())
builder.setTitle("Enviar Sugerencia")

// Configurar un EditText en el cuadro de diálogo para que los usuarios ingresen su sugerencia
val input = EditText(requireContext())
input.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
builder.setView(input)

// Configurar los botones del cuadro de diálogo
builder.setPositiveButton("Enviar") { _, _ ->
// Obtener la sugerencia ingresada por el usuario
val userSuggestion = input.text.toString()

// Aquí la lógica de la sugerencia

// Mostrar un mensaje de confirmación (esto es solo un ejemplo)
Toast.makeText(requireContext(), "Sugerencia enviada: $userSuggestion", Toast.LENGTH_SHORT).show()
}
builder.setNegativeButton("Cancelar") { dialog, _ -> dialog.cancel() }

// Mostrar el cuadro de diálogo
builder.show()
}
}
Loading

0 comments on commit 0cb64dc

Please sign in to comment.