mirror of
https://github.com/shaulascr/ecommerce_serang.git
synced 2025-12-15 07:31:02 +00:00
fix add address
This commit is contained in:
@ -22,7 +22,6 @@ import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import com.alya.ecommerce_serang.data.api.dto.CreateAddressRequest
|
||||
import com.alya.ecommerce_serang.data.api.dto.UserProfile
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.CitiesItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.ProvincesItem
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
@ -37,8 +36,8 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
private lateinit var binding: ActivityAddAddressBinding
|
||||
private lateinit var apiService: ApiService
|
||||
private lateinit var sessionManager: SessionManager
|
||||
private var profileUser: Int = 1
|
||||
private lateinit var locationManager: LocationManager
|
||||
private var profileUserId: Int? = null
|
||||
|
||||
private var isRequestingLocation = false
|
||||
|
||||
@ -80,11 +79,15 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
)
|
||||
windowInsets
|
||||
}
|
||||
viewModel.loadUserProfile()
|
||||
|
||||
// Get user profile from session manager
|
||||
// profileUser =UserProfile.
|
||||
viewModel.userProfile.observe(this){ user ->
|
||||
user?.let { updateProfile(it) }
|
||||
viewModel.userProfile.observe(this) { user ->
|
||||
if (user != null) {
|
||||
profileUserId = user.userId
|
||||
Log.d(TAG, "Fetched userId = $profileUserId") // ✅ debug log
|
||||
} else {
|
||||
Log.e(TAG, "Error get profile")
|
||||
}
|
||||
}
|
||||
|
||||
setupToolbar()
|
||||
@ -94,16 +97,10 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
setupButtonListeners()
|
||||
setupObservers()
|
||||
|
||||
|
||||
|
||||
// Force trigger province loading to ensure it happens
|
||||
viewModel.getProvinces()
|
||||
}
|
||||
|
||||
private fun updateProfile(userProfile: UserProfile){
|
||||
profileUser = userProfile.userId
|
||||
}
|
||||
|
||||
// UI setup methods
|
||||
private fun setupToolbar() {
|
||||
binding.toolbar.setNavigationOnClickListener {
|
||||
@ -280,12 +277,7 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
val postalCode = binding.etKodePos.text.toString().trim()
|
||||
val recipient = binding.etNamaPenerima.text.toString().trim()
|
||||
val phone = binding.etNomorHp.text.toString().trim()
|
||||
val userId = try {
|
||||
profileUser
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Error getting userId, using default", e)
|
||||
1 // Default userId for testing
|
||||
}
|
||||
val userId = profileUserId
|
||||
val isStoreLocation = false
|
||||
|
||||
val provinceId = viewModel.selectedProvinceId
|
||||
@ -333,7 +325,7 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
|
||||
// Create request with all fields
|
||||
val request = CreateAddressRequest(
|
||||
userId = userId,
|
||||
userId = userId!!,
|
||||
lat = latitude!!, // Safe to use !! as we've checked above
|
||||
long = longitude!!,
|
||||
street = street,
|
||||
@ -525,6 +517,6 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "AddAddressViewModel"
|
||||
private const val TAG = "AddAddressActivity"
|
||||
}
|
||||
}
|
||||
|
||||
@ -252,6 +252,24 @@ class AddAddressViewModel(private val repository: OrderRepository, private val u
|
||||
return params
|
||||
}
|
||||
|
||||
fun loadUserProfile() {
|
||||
viewModelScope.launch {
|
||||
when (val result = repository.fetchUserProfile()) {
|
||||
is Result.Success -> {
|
||||
result.data?.let {
|
||||
_userProfile.postValue(it) // send UserProfile to LiveData
|
||||
} ?: _errorMessageUser.postValue("User data not found")
|
||||
}
|
||||
is Result.Error -> {
|
||||
_errorMessageUser.postValue(result.exception.message ?: "Unknown error")
|
||||
}
|
||||
is Result.Loading -> {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "AddAddressViewModel"
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@ import com.alya.ecommerce_serang.ui.profile.mystore.sells.shipment.DetailShipmen
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.SellsViewModel
|
||||
import com.bumptech.glide.Glide
|
||||
import com.google.gson.Gson
|
||||
import java.text.NumberFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Locale
|
||||
@ -96,7 +97,7 @@ class SellsAdapter(
|
||||
val product = order.orderItems?.firstOrNull()
|
||||
tvSellsProductName.text = product?.productName
|
||||
tvSellsProductQty.text = "x${product?.quantity}"
|
||||
tvSellsProductPrice.text = formatPrice(product?.price.toString())
|
||||
tvSellsProductPrice.text = product?.price?.let { formatPrice(it.toInt()) }
|
||||
|
||||
val fullImageUrl = when (val img = product?.productImage) {
|
||||
is String -> {
|
||||
@ -169,7 +170,7 @@ class SellsAdapter(
|
||||
val product = order.orderItems?.firstOrNull()
|
||||
tvSellsProductName.text = product?.productName
|
||||
tvSellsProductQty.text = "x${product?.quantity}"
|
||||
tvSellsProductPrice.text = formatPrice(product?.price.toString())
|
||||
tvSellsProductPrice.text = product?.price?.let { formatPrice(it.toInt()) }
|
||||
|
||||
val fullImageUrl = when (val img = product?.productImage) {
|
||||
is String -> {
|
||||
@ -185,7 +186,7 @@ class SellsAdapter(
|
||||
.into(ivSellsProduct)
|
||||
|
||||
tvSellsQty.text = "${order.orderItems?.size} produk"
|
||||
tvSellsPrice.text = formatPrice(order.totalAmount.toString())
|
||||
tvSellsPrice.text = order.totalAmount?.let { formatPrice(it.toInt()) }
|
||||
}
|
||||
"paid" -> {
|
||||
layoutOrders.visibility = View.GONE
|
||||
@ -308,10 +309,9 @@ class SellsAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatPrice(price: String): String {
|
||||
val priceDouble = price.toDoubleOrNull() ?: 0.0
|
||||
val formattedPrice = String.format(Locale("id", "ID"), "Rp%,.0f", priceDouble)
|
||||
return formattedPrice
|
||||
private fun formatPrice(amount: Int): String {
|
||||
val formatter = NumberFormat.getCurrencyInstance(Locale("in", "ID"))
|
||||
return formatter.format(amount.toLong()).replace(",00", "")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -38,7 +38,6 @@
|
||||
android:src="@drawable/baseline_edit_24"
|
||||
android:focusable="true"
|
||||
android:clickable="true"
|
||||
android:visibility="gone"
|
||||
android:layout_marginHorizontal="16dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"/>
|
||||
|
||||
Reference in New Issue
Block a user