Skip to content

Commit

Permalink
35 allow changing username (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zenin0 authored May 29, 2024
2 parents 21e608b + f638bf1 commit 6e9e76b
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
package com.isanz.inmomarket.ui.portal

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.isanz.inmomarket.InmoMarket
import com.isanz.inmomarket.utils.Constants
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.tasks.await

class PortalViewModel(private val dispatcher: CoroutineDispatcher = Dispatchers.IO) : ViewModel() {

private val _imageUrl = MutableLiveData<String>()
fun getImageRandom() {
viewModelScope.launch(dispatcher) {
val url = try {
val listResult =
InmoMarket.getStorage().reference.child("images/portal/").listAll().await()

fun getImageRandom(): String {
return runBlocking(dispatcher) {
try {
val listResult = InmoMarket.getStorage().reference.child("images/portal/").listAll().await()
val images = listResult.items
val image = images.random()
val downloadUrl = image.downloadUrl.await()
downloadUrl.toString()
} catch (e: Exception) {
Constants.LOGIN_IMAGE
}
_imageUrl.postValue(url)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,18 @@ class LoginActivity : AppCompatActivity() {
}
} else {
Log.w(TAG, "signInWithCredential:failure", task.exception)
Toast.makeText(baseContext,
getString(R.string.authentication_failed), Toast.LENGTH_SHORT).show()
Toast.makeText(
baseContext, getString(R.string.authentication_failed), Toast.LENGTH_SHORT
).show()
}
}
} catch (e: Exception) {
Log.w(TAG, "signInWithCredential:failure", e)
Toast.makeText(baseContext, getString(R.string.authentication_failed), Toast.LENGTH_SHORT).show()
Toast.makeText(
baseContext,
getString(R.string.authentication_failed),
Toast.LENGTH_SHORT
).show()
}
}

Expand Down Expand Up @@ -282,18 +287,18 @@ class LoginActivity : AppCompatActivity() {
val intent = Intent(this, RegisterActivity::class.java)
startActivity(intent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
overrideActivityTransition(OVERRIDE_TRANSITION_OPEN, R.anim.slide_out_right, R.anim.slide_in_left)
overrideActivityTransition(
OVERRIDE_TRANSITION_OPEN,
R.anim.slide_out_right,
R.anim.slide_in_left
)
} else {
overridePendingTransition(R.anim.slide_out_right , R.anim.slide_in_left)
overridePendingTransition(R.anim.slide_out_right, R.anim.slide_in_left)
}
finish()
}

private fun setImage(view: ImageView) {
try {
Glide.with(this).load(portalViewModel.getImageRandom()).centerCrop().into(view)
} catch (e: Exception) {
Log.w(TAG, "Error loading image", e)
}
Glide.with(this).load(portalViewModel.getImageRandom()).centerCrop().into(view)
}
}
23 changes: 7 additions & 16 deletions app/src/main/java/com/isanz/inmomarket/ui/search/SearchFragment.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.isanz.inmomarket.ui.search


import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -95,9 +95,9 @@ class SearchFragment : Fragment(), OnMapReadyCallback {
Manifest.permission.ACCESS_COARSE_LOCATION
)
)
return
} else {
enableUserLocation()
}
enableUserLocation()
}

private fun enableUserLocation() {
Expand All @@ -108,16 +108,9 @@ class SearchFragment : Fragment(), OnMapReadyCallback {
requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissionLauncher.launch(
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
)
return
}
mMap.isMyLocationEnabled = true
mMap.isMyLocationEnabled = true
setMapLocationToUserLocation()
} else {
Toast.makeText(requireContext(), "Ubication not allowed", Toast.LENGTH_SHORT).show()
Expand All @@ -133,12 +126,6 @@ class SearchFragment : Fragment(), OnMapReadyCallback {
requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
requestPermissionLauncher.launch(
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
)
return
}
fusedLocationClient.lastLocation.addOnSuccessListener { location ->
Expand All @@ -156,7 +143,11 @@ class SearchFragment : Fragment(), OnMapReadyCallback {
marker?.tag = latLongIdPairs.first
}
}
} else {
Toast.makeText(requireContext(), "Unable to get current location", Toast.LENGTH_SHORT).show()
}
}.addOnFailureListener { exception ->
Log.e("SearchFragment", "Failed to get last location", exception)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package com.isanz.inmomarket.ui.settings

import android.content.Context
import android.os.Bundle
import android.text.InputType
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.isanz.inmomarket.InmoMarket
import com.isanz.inmomarket.R
import com.isanz.inmomarket.databinding.FragmentSettingsBinding

class SettingsFragment : Fragment() {
Expand Down Expand Up @@ -49,6 +54,37 @@ class SettingsFragment : Fragment() {
setUpCloseAccountButton()
setUpAllowUbicationButton()
setUpChangePasswordButton()
setUpChangeUsername()
}

private fun setUpChangeUsername() {
mBinding.btnChangeUsername.setOnClickListener {
val layout = TextInputLayout(requireContext())
val input = TextInputEditText(requireContext()).apply {
inputType = InputType.TYPE_CLASS_TEXT
hint = getString(R.string.prompt_username)
}

layout.addView(input)

val dialog = MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.change_username))
.setView(layout)
.setPositiveButton(getString(R.string.ok)) { _, _ ->
val newUsername = input.text.toString()
settingsViewModel.changeUsername(newUsername)
}
.setNegativeButton(getString(R.string.cancel)) { dialog, _ ->
dialog.cancel()
}
.show()

layout.layoutParams = (layout.layoutParams as ViewGroup.MarginLayoutParams).apply {
setMargins(50, 0, 50, 0) // Set margins as needed
}

dialog.show()
}
}

