mirror of
https://github.com/shaulascr/ecommerce_serang.git
synced 2025-08-13 10:42:21 +00:00
update fcm
This commit is contained in:
@ -0,0 +1,8 @@
|
|||||||
|
package com.alya.ecommerce_serang.data.api.dto
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class FcmReq (
|
||||||
|
@SerializedName("fcm_req")
|
||||||
|
val fcmToken: String?= null
|
||||||
|
)
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.alya.ecommerce_serang.data.api.response.auth
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class FcmTokenResponse(
|
||||||
|
|
||||||
|
@field:SerializedName("message")
|
||||||
|
val message: String? = null
|
||||||
|
)
|
@ -9,6 +9,7 @@ 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.CourierCostRequest
|
import com.alya.ecommerce_serang.data.api.dto.CourierCostRequest
|
||||||
import com.alya.ecommerce_serang.data.api.dto.CreateAddressRequest
|
import com.alya.ecommerce_serang.data.api.dto.CreateAddressRequest
|
||||||
|
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.OrderRequest
|
import com.alya.ecommerce_serang.data.api.dto.OrderRequest
|
||||||
import com.alya.ecommerce_serang.data.api.dto.OrderRequestBuy
|
import com.alya.ecommerce_serang.data.api.dto.OrderRequestBuy
|
||||||
@ -23,6 +24,7 @@ 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.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.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
|
||||||
import com.alya.ecommerce_serang.data.api.response.auth.LoginResponse
|
import com.alya.ecommerce_serang.data.api.response.auth.LoginResponse
|
||||||
@ -129,6 +131,11 @@ interface ApiService {
|
|||||||
@Body otpRequest: OtpRequest
|
@Body otpRequest: OtpRequest
|
||||||
):OtpResponse
|
):OtpResponse
|
||||||
|
|
||||||
|
@PUT("updatefcm")
|
||||||
|
suspend fun updateFcm(
|
||||||
|
@Body fcmReq: FcmReq
|
||||||
|
): FcmTokenResponse
|
||||||
|
|
||||||
@GET("checkstore")
|
@GET("checkstore")
|
||||||
suspend fun checkStoreUser(
|
suspend fun checkStoreUser(
|
||||||
): HasStoreResponse
|
): HasStoreResponse
|
||||||
|
@ -3,11 +3,13 @@ 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.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
|
||||||
import com.alya.ecommerce_serang.data.api.dto.RegisterRequest
|
import com.alya.ecommerce_serang.data.api.dto.RegisterRequest
|
||||||
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.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
|
||||||
import com.alya.ecommerce_serang.data.api.response.auth.LoginResponse
|
import com.alya.ecommerce_serang.data.api.response.auth.LoginResponse
|
||||||
@ -338,6 +340,10 @@ class UserRepository(private val apiService: ApiService) {
|
|||||||
return apiService.verifValue(request)
|
return apiService.verifValue(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun sendFcm(request: FcmReq): FcmTokenResponse{
|
||||||
|
return apiService.updateFcm(request)
|
||||||
|
}
|
||||||
|
|
||||||
companion object{
|
companion object{
|
||||||
private const val TAG = "UserRepository"
|
private const val TAG = "UserRepository"
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
package com.alya.ecommerce_serang.ui.auth
|
package com.alya.ecommerce_serang.ui.auth
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import com.alya.ecommerce_serang.data.api.dto.FcmReq
|
||||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||||
import com.alya.ecommerce_serang.data.repository.Result
|
import com.alya.ecommerce_serang.data.repository.Result
|
||||||
import com.alya.ecommerce_serang.data.repository.UserRepository
|
import com.alya.ecommerce_serang.data.repository.UserRepository
|
||||||
@ -14,9 +17,13 @@ import com.alya.ecommerce_serang.ui.MainActivity
|
|||||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||||
import com.alya.ecommerce_serang.utils.SessionManager
|
import com.alya.ecommerce_serang.utils.SessionManager
|
||||||
import com.alya.ecommerce_serang.utils.viewmodel.LoginViewModel
|
import com.alya.ecommerce_serang.utils.viewmodel.LoginViewModel
|
||||||
|
import com.google.firebase.FirebaseApp
|
||||||
|
import com.google.firebase.messaging.FirebaseMessaging
|
||||||
|
|
||||||
class LoginActivity : AppCompatActivity() {
|
class LoginActivity : AppCompatActivity() {
|
||||||
|
|
||||||
|
private val TAG = "LoginActivity"
|
||||||
|
|
||||||
private lateinit var binding: ActivityLoginBinding
|
private lateinit var binding: ActivityLoginBinding
|
||||||
private val loginViewModel: LoginViewModel by viewModels{
|
private val loginViewModel: LoginViewModel by viewModels{
|
||||||
BaseViewModelFactory {
|
BaseViewModelFactory {
|
||||||
@ -35,6 +42,11 @@ class LoginActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
setupListeners()
|
setupListeners()
|
||||||
observeLoginState()
|
observeLoginState()
|
||||||
|
|
||||||
|
FirebaseApp.initializeApp(this)
|
||||||
|
|
||||||
|
// Request FCM token at app startup
|
||||||
|
retrieveFCMToken()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupListeners() {
|
private fun setupListeners() {
|
||||||
@ -74,4 +86,35 @@ class LoginActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun retrieveFCMToken() {
|
||||||
|
FirebaseMessaging.getInstance().token
|
||||||
|
.addOnCompleteListener { task ->
|
||||||
|
if (!task.isSuccessful) {
|
||||||
|
Log.e(TAG, "Failed to get FCM token", task.exception)
|
||||||
|
return@addOnCompleteListener
|
||||||
|
}
|
||||||
|
|
||||||
|
val token = task.result
|
||||||
|
// tokenTes = token
|
||||||
|
Log.d(TAG, "FCM token retrieved: $token")
|
||||||
|
|
||||||
|
// Save token locally
|
||||||
|
val sharedPreferences = getSharedPreferences("FCM_PREFS", Context.MODE_PRIVATE)
|
||||||
|
sharedPreferences.edit().putString("FCM_TOKEN", token).apply()
|
||||||
|
|
||||||
|
// Send to your server
|
||||||
|
sendTokenToServer(token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun sendTokenToServer(token: String) {
|
||||||
|
Log.d(TAG, "Would send token to server: $token")
|
||||||
|
val tokenFcm=FcmReq(
|
||||||
|
fcmToken = token
|
||||||
|
)
|
||||||
|
loginViewModel.sendFcm(tokenFcm)
|
||||||
|
Log.d(TAG, "Sent token fcm: $token")
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@ -398,7 +398,7 @@ class RegisterStoreActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
binding.checkboxJnt.setOnCheckedChangeListener { _, isChecked ->
|
binding.checkboxJnt.setOnCheckedChangeListener { _, isChecked ->
|
||||||
handleCourierSelection("jnt", isChecked)
|
handleCourierSelection("tiki", isChecked)
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.checkboxPos.setOnCheckedChangeListener { _, isChecked ->
|
binding.checkboxPos.setOnCheckedChangeListener { _, isChecked ->
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package com.alya.ecommerce_serang.utils.viewmodel
|
package com.alya.ecommerce_serang.utils.viewmodel
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
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.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.response.auth.LoginResponse
|
||||||
import com.alya.ecommerce_serang.data.repository.Result
|
import com.alya.ecommerce_serang.data.repository.Result
|
||||||
import com.alya.ecommerce_serang.data.repository.UserRepository
|
import com.alya.ecommerce_serang.data.repository.UserRepository
|
||||||
@ -13,6 +16,13 @@ class LoginViewModel(private val repository: UserRepository) : ViewModel() {
|
|||||||
private val _loginState = MutableLiveData<Result<LoginResponse>>()
|
private val _loginState = MutableLiveData<Result<LoginResponse>>()
|
||||||
val loginState: LiveData<Result<LoginResponse>> get() = _loginState
|
val loginState: LiveData<Result<LoginResponse>> get() = _loginState
|
||||||
|
|
||||||
|
private val _otpState = MutableLiveData<Result<Unit>>()
|
||||||
|
val otpState: LiveData<Result<Unit>> = _otpState
|
||||||
|
|
||||||
|
// MutableLiveData to store messages from API responses
|
||||||
|
private val _message = MutableLiveData<String>()
|
||||||
|
val message: LiveData<String> = _message
|
||||||
|
|
||||||
fun login(email: String, password: String) {
|
fun login(email: String, password: String) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
_loginState.value = Result.Loading
|
_loginState.value = Result.Loading
|
||||||
@ -20,4 +30,31 @@ class LoginViewModel(private val repository: UserRepository) : ViewModel() {
|
|||||||
_loginState.value = result
|
_loginState.value = result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun sendFcm(token: FcmReq) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
_otpState.value = Result.Loading // Indicating API call in progress
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Call the repository function to request OTP
|
||||||
|
val response: FcmTokenResponse = repository.sendFcm(token)
|
||||||
|
|
||||||
|
// Log and store success message
|
||||||
|
Log.d("RegisterViewModel", "OTP Response: ${response.message}")
|
||||||
|
_message.value = response.message ?: "berhasil" // Store the message for UI feedback
|
||||||
|
|
||||||
|
// Update state to indicate success
|
||||||
|
_otpState.value = Result.Success(Unit)
|
||||||
|
|
||||||
|
} catch (exception: Exception) {
|
||||||
|
// Handle any errors and update state
|
||||||
|
_otpState.value = Result.Error(exception)
|
||||||
|
_message.value = exception.localizedMessage ?: "Failed to request OTP"
|
||||||
|
|
||||||
|
// Log the error for debugging
|
||||||
|
Log.e("RegisterViewModel", "OTP request failed for: $token", exception)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user