mirror of
https://github.com/shaulascr/ecommerce_serang.git
synced 2025-08-10 09:22:21 +00:00
update sent fcmtoken
This commit is contained in:
@ -3,6 +3,6 @@ package com.alya.ecommerce_serang.data.api.dto
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class FcmReq (
|
||||
@SerializedName("fcm_req")
|
||||
@SerializedName("fcm_token")
|
||||
val fcmToken: String?= null
|
||||
)
|
@ -29,7 +29,7 @@ class LoginActivity : AppCompatActivity() {
|
||||
BaseViewModelFactory {
|
||||
val apiService = ApiConfig.getUnauthenticatedApiService()
|
||||
val userRepository = UserRepository(apiService)
|
||||
LoginViewModel(userRepository)
|
||||
LoginViewModel(userRepository, this)
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +46,6 @@ class LoginActivity : AppCompatActivity() {
|
||||
FirebaseApp.initializeApp(this)
|
||||
|
||||
// Request FCM token at app startup
|
||||
retrieveFCMToken()
|
||||
}
|
||||
|
||||
private fun setupListeners() {
|
||||
@ -70,6 +69,7 @@ class LoginActivity : AppCompatActivity() {
|
||||
|
||||
val sessionManager = SessionManager(this)
|
||||
sessionManager.saveToken(accessToken)
|
||||
retrieveFCMToken()
|
||||
// sessionManager.saveUserId(response.userId)
|
||||
|
||||
Toast.makeText(this, "Login Successful", Toast.LENGTH_SHORT).show()
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.alya.ecommerce_serang.utils.viewmodel
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
@ -8,11 +9,14 @@ import androidx.lifecycle.viewModelScope
|
||||
import com.alya.ecommerce_serang.data.api.dto.FcmReq
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.FcmTokenResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.LoginResponse
|
||||
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.utils.SessionManager
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class LoginViewModel(private val repository: UserRepository) : ViewModel() {
|
||||
class LoginViewModel(private val repository: UserRepository, private val context: Context) : ViewModel() {
|
||||
private val _loginState = MutableLiveData<Result<LoginResponse>>()
|
||||
val loginState: LiveData<Result<LoginResponse>> get() = _loginState
|
||||
|
||||
@ -23,6 +27,12 @@ class LoginViewModel(private val repository: UserRepository) : ViewModel() {
|
||||
private val _message = MutableLiveData<String>()
|
||||
val message: LiveData<String> = _message
|
||||
|
||||
private val sessionManager by lazy { SessionManager(context) }
|
||||
|
||||
private fun getAuthenticatedApiService(): ApiService {
|
||||
return ApiConfig.getApiService(sessionManager)
|
||||
}
|
||||
|
||||
fun login(email: String, password: String) {
|
||||
viewModelScope.launch {
|
||||
_loginState.value = Result.Loading
|
||||
@ -37,10 +47,12 @@ class LoginViewModel(private val repository: UserRepository) : ViewModel() {
|
||||
|
||||
try {
|
||||
// Call the repository function to request OTP
|
||||
val response: FcmTokenResponse = repository.sendFcm(token)
|
||||
val authenticatedApiService = getAuthenticatedApiService()
|
||||
val authenticatedOrderRepo = UserRepository(authenticatedApiService)
|
||||
val response: FcmTokenResponse = authenticatedOrderRepo.sendFcm(token)
|
||||
|
||||
// Log and store success message
|
||||
Log.d("RegisterViewModel", "OTP Response: ${response.message}")
|
||||
Log.d("LoginViewModel", "OTP Response: ${response.message}")
|
||||
_message.value = response.message ?: "berhasil" // Store the message for UI feedback
|
||||
|
||||
// Update state to indicate success
|
||||
@ -52,7 +64,7 @@ class LoginViewModel(private val repository: UserRepository) : ViewModel() {
|
||||
_message.value = exception.localizedMessage ?: "Failed to request OTP"
|
||||
|
||||
// Log the error for debugging
|
||||
Log.e("RegisterViewModel", "OTP request failed for: $token", exception)
|
||||
Log.e("LoginViewModel", "OTP request failed for: $token", exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -105,9 +105,9 @@ class RegisterViewModel(private val repository: UserRepository, private val orde
|
||||
|
||||
try {
|
||||
// Call the repository function to request OTP
|
||||
val authenticatedApiService = getAuthenticatedApiService()
|
||||
val authenticatedOrderRepo = UserRepository(authenticatedApiService)
|
||||
val response: OtpResponse = authenticatedOrderRepo.requestOtpRep(email)
|
||||
// val authenticatedApiService = getAuthenticatedApiService()
|
||||
// val authenticatedOrderRepo = UserRepository(authenticatedApiService)
|
||||
val response: OtpResponse = repository.requestOtpRep(email)
|
||||
|
||||
// Log and store success message
|
||||
Log.d("RegisterViewModel", "OTP Response: ${response.message}")
|
||||
|
Reference in New Issue
Block a user