private fun setUpBiometricLoginButton() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ class SettingsViewModel : ViewModel() {
Log.e(Constants.TAG, "deleteChats:failure", e)
}
}

fun changeUsername(newUsername: String) {
try {
val userId = InmoMarket.getAuth().currentUser!!.uid
db.collection("users").document(userId).update("displayName", newUsername)
} catch (e: Exception) {
Log.e(Constants.TAG, "changeUsername:failure", e)
}
}
}
33 changes: 26 additions & 7 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,44 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/sBiometricalLogin" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btnChangePassword"
<LinearLayout
android:id="@+id/llChangePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/common_margin"
android:text="@string/change_password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/sAllowUbication" />
app:layout_constraintTop_toBottomOf="@id/sAllowUbication">

<com.google.android.material.button.MaterialButton
android:id="@+id/btnChangePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="@dimen/common_margin"
android:text="@string/change_password" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btnChangeUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/common_margin"
android:layout_gravity="center"
android:text="@string/change_username" />



</LinearLayout>


<com.google.android.material.button.MaterialButton
android:id="@+id/btnCloseAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/md_theme_light_error"
android:layout_margin="@dimen/common_margin"
android:text="@string/close_account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnChangePassword" />
app:layout_constraintTop_toBottomOf="@id/llChangePassword" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 3 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<string name="chat">Chat starten</string>
<string name="prompt_message">Nachricht</string>
<string name="send_message">Nachricht senden</string>
<string name="lorem_ipsum">Lorem Ipsum ist einfach Blindtext der Druck- und Satzindustrie. Lorem Ipsum ist seit dem 16. Jahrhundert der Standard-Blindtext der Industrie.</string>
<string name="lorem_ipsum">Lorem Ipsum ist einfach Blindtext der Druck- und Satzindustrie. Lorem Ipsum ist seitdem 16. Jahrhundert der Standard-Blindtext der Industrie.</string>
<string name="login">Anmelden</string>
<string name="label_name">Name</string>
<string name="reset_password">Passwort zurücksetzen</string>
Expand Down Expand Up @@ -93,5 +93,7 @@
<string name="biometric_login">Biometrische Anmeldung</string>
<string name="use_account_password">Konto-Passwort verwenden</string>
<string name="authentication_failed">Authentifizierung fehlgeschlagen</string>
<string name="change_username">Benutzernamen ändern</string>
<string name="prompt_username">Benutzername</string>

</resources>
8 changes: 5 additions & 3 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@
<string name="prompt_to">Hasta</string>
<string name="action_sign_in_github">Iniciar sesión con Github</string>
<string name="clear_filters">Borrar filtros</string>
<string name="no_user_loged">Es ist momentan kein Benutzer angemeldet. Bitte melden Sie sich zuerst an.</string>
<string name="login_use_biometric">Melden Sie sich mit Ihrem biometrischen Ausweis an</string>
<string name="biometric_login">Biometrische Anmeldung</string>
<string name="no_user_loged">"No hay ningún usuario conectado actualmente. Por favor, inicia sesión primero. "</string>
<string name="login_use_biometric">Inicia sesión usando tus credenciales biometricass</string>
<string name="biometric_login">Acceso biometrico</string>
<string name="use_account_password">Konto-Passwort verwenden</string>
<string name="authentication_failed">Autenticación fallida</string>
<string name="change_username">Cambiar nombre de usuario</string>
<string name="prompt_username">Nombre de usuario</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
<string name="biometric_login">Connexion biométrique</string>
<string name="use_account_password">Utiliser le mot de passe du compte</string>
<string name="authentication_failed">Authentification échouée</string>
<string name="change_username">Changer le nom d\'utilisateur</string>
<string name="prompt_username">Nom d\'utilisateur</string>


</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,6 @@
<string name="biometric_login">Accesso biometrico</string>
<string name="use_account_password">Utilizza la password dell\'account</string>
<string name="authentication_failed">Autenticazione fallita</string>
<string name="change_username">Cambia nome utente</string>
<string name="prompt_username">Nome utente</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@
<string name="biometric_login">Biometric login</string>
<string name="use_account_password">Use account password</string>
<string name="authentication_failed">Authentication failed.</string>
<string name="change_username">Change username</string>
<string name="prompt_username">Username</string>
<string name="ok" translatable="false">OK</string>
</resources>

0 comments on commit 6e9e76b

Please sign in to comment.