mirror of
https://github.com/shaulascr/ecommerce_serang.git
synced 2025-08-16 03:47:24 +00:00
fix address store, address user, add
This commit is contained in:
@ -394,18 +394,8 @@ interface ApiService {
|
||||
@PUT("mystore/edit")
|
||||
suspend fun updateStoreProfileMultipart(
|
||||
@Part("store_name") storeName: RequestBody,
|
||||
@Part("store_status") storeStatus: RequestBody,
|
||||
@Part("store_description") storeDescription: RequestBody,
|
||||
@Part("is_on_leave") isOnLeave: RequestBody,
|
||||
@Part("city_id") cityId: RequestBody,
|
||||
@Part("province_id") provinceId: RequestBody,
|
||||
@Part("street") street: RequestBody,
|
||||
@Part("subdistrict") subdistrict: RequestBody,
|
||||
@Part("detail") detail: RequestBody,
|
||||
@Part("postal_code") postalCode: RequestBody,
|
||||
@Part("latitude") latitude: RequestBody,
|
||||
@Part("longitude") longitude: RequestBody,
|
||||
@Part("user_phone") userPhone: RequestBody,
|
||||
@Part("store_type_id") storeTypeId: RequestBody,
|
||||
@Part storeimg: MultipartBody.Part?
|
||||
): Response<StoreDataResponse>
|
||||
@ -457,10 +447,10 @@ interface ApiService {
|
||||
@Body addressData: HashMap<String, Any?>
|
||||
): Response<StoreAddressResponse>
|
||||
|
||||
@PUT("profile/address/edit/{idAddress}")
|
||||
@PUT("profile/address/edit/{id}")
|
||||
suspend fun updateAddress(
|
||||
@Path("id") addressId: Int,
|
||||
@Body params: Map<String, Any>
|
||||
@Body params: Map<String, @JvmSuppressWildcards Any>
|
||||
): Response<UpdateAddressResponse>
|
||||
|
||||
@POST("search")
|
||||
|
@ -3,6 +3,7 @@ package com.alya.ecommerce_serang.data.repository
|
||||
import android.util.Log
|
||||
import com.alya.ecommerce_serang.data.api.dto.CityResponse
|
||||
import com.alya.ecommerce_serang.data.api.dto.ProvinceResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.SubdistrictResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.AddressResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.UpdateAddressResponse
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiService
|
||||
@ -187,4 +188,9 @@ class AddressRepository(private val apiService: ApiService) {
|
||||
suspend fun updateAddress(addressId: Int, params: Map<String, Any>): Response<UpdateAddressResponse> {
|
||||
return apiService.updateAddress(addressId, params)
|
||||
}
|
||||
|
||||
suspend fun getListSubdistrict(cityId : String): SubdistrictResponse? {
|
||||
val response = apiService.getSubdistrict(cityId)
|
||||
return if (response.isSuccessful) response.body() else null
|
||||
}
|
||||
}
|
@ -53,48 +53,49 @@ class MyStoreRepository(private val apiService: ApiService) {
|
||||
|
||||
suspend fun updateStoreProfile(
|
||||
storeName: RequestBody,
|
||||
storeStatus: RequestBody,
|
||||
storeDescription: RequestBody,
|
||||
isOnLeave: RequestBody,
|
||||
cityId: RequestBody,
|
||||
provinceId: RequestBody,
|
||||
street: RequestBody,
|
||||
subdistrict: RequestBody,
|
||||
detail: RequestBody,
|
||||
postalCode: RequestBody,
|
||||
latitude: RequestBody,
|
||||
longitude: RequestBody,
|
||||
userPhone: RequestBody,
|
||||
storeType: RequestBody,
|
||||
storeimg: MultipartBody.Part?
|
||||
): Response<StoreDataResponse> {
|
||||
return apiService.updateStoreProfileMultipart(
|
||||
storeName, storeStatus, storeDescription, isOnLeave, cityId, provinceId,
|
||||
street, subdistrict, detail, postalCode, latitude, longitude, userPhone, storeType, storeimg
|
||||
)
|
||||
): Response<StoreDataResponse>? {
|
||||
|
||||
return try {
|
||||
Log.d(TAG, "storeName: $storeName")
|
||||
Log.d(TAG, "storeDescription: $storeDescription")
|
||||
Log.d(TAG, "isOnLeave: $isOnLeave")
|
||||
Log.d(TAG, "storeType: $storeType")
|
||||
Log.d(TAG, "storeimg: ${storeimg?.headers}")
|
||||
|
||||
apiService.updateStoreProfileMultipart(
|
||||
storeName, storeDescription, isOnLeave, storeType, storeimg
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error updating store profile", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getSellList(status: String): Result<OrderListResponse> {
|
||||
return try {
|
||||
Log.d("SellsRepository", "Add Evidence : $status")
|
||||
Log.d(TAG, "Add Evidence : $status")
|
||||
val response = apiService.getSellList(status)
|
||||
|
||||
if (response.isSuccessful) {
|
||||
val allListSell = response.body()
|
||||
if (allListSell != null) {
|
||||
Log.d("SellsRepository", "Add Evidence successfully: ${allListSell.message}")
|
||||
Log.d(TAG, "Add Evidence successfully: ${allListSell.message}")
|
||||
Result.Success(allListSell)
|
||||
} else {
|
||||
Log.e("SellsRepository", "Response body was null")
|
||||
Log.e(TAG, "Response body was null")
|
||||
Result.Error(Exception("Empty response from server"))
|
||||
}
|
||||
} else {
|
||||
val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||
Log.e("SellsRepository", "Error Add Evidence : $errorBody")
|
||||
Log.e(TAG, "Error Add Evidence : $errorBody")
|
||||
Result.Error(Exception(errorBody))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("SellsRepository", "Exception Add Evidence ", e)
|
||||
Log.e(TAG, "Exception Add Evidence ", e)
|
||||
Result.Error(e)
|
||||
}
|
||||
}
|
||||
@ -119,7 +120,7 @@ class MyStoreRepository(private val apiService: ApiService) {
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("MyStoreRepository", "Error fetching balance", e)
|
||||
Log.e(TAG, "Error fetching balance", e)
|
||||
Result.Error(e)
|
||||
}
|
||||
}
|
||||
@ -133,11 +134,15 @@ class MyStoreRepository(private val apiService: ApiService) {
|
||||
throw Exception("Failed to fetch store products: ${response.message()}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("ProductRepository", "Error fetching store products", e)
|
||||
Log.e(TAG, "Error fetching store products", e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private var TAG = "MyStoreRepository"
|
||||
}
|
||||
|
||||
// private fun fetchBalance() {
|
||||
// showLoading(true)
|
||||
// lifecycleScope.launch {
|
||||
|
@ -99,7 +99,6 @@ class AddressActivity : AppCompatActivity() {
|
||||
|
||||
viewModel.selectedAddressId.observe(this) { selectedId ->
|
||||
adapter.setSelectedAddressId(selectedId)
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
@ -120,6 +119,7 @@ class AddressActivity : AppCompatActivity() {
|
||||
val intent = Intent()
|
||||
intent.putExtra(EXTRA_ADDRESS_ID, addressId)
|
||||
setResult(RESULT_OK, intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -21,6 +21,8 @@ class AddressViewModel(private val repository: OrderRepository): ViewModel() {
|
||||
val response = repository.getAddress()
|
||||
response?.let {
|
||||
_addresses.value = it.addresses
|
||||
?.filter { address -> address.isStoreLocation == false }
|
||||
?: emptyList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,10 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.alya.ecommerce_serang.data.api.dto.City
|
||||
import com.alya.ecommerce_serang.data.api.dto.Province
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.SubdistrictsItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.AddressesItem
|
||||
import com.alya.ecommerce_serang.data.repository.AddressRepository
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class AddressViewModel(private val addressRepository: AddressRepository) : ViewModel() {
|
||||
@ -33,6 +35,12 @@ class AddressViewModel(private val addressRepository: AddressRepository) : ViewM
|
||||
private val _saveSuccess = MutableLiveData<Boolean>()
|
||||
val saveSuccess: LiveData<Boolean> get() = _saveSuccess
|
||||
|
||||
private val _subdistrictState = MutableLiveData<Result<List<SubdistrictsItem>>>()
|
||||
val subdistrictState: LiveData<Result<List<SubdistrictsItem>>> = _subdistrictState
|
||||
|
||||
|
||||
var selectedSubdistrict: String? = null
|
||||
val subdistrict = MutableLiveData<String>()
|
||||
|
||||
fun fetchProvinces() {
|
||||
viewModelScope.launch {
|
||||
@ -64,6 +72,29 @@ class AddressViewModel(private val addressRepository: AddressRepository) : ViewM
|
||||
}
|
||||
}
|
||||
|
||||
fun getSubdistrict(cityId: String) {
|
||||
_subdistrictState.value = Result.Loading
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
|
||||
selectedSubdistrict = cityId
|
||||
val result = addressRepository.getListSubdistrict(cityId)
|
||||
result?.let {
|
||||
_subdistrictState.postValue(Result.Success(it.subdistricts))
|
||||
Log.d(TAG, "Cities loaded for province $cityId: ${it.subdistricts.size}")
|
||||
} ?: run {
|
||||
_subdistrictState.postValue(Result.Error(Exception("Failed to load cities")))
|
||||
Log.e(TAG, "City result was null for province $cityId")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_subdistrictState.postValue(Result.Error(Exception(e.message ?: "Error loading cities")))
|
||||
Log.e(TAG, "Error fetching cities for province $cityId", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// fun fetchProvinces() {
|
||||
// Log.d(TAG, "fetchProvinces() called")
|
||||
// _isLoading.value = true
|
||||
@ -223,6 +254,7 @@ class AddressViewModel(private val addressRepository: AddressRepository) : ViewM
|
||||
}
|
||||
|
||||
private fun buildUpdateBody(oldAddress: AddressesItem, newAddress: AddressesItem): Map<String, Any> {
|
||||
|
||||
val params = mutableMapOf<String, Any>()
|
||||
|
||||
fun addIfChanged(key: String, oldValue: Any?, newValue: Any?) {
|
||||
|
@ -21,6 +21,8 @@ import java.text.NumberFormat
|
||||
import java.util.Locale
|
||||
|
||||
class MyStoreViewModel(private val repository: MyStoreRepository): ViewModel() {
|
||||
private var TAG = "MyStoreViewModel"
|
||||
|
||||
private val _myStoreProfile = MutableLiveData<Store?>()
|
||||
val myStoreProfile: LiveData<Store?> = _myStoreProfile
|
||||
|
||||
@ -84,30 +86,40 @@ class MyStoreViewModel(private val repository: MyStoreRepository): ViewModel() {
|
||||
|
||||
if (store == null) {
|
||||
_errorMessage.postValue("Data toko tidak tersedia")
|
||||
Log.e(TAG, "Store data is null")
|
||||
return@launch
|
||||
}
|
||||
|
||||
Log.d("UpdateStoreProfileVM", "Calling repository with params:")
|
||||
Log.d("UpdateStoreProfileVM", "storeName: $storeName")
|
||||
Log.d("UpdateStoreProfileVM", "description: $description")
|
||||
Log.d("UpdateStoreProfileVM", "isOnLeave: $isOnLeave")
|
||||
Log.d("UpdateStoreProfileVM", "storeType: $storeType")
|
||||
Log.d("UpdateStoreProfileVM", "storeImage: ${storeImage?.headers}")
|
||||
|
||||
val response = repository.updateStoreProfile(
|
||||
storeName = storeName,
|
||||
storeStatus = "active".toRequestBody(),
|
||||
storeDescription = description,
|
||||
isOnLeave = isOnLeave,
|
||||
cityId = store.cityId.toString().toRequestBody(),
|
||||
provinceId = store.provinceId.toString().toRequestBody(),
|
||||
street = store.street.toRequestBody(),
|
||||
subdistrict = store.subdistrict.toRequestBody(),
|
||||
detail = store.detail.toRequestBody(),
|
||||
postalCode = store.postalCode.toRequestBody(),
|
||||
latitude = store.latitude.toRequestBody(),
|
||||
longitude = store.longitude.toRequestBody(),
|
||||
userPhone = store.phone.toRequestBody(),
|
||||
storeType = storeType,
|
||||
storeimg = storeImage
|
||||
)
|
||||
if (response.isSuccessful) _updateStoreProfileResult.postValue(response.body())
|
||||
else _errorMessage.postValue("Gagal memperbarui profil")
|
||||
|
||||
if (response != null) {
|
||||
if (response.isSuccessful) {
|
||||
_updateStoreProfileResult.postValue(response.body())
|
||||
Log.d(TAG, "Update successful: ${response.body()}")
|
||||
} else {
|
||||
_errorMessage.postValue("Gagal memperbarui profil")
|
||||
Log.e(TAG, "Update failed: ${response.errorBody()?.string()}")
|
||||
}
|
||||
} else {
|
||||
_errorMessage.postValue("Terjadi kesalahan jaringan atau server")
|
||||
Log.e(TAG, "Repository returned null response")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_errorMessage.postValue(e.message ?: "Unexpected error")
|
||||
Log.e(TAG, "Exception updating store profile", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -120,13 +132,13 @@ class MyStoreViewModel(private val repository: MyStoreRepository): ViewModel() {
|
||||
result.data.orders.size ?: 0
|
||||
}
|
||||
is Result.Error -> {
|
||||
Log.e("SellsViewModel", "Error getting orders count: ${result.exception.message}")
|
||||
Log.e(TAG, "Error getting orders count: ${result.exception.message}")
|
||||
0
|
||||
}
|
||||
is Result.Loading -> 0
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("SellsViewModel", "Exception getting orders count", e)
|
||||
Log.e(TAG, "Exception getting orders count", e)
|
||||
0
|
||||
}
|
||||
}
|
||||
@ -138,7 +150,7 @@ class MyStoreViewModel(private val repository: MyStoreRepository): ViewModel() {
|
||||
|
||||
statuses.forEach { status ->
|
||||
counts[status] = getTotalOrdersByStatus(status)
|
||||
Log.d("SellsViewModel", "Status: $status, countOrder=${counts[status]}")
|
||||
Log.d(TAG, "Status: $status, countOrder=${counts[status]}")
|
||||
}
|
||||
|
||||
return counts
|
||||
|
@ -37,6 +37,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:src="@drawable/baseline_edit_24"
|
||||
android:layout_marginHorizontal="16dp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"/>
|
||||
<TextView
|
||||
|
Reference in New Issue
Block a user