mirror of
https://github.com/shaulascr/ecommerce_serang.git
synced 2025-12-15 23:51:01 +00:00
change password
This commit is contained in:
@ -0,0 +1,6 @@
|
|||||||
|
package com.alya.ecommerce_serang.data.api.dto
|
||||||
|
|
||||||
|
data class ChangePasswordRequest(
|
||||||
|
val currentPassword: String,
|
||||||
|
val newPassword: String
|
||||||
|
)
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
package com.alya.ecommerce_serang.data.api.response.auth
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class ChangePassResponse(
|
||||||
|
|
||||||
|
@field:SerializedName("message")
|
||||||
|
val message: String? = null
|
||||||
|
)
|
||||||
@ -5,6 +5,7 @@ import com.alya.ecommerce_serang.data.api.dto.AddEvidenceRequest
|
|||||||
import com.alya.ecommerce_serang.data.api.dto.AddPaymentInfoResponse
|
import com.alya.ecommerce_serang.data.api.dto.AddPaymentInfoResponse
|
||||||
import com.alya.ecommerce_serang.data.api.dto.CancelOrderReq
|
import com.alya.ecommerce_serang.data.api.dto.CancelOrderReq
|
||||||
import com.alya.ecommerce_serang.data.api.dto.CartItem
|
import com.alya.ecommerce_serang.data.api.dto.CartItem
|
||||||
|
import com.alya.ecommerce_serang.data.api.dto.ChangePasswordRequest
|
||||||
import com.alya.ecommerce_serang.data.api.dto.CityResponse
|
import com.alya.ecommerce_serang.data.api.dto.CityResponse
|
||||||
import com.alya.ecommerce_serang.data.api.dto.CompletedOrderRequest
|
import com.alya.ecommerce_serang.data.api.dto.CompletedOrderRequest
|
||||||
import com.alya.ecommerce_serang.data.api.dto.ConfirmPaymentRequest
|
import com.alya.ecommerce_serang.data.api.dto.ConfirmPaymentRequest
|
||||||
@ -27,6 +28,7 @@ import com.alya.ecommerce_serang.data.api.dto.StoreAddressResponse
|
|||||||
import com.alya.ecommerce_serang.data.api.dto.UpdateCart
|
import com.alya.ecommerce_serang.data.api.dto.UpdateCart
|
||||||
import com.alya.ecommerce_serang.data.api.dto.UpdateChatRequest
|
import com.alya.ecommerce_serang.data.api.dto.UpdateChatRequest
|
||||||
import com.alya.ecommerce_serang.data.api.dto.VerifRegisReq
|
import com.alya.ecommerce_serang.data.api.dto.VerifRegisReq
|
||||||
|
import com.alya.ecommerce_serang.data.api.response.auth.ChangePassResponse
|
||||||
import com.alya.ecommerce_serang.data.api.response.auth.CheckStoreResponse
|
import com.alya.ecommerce_serang.data.api.response.auth.CheckStoreResponse
|
||||||
import com.alya.ecommerce_serang.data.api.response.auth.FcmTokenResponse
|
import com.alya.ecommerce_serang.data.api.response.auth.FcmTokenResponse
|
||||||
import com.alya.ecommerce_serang.data.api.response.auth.HasStoreResponse
|
import com.alya.ecommerce_serang.data.api.response.auth.HasStoreResponse
|
||||||
@ -530,6 +532,11 @@ interface ApiService {
|
|||||||
@Body request: ResetPassReq
|
@Body request: ResetPassReq
|
||||||
): Response<ResetPassResponse>
|
): Response<ResetPassResponse>
|
||||||
|
|
||||||
|
@POST("changepass")
|
||||||
|
suspend fun changePassword(
|
||||||
|
@Body request: ChangePasswordRequest
|
||||||
|
): Response<ChangePassResponse>
|
||||||
|
|
||||||
@GET("profile/address/detail/{id}")
|
@GET("profile/address/detail/{id}")
|
||||||
suspend fun getDetailAddress(
|
suspend fun getDetailAddress(
|
||||||
@Path("id") addressId: Int
|
@Path("id") addressId: Int
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package com.alya.ecommerce_serang.data.repository
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import com.alya.ecommerce_serang.data.api.dto.ChangePasswordRequest
|
||||||
import com.alya.ecommerce_serang.data.api.dto.FcmReq
|
import com.alya.ecommerce_serang.data.api.dto.FcmReq
|
||||||
import com.alya.ecommerce_serang.data.api.dto.LoginRequest
|
import com.alya.ecommerce_serang.data.api.dto.LoginRequest
|
||||||
import com.alya.ecommerce_serang.data.api.dto.OtpRequest
|
import com.alya.ecommerce_serang.data.api.dto.OtpRequest
|
||||||
@ -10,6 +11,7 @@ import com.alya.ecommerce_serang.data.api.dto.RegisterRequest
|
|||||||
import com.alya.ecommerce_serang.data.api.dto.ResetPassReq
|
import com.alya.ecommerce_serang.data.api.dto.ResetPassReq
|
||||||
import com.alya.ecommerce_serang.data.api.dto.UserProfile
|
import com.alya.ecommerce_serang.data.api.dto.UserProfile
|
||||||
import com.alya.ecommerce_serang.data.api.dto.VerifRegisReq
|
import com.alya.ecommerce_serang.data.api.dto.VerifRegisReq
|
||||||
|
import com.alya.ecommerce_serang.data.api.response.auth.ChangePassResponse
|
||||||
import com.alya.ecommerce_serang.data.api.response.auth.FcmTokenResponse
|
import com.alya.ecommerce_serang.data.api.response.auth.FcmTokenResponse
|
||||||
import com.alya.ecommerce_serang.data.api.response.auth.HasStoreResponse
|
import com.alya.ecommerce_serang.data.api.response.auth.HasStoreResponse
|
||||||
import com.alya.ecommerce_serang.data.api.response.auth.ListStoreTypeResponse
|
import com.alya.ecommerce_serang.data.api.response.auth.ListStoreTypeResponse
|
||||||
@ -516,6 +518,29 @@ class UserRepository(private val apiService: ApiService) {
|
|||||||
Result.Error(e)
|
Result.Error(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun changePassword(currentPassword: String, newPassword: String): Result<ChangePassResponse> {
|
||||||
|
return try {
|
||||||
|
val request = ChangePasswordRequest(currentPassword, newPassword)
|
||||||
|
val response = apiService.changePassword(request) // Make the API call
|
||||||
|
|
||||||
|
if (response.isSuccessful) {
|
||||||
|
val changePassResponse = response.body()
|
||||||
|
if (changePassResponse != null) {
|
||||||
|
Result.Success(changePassResponse) // Return success with the response message
|
||||||
|
} else {
|
||||||
|
Result.Error(Exception("Empty response from server"))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||||
|
Log.e(TAG, "Error changing password: $errorBody")
|
||||||
|
Result.Error(Exception(errorBody))
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Result.Error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object{
|
companion object{
|
||||||
private const val TAG = "UserRepository"
|
private const val TAG = "UserRepository"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,21 +1,73 @@
|
|||||||
package com.alya.ecommerce_serang.ui.profile
|
package com.alya.ecommerce_serang.ui.profile
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.ViewCompat
|
import androidx.core.view.ViewCompat
|
||||||
import androidx.core.view.WindowInsetsCompat
|
import androidx.core.view.WindowInsetsCompat
|
||||||
|
import androidx.lifecycle.Observer
|
||||||
import com.alya.ecommerce_serang.R
|
import com.alya.ecommerce_serang.R
|
||||||
|
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||||
|
import com.alya.ecommerce_serang.data.api.retrofit.ApiService
|
||||||
|
import com.alya.ecommerce_serang.data.repository.Result
|
||||||
|
import com.alya.ecommerce_serang.data.repository.UserRepository
|
||||||
|
import com.alya.ecommerce_serang.databinding.ActivityChangePasswordBinding
|
||||||
|
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||||
|
import com.alya.ecommerce_serang.utils.SessionManager
|
||||||
|
import com.alya.ecommerce_serang.utils.viewmodel.ProfileViewModel
|
||||||
|
import kotlin.getValue
|
||||||
|
|
||||||
class ChangePasswordActivity : AppCompatActivity() {
|
class ChangePasswordActivity : AppCompatActivity() {
|
||||||
|
private lateinit var binding: ActivityChangePasswordBinding
|
||||||
|
private lateinit var apiService: ApiService
|
||||||
|
private lateinit var sessionManager: SessionManager
|
||||||
|
|
||||||
|
private val viewModel: ProfileViewModel by viewModels {
|
||||||
|
BaseViewModelFactory {
|
||||||
|
apiService = ApiConfig.getApiService(sessionManager)
|
||||||
|
val userRepository = UserRepository(apiService)
|
||||||
|
ProfileViewModel(userRepository)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
binding = ActivityChangePasswordBinding.inflate(layoutInflater)
|
||||||
setContentView(R.layout.activity_change_password)
|
setContentView(binding.root)
|
||||||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
|
|
||||||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
sessionManager = SessionManager(this)
|
||||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
|
||||||
insets
|
// Listen for the result of the password change
|
||||||
|
viewModel.changePasswordResult.observe(this, Observer { result ->
|
||||||
|
when (result) {
|
||||||
|
is Result.Error -> {
|
||||||
|
Toast.makeText(
|
||||||
|
this,
|
||||||
|
"Gagal mengubah kata sandi: ${result.exception.message}",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
is Result.Success -> {
|
||||||
|
Toast.makeText(this, "Berhasil mengubah kata sandi", Toast.LENGTH_SHORT).show()
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
is Result.Loading -> {}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Button to trigger password change
|
||||||
|
binding.btnChangePass.setOnClickListener {
|
||||||
|
val currentPassword = binding.etLoginPassword.text.toString()
|
||||||
|
val newPassword = binding.etLoginNewPassword.text.toString()
|
||||||
|
|
||||||
|
if (currentPassword.isNotEmpty() && newPassword.isNotEmpty()) {
|
||||||
|
// Call change password function from ViewModel
|
||||||
|
viewModel.changePassword(currentPassword, newPassword)
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, "Lengkapi data", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -8,6 +8,7 @@ import androidx.lifecycle.MutableLiveData
|
|||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.alya.ecommerce_serang.data.api.dto.UserProfile
|
import com.alya.ecommerce_serang.data.api.dto.UserProfile
|
||||||
|
import com.alya.ecommerce_serang.data.api.response.auth.ChangePassResponse
|
||||||
import com.alya.ecommerce_serang.data.api.response.auth.HasStoreResponse
|
import com.alya.ecommerce_serang.data.api.response.auth.HasStoreResponse
|
||||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.EditProfileResponse
|
import com.alya.ecommerce_serang.data.api.response.customer.profile.EditProfileResponse
|
||||||
import com.alya.ecommerce_serang.data.repository.Result
|
import com.alya.ecommerce_serang.data.repository.Result
|
||||||
@ -26,7 +27,7 @@ class ProfileViewModel(private val userRepository: UserRepository) : ViewModel()
|
|||||||
|
|
||||||
private val _checkStore = MutableLiveData<Boolean>()
|
private val _checkStore = MutableLiveData<Boolean>()
|
||||||
val checkStore: LiveData<Boolean> = _checkStore
|
val checkStore: LiveData<Boolean> = _checkStore
|
||||||
|
val changePasswordResult = MutableLiveData<Result<ChangePassResponse>>()
|
||||||
private val _logout = MutableLiveData<Boolean>()
|
private val _logout = MutableLiveData<Boolean>()
|
||||||
val logout : LiveData<Boolean> = _logout
|
val logout : LiveData<Boolean> = _logout
|
||||||
|
|
||||||
@ -110,6 +111,20 @@ class ProfileViewModel(private val userRepository: UserRepository) : ViewModel()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun changePassword(currentPassword: String, newPassword: String) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
try {
|
||||||
|
// Call the repository to change the password
|
||||||
|
val result = userRepository.changePassword(currentPassword, newPassword)
|
||||||
|
|
||||||
|
// Post the result (success or error) to LiveData
|
||||||
|
changePasswordResult.postValue(result)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
// Handle any unexpected errors
|
||||||
|
changePasswordResult.postValue(Result.Error(e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "ProfileViewModel"
|
private const val TAG = "ProfileViewModel"
|
||||||
|
|||||||
Reference in New Issue
Block a user