mirror of
https://github.com/shaulascr/ecommerce_serang.git
synced 2025-12-16 16:01:02 +00:00
Compare commits
47 Commits
f539bfb9f0
...
screen-fea
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d24673107 | |||
| b6b701fa3b | |||
| 94bd32d6b0 | |||
| 6baf4ee5ce | |||
| ff8654d12a | |||
| 22122d631b | |||
| 6efbcde784 | |||
| a6d5d10e78 | |||
| f373035f8e | |||
| 8d9815d89f | |||
| 1f41b4681f | |||
| 3d8e82e3b5 | |||
| d4eacf6c7c | |||
| 7351f9c5b7 | |||
| 9fefe1d818 | |||
| 0a8dac4d23 | |||
| 77ea5ed90a | |||
| c303b419ed | |||
| bad456f842 | |||
| ed60528049 | |||
| a9c2f9c103 | |||
| d08f5465d1 | |||
| eab3884fd6 | |||
| 930688f50d | |||
| b70c671710 | |||
| 3ac0461c7c | |||
| 61e8e1fe3c | |||
| 39aa079003 | |||
| 577fb27495 | |||
| 8b76077a77 | |||
| 6194dca259 | |||
| 97be7a8f62 | |||
| d6d27e1c61 | |||
| 5992539ef3 | |||
| c6c1c9f348 | |||
| 7f01914cb0 | |||
| f43d160fe1 | |||
| bbaf6ed45d | |||
| 9299fcbad1 | |||
| 8b2a092465 | |||
| adf324e15d | |||
| f7f198e46f | |||
| 331410eb98 | |||
| db38654159 | |||
| 0cbaecd0cd | |||
| 2423f45968 | |||
| c2bed56bf5 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
*.iml
|
||||
*.log
|
||||
.gradle
|
||||
/local.properties
|
||||
/.idea/caches
|
||||
@ -12,4 +13,4 @@
|
||||
/captures
|
||||
.externalNativeBuild
|
||||
.cxx
|
||||
local.properties
|
||||
/app/google-services.json
|
||||
|
||||
13
.idea/deviceManager.xml
generated
Normal file
13
.idea/deviceManager.xml
generated
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DeviceTable">
|
||||
<option name="columnSorters">
|
||||
<list>
|
||||
<ColumnSorterState>
|
||||
<option name="column" value="Name" />
|
||||
<option name="order" value="ASCENDING" />
|
||||
</ColumnSorterState>
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
68
README.md
Normal file
68
README.md
Normal file
@ -0,0 +1,68 @@
|
||||
# E-Commerce Serang (Android App)
|
||||
|
||||
A mobile e-commerce platform built with **Kotlin** (Android) and a backend in **Express**, tailored for small businesses (UMKM). Supports browsing, ordering, chatting, and tracking — with **shipping cost calculation (RajaOngkir)** and **push notifications** via Firebase Cloud Messaging.
|
||||
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
This Android app includes:
|
||||
|
||||
- Account registration, login, and OTP verification
|
||||
- Browsing products by category and store
|
||||
- Cart and checkout orders
|
||||
- Shipping cost estimation via RajaOngkir API
|
||||
- Checkout order, tracking, and status updates
|
||||
- Real-time buyer–seller chat
|
||||
- Store registration and product management
|
||||
- Store Balance as active status
|
||||
- Top up store balance
|
||||
- Write rating and feedback for purchased products
|
||||
- Push notifications for user activity
|
||||
|
||||
The app communicates with a custom backend server via REST API and WebSocket.
|
||||
|
||||
## Tech Stack
|
||||
- MVVM architecture
|
||||
- Retrofit for API communication
|
||||
- Hilt for dependency injection
|
||||
- Socket.IO client for real-time chat
|
||||
- Firebase Cloud Messaging (FCM) for notifications
|
||||
- Coroutines for async operations
|
||||
- Glide for image loading
|
||||
- ViewBinding for UI access
|
||||
- LiveData and StateFlow for reactive UI
|
||||
- RajaOngkir API integration for shipping cost
|
||||
|
||||
## Project Structure
|
||||
- api/retrofit/
|
||||
- data/
|
||||
- di/
|
||||
- ui/
|
||||
- auth/
|
||||
- home/
|
||||
- cart/
|
||||
- order/
|
||||
- history/
|
||||
- review/
|
||||
- chat/
|
||||
- profile/
|
||||
- store/
|
||||
- addProduct/
|
||||
- sells/
|
||||
- balance/
|
||||
- review/
|
||||
- product/
|
||||
- notif/
|
||||
- utils/
|
||||
- google-services.json
|
||||
|
||||
## How to Run
|
||||
|
||||
1. Clone this project and open it in Android Studio
|
||||
2. Add your `google-services.json` for Firebase (FCM)
|
||||
3. Update the API base URL in the Retrofit client
|
||||
5. Settings BASE_URL in your local.properties
|
||||
4. Build and run on an emulator or physical device
|
||||
|
||||
|
||||
3
app/.gitignore
vendored
3
app/.gitignore
vendored
@ -1 +1,2 @@
|
||||
/build
|
||||
/build
|
||||
google-services.json
|
||||
@ -124,7 +124,4 @@ dependencies {
|
||||
implementation(platform("com.google.firebase:firebase-bom:33.13.0"))
|
||||
implementation("com.google.firebase:firebase-analytics")
|
||||
implementation("com.google.firebase:firebase-messaging-ktx")
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,29 +0,0 @@
|
||||
{
|
||||
"project_info": {
|
||||
"project_number": "284675201257",
|
||||
"project_id": "ecommerce-serang",
|
||||
"storage_bucket": "ecommerce-serang.firebasestorage.app"
|
||||
},
|
||||
"client": [
|
||||
{
|
||||
"client_info": {
|
||||
"mobilesdk_app_id": "1:284675201257:android:2755670e3dbb1b48683878",
|
||||
"android_client_info": {
|
||||
"package_name": "com.alya.ecommerce_serang"
|
||||
}
|
||||
},
|
||||
"oauth_client": [],
|
||||
"api_key": [
|
||||
{
|
||||
"current_key": "AIzaSyB-nWHsVbdV4PPIH06JZSStIVXjv9Qc4iU"
|
||||
}
|
||||
],
|
||||
"services": {
|
||||
"appinvite_service": {
|
||||
"other_platform_oauth_client": []
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"configuration_version": "1"
|
||||
}
|
||||
@ -29,6 +29,12 @@
|
||||
android:theme="@style/Theme.Ecommerce_serang"
|
||||
android:usesCleartextTraffic="true"
|
||||
tools:targetApi="31">
|
||||
<activity
|
||||
android:name=".ui.auth.ResetPassActivity"
|
||||
android:exported="false" />
|
||||
<activity
|
||||
android:name=".ui.profile.mystore.StoreSuspendedActivity"
|
||||
android:exported="false" />
|
||||
<activity
|
||||
android:name=".ui.profile.mystore.StoreOnReviewActivity"
|
||||
android:exported="false" />
|
||||
@ -82,12 +88,11 @@
|
||||
<!-- android:name="androidx.startup.InitializationProvider" -->
|
||||
<!-- android:authorities="${applicationId}.androidx-startup" -->
|
||||
<!-- tools:node="remove" /> -->
|
||||
<service
|
||||
android:name=".ui.notif.SimpleWebSocketService"
|
||||
android:enabled="true"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="dataSync" />
|
||||
|
||||
<!-- <service -->
|
||||
<!-- android:name=".ui.notif.SimpleWebSocketService" -->
|
||||
<!-- android:enabled="true" -->
|
||||
<!-- android:exported="false" -->
|
||||
<!-- android:foregroundServiceType="dataSync" /> -->
|
||||
<activity
|
||||
android:name=".ui.profile.mystore.chat.ChatStoreActivity"
|
||||
android:exported="false"
|
||||
|
||||
BIN
app/src/main/ic_launcher-playstore.png
Normal file
BIN
app/src/main/ic_launcher-playstore.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
@ -2,30 +2,20 @@ package com.alya.ecommerce_serang.data.api.dto
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class City(
|
||||
@SerializedName("city_id")
|
||||
val cityId: String,
|
||||
data class CityResponse(
|
||||
|
||||
@SerializedName("city_name")
|
||||
val cityName: String,
|
||||
@field:SerializedName("cities")
|
||||
val cities: List<City>,
|
||||
|
||||
@SerializedName("province_id")
|
||||
val provinceId: String,
|
||||
|
||||
@SerializedName("province")
|
||||
val provinceName: String,
|
||||
|
||||
@SerializedName("type")
|
||||
val type: String,
|
||||
|
||||
@SerializedName("postal_code")
|
||||
val postalCode: String
|
||||
@field:SerializedName("message")
|
||||
val message: String
|
||||
)
|
||||
|
||||
data class CityResponse(
|
||||
@SerializedName("message")
|
||||
val message: String,
|
||||
data class City(
|
||||
|
||||
@SerializedName("cities")
|
||||
val data: List<City>
|
||||
)
|
||||
@field:SerializedName("city_name")
|
||||
val cityName: String,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: String
|
||||
)
|
||||
|
||||
@ -2,12 +2,15 @@ package com.alya.ecommerce_serang.data.api.dto
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class CreateAddressRequest (
|
||||
data class CreateAddressRequest(
|
||||
@SerializedName("userId")
|
||||
val userId: Int,
|
||||
|
||||
@SerializedName("latitude")
|
||||
val lat: Double? = null,
|
||||
val lat: Double,
|
||||
|
||||
@SerializedName("longitude")
|
||||
val long: Double? = null,
|
||||
val long: Double,
|
||||
|
||||
@SerializedName("street")
|
||||
val street: String,
|
||||
@ -16,26 +19,26 @@ data class CreateAddressRequest (
|
||||
val subDistrict: String,
|
||||
|
||||
@SerializedName("city_id")
|
||||
val cityId: Int,
|
||||
val cityId: String,
|
||||
|
||||
@SerializedName("province_id")
|
||||
val provId: Int,
|
||||
|
||||
@SerializedName("postal_code")
|
||||
val postCode: String? = null,
|
||||
val postCode: String,
|
||||
|
||||
@SerializedName("village_id")
|
||||
val idVillage: String?, // nullable for now
|
||||
|
||||
@SerializedName("detail")
|
||||
val detailAddress: String? = null,
|
||||
val detailAddress: String,
|
||||
|
||||
@SerializedName("user_id")
|
||||
val userId: Int,
|
||||
@SerializedName("is_store_location")
|
||||
val isStoreLocation: Boolean,
|
||||
|
||||
@SerializedName("recipient")
|
||||
val recipient: String,
|
||||
|
||||
@SerializedName("phone")
|
||||
val phone: String,
|
||||
|
||||
@SerializedName("is_store_location")
|
||||
val isStoreLocation: Boolean
|
||||
|
||||
val phone: String
|
||||
)
|
||||
@ -90,7 +90,7 @@ data class OrdersItem(
|
||||
val orderId: Int,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: Int,
|
||||
val cityId: String,
|
||||
|
||||
var displayStatus: String? = null
|
||||
)
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
package com.alya.ecommerce_serang.data.api.dto
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class ResetPassReq (
|
||||
@SerializedName("emailOrPhone")
|
||||
val emailOrPhone: String
|
||||
)
|
||||
@ -98,5 +98,5 @@ data class Store(
|
||||
val storeDescription: String,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: Int
|
||||
val cityId: String
|
||||
)
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
package com.alya.ecommerce_serang.data.api.dto
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class UpdateAddressReq(
|
||||
@SerializedName("street")
|
||||
val street: String? = "",
|
||||
|
||||
@SerializedName("subdistrict")
|
||||
val subdistrict: String? = "",
|
||||
|
||||
@SerializedName("postal_code")
|
||||
val postalCCode: String? = "",
|
||||
|
||||
@SerializedName("detail")
|
||||
val detail: String? = "",
|
||||
|
||||
@SerializedName("city_id")
|
||||
val cityId: String? = "",
|
||||
|
||||
@SerializedName("province_id")
|
||||
val provId: String? = "",
|
||||
|
||||
@SerializedName("phone")
|
||||
val phone: String? = ""
|
||||
|
||||
|
||||
)
|
||||
@ -0,0 +1,9 @@
|
||||
package com.alya.ecommerce_serang.data.api.response.auth
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class ResetPassResponse(
|
||||
|
||||
@field:SerializedName("message")
|
||||
val message: String
|
||||
)
|
||||
@ -12,10 +12,8 @@ data class ListProvinceResponse(
|
||||
)
|
||||
|
||||
data class ProvincesItem(
|
||||
|
||||
@field:SerializedName("province")
|
||||
val province: String,
|
||||
|
||||
@field:SerializedName("province_id")
|
||||
val provinceId: String
|
||||
val provinceId: String,
|
||||
@field:SerializedName("province")
|
||||
val province: String
|
||||
)
|
||||
|
||||
@ -56,7 +56,7 @@ data class Orders(
|
||||
val orderItems: List<OrderListItemsItem>,
|
||||
|
||||
@field:SerializedName("auto_completed_at")
|
||||
val autoCompletedAt: String? = null,
|
||||
val autoCompletedAt: String,
|
||||
|
||||
@field:SerializedName("is_store_location")
|
||||
val isStoreLocation: Boolean? = null,
|
||||
@ -119,7 +119,7 @@ data class Orders(
|
||||
val orderId: Int,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: Int
|
||||
val cityId: String
|
||||
)
|
||||
|
||||
data class OrderListItemsItem(
|
||||
|
||||
@ -15,7 +15,7 @@ data class OrderListResponse(
|
||||
data class OrderItemsItem(
|
||||
|
||||
@field:SerializedName("review_id")
|
||||
val reviewId: Int? = null,
|
||||
val reviewId: Int,
|
||||
|
||||
@field:SerializedName("quantity")
|
||||
val quantity: Int,
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
package com.alya.ecommerce_serang.data.api.response.customer.order
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class SubdistrictResponse(
|
||||
|
||||
@field:SerializedName("subdistricts")
|
||||
val subdistricts: List<SubdistrictsItem>,
|
||||
|
||||
@field:SerializedName("message")
|
||||
val message: String
|
||||
)
|
||||
|
||||
data class SubdistrictsItem(
|
||||
|
||||
@field:SerializedName("subdistrict_id")
|
||||
val subdistrictId: String,
|
||||
|
||||
@field:SerializedName("subdistrict_name")
|
||||
val subdistrictName: String
|
||||
)
|
||||
@ -0,0 +1,24 @@
|
||||
package com.alya.ecommerce_serang.data.api.response.customer.order
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class VillagesResponse(
|
||||
|
||||
@field:SerializedName("villages")
|
||||
val villages: List<VillagesItem>,
|
||||
|
||||
@field:SerializedName("message")
|
||||
val message: String
|
||||
)
|
||||
|
||||
data class VillagesItem(
|
||||
|
||||
@field:SerializedName("village_id")
|
||||
val villageId: String,
|
||||
|
||||
@field:SerializedName("village_name")
|
||||
val villageName: String,
|
||||
|
||||
@field:SerializedName("postal_code")
|
||||
val postalCode: String
|
||||
)
|
||||
@ -82,7 +82,8 @@ data class Product(
|
||||
|
||||
data class CartItemCheckoutInfo(
|
||||
val cartItem: CartItemsItem,
|
||||
val isWholesale: Boolean
|
||||
val isWholesale: Boolean,
|
||||
val wholesalePrice: Int? = null
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -114,7 +114,7 @@ data class Store(
|
||||
val storeDescription: String,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: Int
|
||||
val cityId: String
|
||||
)
|
||||
|
||||
data class ShippingItem(
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
package com.alya.ecommerce_serang.data.api.response.customer.profile
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class AddressDetailResponse(
|
||||
|
||||
@field:SerializedName("address")
|
||||
val address: AddressDetail,
|
||||
|
||||
@field:SerializedName("message")
|
||||
val message: String
|
||||
)
|
||||
|
||||
data class AddressDetail(
|
||||
|
||||
@field:SerializedName("village_id")
|
||||
val villageId: String?,
|
||||
|
||||
@field:SerializedName("is_store_location")
|
||||
val isStoreLocation: Boolean,
|
||||
|
||||
@field:SerializedName("latitude")
|
||||
val latitude: String,
|
||||
|
||||
@field:SerializedName("province_name")
|
||||
val provinceName: String?,
|
||||
|
||||
@field:SerializedName("subdistrict_id")
|
||||
val subdistrictId: String?,
|
||||
|
||||
@field:SerializedName("city_name")
|
||||
val cityName: String?,
|
||||
|
||||
@field:SerializedName("user_id")
|
||||
val userId: Int,
|
||||
|
||||
@field:SerializedName("province_id")
|
||||
val provinceId: String,
|
||||
|
||||
@field:SerializedName("phone")
|
||||
val phone: String?,
|
||||
|
||||
@field:SerializedName("street")
|
||||
val street: String,
|
||||
|
||||
@field:SerializedName("subdistrict")
|
||||
val subdistrict: String,
|
||||
|
||||
@field:SerializedName("recipient")
|
||||
val recipient: String?,
|
||||
|
||||
@field:SerializedName("id")
|
||||
val id: Int,
|
||||
|
||||
@field:SerializedName("detail")
|
||||
val detail: String,
|
||||
|
||||
@field:SerializedName("village_name")
|
||||
val villageName: String,
|
||||
|
||||
@field:SerializedName("postal_code")
|
||||
val postalCode: String,
|
||||
|
||||
@field:SerializedName("longitude")
|
||||
val longitude: String,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: String
|
||||
)
|
||||
@ -4,15 +4,18 @@ import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class AddressResponse(
|
||||
|
||||
@field:SerializedName("addresses")
|
||||
@field:SerializedName("addresses")
|
||||
val addresses: List<AddressesItem>,
|
||||
|
||||
@field:SerializedName("message")
|
||||
@field:SerializedName("message")
|
||||
val message: String
|
||||
)
|
||||
|
||||
data class AddressesItem(
|
||||
|
||||
@field:SerializedName("village_id")
|
||||
val villageId: String?,
|
||||
|
||||
@field:SerializedName("is_store_location")
|
||||
val isStoreLocation: Boolean,
|
||||
|
||||
@ -23,10 +26,10 @@ data class AddressesItem(
|
||||
val userId: Int,
|
||||
|
||||
@field:SerializedName("province_id")
|
||||
val provinceId: Int,
|
||||
val provinceId: String,
|
||||
|
||||
@field:SerializedName("phone")
|
||||
val phone: String,
|
||||
val phone: String?,
|
||||
|
||||
@field:SerializedName("street")
|
||||
val street: String,
|
||||
@ -35,7 +38,7 @@ data class AddressesItem(
|
||||
val subdistrict: String,
|
||||
|
||||
@field:SerializedName("recipient")
|
||||
val recipient: String,
|
||||
val recipient: String?,
|
||||
|
||||
@field:SerializedName("id")
|
||||
val id: Int,
|
||||
@ -50,5 +53,5 @@ data class AddressesItem(
|
||||
val longitude: String,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: Int
|
||||
val cityId: String
|
||||
)
|
||||
|
||||
@ -0,0 +1,57 @@
|
||||
package com.alya.ecommerce_serang.data.api.response.customer.profile
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class UpdateAddressResponse(
|
||||
|
||||
@field:SerializedName("address")
|
||||
val address: Address,
|
||||
|
||||
@field:SerializedName("message")
|
||||
val message: String
|
||||
)
|
||||
|
||||
data class Address(
|
||||
|
||||
@field:SerializedName("village_id")
|
||||
val villageId: String,
|
||||
|
||||
@field:SerializedName("is_store_location")
|
||||
val isStoreLocation: Boolean,
|
||||
|
||||
@field:SerializedName("latitude")
|
||||
val latitude: String,
|
||||
|
||||
@field:SerializedName("user_id")
|
||||
val userId: Int,
|
||||
|
||||
@field:SerializedName("province_id")
|
||||
val provinceId: String,
|
||||
|
||||
@field:SerializedName("phone")
|
||||
val phone: Any,
|
||||
|
||||
@field:SerializedName("street")
|
||||
val street: String,
|
||||
|
||||
@field:SerializedName("subdistrict")
|
||||
val subdistrict: String,
|
||||
|
||||
@field:SerializedName("recipient")
|
||||
val recipient: Any,
|
||||
|
||||
@field:SerializedName("id")
|
||||
val id: Int,
|
||||
|
||||
@field:SerializedName("detail")
|
||||
val detail: String,
|
||||
|
||||
@field:SerializedName("postal_code")
|
||||
val postalCode: String,
|
||||
|
||||
@field:SerializedName("longitude")
|
||||
val longitude: String,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: String
|
||||
)
|
||||
@ -35,7 +35,7 @@ data class Store(
|
||||
val detail: String,
|
||||
@SerializedName("is_store_location") val isStoreLocation: Boolean,
|
||||
@SerializedName("user_id") val userId: Int,
|
||||
@SerializedName("city_id") val cityId: Int,
|
||||
@SerializedName("city_id") val cityId: String,
|
||||
@SerializedName("province_id") val provinceId: Int,
|
||||
val phone: String?,
|
||||
val recipient: String?,
|
||||
|
||||
@ -132,5 +132,5 @@ data class Orders(
|
||||
val username: String? = null,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: Int? = null
|
||||
val cityId: String? = null
|
||||
)
|
||||
@ -129,7 +129,7 @@ data class OrdersItem(
|
||||
val status: String? = null,
|
||||
|
||||
@field:SerializedName("city_id")
|
||||
val cityId: Int? = null,
|
||||
val cityId: String? = null,
|
||||
|
||||
var displayStatus: String? = null
|
||||
)
|
||||
|
||||
@ -15,10 +15,14 @@ class ApiConfig {
|
||||
|
||||
val loggingInterceptor = HttpLoggingInterceptor().apply {
|
||||
level = HttpLoggingInterceptor.Level.BODY
|
||||
//httplogginginterceptor ntuk debug dan monitoring request/response
|
||||
}
|
||||
|
||||
val authInterceptor = AuthInterceptor(tokenManager)
|
||||
// utk tambak token auth otomatis pada header
|
||||
|
||||
// Konfigurasi OkHttpClient
|
||||
//Low-level HTTP client yang melakukan actual network request
|
||||
val client = OkHttpClient.Builder()
|
||||
.addInterceptor(loggingInterceptor)
|
||||
.addInterceptor(authInterceptor)
|
||||
@ -27,13 +31,17 @@ class ApiConfig {
|
||||
.writeTimeout(300, TimeUnit.SECONDS) // 5 minutes
|
||||
.build()
|
||||
|
||||
// Konfigurasi Retrofit
|
||||
val retrofit = Retrofit.Builder()
|
||||
//almat domain backend
|
||||
.baseUrl(BuildConfig.BASE_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
//gson convertes: mengkonversi JSON ke object Kotlin dan sebaliknya
|
||||
.client(client)
|
||||
.build()
|
||||
|
||||
return retrofit.create(ApiService::class.java)
|
||||
// retrofit : menyederhanakan HTTP Request dgn mengubah interface Kotlin di ApiService menjadi HTTP calls secara otomatis
|
||||
}
|
||||
|
||||
fun getUnauthenticatedApiService(): ApiService {
|
||||
|
||||
@ -19,6 +19,7 @@ import com.alya.ecommerce_serang.data.api.dto.OtpRequest
|
||||
import com.alya.ecommerce_serang.data.api.dto.PaymentConfirmRequest
|
||||
import com.alya.ecommerce_serang.data.api.dto.ProvinceResponse
|
||||
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.ReviewProductItem
|
||||
import com.alya.ecommerce_serang.data.api.dto.SearchRequest
|
||||
import com.alya.ecommerce_serang.data.api.dto.ShippingServiceRequest
|
||||
@ -36,6 +37,7 @@ import com.alya.ecommerce_serang.data.api.response.auth.LoginResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.OtpResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.RegisterResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.RegisterStoreResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.ResetPassResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.VerifRegisterResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.chat.ChatHistoryResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.chat.ChatListResponse
|
||||
@ -53,29 +55,33 @@ import com.alya.ecommerce_serang.data.api.response.customer.order.ListCityRespon
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.ListProvinceResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.OrderDetailResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.OrderListResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.SubdistrictResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.VillagesResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.AllProductResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.CategoryResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.DetailStoreProductResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.ProductResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.ReviewProductResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.StoreResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.AddressDetailResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.AddressResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.CreateAddressResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.EditProfileResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.ProfileResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.UpdateAddressResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.order.AddEvidenceResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.order.ComplaintResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.order.CompletedOrderResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.product.CreateSearchResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.product.SearchHistoryResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.sells.PaymentConfirmationResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.GenericResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.product.CreateProductResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.product.DeleteProductResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.product.UpdateProductResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.product.ViewStoreProductsResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.GenericResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.profile.StoreDataResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.review.ProductReviewResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.sells.PaymentConfirmationResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.topup.BalanceTopUpResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.topup.TopUpResponse
|
||||
import okhttp3.MultipartBody
|
||||
@ -389,18 +395,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>
|
||||
@ -452,6 +448,12 @@ interface ApiService {
|
||||
@Body addressData: HashMap<String, Any?>
|
||||
): Response<StoreAddressResponse>
|
||||
|
||||
@PUT("profile/address/edit/{id}")
|
||||
suspend fun updateAddress(
|
||||
@Path("id") addressId: Int,
|
||||
@Body params: Map<String, @JvmSuppressWildcards Any>
|
||||
): Response<UpdateAddressResponse>
|
||||
|
||||
@POST("search")
|
||||
suspend fun saveSearchQuery(
|
||||
@Body searchRequest: SearchRequest
|
||||
@ -512,4 +514,24 @@ interface ApiService {
|
||||
@GET("store/reviews")
|
||||
suspend fun getStoreProductReview(
|
||||
): Response<ProductReviewResponse>
|
||||
|
||||
@GET("subdistrict/{cityId}")
|
||||
suspend fun getSubdistrict(
|
||||
@Path("cityId") cityId: String
|
||||
): Response<SubdistrictResponse>
|
||||
|
||||
@GET("villages/{subdistrictId}")
|
||||
suspend fun getVillages(
|
||||
@Path("subdistrictId") subdistrictId: String
|
||||
): Response<VillagesResponse>
|
||||
|
||||
@POST("resetpass")
|
||||
suspend fun postResetPass(
|
||||
@Body request: ResetPassReq
|
||||
): Response<ResetPassResponse>
|
||||
|
||||
@GET("profile/address/detail/{id}")
|
||||
suspend fun getDetailAddress(
|
||||
@Path("id") addressId: Int
|
||||
): Response<AddressDetailResponse>
|
||||
}
|
||||
@ -1,131 +1,140 @@
|
||||
package com.alya.ecommerce_serang.data.repository
|
||||
|
||||
import android.util.Log
|
||||
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.dto.StoreAddress
|
||||
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
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.json.JSONObject
|
||||
import retrofit2.Response
|
||||
|
||||
class AddressRepository(private val apiService: ApiService) {
|
||||
|
||||
private val TAG = "AddressRepository"
|
||||
|
||||
suspend fun getProvinces(): List<Province> = withContext(Dispatchers.IO) {
|
||||
Log.d(TAG, "getProvinces() called")
|
||||
try {
|
||||
val response = apiService.getProvinces()
|
||||
Log.d(TAG, "getProvinces() response: isSuccessful=${response.isSuccessful}, code=${response.code()}")
|
||||
// suspend fun getProvinces(): List<Province> = withContext(Dispatchers.IO) {
|
||||
// Log.d(TAG, "getProvinces() called")
|
||||
// try {
|
||||
// val response = apiService.getProvinces()
|
||||
// Log.d(TAG, "getProvinces() response: isSuccessful=${response.isSuccessful}, code=${response.code()}")
|
||||
//
|
||||
// // Log the raw response body for debugging
|
||||
// val rawBody = response.raw().toString()
|
||||
// Log.d(TAG, "Raw response: $rawBody")
|
||||
//
|
||||
// if (response.isSuccessful) {
|
||||
// val responseBody = response.body()
|
||||
// Log.d(TAG, "Response body: ${Gson().toJson(responseBody)}")
|
||||
//
|
||||
// val provinces = responseBody?.data ?: emptyList()
|
||||
// Log.d(TAG, "getProvinces() success, got ${provinces.size} provinces")
|
||||
// return@withContext provinces
|
||||
// } else {
|
||||
// val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||
// Log.e(TAG, "getProvinces() error: $errorBody")
|
||||
// throw Exception("API Error (${response.code()}): $errorBody")
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Exception in getProvinces()", e)
|
||||
// throw Exception("Network error: ${e.message}")
|
||||
// }
|
||||
// }
|
||||
|
||||
// Log the raw response body for debugging
|
||||
val rawBody = response.raw().toString()
|
||||
Log.d(TAG, "Raw response: $rawBody")
|
||||
// suspend fun getCities(provinceId: String): List<City> = withContext(Dispatchers.IO) {
|
||||
// Log.d(TAG, "getCities() called with provinceId: $provinceId")
|
||||
// try {
|
||||
// val response = apiService.getCities(provinceId)
|
||||
// Log.d(TAG, "getCities() response: isSuccessful=${response.isSuccessful}, code=${response.code()}")
|
||||
//
|
||||
// if (response.isSuccessful) {
|
||||
// val responseBody = response.body()
|
||||
// Log.d(TAG, "Response body: ${Gson().toJson(responseBody)}")
|
||||
//
|
||||
// val cities = responseBody?.data ?: emptyList()
|
||||
// Log.d(TAG, "getCities() success, got ${cities.size} cities")
|
||||
// return@withContext cities
|
||||
// } else {
|
||||
// val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||
// Log.e(TAG, "getCities() error: $errorBody")
|
||||
// throw Exception("API Error (${response.code()}): $errorBody")
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Exception in getCities()", e)
|
||||
// throw Exception("Network error: ${e.message}")
|
||||
// }
|
||||
// }
|
||||
|
||||
if (response.isSuccessful) {
|
||||
val responseBody = response.body()
|
||||
Log.d(TAG, "Response body: ${Gson().toJson(responseBody)}")
|
||||
|
||||
val provinces = responseBody?.data ?: emptyList()
|
||||
Log.d(TAG, "getProvinces() success, got ${provinces.size} provinces")
|
||||
return@withContext provinces
|
||||
} else {
|
||||
val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||
Log.e(TAG, "getProvinces() error: $errorBody")
|
||||
throw Exception("API Error (${response.code()}): $errorBody")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Exception in getProvinces()", e)
|
||||
throw Exception("Network error: ${e.message}")
|
||||
}
|
||||
suspend fun getProvinces(): Response<ProvinceResponse> {
|
||||
return apiService.getProvinces()
|
||||
}
|
||||
|
||||
suspend fun getCities(provinceId: String): List<City> = withContext(Dispatchers.IO) {
|
||||
Log.d(TAG, "getCities() called with provinceId: $provinceId")
|
||||
try {
|
||||
val response = apiService.getCities(provinceId)
|
||||
Log.d(TAG, "getCities() response: isSuccessful=${response.isSuccessful}, code=${response.code()}")
|
||||
|
||||
if (response.isSuccessful) {
|
||||
val responseBody = response.body()
|
||||
Log.d(TAG, "Response body: ${Gson().toJson(responseBody)}")
|
||||
|
||||
val cities = responseBody?.data ?: emptyList()
|
||||
Log.d(TAG, "getCities() success, got ${cities.size} cities")
|
||||
return@withContext cities
|
||||
} else {
|
||||
val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||
Log.e(TAG, "getCities() error: $errorBody")
|
||||
throw Exception("API Error (${response.code()}): $errorBody")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Exception in getCities()", e)
|
||||
throw Exception("Network error: ${e.message}")
|
||||
}
|
||||
suspend fun getCities(provinceId: String): Response<CityResponse> {
|
||||
return apiService.getCities(provinceId)
|
||||
}
|
||||
|
||||
suspend fun getStoreAddress(): StoreAddress? = withContext(Dispatchers.IO) {
|
||||
Log.d(TAG, "getStoreAddress() called")
|
||||
try {
|
||||
val response = apiService.getStoreAddress()
|
||||
Log.d(TAG, "getStoreAddress() response: isSuccessful=${response.isSuccessful}, code=${response.code()}")
|
||||
|
||||
if (response.isSuccessful) {
|
||||
val responseBody = response.body()
|
||||
val rawJson = Gson().toJson(responseBody)
|
||||
Log.d(TAG, "Response body: $rawJson")
|
||||
|
||||
val address = responseBody?.data
|
||||
Log.d(TAG, "getStoreAddress() success, address: $address")
|
||||
|
||||
// Convert numeric strings to proper types if needed
|
||||
address?.let {
|
||||
// Handle city_id if it's a number
|
||||
if (it.cityId.isBlank() && rawJson.contains("city_id")) {
|
||||
try {
|
||||
val cityId = JSONObject(rawJson).getJSONObject("store").optInt("city_id", 0)
|
||||
if (cityId > 0) {
|
||||
it.javaClass.getDeclaredField("cityId").apply {
|
||||
isAccessible = true
|
||||
set(it, cityId.toString())
|
||||
}
|
||||
Log.d(TAG, "Updated cityId to: ${it.cityId}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error parsing city_id", e)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle province_id if it's a number
|
||||
if (it.provinceId.isBlank() && rawJson.contains("province_id")) {
|
||||
try {
|
||||
val provinceId = JSONObject(rawJson).getJSONObject("store").optInt("province_id", 0)
|
||||
if (provinceId > 0) {
|
||||
it.javaClass.getDeclaredField("provinceId").apply {
|
||||
isAccessible = true
|
||||
set(it, provinceId.toString())
|
||||
}
|
||||
Log.d(TAG, "Updated provinceId to: ${it.provinceId}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error parsing province_id", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return@withContext address
|
||||
} else {
|
||||
val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||
Log.e(TAG, "getStoreAddress() error: $errorBody")
|
||||
throw Exception("API Error (${response.code()}): $errorBody")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Exception in getStoreAddress()", e)
|
||||
throw Exception("Network error: ${e.message}")
|
||||
}
|
||||
}
|
||||
// suspend fun getStoreAddress(): StoreAddress? = withContext(Dispatchers.IO) {
|
||||
// Log.d(TAG, "getStoreAddress() called")
|
||||
// try {
|
||||
// val response = apiService.getStoreAddress()
|
||||
// Log.d(TAG, "getStoreAddress() response: isSuccessful=${response.isSuccessful}, code=${response.code()}")
|
||||
//
|
||||
// if (response.isSuccessful) {
|
||||
// val responseBody = response.body()
|
||||
// val rawJson = Gson().toJson(responseBody)
|
||||
// Log.d(TAG, "Response body: $rawJson")
|
||||
//
|
||||
// val address = responseBody?.data
|
||||
// Log.d(TAG, "getStoreAddress() success, address: $address")
|
||||
//
|
||||
// // Convert numeric strings to proper types if needed
|
||||
// address?.let {
|
||||
// // Handle city_id if it's a number
|
||||
// if (it.cityId.isBlank() && rawJson.contains("city_id")) {
|
||||
// try {
|
||||
// val cityId = JSONObject(rawJson).getJSONObject("store").optInt("city_id", 0)
|
||||
// if (cityId > 0) {
|
||||
// it.javaClass.getDeclaredField("cityId").apply {
|
||||
// isAccessible = true
|
||||
// set(it, cityId.toString())
|
||||
// }
|
||||
// Log.d(TAG, "Updated cityId to: ${it.cityId}")
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Error parsing city_id", e)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Handle province_id if it's a number
|
||||
// if (it.provinceId.isBlank() && rawJson.contains("province_id")) {
|
||||
// try {
|
||||
// val provinceId = JSONObject(rawJson).getJSONObject("store").optInt("province_id", 0)
|
||||
// if (provinceId > 0) {
|
||||
// it.javaClass.getDeclaredField("provinceId").apply {
|
||||
// isAccessible = true
|
||||
// set(it, provinceId.toString())
|
||||
// }
|
||||
// Log.d(TAG, "Updated provinceId to: ${it.provinceId}")
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Error parsing province_id", e)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return@withContext address
|
||||
// } else {
|
||||
// val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||
// Log.e(TAG, "getStoreAddress() error: $errorBody")
|
||||
// throw Exception("API Error (${response.code()}): $errorBody")
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Exception in getStoreAddress()", e)
|
||||
// throw Exception("Network error: ${e.message}")
|
||||
// }
|
||||
// }
|
||||
|
||||
suspend fun saveStoreAddress(
|
||||
provinceId: String,
|
||||
@ -171,4 +180,17 @@ class AddressRepository(private val apiService: ApiService) {
|
||||
throw Exception("Network error: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getStoreAddress(): Response<AddressResponse> {
|
||||
return apiService.getAddress()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,12 @@
|
||||
package com.alya.ecommerce_serang.data.repository
|
||||
|
||||
import android.util.Log
|
||||
import com.alya.ecommerce_serang.data.api.dto.ProductsItem
|
||||
import com.alya.ecommerce_serang.data.api.dto.Store
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.ListStoreTypeResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.StoreResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.profile.StoreDataResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.sells.OrderListResponse
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiService
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
@ -51,24 +53,129 @@ 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(TAG, "Add Evidence : $status")
|
||||
val response = apiService.getSellList(status)
|
||||
|
||||
if (response.isSuccessful) {
|
||||
val allListSell = response.body()
|
||||
if (allListSell != null) {
|
||||
Log.d(TAG, "Add Evidence successfully: ${allListSell.message}")
|
||||
Result.Success(allListSell)
|
||||
} else {
|
||||
Log.e(TAG, "Response body was null")
|
||||
Result.Error(Exception("Empty response from server"))
|
||||
}
|
||||
} else {
|
||||
val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||
Log.e(TAG, "Error Add Evidence : $errorBody")
|
||||
Result.Error(Exception(errorBody))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Exception Add Evidence ", e)
|
||||
Result.Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getBalance(): Result<com.alya.ecommerce_serang.data.api.response.store.StoreResponse> {
|
||||
return try {
|
||||
val response = apiService.getMyStoreData()
|
||||
|
||||
if (response.isSuccessful) {
|
||||
val body = response.body()
|
||||
?: return Result.Error(IllegalStateException("Response body is null"))
|
||||
|
||||
// Validate the balance field
|
||||
val balanceRaw = body.store.balance
|
||||
balanceRaw.toDoubleOrNull()
|
||||
?: return Result.Error(NumberFormatException("Invalid balance format: $balanceRaw"))
|
||||
|
||||
Result.Success(body)
|
||||
} else {
|
||||
Result.Error(
|
||||
Exception("Failed to load balance: ${response.code()} ${response.message()}")
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error fetching balance", e)
|
||||
Result.Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun fetchMyStoreProducts(): List<ProductsItem> {
|
||||
return try {
|
||||
val response = apiService.getStoreProduct()
|
||||
if (response.isSuccessful) {
|
||||
response.body()?.products?.filterNotNull() ?: emptyList()
|
||||
} else {
|
||||
throw Exception("Failed to fetch store products: ${response.message()}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error fetching store products", e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private var TAG = "MyStoreRepository"
|
||||
}
|
||||
|
||||
// private fun fetchBalance() {
|
||||
// showLoading(true)
|
||||
// lifecycleScope.launch {
|
||||
// try {
|
||||
// val response = ApiConfig.getApiService(sessionManager).getMyStoreData()
|
||||
// if (response.isSuccessful && response.body() != null) {
|
||||
// val storeData = response.body()!!
|
||||
// val balance = storeData.store.balance
|
||||
//
|
||||
// // Format the balance
|
||||
// try {
|
||||
// val balanceValue = balance.toDouble()
|
||||
// binding.tvBalance.text = String.format("Rp%,.0f", balanceValue)
|
||||
// } catch (e: Exception) {
|
||||
// binding.tvBalance.text = "Rp$balance"
|
||||
// }
|
||||
// } else {
|
||||
// Toast.makeText(
|
||||
// this@BalanceActivity,
|
||||
// "Gagal memuat data saldo: ${response.message()}",
|
||||
// Toast.LENGTH_SHORT
|
||||
// ).show()
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Error fetching balance", e)
|
||||
// Toast.makeText(
|
||||
// this@BalanceActivity,
|
||||
// "Error: ${e.message}",
|
||||
// Toast.LENGTH_SHORT
|
||||
// ).show()
|
||||
// } finally {
|
||||
// showLoading(false)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@ -20,12 +20,16 @@ import com.alya.ecommerce_serang.data.api.response.customer.order.ListCityRespon
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.ListProvinceResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.OrderDetailResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.OrderListResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.SubdistrictResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.VillagesResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.DetailPaymentItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.ProductResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.StoreItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.product.StoreResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.AddressDetailResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.AddressResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.CreateAddressResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.UpdateAddressResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.order.AddEvidenceResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.order.ComplaintResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.order.CompletedOrderResponse
|
||||
@ -294,6 +298,43 @@ class OrderRepository(private val apiService: ApiService) {
|
||||
return if (response.isSuccessful) response.body() else null
|
||||
}
|
||||
|
||||
suspend fun getListSubdistrict(cityId : String): SubdistrictResponse? {
|
||||
val response = apiService.getSubdistrict(cityId)
|
||||
return if (response.isSuccessful) response.body() else null
|
||||
}
|
||||
|
||||
suspend fun getListVillages(subId: String): VillagesResponse? {
|
||||
val response = apiService.getVillages(subId)
|
||||
return if (response.isSuccessful) response.body() else null
|
||||
}
|
||||
|
||||
suspend fun getAddressDetail(addressId: Int): AddressDetailResponse? {
|
||||
return try {
|
||||
Log.d("Order Repository", "Fetching address detail for ID: $addressId")
|
||||
|
||||
val response = apiService.getDetailAddress(addressId)
|
||||
|
||||
Log.d("Order Repository", "Response code: ${response.code()}")
|
||||
Log.d("Order Repository", "Response message: ${response.message()}")
|
||||
|
||||
if (response.isSuccessful) {
|
||||
val body = response.body()
|
||||
Log.d("Order Repository", "Address detail response body: $body")
|
||||
body
|
||||
} else {
|
||||
Log.w("Order Repository", "Failed to get address detail. Error body: ${response.errorBody()?.string()}")
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("Order Repository", "Error getting address detail", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateAddress(addressId: Int, params: Map<String, Any>): Response<UpdateAddressResponse> {
|
||||
return apiService.updateAddress(addressId, params)
|
||||
}
|
||||
|
||||
suspend fun fetchUserProfile(): Result<UserProfile?> {
|
||||
return try {
|
||||
val response = apiService.getUserProfile()
|
||||
@ -319,6 +360,7 @@ class OrderRepository(private val apiService: ApiService) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
suspend fun uploadPaymentProof(request: AddEvidenceMultipartRequest): Result<AddEvidenceResponse> {
|
||||
return try {
|
||||
Log.d("OrderRepository", "Uploading payment proof...")
|
||||
|
||||
@ -7,6 +7,7 @@ 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.OtpRequest
|
||||
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.UserProfile
|
||||
import com.alya.ecommerce_serang.data.api.dto.VerifRegisReq
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.FcmTokenResponse
|
||||
@ -18,9 +19,12 @@ import com.alya.ecommerce_serang.data.api.response.auth.NotifstoreItem
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.OtpResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.RegisterResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.RegisterStoreResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.ResetPassResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.VerifRegisterResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.ListCityResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.ListProvinceResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.SubdistrictResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.VillagesResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.EditProfileResponse
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiService
|
||||
import com.alya.ecommerce_serang.utils.FileUtils
|
||||
@ -68,6 +72,16 @@ class UserRepository(private val apiService: ApiService) {
|
||||
return if (response.isSuccessful) response.body() else null
|
||||
}
|
||||
|
||||
suspend fun getListSubdistrict(cityId : String): SubdistrictResponse? {
|
||||
val response = apiService.getSubdistrict(cityId)
|
||||
return if (response.isSuccessful) response.body() else null
|
||||
}
|
||||
|
||||
suspend fun getListVillages(subId: String): VillagesResponse? {
|
||||
val response = apiService.getVillages(subId)
|
||||
return if (response.isSuccessful) response.body() else null
|
||||
}
|
||||
|
||||
suspend fun registerUser(request: RegisterRequest): RegisterResponse {
|
||||
val response = apiService.register(request) // API call
|
||||
|
||||
@ -87,7 +101,7 @@ class UserRepository(private val apiService: ApiService) {
|
||||
longitude: String,
|
||||
street: String,
|
||||
subdistrict: String,
|
||||
cityId: Int,
|
||||
cityId: String,
|
||||
provinceId: Int,
|
||||
postalCode: Int,
|
||||
detail: String,
|
||||
@ -266,6 +280,11 @@ class UserRepository(private val apiService: ApiService) {
|
||||
val requestFile = compressedFile.asRequestBody(mimeType.toMediaTypeOrNull())
|
||||
Log.d(TAG, "$formName compressed size: ${compressedFile.length() / 1024} KB")
|
||||
|
||||
val compressedSizeMB = compressedFile.length().toDouble() / (1024 * 1024)
|
||||
if (compressedSizeMB > 1) {
|
||||
throw IllegalArgumentException("$formName lebih dari 1 MB setelah kompresi")
|
||||
}
|
||||
|
||||
MultipartBody.Part.createFormData(formName, compressedFile.name, requestFile)
|
||||
} else {
|
||||
throw IllegalArgumentException("$formName harus berupa file gambar (JPEG, JPG, atau PNG)")
|
||||
@ -473,6 +492,30 @@ class UserRepository(private val apiService: ApiService) {
|
||||
Result.Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun resetPassword(request: ResetPassReq): Result<ResetPassResponse>{
|
||||
return try {
|
||||
val response = apiService.postResetPass(request)
|
||||
|
||||
if (response.isSuccessful){
|
||||
val resetPassResponse = response.body()
|
||||
if (resetPassResponse != null) {
|
||||
Result.Success(resetPassResponse)
|
||||
}
|
||||
else {
|
||||
Result.Error(Exception("Empty response from server"))
|
||||
}
|
||||
}
|
||||
else {
|
||||
val errorBody = response.errorBody()?.string() ?: "Unknown error"
|
||||
Log.e(TAG, "Error RESET PASS address: $errorBody")
|
||||
Result.Error(Exception(errorBody))
|
||||
}
|
||||
}
|
||||
catch (e: Exception){
|
||||
Result.Error(e)
|
||||
}
|
||||
}
|
||||
companion object{
|
||||
private const val TAG = "UserRepository"
|
||||
}
|
||||
|
||||
@ -8,9 +8,7 @@ import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import com.alya.ecommerce_serang.data.api.dto.FcmReq
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
@ -43,20 +41,18 @@ class LoginActivity : AppCompatActivity() {
|
||||
setContentView(binding.root)
|
||||
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
|
||||
enableEdgeToEdge()
|
||||
|
||||
// Apply insets to your root layout
|
||||
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
|
||||
val systemBars = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
view.setPadding(
|
||||
systemBars.left,
|
||||
systemBars.top,
|
||||
systemBars.right,
|
||||
systemBars.bottom
|
||||
)
|
||||
windowInsets
|
||||
}
|
||||
// ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
|
||||
// val systemBars = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
// view.setPadding(
|
||||
// systemBars.left,
|
||||
// systemBars.top,
|
||||
// systemBars.right,
|
||||
// systemBars.bottom
|
||||
// )
|
||||
// windowInsets
|
||||
// }
|
||||
|
||||
// onBackPressedDispatcher.addCallback(this) {
|
||||
// // Handle the back button event
|
||||
@ -86,6 +82,11 @@ class LoginActivity : AppCompatActivity() {
|
||||
startActivity(Intent(this, RegisterActivity::class.java))
|
||||
finish()
|
||||
}
|
||||
|
||||
binding.tvForgetPassword.setOnClickListener {
|
||||
startActivity(Intent(this, ResetPassActivity::class.java))
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
private fun observeLoginState() {
|
||||
@ -105,6 +106,7 @@ class LoginActivity : AppCompatActivity() {
|
||||
finish()
|
||||
}
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Error -> {
|
||||
Log.e("LoginActivity", "Login Failed: ${result.exception.message}")
|
||||
Toast.makeText(this, "Login Failed: ${result.exception.message}", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
is Result.Loading -> {
|
||||
|
||||
@ -43,24 +43,6 @@ class RegisterActivity : AppCompatActivity() {
|
||||
setContentView(binding.root)
|
||||
sessionManager = SessionManager(this)
|
||||
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
|
||||
enableEdgeToEdge()
|
||||
|
||||
// Apply insets to your root layout
|
||||
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
|
||||
val systemBars = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
view.setPadding(
|
||||
systemBars.left,
|
||||
systemBars.top,
|
||||
systemBars.right,
|
||||
systemBars.bottom
|
||||
)
|
||||
windowInsets
|
||||
}
|
||||
|
||||
|
||||
|
||||
Log.d("RegisterActivity", "Token in storage: '${sessionManager.getToken()}'")
|
||||
Log.d("RegisterActivity", "User ID in storage: '${sessionManager.getUserId()}'")
|
||||
|
||||
@ -104,7 +86,7 @@ class RegisterActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
// Function to navigate to the next fragment
|
||||
// navigate step register in fragment
|
||||
fun navigateToStep(step: Int, userData: RegisterRequest?) {
|
||||
val fragment = when (step) {
|
||||
1 -> RegisterStep1Fragment.newInstance()
|
||||
|
||||
@ -0,0 +1,123 @@
|
||||
package com.alya.ecommerce_serang.ui.auth
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.data.repository.UserRepository
|
||||
import com.alya.ecommerce_serang.databinding.ActivityResetPassBinding
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.LoginViewModel
|
||||
|
||||
class ResetPassActivity : AppCompatActivity() {
|
||||
|
||||
private val TAG = "ResetPassActivity"
|
||||
private lateinit var binding: ActivityResetPassBinding
|
||||
|
||||
private val loginViewModel: LoginViewModel by viewModels{
|
||||
BaseViewModelFactory {
|
||||
val apiService = ApiConfig.getUnauthenticatedApiService()
|
||||
val userRepository = UserRepository(apiService)
|
||||
LoginViewModel(userRepository, this)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
binding = ActivityResetPassBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
// enableEdgeToEdge()
|
||||
|
||||
setupToolbar()
|
||||
setupUI()
|
||||
|
||||
}
|
||||
|
||||
private fun setupToolbar(){
|
||||
binding.headerResetPass.headerLeftIcon.setOnClickListener{
|
||||
finish()
|
||||
}
|
||||
binding.headerResetPass.headerTitle.text = "Lupa Password"
|
||||
}
|
||||
|
||||
private fun setupUI(){
|
||||
binding.btnReset.setOnClickListener {
|
||||
val email = binding.etEmail.text.toString().trim()
|
||||
if (email.isNotEmpty()) {
|
||||
loginViewModel.resetPassword(email)
|
||||
} else {
|
||||
binding.etEmail.error = "Masukkan Email Anda"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun observeResetPassword() {
|
||||
loginViewModel.resetPasswordState.observe(this) { result ->
|
||||
when (result) {
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Loading -> {
|
||||
showLoading(true)
|
||||
}
|
||||
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Success -> {
|
||||
showLoading(false)
|
||||
handleSuccess("Silahkan cek email anda untuk melihat password anda.")
|
||||
Log.d(TAG, "Success rest password: ${result.data.message}")
|
||||
}
|
||||
|
||||
is Result.Error -> {
|
||||
showLoading(false)
|
||||
handleError("Email anda salah atau tidak ditemukan.")
|
||||
Log.e(TAG, "Error reset password ${result.exception.message}")
|
||||
}
|
||||
|
||||
null -> {
|
||||
// Initial state
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showLoading(isLoading: Boolean) {
|
||||
if (isLoading) {
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
binding.btnReset.isEnabled = false
|
||||
binding.etEmail.isEnabled = false
|
||||
} else {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
binding.btnReset.isEnabled = true
|
||||
binding.etEmail.isEnabled = true
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleSuccess(message: String) {
|
||||
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
|
||||
|
||||
// Show success dialog and navigate back to login
|
||||
AlertDialog.Builder(this)
|
||||
.setTitle("Berhasil Ubah Password")
|
||||
.setMessage(message)
|
||||
.setPositiveButton("OK") { _, _ ->
|
||||
// Navigate back to login activity
|
||||
finish()
|
||||
}
|
||||
.setCancelable(false)
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun handleError(errorMessage: String) {
|
||||
Toast.makeText(this, "Error: $errorMessage", Toast.LENGTH_LONG).show()
|
||||
|
||||
// Optionally show error dialog
|
||||
AlertDialog.Builder(this)
|
||||
.setTitle("Gagal Ubah Password")
|
||||
.setMessage(errorMessage)
|
||||
.setPositiveButton("OK", null)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
@ -204,6 +204,13 @@ class RegisterStep1Fragment : Fragment() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
registerViewModel.toastMessage.observe(viewLifecycleOwner){ event ->
|
||||
//memanggil toast check value email dan phone
|
||||
event.getContentIfNotHandled()?.let { msg ->
|
||||
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateAndProceed() {
|
||||
|
||||
@ -88,7 +88,7 @@ class RegisterStep2Fragment : Fragment() {
|
||||
|
||||
// Update the email sent message
|
||||
userData?.let {
|
||||
binding.tvEmailSent.text = "We've sent a verification code to ${it.email}"
|
||||
binding.tvEmailSent.text = "Kami telah mengirimkan kode OTP ke alamat email ${it.email}. Silahkan periksa email anda."
|
||||
}
|
||||
|
||||
// Start the resend cooldown timer
|
||||
@ -119,7 +119,7 @@ class RegisterStep2Fragment : Fragment() {
|
||||
Log.d(TAG, "verifyOtp called with OTP: $otp")
|
||||
|
||||
if (otp.isEmpty()) {
|
||||
Toast.makeText(requireContext(), "Please enter the verification code", Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(requireContext(), "Masukkan kode OTP anda", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
@ -153,13 +153,13 @@ class RegisterStep2Fragment : Fragment() {
|
||||
}
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Success -> {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
Toast.makeText(requireContext(), "Verification code resent", Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(requireContext(), "Kode OTP sudah dikirim", Toast.LENGTH_SHORT).show()
|
||||
startResendCooldown()
|
||||
}
|
||||
is Result.Error -> {
|
||||
Log.e(TAG, "OTP request: Error - ${result.exception.message}")
|
||||
binding.progressBar.visibility = View.GONE
|
||||
Toast.makeText(requireContext(), "Failed to resend code: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(requireContext(), "Gagal mengirim kode OTP", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
else -> {
|
||||
Log.d(TAG, "OTP request: Unknown state")
|
||||
@ -180,7 +180,7 @@ class RegisterStep2Fragment : Fragment() {
|
||||
countDownTimer = object : CountDownTimer(30000, 1000) {
|
||||
override fun onTick(millisUntilFinished: Long) {
|
||||
timeRemaining = (millisUntilFinished / 1000).toInt()
|
||||
binding.tvTimer.text = "Resend available in 00:${String.format("%02d", timeRemaining)}"
|
||||
binding.tvTimer.text = "Kirim ulang OTP dalam waktu 00:${String.format("%02d", timeRemaining)}"
|
||||
if (timeRemaining % 5 == 0) {
|
||||
Log.d(TAG, "Cooldown remaining: $timeRemaining seconds")
|
||||
}
|
||||
@ -188,7 +188,7 @@ class RegisterStep2Fragment : Fragment() {
|
||||
|
||||
override fun onFinish() {
|
||||
Log.d(TAG, "Cooldown finished, enabling resend button")
|
||||
binding.tvTimer.text = "You can now resend the code"
|
||||
binding.tvTimer.text = "Dapat mengirim ulang kode OTP"
|
||||
binding.tvResendOtp.isEnabled = true
|
||||
binding.tvResendOtp.setTextColor(ContextCompat.getColor(requireContext(), R.color.blue1))
|
||||
timeRemaining = 0
|
||||
@ -222,7 +222,8 @@ class RegisterStep2Fragment : Fragment() {
|
||||
binding.btnVerify.isEnabled = true
|
||||
|
||||
// Show error message
|
||||
Toast.makeText(requireContext(), "Registration Failed: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
Log.e(TAG, "Registration Failed: ${result.exception.message}")
|
||||
Toast.makeText(requireContext(), "Gagal melakukan regsitrasi", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
else -> {
|
||||
Log.d(TAG, "Registration: Unknown state")
|
||||
@ -251,15 +252,10 @@ class RegisterStep2Fragment : Fragment() {
|
||||
sessionManager.saveToken(accessToken)
|
||||
Log.d(TAG, "Token saved to SessionManager: $accessToken")
|
||||
|
||||
// Also save user ID if available in the login response
|
||||
// result.data.?.let { userId ->
|
||||
// sessionManager.saveUserId(userId)
|
||||
// }
|
||||
|
||||
Log.d(TAG, "Login successful, token saved: $accessToken")
|
||||
|
||||
// Proceed to Step 3
|
||||
Log.d(TAG, "Proceeding to Step 3 after successful login")
|
||||
|
||||
// call navigate register step from activity
|
||||
(activity as? RegisterActivity)?.navigateToStep(3, null )
|
||||
}
|
||||
is Result.Error -> {
|
||||
@ -269,7 +265,7 @@ class RegisterStep2Fragment : Fragment() {
|
||||
|
||||
// Show error message but continue to Step 3 anyway
|
||||
Log.e(TAG, "Login failed but proceeding to Step 3", result.exception)
|
||||
Toast.makeText(requireContext(), "Note: Auto-login failed, but registration was successful", Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(requireContext(), "Berhasil membuat akun, namun belum login", Toast.LENGTH_SHORT).show()
|
||||
|
||||
// Proceed to Step 3
|
||||
(activity as? RegisterActivity)?.navigateToStep(3, null)
|
||||
|
||||
@ -23,11 +23,14 @@ import com.alya.ecommerce_serang.ui.auth.LoginActivity
|
||||
import com.alya.ecommerce_serang.ui.auth.RegisterActivity
|
||||
import com.alya.ecommerce_serang.ui.order.address.CityAdapter
|
||||
import com.alya.ecommerce_serang.ui.order.address.ProvinceAdapter
|
||||
import com.alya.ecommerce_serang.ui.order.address.SubdsitrictAdapter
|
||||
import com.alya.ecommerce_serang.ui.order.address.ViewState
|
||||
import com.alya.ecommerce_serang.ui.order.address.VillagesAdapter
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.RegisterViewModel
|
||||
import com.google.android.material.progressindicator.LinearProgressIndicator
|
||||
import com.google.gson.Gson
|
||||
|
||||
class RegisterStep3Fragment : Fragment() {
|
||||
private var _binding: FragmentRegisterStep3Binding? = null
|
||||
@ -49,6 +52,8 @@ class RegisterStep3Fragment : Fragment() {
|
||||
// For province and city selection
|
||||
private val provinceAdapter by lazy { ProvinceAdapter(requireContext()) }
|
||||
private val cityAdapter by lazy { CityAdapter(requireContext()) }
|
||||
private val subdistrictAdapter by lazy { SubdsitrictAdapter(requireContext()) }
|
||||
private val villagesAdapter by lazy { VillagesAdapter(requireContext()) }
|
||||
|
||||
companion object {
|
||||
private const val TAG = "RegisterStep3Fragment"
|
||||
@ -114,7 +119,7 @@ class RegisterStep3Fragment : Fragment() {
|
||||
// Observe address submission state
|
||||
observeAddressSubmissionState()
|
||||
|
||||
// Load provinces
|
||||
// Load provinces from raja ongkir
|
||||
Log.d(TAG, "Requesting provinces data")
|
||||
registerViewModel.getProvinces()
|
||||
setupProvinceObserver()
|
||||
@ -171,9 +176,10 @@ class RegisterStep3Fragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun setupAutoComplete() {
|
||||
// Same implementation as before
|
||||
binding.autoCompleteProvinsi.setAdapter(provinceAdapter)
|
||||
binding.autoCompleteKabupaten.setAdapter(cityAdapter)
|
||||
binding.autoCompleteKecamatan.setAdapter(subdistrictAdapter)
|
||||
binding.autoCompleteDesa.setAdapter(villagesAdapter)
|
||||
|
||||
binding.autoCompleteProvinsi.setOnClickListener {
|
||||
binding.autoCompleteProvinsi.showDropDown()
|
||||
@ -188,6 +194,24 @@ class RegisterStep3Fragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
binding.autoCompleteKecamatan.setOnClickListener {
|
||||
if (subdistrictAdapter.count > 0) {
|
||||
Log.d(TAG, "Subdistrict dropdown clicked, showing ${subdistrictAdapter.count} cities")
|
||||
binding.autoCompleteKecamatan.showDropDown()
|
||||
} else {
|
||||
Toast.makeText(requireContext(), "Pilih kabupaten / kota terlebih dahulu", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
binding.autoCompleteDesa.setOnClickListener {
|
||||
if (villagesAdapter.count > 0) {
|
||||
Log.d(TAG, "Village dropdown clicked, showing ${villagesAdapter.count} cities")
|
||||
binding.autoCompleteDesa.showDropDown()
|
||||
} else {
|
||||
Toast.makeText(requireContext(), "Pilih kecamatan terlebih dahulu", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
binding.autoCompleteProvinsi.setOnItemClickListener { _, _, position, _ ->
|
||||
val provinceId = provinceAdapter.getProvinceId(position)
|
||||
Log.d(TAG, "Province selected at position $position, ID: $provinceId")
|
||||
@ -206,13 +230,50 @@ class RegisterStep3Fragment : Fragment() {
|
||||
|
||||
cityId?.let { id ->
|
||||
Log.d(TAG, "Selected city ID set to: $id")
|
||||
registerViewModel.selectedCityId = id
|
||||
registerViewModel.updateSelectedCityId(id)
|
||||
registerViewModel.getSubdistrict(id)
|
||||
binding.autoCompleteKecamatan.text.clear()
|
||||
}
|
||||
}
|
||||
|
||||
binding.autoCompleteKecamatan.setOnItemClickListener{ _, _, position, _ ->
|
||||
val subdistrictId = subdistrictAdapter.getSubdistrictId(position)
|
||||
val subdistictName = subdistrictAdapter.getSubdistrictName(position)
|
||||
Log.d(TAG, "Subdistrict selected at position $position, ID: $subdistrictId, name: $subdistictName")
|
||||
|
||||
subdistrictId?.let { id ->
|
||||
Log.d(TAG, "Selected subdistrict ID set to: $id")
|
||||
registerViewModel.selectedSubdistrict = id
|
||||
registerViewModel.getVillages(id)
|
||||
binding.autoCompleteDesa.text.clear()
|
||||
}
|
||||
|
||||
subdistictName?.let { name ->
|
||||
Log.d(TAG, "Selected name subdistrict set to: $name")
|
||||
registerViewModel.subdistrictName = name
|
||||
}
|
||||
}
|
||||
|
||||
binding.autoCompleteDesa.setOnItemClickListener{ _, _, position, _ ->
|
||||
val villageId = villagesAdapter.getVillageId(position)
|
||||
// val postalCode = villagesAdapter.getPostalCode(position)
|
||||
Log.d(TAG, "Village selected at position $position, ID: $villageId")
|
||||
|
||||
villageId?.let { id ->
|
||||
Log.d(TAG, "Selected village ID set to: $id")
|
||||
registerViewModel.selectedVillages = id
|
||||
}
|
||||
|
||||
// postalCode?.let { postCode ->
|
||||
// registerViewModel.selectedPostalCode = postCode
|
||||
// }
|
||||
|
||||
// binding.etKodePos.setText(registerViewModel.selectedPostalCode ?: "")
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupProvinceObserver() {
|
||||
// Same implementation as before
|
||||
// pake raja ongkir
|
||||
registerViewModel.provincesState.observe(viewLifecycleOwner) { state ->
|
||||
when (state) {
|
||||
is ViewState.Loading -> {
|
||||
@ -256,8 +317,46 @@ class RegisterStep3Fragment : Fragment() {
|
||||
}
|
||||
}
|
||||
}
|
||||
registerViewModel.subdistrictState.observe(viewLifecycleOwner) { state ->
|
||||
when (state) {
|
||||
is ViewState.Loading -> {
|
||||
binding.progressBarKecamatan.visibility = View.VISIBLE
|
||||
}
|
||||
is ViewState.Success -> {
|
||||
Log.d(TAG, "Subdistrict: Success - received ${state.data.size} kecamatan")
|
||||
binding.progressBarKecamatan.visibility = View.GONE
|
||||
subdistrictAdapter.updateData(state.data)
|
||||
Log.d(TAG, "Updated subdistrict adapter with ${state.data.size} items")
|
||||
}
|
||||
is ViewState.Error -> {
|
||||
Log.e(TAG, "Subdistrict: Error - ${state.message}")
|
||||
binding.progressBarKecamatan.visibility = View.GONE
|
||||
showError("Failed to load kecamatan: ${state.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
registerViewModel.villagesState.observe(viewLifecycleOwner) { state ->
|
||||
when (state) {
|
||||
is ViewState.Loading -> {
|
||||
binding.progressBarDesa.visibility = View.VISIBLE
|
||||
}
|
||||
is ViewState.Success -> {
|
||||
Log.d(TAG, "Village: Success - received ${state.data.size} desa")
|
||||
binding.progressBarDesa.visibility = View.GONE
|
||||
villagesAdapter.updateData(state.data)
|
||||
Log.d(TAG, "Updated village adapter with ${state.data.size} items")
|
||||
}
|
||||
is ViewState.Error -> {
|
||||
Log.e(TAG, "Village: Error - ${state.message}")
|
||||
binding.progressBarDesa.visibility = View.GONE
|
||||
showError("Failed to load desa: ${state.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun submitAddress() {
|
||||
Log.d(TAG, "submitAddress called")
|
||||
if (!validateAddressForm()) {
|
||||
@ -276,13 +375,16 @@ class RegisterStep3Fragment : Fragment() {
|
||||
Log.d(TAG, "Using user ID: $userId")
|
||||
|
||||
val street = binding.etDetailAlamat.text.toString().trim()
|
||||
val subDistrict = binding.etKecamatan.text.toString().trim()
|
||||
val postalCode = binding.etKodePos.text.toString().trim()
|
||||
val recipient = binding.etNamaPenerima.text.toString().trim()
|
||||
val phone = binding.etNomorHp.text.toString().trim()
|
||||
val postalCode = binding.etKodePos.text.toString().trim()
|
||||
|
||||
val provinceId = registerViewModel.selectedProvinceId?.toInt() ?: 0
|
||||
val cityId = registerViewModel.selectedCityId?.toInt() ?: 0
|
||||
val cityId = registerViewModel.selectedCityId.toString()
|
||||
val subDistrict = registerViewModel.subdistrictName.toString()
|
||||
// val postalCode = registerViewModel.selectedPostalCode.toString()
|
||||
|
||||
val villageId = registerViewModel.selectedVillages ?: ""
|
||||
|
||||
Log.d(TAG, "Address data - Street: $street, SubDistrict: $subDistrict, PostalCode: $postalCode")
|
||||
Log.d(TAG, "Address data - Recipient: $recipient, Phone: $phone")
|
||||
@ -291,21 +393,25 @@ class RegisterStep3Fragment : Fragment() {
|
||||
|
||||
// Create address request
|
||||
val addressRequest = CreateAddressRequest(
|
||||
userId = user.id, // must match the type expected in the DB
|
||||
lat = defaultLatitude,
|
||||
long = defaultLongitude,
|
||||
street = street,
|
||||
subDistrict = subDistrict,
|
||||
cityId = cityId,
|
||||
cityId = cityId, // ⚠️ Make sure this is Int
|
||||
provId = provinceId,
|
||||
postCode = postalCode,
|
||||
idVillage = villageId, // Or provide a real ID if needed
|
||||
detailAddress = street,
|
||||
userId = userId,
|
||||
isStoreLocation = false,
|
||||
recipient = recipient,
|
||||
phone = phone,
|
||||
isStoreLocation = false
|
||||
phone = phone
|
||||
)
|
||||
|
||||
Log.d(TAG, "Address request created: $addressRequest")
|
||||
val gson = Gson()
|
||||
val jsonString = gson.toJson(addressRequest)
|
||||
Log.d(TAG, "Request JSON: $jsonString")
|
||||
|
||||
// Show loading
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
@ -318,13 +424,13 @@ class RegisterStep3Fragment : Fragment() {
|
||||
|
||||
private fun validateAddressForm(): Boolean {
|
||||
val street = binding.etDetailAlamat.text.toString().trim()
|
||||
val subDistrict = binding.etKecamatan.text.toString().trim()
|
||||
val postalCode = binding.etKodePos.text.toString().trim()
|
||||
val recipient = binding.etNamaPenerima.text.toString().trim()
|
||||
val phone = binding.etNomorHp.text.toString().trim()
|
||||
|
||||
val provinceId = registerViewModel.selectedProvinceId
|
||||
val cityId = registerViewModel.selectedCityId
|
||||
val subDistrict = registerViewModel.selectedSubdistrict
|
||||
val postalCode = registerViewModel.selectedPostalCode
|
||||
|
||||
Log.d(TAG, "Validating - Street: $street, SubDistrict: $subDistrict, PostalCode: $postalCode")
|
||||
Log.d(TAG, "Validating - Recipient: $recipient, Phone: $phone")
|
||||
@ -362,6 +468,12 @@ class RegisterStep3Fragment : Fragment() {
|
||||
return false
|
||||
}
|
||||
|
||||
if (subDistrict == null) {
|
||||
showError("Pilih kota/kabupaten terlebih dahulu")
|
||||
binding.autoCompleteKecamatan.requestFocus()
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@ -409,8 +521,4 @@ class RegisterStep3Fragment : Fragment() {
|
||||
ViewCompat.setWindowInsetsAnimationCallback(binding.root, null)
|
||||
_binding = null
|
||||
}
|
||||
//
|
||||
// // Data classes for province and city
|
||||
// data class Province(val id: String, val name: String)
|
||||
// data class City(val id: String, val name: String)
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package com.alya.ecommerce_serang.ui.cart
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
@ -29,6 +30,8 @@ class CartActivity : AppCompatActivity() {
|
||||
private lateinit var sessionManager: SessionManager
|
||||
private lateinit var storeAdapter: StoreAdapter
|
||||
|
||||
private var TAG = "Cart Activity"
|
||||
|
||||
private val viewModel: CartViewModel by viewModels {
|
||||
BaseViewModelFactory {
|
||||
val apiService = ApiConfig.getApiService(sessionManager)
|
||||
@ -134,18 +137,52 @@ class CartActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun startCheckoutWithWholesaleInfo(checkoutItems: List<CartItemCheckoutInfo>) {
|
||||
// Extract cart item IDs and wholesale status
|
||||
val cartItemIds = checkoutItems.map { it.cartItem.cartItemId }
|
||||
val wholesaleArray = checkoutItems.map { it.isWholesale }.toBooleanArray()
|
||||
val wholesalePriceMap = viewModel.cartItemWholesalePrice.value ?: emptyMap()
|
||||
|
||||
// Start checkout activity with the cart items and wholesale info
|
||||
CheckoutActivity.startForCart(this, cartItemIds, wholesaleArray)
|
||||
val updatedItems = checkoutItems.map { info ->
|
||||
val wholesalePrice = wholesalePriceMap[info.cartItem.cartItemId]
|
||||
|
||||
val updatedCartItem = if (info.isWholesale && wholesalePrice != null) {
|
||||
// Replace the price with wholesale price
|
||||
info.cartItem.copy(price = wholesalePrice.toInt())
|
||||
} else {
|
||||
info.cartItem
|
||||
}
|
||||
|
||||
// Debug log
|
||||
Log.d(
|
||||
TAG,
|
||||
"cartItemId: ${updatedCartItem.cartItemId}, " +
|
||||
"isWholesale: ${info.isWholesale}, " +
|
||||
"wholesalePrice: $wholesalePrice, " +
|
||||
"finalPrice: ${updatedCartItem.price}"
|
||||
)
|
||||
|
||||
info.copy(cartItem = updatedCartItem)
|
||||
}
|
||||
|
||||
val cartItemIds = updatedItems.map { it.cartItem.cartItemId }
|
||||
val wholesaleArray = updatedItems.map { it.isWholesale }.toBooleanArray()
|
||||
|
||||
// FIX: Pass wholesale prices as IntArray
|
||||
val wholesalePricesArray = updatedItems.map { info ->
|
||||
if (info.isWholesale) {
|
||||
val wholesalePrice = wholesalePriceMap[info.cartItem.cartItemId]
|
||||
wholesalePrice?.toInt() ?: info.cartItem.price
|
||||
} else {
|
||||
info.cartItem.price
|
||||
}
|
||||
}.toIntArray()
|
||||
|
||||
CheckoutActivity.startForCart(this, cartItemIds, wholesaleArray, wholesalePricesArray)
|
||||
}
|
||||
|
||||
private fun observeViewModel() {
|
||||
viewModel.cartItems.observe(this) { cartItems ->
|
||||
if (cartItems.isNullOrEmpty()) {
|
||||
binding.emptyCart.visibility = View.VISIBLE
|
||||
showEmptyState(true)
|
||||
|
||||
} else {
|
||||
showEmptyState(false)
|
||||
storeAdapter.submitList(cartItems)
|
||||
@ -153,7 +190,8 @@ class CartActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
viewModel.isLoading.observe(this) { isLoading ->
|
||||
// Show/hide loading indicator if needed
|
||||
binding.progressBarCart?.visibility = if (isLoading) View.VISIBLE else View.GONE
|
||||
Log.d("CartActivity", "Loading state: $isLoading")
|
||||
}
|
||||
|
||||
viewModel.errorMessage.observe(this) { errorMessage ->
|
||||
@ -229,7 +267,5 @@ class CartActivity : AppCompatActivity() {
|
||||
val format = NumberFormat.getCurrencyInstance(Locale("id", "ID"))
|
||||
return format.format(amount).replace("Rp", "Rp ")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -27,6 +27,7 @@ import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsAnimationCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.alya.ecommerce_serang.BuildConfig.BASE_URL
|
||||
import com.alya.ecommerce_serang.R
|
||||
@ -63,6 +64,8 @@ class ChatActivity : AppCompatActivity() {
|
||||
// For image attachment
|
||||
private var tempImageUri: Uri? = null
|
||||
|
||||
private var imageAttach = false
|
||||
|
||||
// Typing indicator handler
|
||||
private val typingHandler = android.os.Handler(android.os.Looper.getMainLooper())
|
||||
private val stopTypingRunnable = Runnable {
|
||||
@ -127,6 +130,7 @@ class ChatActivity : AppCompatActivity() {
|
||||
return
|
||||
}
|
||||
|
||||
// set up data toko
|
||||
binding.tvStoreName.text = storeName
|
||||
val fullImageUrl = when (val img = storeImg) {
|
||||
is String -> {
|
||||
@ -140,7 +144,7 @@ class ChatActivity : AppCompatActivity() {
|
||||
.placeholder(R.drawable.placeholder_image)
|
||||
.into(binding.imgProfile)
|
||||
|
||||
// Set chat parameters to ViewModel
|
||||
// Set chat parameter to send to ViewModel with product
|
||||
viewModel.setChatParameters(
|
||||
storeId = storeId,
|
||||
productId = productId,
|
||||
@ -157,16 +161,17 @@ class ChatActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
// Setup UI components
|
||||
// rv isi chat
|
||||
setupRecyclerView()
|
||||
setupWindowInsets()
|
||||
setupListeners()
|
||||
setupTypingIndicator()
|
||||
// observe listener from viewmodel
|
||||
observeViewModel()
|
||||
|
||||
// If opened from ChatListFragment with a valid chatRoomId
|
||||
if (chatRoomId > 0) {
|
||||
// Directly set the chatRoomId and load chat history
|
||||
viewModel._chatRoomId.value = chatRoomId
|
||||
viewModel.setChatRoomId(chatRoomId)
|
||||
}
|
||||
}
|
||||
|
||||
@ -269,6 +274,7 @@ class ChatActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
// Options button
|
||||
binding.btnOptions.visibility = View.GONE
|
||||
binding.btnOptions.setOnClickListener {
|
||||
showOptionsMenu()
|
||||
}
|
||||
@ -281,6 +287,7 @@ class ChatActivity : AppCompatActivity() {
|
||||
// This will automatically handle product attachment if enabled
|
||||
viewModel.sendMessage(message)
|
||||
binding.editTextMessage.text.clear()
|
||||
binding.layoutAttachImage.visibility = View.GONE
|
||||
|
||||
// Instantly scroll to show new message
|
||||
binding.recyclerChat.postDelayed({
|
||||
@ -291,24 +298,33 @@ class ChatActivity : AppCompatActivity() {
|
||||
|
||||
// Attachment button
|
||||
binding.btnAttachment.setOnClickListener {
|
||||
this.currentFocus?.let { view ->
|
||||
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
|
||||
imm?.hideSoftInputFromWindow(view.windowToken, 0)
|
||||
}
|
||||
checkPermissionsAndShowImagePicker()
|
||||
}
|
||||
|
||||
binding.btnCloseChat.setOnClickListener{
|
||||
binding.layoutAttachImage.visibility = View.GONE
|
||||
imageAttach = false
|
||||
viewModel.clearSelectedImage()
|
||||
}
|
||||
|
||||
// Product card click to enable/disable product attachment
|
||||
binding.productContainer.setOnClickListener {
|
||||
toggleProductAttachment()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun toggleProductAttachment() {
|
||||
val currentState = viewModel.state.value
|
||||
if (currentState?.hasProductAttachment == true) {
|
||||
// Disable product attachment
|
||||
viewModel.disableProductAttachment()
|
||||
updateProductAttachmentUI(false)
|
||||
Toast.makeText(this, "Product attachment disabled", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
// Enable product attachment
|
||||
viewModel.enableProductAttachment()
|
||||
updateProductAttachmentUI(true)
|
||||
Toast.makeText(this, "Product will be attached to your next message", Toast.LENGTH_SHORT).show()
|
||||
@ -389,77 +405,76 @@ class ChatActivity : AppCompatActivity() {
|
||||
}
|
||||
})
|
||||
|
||||
viewModel.state.observe(this, Observer { state ->
|
||||
Log.d(TAG, "State updated - Messages: ${state.messages.size}")
|
||||
lifecycleScope.launchWhenStarted {
|
||||
viewModel.state.collect() { state ->
|
||||
Log.d(TAG, "State updated - Messages: ${state.messages.size}")
|
||||
|
||||
// Update messages
|
||||
val previousCount = chatAdapter.itemCount
|
||||
// Update messages
|
||||
val previousCount = chatAdapter.itemCount
|
||||
|
||||
val displayItems = viewModel.getDisplayItems()
|
||||
val displayItems = viewModel.getDisplayItems()
|
||||
|
||||
chatAdapter.submitList(displayItems) {
|
||||
Log.d(TAG, "Messages submitted to adapter")
|
||||
// Only auto-scroll for new messages or initial load
|
||||
if (previousCount == 0 || state.messages.size > previousCount) {
|
||||
scrollToBottomInstant()
|
||||
}
|
||||
}
|
||||
|
||||
// Update product info
|
||||
if (!state.productName.isNullOrEmpty()) {
|
||||
binding.tvProductName.text = state.productName
|
||||
binding.tvProductPrice.text = state.productPrice
|
||||
binding.ratingBar.rating = state.productRating
|
||||
binding.tvRating.text = state.productRating.toString()
|
||||
binding.tvSellerName.text = state.storeName
|
||||
binding.tvStoreName.text = state.storeName
|
||||
|
||||
val fullImageUrl = when (val img = state.productImageUrl) {
|
||||
is String -> {
|
||||
if (img.startsWith("/")) BASE_URL + img.substring(1) else img
|
||||
chatAdapter.submitList(displayItems) {
|
||||
Log.d(TAG, "Messages submitted to adapter")
|
||||
// Only auto-scroll for new messages or initial load
|
||||
if (previousCount == 0 || state.messages.size > previousCount) {
|
||||
scrollToBottomInstant()
|
||||
}
|
||||
else -> R.drawable.placeholder_image
|
||||
}
|
||||
|
||||
if (!state.productImageUrl.isNullOrEmpty()) {
|
||||
Glide.with(this@ChatActivity)
|
||||
.load(fullImageUrl)
|
||||
.centerCrop()
|
||||
.placeholder(R.drawable.placeholder_image)
|
||||
.error(R.drawable.placeholder_image)
|
||||
.into(binding.imgProduct)
|
||||
// layout attach product
|
||||
if (!state.productName.isNullOrEmpty()) {
|
||||
binding.tvProductName.text = state.productName
|
||||
binding.tvProductPrice.text = state.productPrice
|
||||
binding.ratingBar.rating = state.productRating
|
||||
binding.tvRating.text = state.productRating.toString()
|
||||
binding.tvSellerName.text = state.storeName
|
||||
binding.tvStoreName.text = state.storeName
|
||||
|
||||
val fullImageUrl = when (val img = state.productImageUrl) {
|
||||
is String -> {
|
||||
if (img.startsWith("/")) BASE_URL + img.substring(1) else img
|
||||
}
|
||||
|
||||
else -> R.drawable.placeholder_image
|
||||
}
|
||||
|
||||
if (!state.productImageUrl.isNullOrEmpty()) {
|
||||
Glide.with(this@ChatActivity)
|
||||
.load(fullImageUrl)
|
||||
.centerCrop()
|
||||
.placeholder(R.drawable.placeholder_image)
|
||||
.error(R.drawable.placeholder_image)
|
||||
.into(binding.imgProduct)
|
||||
}
|
||||
updateProductCardUI(state.hasProductAttachment)
|
||||
|
||||
binding.productContainer.visibility = View.GONE
|
||||
} else {
|
||||
binding.productContainer.visibility = View.GONE
|
||||
}
|
||||
updateProductCardUI(state.hasProductAttachment)
|
||||
|
||||
binding.productContainer.visibility = View.GONE
|
||||
} else {
|
||||
binding.productContainer.visibility = View.GONE
|
||||
updateInputHint(state)
|
||||
|
||||
// Update attachment hint
|
||||
if (state.hasAttachment) {
|
||||
binding.layoutAttachImage.visibility = View.VISIBLE
|
||||
} else {
|
||||
binding.editTextMessage.hint = getString(R.string.write_message)
|
||||
}
|
||||
|
||||
// Show error if any
|
||||
state.error?.let { error ->
|
||||
Toast.makeText(this@ChatActivity, error, Toast.LENGTH_SHORT).show()
|
||||
viewModel.clearError()
|
||||
}
|
||||
}
|
||||
|
||||
updateInputHint(state)
|
||||
|
||||
// Update attachment hint
|
||||
if (state.hasAttachment) {
|
||||
binding.editTextMessage.hint = getString(R.string.image_attached)
|
||||
} else {
|
||||
binding.editTextMessage.hint = getString(R.string.write_message)
|
||||
}
|
||||
|
||||
// Show typing indicator
|
||||
binding.tvTypingIndicator.visibility =
|
||||
if (state.isOtherUserTyping) View.VISIBLE else View.GONE
|
||||
|
||||
// Show error if any
|
||||
state.error?.let { error ->
|
||||
Toast.makeText(this@ChatActivity, error, Toast.LENGTH_SHORT).show()
|
||||
viewModel.clearError()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateInputHint(state: ChatUiState) {
|
||||
binding.editTextMessage.hint = when {
|
||||
state.hasAttachment -> getString(R.string.image_attached)
|
||||
state.hasAttachment -> getString(R.string.write_message)
|
||||
state.hasProductAttachment -> "Type your message (product will be attached)"
|
||||
else -> getString(R.string.write_message)
|
||||
}
|
||||
@ -480,7 +495,7 @@ class ChatActivity : AppCompatActivity() {
|
||||
Toast.makeText(this, "Opening: ${productInfo.productName}", Toast.LENGTH_SHORT).show()
|
||||
|
||||
// You can navigate to product detail here
|
||||
navigateToProductDetail(productInfo.productId)
|
||||
navigateToProductDetail(productInfo.productId)
|
||||
}
|
||||
|
||||
private fun navigateToProductDetail(productId: Int) {
|
||||
@ -504,6 +519,7 @@ class ChatActivity : AppCompatActivity() {
|
||||
getString(R.string.cancel)
|
||||
)
|
||||
|
||||
|
||||
AlertDialog.Builder(this)
|
||||
.setTitle(getString(R.string.options))
|
||||
.setItems(options) { dialog, which ->
|
||||
@ -578,7 +594,21 @@ class ChatActivity : AppCompatActivity() {
|
||||
|
||||
private fun handleSelectedImage(uri: Uri) {
|
||||
try {
|
||||
Log.d(TAG, "Processing selected image: $uri")
|
||||
Log.d(TAG, "Processing selected image: ${uri.toString()}")
|
||||
imageAttach = true
|
||||
binding.layoutAttachImage.visibility = View.VISIBLE
|
||||
val fullImageUrl = when (val img = uri.toString()) {
|
||||
is String -> {
|
||||
if (img.startsWith("/")) BASE_URL + img.substring(1) else img
|
||||
}
|
||||
else -> R.drawable.placeholder_image
|
||||
}
|
||||
|
||||
Glide.with(this)
|
||||
.load(fullImageUrl)
|
||||
.placeholder(R.drawable.placeholder_image)
|
||||
.into(binding.ivAttach)
|
||||
Log.d(TAG, "Display attach image: $uri")
|
||||
|
||||
// Always use the copy-to-cache approach for reliability
|
||||
contentResolver.openInputStream(uri)?.use { inputStream ->
|
||||
@ -598,6 +628,7 @@ class ChatActivity : AppCompatActivity() {
|
||||
|
||||
Log.d(TAG, "Image processed successfully: ${outputFile.absolutePath}, size: ${outputFile.length()}")
|
||||
viewModel.setSelectedImageFile(outputFile)
|
||||
|
||||
Toast.makeText(this, "Image selected", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Log.e(TAG, "Failed to create image file")
|
||||
@ -681,25 +712,4 @@ class ChatActivity : AppCompatActivity() {
|
||||
context.startActivity(intent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//if implement typing status
|
||||
// private fun handleConnectionState(state: ConnectionState) {
|
||||
// when (state) {
|
||||
// is ConnectionState.Connected -> {
|
||||
// binding.tvConnectionStatus.visibility = View.GONE
|
||||
// }
|
||||
// is ConnectionState.Connecting -> {
|
||||
// binding.tvConnectionStatus.visibility = View.VISIBLE
|
||||
// binding.tvConnectionStatus.text = getString(R.string.connecting)
|
||||
// }
|
||||
// is ConnectionState.Disconnected -> {
|
||||
// binding.tvConnectionStatus.visibility = View.VISIBLE
|
||||
// binding.tvConnectionStatus.text = getString(R.string.disconnected_reconnecting)
|
||||
// }
|
||||
// is ConnectionState.Error -> {
|
||||
// binding.tvConnectionStatus.visibility = View.VISIBLE
|
||||
// binding.tvConnectionStatus.text = getString(R.string.connection_error, state.message)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@ -209,7 +209,7 @@ class ChatAdapter(
|
||||
binding.tvProductPrice.text = product.productPrice
|
||||
|
||||
// Load product image
|
||||
val fullImageUrl = if (product.productImage.startsWith("/")) {
|
||||
val fullImageUrl = if (product.productImage!!.startsWith("/")) {
|
||||
BASE_URL + product.productImage.substring(1)
|
||||
} else {
|
||||
product.productImage
|
||||
@ -246,7 +246,7 @@ class ChatAdapter(
|
||||
binding.tvProductPrice.text = product.productPrice
|
||||
|
||||
// Load product image
|
||||
val fullImageUrl = if (product.productImage.startsWith("/")) {
|
||||
val fullImageUrl = if (product.productImage!!.startsWith("/")) {
|
||||
BASE_URL + product.productImage.substring(1)
|
||||
} else {
|
||||
product.productImage
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.alya.ecommerce_serang.ui.chat
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@ -55,31 +56,44 @@ class ChatListFragment : Fragment() {
|
||||
viewModel.chatList.observe(viewLifecycleOwner) { result ->
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
val adapter = ChatListAdapter(result.data) { chatItem ->
|
||||
// Use the ChatActivity.createIntent factory method for proper navigation
|
||||
ChatActivity.createIntent(
|
||||
context = requireActivity(),
|
||||
storeId = chatItem.storeId,
|
||||
productId = 0, // Default value since we don't have it in ChatListItem
|
||||
productName = null, // Null is acceptable as per ChatActivity
|
||||
productPrice = "",
|
||||
productImage = null,
|
||||
productRating = null,
|
||||
storeName = chatItem.storeName,
|
||||
chatRoomId = chatItem.chatRoomId,
|
||||
storeImage = chatItem.storeImage
|
||||
)
|
||||
val data = result.data
|
||||
|
||||
binding.tvEmptyChat.visibility = View.GONE
|
||||
if (data.isNotEmpty()) {
|
||||
val adapter = ChatListAdapter(data) { chatItem ->
|
||||
ChatActivity.createIntent(
|
||||
context = requireActivity(),
|
||||
storeId = chatItem.storeId,
|
||||
productId = 0,
|
||||
productName = null,
|
||||
productPrice = "",
|
||||
productImage = null,
|
||||
productRating = null,
|
||||
storeName = chatItem.storeName,
|
||||
chatRoomId = chatItem.chatRoomId,
|
||||
storeImage = chatItem.storeImage
|
||||
)
|
||||
}
|
||||
binding.chatListRecyclerView.adapter = adapter
|
||||
} else {
|
||||
binding.tvEmptyChat.visibility = View.VISIBLE
|
||||
}
|
||||
binding.chatListRecyclerView.adapter = adapter
|
||||
}
|
||||
is Result.Error -> {
|
||||
binding.tvEmptyChat.visibility = View.VISIBLE
|
||||
Toast.makeText(requireContext(), "Failed to load chats", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
Result.Loading -> {
|
||||
binding.progressBarChat.visibility = View.VISIBLE
|
||||
// Optional: show progress bar
|
||||
}
|
||||
}
|
||||
}
|
||||
//loading chat list
|
||||
viewModel.isLoading.observe(viewLifecycleOwner) { isLoading ->
|
||||
binding.progressBarChat?.visibility = if (isLoading) View.VISIBLE else View.GONE
|
||||
Log.d(TAG, "Loading state: $isLoading")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -89,6 +103,6 @@ class ChatListFragment : Fragment() {
|
||||
}
|
||||
|
||||
companion object{
|
||||
|
||||
private var TAG = "ChatListFragment"
|
||||
}
|
||||
}
|
||||
@ -14,6 +14,9 @@ import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.utils.Constants
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
import java.text.SimpleDateFormat
|
||||
@ -23,6 +26,28 @@ import java.util.Locale
|
||||
import java.util.TimeZone
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* ChatViewModel - Manages chat functionality for both buyers and store owners
|
||||
*
|
||||
* ARCHITECTURE OVERVIEW:
|
||||
* - Handles real-time messaging via Socket.IO
|
||||
* - Manages chat state using LiveData/MutableLiveData pattern
|
||||
* - Supports multiple message types: TEXT, IMAGE, PRODUCT
|
||||
* - Maintains separate flows for buyer and store owner chat
|
||||
*
|
||||
* KEY RESPONSIBILITIES:
|
||||
* 1. Socket connection management and real-time message handling
|
||||
* 2. Message sending/receiving with different attachment types
|
||||
* 3. Chat history loading and message status updates
|
||||
* 4. Product attachment functionality for commerce integration
|
||||
* 5. User session management and authentication
|
||||
*
|
||||
* STATE MANAGEMENT PATTERN:
|
||||
* - All UI state updates go through updateState() helper function
|
||||
* - State updates are atomic and follow immutable pattern
|
||||
* - Error states are cleared explicitly via clearError()
|
||||
*/
|
||||
|
||||
@HiltViewModel
|
||||
class ChatViewModel @Inject constructor(
|
||||
private val chatRepository: ChatRepository,
|
||||
@ -34,9 +59,12 @@ class ChatViewModel @Inject constructor(
|
||||
// Product attachment flag
|
||||
private var shouldAttachProduct = false
|
||||
|
||||
// UI state using LiveData
|
||||
private val _state = MutableLiveData(ChatUiState())
|
||||
val state: LiveData<ChatUiState> = _state
|
||||
// use state for more seamless responsive
|
||||
private val _state = MutableStateFlow(ChatUiState())
|
||||
val state: StateFlow<ChatUiState> = _state
|
||||
|
||||
private val _isLoading = MutableLiveData<Boolean>()
|
||||
val isLoading: LiveData<Boolean> = _isLoading
|
||||
|
||||
val _chatRoomId = MutableLiveData<Int>(0)
|
||||
val chatRoomId: LiveData<Int> = _chatRoomId
|
||||
@ -68,16 +96,21 @@ class ChatViewModel @Inject constructor(
|
||||
|
||||
init {
|
||||
Log.d(TAG, "ChatViewModel initialized")
|
||||
socketService.connect() // 🛠 force connection
|
||||
setupSocketListeners() // 🛠 always listen, even before user data
|
||||
initializeUser()
|
||||
}
|
||||
|
||||
private fun initializeUser() {
|
||||
_isLoading.value = true
|
||||
viewModelScope.launch {
|
||||
Log.d(TAG, "Initializing user session...")
|
||||
|
||||
when (val result = chatRepository.fetchUserProfile()) {
|
||||
is Result.Success -> {
|
||||
currentUserId = result.data?.userId
|
||||
_isLoading.value = false
|
||||
|
||||
Log.d(TAG, "User session initialized - User ID: $currentUserId")
|
||||
|
||||
if (currentUserId == null || currentUserId == 0) {
|
||||
@ -85,14 +118,17 @@ class ChatViewModel @Inject constructor(
|
||||
updateState { it.copy(error = "User authentication error. Please login again.") }
|
||||
} else {
|
||||
Log.d(TAG, "Setting up socket listeners...")
|
||||
socketService.connect()
|
||||
setupSocketListeners()
|
||||
}
|
||||
}
|
||||
is Result.Error -> {
|
||||
_isLoading.value = false
|
||||
Log.e(TAG, "Failed to fetch user profile: ${result.exception.message}")
|
||||
updateState { it.copy(error = "User authentication error. Please login again.") }
|
||||
}
|
||||
is Result.Loading -> {
|
||||
_isLoading.value = true
|
||||
Log.d(TAG, "Loading user profile...")
|
||||
}
|
||||
}
|
||||
@ -201,26 +237,116 @@ class ChatViewModel @Inject constructor(
|
||||
|
||||
if (connectionState is ConnectionState.Connected) {
|
||||
Log.d(TAG, "Socket connected, joining room...")
|
||||
socketService.joinRoom()
|
||||
val roomId = _chatRoomId.value
|
||||
if (roomId != null && roomId > 0) {
|
||||
socketService.joinRoom(roomId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// viewModelScope.launch {
|
||||
// socketService.newMessages.collect { chatLine ->
|
||||
// chatLine?.let {
|
||||
// Log.d(TAG, "NEW message received in ViewModel: ${it.message}")
|
||||
// val updatedMessages = _state.value.messages.toMutableList()
|
||||
// updatedMessages.add(convertChatLineToUiMessage(it))
|
||||
// updateState { it.copy(messages = updatedMessages) }
|
||||
//
|
||||
// if (it.senderId != currentUserId) {
|
||||
// updateMessageStatus(it.id, Constants.STATUS_READ)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
viewModelScope.launch {
|
||||
socketService.newMessages.collect { chatLine ->
|
||||
chatLine?.let {
|
||||
Log.d(TAG, "New message received via socket - ID: ${it.id}, SenderID: ${it.senderId}")
|
||||
val currentMessages = _state.value?.messages ?: listOf()
|
||||
val updatedMessages = currentMessages.toMutableList().apply {
|
||||
add(convertChatLineToUiMessage(it))
|
||||
Log.d("ChatViewModel", "Collected new message from SocketIOService: ${chatLine.message}")
|
||||
chatLine?.let { incomingChatLine ->
|
||||
// 1. First update: Add the message to the list (potentially without full product info)
|
||||
_state.update { currentState ->
|
||||
val existingMessageIndex =
|
||||
currentState.messages.indexOfFirst { it.id == incomingChatLine.id }
|
||||
val messagesAfterInitialUpdate = if (existingMessageIndex != -1) {
|
||||
// If message exists (e.g., status update), just update it
|
||||
val updatedList = currentState.messages.toMutableList()
|
||||
updatedList[existingMessageIndex] = mapChatLineToUiMessage(
|
||||
incomingChatLine,
|
||||
updatedList[existingMessageIndex].productInfo
|
||||
) // Preserve existing productInfo if any
|
||||
updatedList
|
||||
} else {
|
||||
// New message, add it
|
||||
(currentState.messages + mapChatLineToUiMessage(incomingChatLine)).distinctBy { msg -> msg.id }
|
||||
}
|
||||
// Sort after any update/addition
|
||||
currentState.copy(messages = messagesAfterInitialUpdate.sortedBy { msg ->
|
||||
SimpleDateFormat(
|
||||
"yyyy-MM-dd HH:mm:ss",
|
||||
Locale.getDefault()
|
||||
).parse(msg.createdAt)?.time
|
||||
})
|
||||
}
|
||||
updateState { it.copy(messages = updatedMessages) }
|
||||
|
||||
if (it.senderId != currentUserId) {
|
||||
Log.d(TAG, "Marking message as read: ${it.id}")
|
||||
updateMessageStatus(it.id, Constants.STATUS_READ)
|
||||
// 2. If it's a product message and needs details, fetch them
|
||||
if (incomingChatLine.productId != 0) { // Check if it's a product message
|
||||
viewModelScope.launch {
|
||||
Log.d(
|
||||
TAG,
|
||||
"Fetching product detail for ID: ${incomingChatLine.productId}"
|
||||
)
|
||||
|
||||
// Call your repository function directly
|
||||
val productResponse =
|
||||
chatRepository.fetchProductDetail(incomingChatLine.productId)
|
||||
|
||||
if (productResponse != null && productResponse.product != null) {
|
||||
val fetchedProduct =
|
||||
productResponse.product // Access the nested product object
|
||||
Log.d(
|
||||
TAG,
|
||||
"Successfully fetched product: ${fetchedProduct.productName}"
|
||||
)
|
||||
|
||||
// Create a complete ProductInfo object
|
||||
val fullProductInfo = ProductInfo(
|
||||
productId = fetchedProduct.productId,
|
||||
productName = fetchedProduct.productName, // Use productName from fetched data
|
||||
productPrice = fetchedProduct.price, // Use productPrice from fetched data
|
||||
productImage = fetchedProduct.image, // Use productImage from fetched data
|
||||
productRating = fetchedProduct.rating.toFloat(),
|
||||
storeName = fetchedProduct.productName // Use storeName from fetched data
|
||||
)
|
||||
|
||||
// --- PHASE 3: Second UI update (fill in full product info) ---
|
||||
_state.update { currentState ->
|
||||
val updatedMessages = currentState.messages.map { msg ->
|
||||
if (msg.id == incomingChatLine.id) {
|
||||
// Found the message, update its productInfo with full details
|
||||
msg.copy(productInfo = fullProductInfo)
|
||||
} else {
|
||||
msg
|
||||
}
|
||||
}
|
||||
currentState.copy(messages = updatedMessages)
|
||||
}
|
||||
} else {
|
||||
Log.e(
|
||||
TAG,
|
||||
"Failed to fetch product detail for ID ${incomingChatLine.productId} or product data is null."
|
||||
)
|
||||
// Optionally, update message status to indicate error in product loading
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// // Your existing logic for clearing typing status etc.
|
||||
// if (incomingChatLine.isTyping == false && incomingChatLine.from?.id != sessionManager.getUserId()?.toIntOrNull()) {
|
||||
// _state.update { it.copy(isOtherUserTyping = false) }
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -241,10 +367,10 @@ class ChatViewModel @Inject constructor(
|
||||
if (roomId <= 0) {
|
||||
Log.e(TAG, "Cannot join room: Invalid room ID")
|
||||
return
|
||||
} else if (roomId > 0){
|
||||
Log.d(TAG, "Joining socket room: $roomId")
|
||||
socketService.joinRoom(roomId)
|
||||
}
|
||||
|
||||
Log.d(TAG, "Joining socket room: $roomId")
|
||||
socketService.joinRoom()
|
||||
}
|
||||
|
||||
fun sendTypingStatus(isTyping: Boolean) {
|
||||
@ -313,10 +439,13 @@ class ChatViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
fun getChatList() {
|
||||
_isLoading.value = true
|
||||
Log.d(TAG, "Getting chat list...")
|
||||
viewModelScope.launch {
|
||||
_chatList.value = Result.Loading
|
||||
// _chatList.value = Result.Loading
|
||||
_chatList.value = chatRepository.getListChat()
|
||||
_isLoading.value = false
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -695,7 +824,7 @@ class ChatViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
//update message status
|
||||
//update message status
|
||||
fun updateMessageStatus(messageId: Int, status: String) {
|
||||
Log.d(TAG, "Updating message status - ID: $messageId, Status: $status")
|
||||
|
||||
@ -723,13 +852,26 @@ class ChatViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
//set image attachment
|
||||
//set image attachment
|
||||
fun setSelectedImageFile(file: File?) {
|
||||
selectedImageFile = file
|
||||
updateState { it.copy(hasAttachment = file != null) }
|
||||
Log.d(TAG, "Image attachment ${if (file != null) "selected: ${file.name}" else "cleared"}")
|
||||
}
|
||||
|
||||
fun clearSelectedImage() {
|
||||
Log.d(TAG, "Clearing selected image attachment")
|
||||
|
||||
selectedImageFile?.let { file ->
|
||||
Log.d(TAG, "Clearing image file: ${file.name}")
|
||||
}
|
||||
|
||||
selectedImageFile = null
|
||||
updateState { it.copy(hasAttachment = false) }
|
||||
|
||||
Log.d(TAG, "Image attachment cleared successfully")
|
||||
}
|
||||
|
||||
// convert form chatLine api to UI chat messages
|
||||
private fun convertChatLineToUiMessage(chatLine: ChatLine): ChatUiMessage {
|
||||
val formattedTime = formatTimestamp(chatLine.createdAt)
|
||||
@ -745,7 +887,7 @@ class ChatViewModel @Inject constructor(
|
||||
)
|
||||
}
|
||||
|
||||
// convert chat history item to ui
|
||||
// convert chat history item to ui
|
||||
private fun convertChatLineToUiMessageHistory(chatItem: ChatItem): ChatUiMessage {
|
||||
val formattedTime = formatTimestamp(chatItem.createdAt)
|
||||
|
||||
@ -886,7 +1028,7 @@ class ChatViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
//format price
|
||||
//format price
|
||||
private fun formatPrice(price: String): String {
|
||||
return if (price.startsWith("Rp")) price else "Rp$price"
|
||||
}
|
||||
@ -912,9 +1054,7 @@ class ChatViewModel @Inject constructor(
|
||||
|
||||
// helper function to update live data
|
||||
private fun updateState(update: (ChatUiState) -> ChatUiState) {
|
||||
_state.value?.let {
|
||||
_state.value = update(it)
|
||||
}
|
||||
_state.value = update(_state.value)
|
||||
}
|
||||
|
||||
//clear any error messages
|
||||
@ -1007,6 +1147,73 @@ class ChatViewModel @Inject constructor(
|
||||
private fun isThisYear(messageCalendar: Calendar, today: Calendar): Boolean {
|
||||
return messageCalendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
|
||||
}
|
||||
|
||||
fun setChatRoomId(roomId: Int) {
|
||||
_chatRoomId.value = roomId
|
||||
joinSocketRoom(roomId)
|
||||
loadChatHistory(roomId)
|
||||
}
|
||||
|
||||
private fun convertToUiMessage(chatLine: ChatLine): ChatUiMessage {
|
||||
|
||||
val formattedTime = formatTimestamp(chatLine.createdAt)
|
||||
return ChatUiMessage(
|
||||
id = chatLine.id,
|
||||
message = chatLine.message,
|
||||
attachment = chatLine.attachment,
|
||||
status = chatLine.status,
|
||||
time = formattedTime, // or format from createdAt if needed
|
||||
isSentByMe = chatLine.senderId == currentUserId,
|
||||
messageType = MessageType.TEXT, // or detect from chatLine if needed
|
||||
productInfo = null, // optional, if applicable
|
||||
createdAt = chatLine.createdAt
|
||||
)
|
||||
}
|
||||
|
||||
private fun mapChatLineToUiMessage(chatLine: ChatLine, fetchedProductInfo: ProductInfo? = null): ChatUiMessage {
|
||||
val isSentByMe = chatLine.senderId == sessionManager.getUserId()?.toIntOrNull() // Using senderId now
|
||||
val formattedTime = try {
|
||||
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).apply {
|
||||
timeZone = TimeZone.getTimeZone("UTC")
|
||||
}
|
||||
val outputFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
|
||||
val date = inputFormat.parse(chatLine.createdAt)
|
||||
date?.let { outputFormat.format(it) } ?: ""
|
||||
} catch (e: Exception) {
|
||||
Log.e("ChatViewModel", "Error parsing date: ${chatLine.createdAt}", e)
|
||||
""
|
||||
}
|
||||
|
||||
// Determine message type based on what ChatLine provides
|
||||
val messageType = when {
|
||||
chatLine.attachment?.isNotEmpty() == true -> MessageType.IMAGE
|
||||
chatLine.productId != 0 -> MessageType.PRODUCT // If productId is non-zero, it's a product message
|
||||
else -> MessageType.TEXT
|
||||
}
|
||||
|
||||
// Initialize productInfo: if fetchedProductInfo is provided, use it.
|
||||
// Otherwise, if ChatLine has a productId, create a ProductInfo with just the ID.
|
||||
// If no productId, it's null.
|
||||
val productInfo = fetchedProductInfo ?: if (chatLine.productId != 0) {
|
||||
// Create a placeholder ProductInfo with just the ID for initial display
|
||||
// The full details will be fetched later
|
||||
ProductInfo(productId = chatLine.productId)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
return ChatUiMessage(
|
||||
id = chatLine.id,
|
||||
message = chatLine.message,
|
||||
attachment = chatLine.attachment,
|
||||
status = chatLine.status,
|
||||
time = formattedTime,
|
||||
isSentByMe = isSentByMe,
|
||||
messageType = messageType,
|
||||
productInfo = productInfo, // Use the determined productInfo
|
||||
createdAt = chatLine.createdAt
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
enum class MessageType {
|
||||
@ -1016,12 +1223,12 @@ enum class MessageType {
|
||||
}
|
||||
|
||||
data class ProductInfo(
|
||||
val productId: Int,
|
||||
val productName: String,
|
||||
val productPrice: String,
|
||||
val productImage: String,
|
||||
val productRating: Float,
|
||||
val storeName: String
|
||||
val productId: Int, // Keep productId here
|
||||
val productName: String? = null, // Make nullable
|
||||
val productPrice: String? = null, // Make nullable
|
||||
val productImage: String? = null, // Make nullable
|
||||
val productRating: Float = 0f, // Default value
|
||||
val storeName: String? = null
|
||||
)
|
||||
|
||||
// representing chat messages to UI
|
||||
@ -1037,8 +1244,6 @@ data class ChatUiMessage(
|
||||
val createdAt: String
|
||||
)
|
||||
|
||||
|
||||
|
||||
// representing UI state to screen
|
||||
data class ChatUiState(
|
||||
val messages: List<ChatUiMessage> = emptyList(),
|
||||
@ -1056,4 +1261,8 @@ data class ChatUiState(
|
||||
val productImageUrl: String = "",
|
||||
val productRating: Float = 0f,
|
||||
val storeName: String = ""
|
||||
)
|
||||
)
|
||||
|
||||
//data class ChatUiState(
|
||||
// val messages: List<ChatUiMessage> = emptyList()
|
||||
//)
|
||||
|
||||
@ -10,14 +10,24 @@ import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.google.gson.Gson
|
||||
import io.socket.client.IO
|
||||
import io.socket.client.Socket
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import org.json.JSONObject
|
||||
import java.net.URISyntaxException
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
class SocketIOService(
|
||||
@Singleton
|
||||
class SocketIOService @Inject constructor(
|
||||
private val sessionManager: SessionManager
|
||||
) {
|
||||
private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private val TAG = "SocketIOService"
|
||||
|
||||
// Socket.IO client
|
||||
@ -30,8 +40,8 @@ class SocketIOService(
|
||||
private val _connectionState = MutableStateFlow<ConnectionState>(ConnectionState.Disconnected())
|
||||
val connectionState: StateFlow<ConnectionState> = _connectionState
|
||||
|
||||
private val _newMessages = MutableStateFlow<ChatLine?>(null)
|
||||
val newMessages: StateFlow<ChatLine?> = _newMessages
|
||||
private val _newMessages = MutableSharedFlow<ChatLine>(extraBufferCapacity = 1) // Using extraBufferCapacity for a non-suspending emit
|
||||
val newMessages: SharedFlow<ChatLine> = _newMessages
|
||||
|
||||
private val _typingStatus = MutableStateFlow<TypingStatus?>(null)
|
||||
val typingStatus: StateFlow<TypingStatus?> = _typingStatus
|
||||
@ -85,63 +95,95 @@ class SocketIOService(
|
||||
* Sets up Socket.IO event listeners
|
||||
*/
|
||||
private fun setupSocketListeners() {
|
||||
socket?.let { socket ->
|
||||
// Connection events
|
||||
socket.on(Socket.EVENT_CONNECT) {
|
||||
Log.d(TAG, "Socket.IO connected")
|
||||
isConnected = true
|
||||
_connectionState.value = ConnectionState.Connected
|
||||
_connectionStateLiveData.postValue(ConnectionState.Connected)
|
||||
}
|
||||
|
||||
socket.on(Socket.EVENT_DISCONNECT) {
|
||||
Log.d(TAG, "Socket.IO disconnected")
|
||||
isConnected = false
|
||||
_connectionState.value = ConnectionState.Disconnected("Disconnected from server")
|
||||
_connectionStateLiveData.postValue(ConnectionState.Disconnected("Disconnected from server"))
|
||||
}
|
||||
socket?.on(Constants.EVENT_NEW_MESSAGE) { args -> // Use the event name your server emits
|
||||
Log.d(TAG, "Raw event received on ${Constants.EVENT_NEW_MESSAGE}: ${args.firstOrNull()}") // Check raw args
|
||||
|
||||
socket.on(Socket.EVENT_CONNECT_ERROR) { args ->
|
||||
val error = if (args.isNotEmpty() && args[0] != null) args[0].toString() else "Unknown error"
|
||||
Log.e(TAG, "Socket.IO connection error: $error")
|
||||
isConnected = false
|
||||
_connectionState.value = ConnectionState.Error("Connection error: $error")
|
||||
_connectionStateLiveData.postValue(ConnectionState.Error("Connection error: $error"))
|
||||
}
|
||||
|
||||
// Chat events
|
||||
socket.on(Constants.EVENT_NEW_MESSAGE) { args ->
|
||||
if (args.isNotEmpty()) {
|
||||
try {
|
||||
if (args.isNotEmpty() && args[0] != null) {
|
||||
val messageJson = args[0].toString()
|
||||
Log.d(TAG, "Received new message: $messageJson")
|
||||
val chatLine = Gson().fromJson(messageJson, ChatLine::class.java)
|
||||
_newMessages.value = chatLine
|
||||
_newMessagesLiveData.postValue(chatLine)
|
||||
val messageJson = args[0].toString()
|
||||
val chatLine = Gson().fromJson(messageJson, ChatLine::class.java)
|
||||
Log.d(TAG, "Successfully parsed ChatLine: ${chatLine.message}")
|
||||
Log.d(TAG, "Emitting new message to _newMessages SharedFlow...") // New log
|
||||
|
||||
// Use the serviceScope to launch a coroutine for emit()
|
||||
serviceScope.launch {
|
||||
_newMessages.emit(chatLine) // This ensures every message is processed
|
||||
Log.d(TAG, "New message emitted to SharedFlow.") // New log after emit
|
||||
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error parsing new message event", e)
|
||||
}
|
||||
}
|
||||
|
||||
socket.on(Constants.EVENT_TYPING) { args ->
|
||||
try {
|
||||
if (args.isNotEmpty() && args[0] != null) {
|
||||
val typingData = args[0] as JSONObject
|
||||
val userId = typingData.getInt("userId")
|
||||
val roomId = typingData.getInt("roomId")
|
||||
val isTyping = typingData.getBoolean("isTyping")
|
||||
|
||||
Log.d(TAG, "Received typing status: User $userId in room $roomId is typing: $isTyping")
|
||||
val status = TypingStatus(userId, roomId, isTyping)
|
||||
_typingStatus.value = status
|
||||
_typingStatusLiveData.postValue(status)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error parsing typing event", e)
|
||||
Log.e(TAG, "Error parsing or emitting new message: ${e.message}", e)
|
||||
}
|
||||
} else {
|
||||
Log.w(TAG, "Received empty args for ${Constants.EVENT_NEW_MESSAGE}")
|
||||
}
|
||||
}
|
||||
// socket?.on(Constants.EVENT_NEW_MESSAGE) { args ->
|
||||
// if (args.isNotEmpty()) {
|
||||
// val messageJson = args[0].toString()
|
||||
// val chatLine = Gson().fromJson(messageJson, ChatLine::class.java)
|
||||
// Log.d("SocketIOService", "Message received: ${chatLine.message}")
|
||||
// _newMessages.value = chatLine
|
||||
// }
|
||||
// }
|
||||
// socket?.let { socket ->
|
||||
// // Connection events
|
||||
// socket.on(Socket.EVENT_CONNECT) {
|
||||
// Log.d(TAG, "Socket.IO connected")
|
||||
// isConnected = true
|
||||
// _connectionState.value = ConnectionState.Connected
|
||||
// _connectionStateLiveData.postValue(ConnectionState.Connected)
|
||||
// }
|
||||
//
|
||||
// socket.on(Socket.EVENT_DISCONNECT) {
|
||||
// Log.d(TAG, "Socket.IO disconnected")
|
||||
// isConnected = false
|
||||
// _connectionState.value = ConnectionState.Disconnected("Disconnected from server")
|
||||
// _connectionStateLiveData.postValue(ConnectionState.Disconnected("Disconnected from server"))
|
||||
// }
|
||||
//
|
||||
// socket.on(Socket.EVENT_CONNECT_ERROR) { args ->
|
||||
// val error = if (args.isNotEmpty() && args[0] != null) args[0].toString() else "Unknown error"
|
||||
// Log.e(TAG, "Socket.IO connection error: $error")
|
||||
// isConnected = false
|
||||
// _connectionState.value = ConnectionState.Error("Connection error: $error")
|
||||
// _connectionStateLiveData.postValue(ConnectionState.Error("Connection error: $error"))
|
||||
// }
|
||||
//
|
||||
// // Chat events
|
||||
// socket.on(Constants.EVENT_NEW_MESSAGE) { args ->
|
||||
// try {
|
||||
// if (args.isNotEmpty() && args[0] != null) {
|
||||
// val messageJson = args[0].toString()
|
||||
// Log.d(TAG, "Received new message: $messageJson")
|
||||
// val chatLine = Gson().fromJson(messageJson, ChatLine::class.java)
|
||||
// _newMessages.value = chatLine
|
||||
// _newMessagesLiveData.postValue(chatLine)
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Error parsing new message event", e)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// socket.on(Constants.EVENT_TYPING) { args ->
|
||||
// try {
|
||||
// if (args.isNotEmpty() && args[0] != null) {
|
||||
// val typingData = args[0] as JSONObject
|
||||
// val userId = typingData.getInt("userId")
|
||||
// val roomId = typingData.getInt("roomId")
|
||||
// val isTyping = typingData.getBoolean("isTyping")
|
||||
//
|
||||
// Log.d(TAG, "Received typing status: User $userId in room $roomId is typing: $isTyping")
|
||||
// val status = TypingStatus(userId, roomId, isTyping)
|
||||
// _typingStatus.value = status
|
||||
// _typingStatusLiveData.postValue(status)
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Error parsing typing event", e)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
@ -159,22 +201,29 @@ class SocketIOService(
|
||||
/**
|
||||
* Joins a specific chat room
|
||||
*/
|
||||
fun joinRoom() {
|
||||
fun joinRoom(roomId: Int) {
|
||||
// if (!isConnected) {
|
||||
// connect()
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// // Get user ID from SessionManager
|
||||
// val userId = sessionManager.getUserId()
|
||||
// if (userId.isNullOrEmpty()) {
|
||||
// Log.e(TAG, "Cannot join room: User ID is null or empty")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// // Join the room using the current user's ID
|
||||
// socket?.emit("joinRoom", roomId) // ✅
|
||||
// Log.d(TAG, "Joined room ID: $roomId")
|
||||
// Log.d(TAG, "Joined room for user: $userId")
|
||||
if (!isConnected) {
|
||||
connect()
|
||||
return
|
||||
}
|
||||
|
||||
// Get user ID from SessionManager
|
||||
val userId = sessionManager.getUserId()
|
||||
if (userId.isNullOrEmpty()) {
|
||||
Log.e(TAG, "Cannot join room: User ID is null or empty")
|
||||
return
|
||||
}
|
||||
|
||||
// Join the room using the current user's ID
|
||||
socket?.emit("joinRoom", userId)
|
||||
Log.d(TAG, "Joined room for user: $userId")
|
||||
socket?.emit("joinRoom", roomId)
|
||||
Log.d(TAG, "Joined room ID: $roomId")
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -208,25 +208,6 @@ class HomeFragment : Fragment() {
|
||||
private fun initUi() {
|
||||
// For LightStatusBar
|
||||
setLightStatusBar()
|
||||
// val banners = binding.banners
|
||||
// banners.offscreenPageLimit = 1
|
||||
//
|
||||
// val nextItemVisiblePx = resources.getDimension(R.dimen.viewpager_next_item_visible)
|
||||
// val currentItemHorizontalMarginPx =
|
||||
// resources.getDimension(R.dimen.viewpager_current_item_horizontal_margin)
|
||||
// val pageTranslationX = nextItemVisiblePx + currentItemHorizontalMarginPx
|
||||
//
|
||||
// banners.setPageTransformer { page, position ->
|
||||
// page.translationX = -pageTranslationX * position
|
||||
// page.scaleY = 1 - (0.25f * kotlin.math.abs(position))
|
||||
// }
|
||||
//
|
||||
// banners.addItemDecoration(
|
||||
// HorizontalMarginItemDecoration(
|
||||
// requireContext(),
|
||||
// R.dimen.viewpager_current_item_horizontal_margin
|
||||
// )
|
||||
// )
|
||||
}
|
||||
|
||||
private fun handleProductClick(product: ProductsItem) {
|
||||
@ -248,8 +229,4 @@ class HomeFragment : Fragment() {
|
||||
categoryAdapter = null
|
||||
_binding = null
|
||||
}
|
||||
|
||||
// private fun showLoading(isLoading: Boolean) {
|
||||
// binding.progressBar.isVisible = isLoading
|
||||
// }
|
||||
}
|
||||
@ -2,6 +2,7 @@ package com.alya.ecommerce_serang.ui.home
|
||||
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
@ -65,6 +66,16 @@ class SearchResultsAdapter(
|
||||
|
||||
val storeName = product.storeId?.let { storeMap[it]?.storeName } ?: "Unknown Store"
|
||||
binding.tvStoreName.text = storeName
|
||||
val ratingStr = product.rating
|
||||
val ratingValue = ratingStr?.toFloatOrNull()
|
||||
|
||||
if (ratingValue != null && ratingValue > 0f) {
|
||||
binding.rating.text = String.format("%.1f", ratingValue)
|
||||
binding.rating.visibility = View.VISIBLE
|
||||
} else {
|
||||
binding.rating.text = "Belum ada rating"
|
||||
binding.rating.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
@ -95,26 +96,43 @@ class CheckoutActivity : AppCompatActivity() {
|
||||
// Process Cart checkout flow
|
||||
val cartItemIds = intent.getIntArrayExtra(EXTRA_CART_ITEM_IDS)?.toList() ?: emptyList()
|
||||
val isWholesaleArray = intent.getBooleanArrayExtra(EXTRA_CART_ITEM_WHOLESALE)
|
||||
val wholesalePricesArray = intent.getIntArrayExtra(EXTRA_CART_ITEM_WHOLESALE_PRICES)
|
||||
|
||||
if (cartItemIds.isNotEmpty()) {
|
||||
// Create a map of cart item IDs to wholesale status if available
|
||||
val wholesaleMap = if (isWholesaleArray != null && isWholesaleArray.size == cartItemIds.size) {
|
||||
cartItemIds.mapIndexed { index, id -> id to isWholesaleArray[index] }.toMap()
|
||||
// Build map of cartItemId -> isWholesale
|
||||
val isWholesaleMap = if (isWholesaleArray != null && isWholesaleArray.size == cartItemIds.size) {
|
||||
cartItemIds.mapIndexed { index, id ->
|
||||
id to isWholesaleArray[index]
|
||||
}.toMap()
|
||||
} else {
|
||||
emptyMap()
|
||||
}
|
||||
|
||||
viewModel.initializeFromCart(cartItemIds, wholesaleMap)
|
||||
// Build wholesalePriceMap - FIX: Map cartItemId to wholesale price
|
||||
val wholesalePriceMap = if (wholesalePricesArray != null && wholesalePricesArray.size == cartItemIds.size) {
|
||||
cartItemIds.mapIndexed { index, id ->
|
||||
id to wholesalePricesArray[index]
|
||||
}.toMap()
|
||||
} else {
|
||||
emptyMap()
|
||||
}
|
||||
|
||||
viewModel.initializeFromCart(
|
||||
cartItemIds,
|
||||
isWholesaleMap,
|
||||
wholesalePriceMap
|
||||
)
|
||||
|
||||
Log.d("CheckoutActivity", "Cart IDs: $cartItemIds")
|
||||
Log.d("CheckoutActivity", "IsWholesaleArray: ${isWholesaleArray?.joinToString()}")
|
||||
Log.d("CheckoutActivity", "WholesalePricesArray: ${wholesalePricesArray?.joinToString()}")
|
||||
Log.d("CheckoutActivity", "IsWholesaleMap: $isWholesaleMap")
|
||||
Log.d("CheckoutActivity", "WholesalePriceMap: $wholesalePriceMap")
|
||||
} else {
|
||||
Toast.makeText(this, "Error: No cart items specified", Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(this, "Tidak ada item keranjang", Toast.LENGTH_SHORT).show()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
// viewModel.getPaymentMethods { paymentMethods ->
|
||||
// // Logging is just for debugging
|
||||
// Log.d("CheckoutActivity", "Loaded ${paymentMethods.size} payment methods")
|
||||
// }
|
||||
}
|
||||
|
||||
private fun setupToolbar() {
|
||||
@ -165,7 +183,7 @@ class CheckoutActivity : AppCompatActivity() {
|
||||
// Observe loading state
|
||||
viewModel.isLoading.observe(this) { isLoading ->
|
||||
binding.btnPay.isEnabled = !isLoading
|
||||
// Show/hide loading indicator if you have one
|
||||
|
||||
}
|
||||
|
||||
// Observe error messages
|
||||
@ -273,10 +291,14 @@ class CheckoutActivity : AppCompatActivity() {
|
||||
private fun updateShippingUI(shipName: String, shipService: String, shipEtd: String, shipPrice: Int) {
|
||||
if (shipName.isNotEmpty() && shipService.isNotEmpty()) {
|
||||
// Display shipping name and service in one line
|
||||
binding.cardShipment.visibility = View.VISIBLE
|
||||
|
||||
binding.tvCourierName.text = "$shipName $shipService"
|
||||
binding.tvDeliveryEstimate.text = "$shipEtd hari kerja"
|
||||
binding.tvShippingPrice.text = formatCurrency(shipPrice.toDouble())
|
||||
binding.rbJne.isChecked = true
|
||||
} else {
|
||||
binding.cardShipment.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
@ -416,6 +438,7 @@ class CheckoutActivity : AppCompatActivity() {
|
||||
const val EXTRA_PRICE = "PRICE"
|
||||
const val EXTRA_ISWHOLESALE = "ISWHOLESALE"
|
||||
const val EXTRA_CART_ITEM_WHOLESALE = "EXTRA_CART_ITEM_WHOLESALE"
|
||||
const val EXTRA_CART_ITEM_WHOLESALE_PRICES = "EXTRA_CART_ITEM_WHOLESALE_PRICES"
|
||||
|
||||
// Helper methods for starting activity
|
||||
|
||||
@ -449,13 +472,17 @@ class CheckoutActivity : AppCompatActivity() {
|
||||
fun startForCart(
|
||||
context: Context,
|
||||
cartItemIds: List<Int>,
|
||||
isWholesaleArray: BooleanArray? = null
|
||||
isWholesaleArray: BooleanArray? = null,
|
||||
wholesalePrices: IntArray? = null
|
||||
) {
|
||||
val intent = Intent(context, CheckoutActivity::class.java).apply {
|
||||
putExtra(EXTRA_CART_ITEM_IDS, cartItemIds.toIntArray())
|
||||
if (isWholesaleArray != null) {
|
||||
putExtra(EXTRA_CART_ITEM_WHOLESALE, isWholesaleArray)
|
||||
}
|
||||
if (wholesalePrices != null) {
|
||||
putExtra(EXTRA_CART_ITEM_WHOLESALE_PRICES, wholesalePrices)
|
||||
}
|
||||
}
|
||||
context.startActivity(intent)
|
||||
}
|
||||
|
||||
@ -64,7 +64,7 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
shipPrice = 0, // Will be set when user selects shipping
|
||||
shipName = "",
|
||||
shipService = "",
|
||||
isNego = false, // Default value
|
||||
isNego = false, // Default value
|
||||
productId = productId,
|
||||
quantity = quantity,
|
||||
shipEtd = "",
|
||||
@ -93,30 +93,46 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
}
|
||||
|
||||
// Initialize checkout from cart
|
||||
fun initializeFromCart(cartItemIds: List<Int>, isWholesaleMap: Map<Int, Boolean> = emptyMap()) {
|
||||
fun initializeFromCart(
|
||||
cartItemIds: List<Int>,
|
||||
isWholesaleMap: Map<Int, Boolean> = emptyMap(),
|
||||
wholesalePriceMap: Map<Int, Int> = emptyMap()
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
_isLoading.value = true
|
||||
|
||||
try {
|
||||
// Get cart data
|
||||
val cartResult = repository.getCart()
|
||||
|
||||
if (cartResult is Result.Success) {
|
||||
// Find matching cart items
|
||||
val matchingItems = mutableListOf<CartItemsItem>()
|
||||
var storeData: DataItemCart? = null
|
||||
|
||||
for (store in cartResult.data) {
|
||||
val storeItems = store.cartItems.filter { it.cartItemId in cartItemIds }
|
||||
if (storeItems.isNotEmpty()) {
|
||||
matchingItems.addAll(storeItems)
|
||||
// ✅ Apply wholesale prices - Replace item prices with wholesale prices
|
||||
val updatedItems = storeItems.map { item ->
|
||||
val wholesalePrice = wholesalePriceMap[item.cartItemId]
|
||||
val isWholesale = isWholesaleMap[item.cartItemId] ?: false
|
||||
|
||||
// Use wholesale price if item is wholesale and price exists
|
||||
if (isWholesale && wholesalePrice != null) {
|
||||
Log.d(TAG, "Applying wholesale price for item ${item.cartItemId}: ${item.price} -> $wholesalePrice")
|
||||
item.copy(price = wholesalePrice)
|
||||
} else {
|
||||
Log.d(TAG, "Using regular price for item ${item.cartItemId}: ${item.price}")
|
||||
item
|
||||
}
|
||||
}
|
||||
|
||||
matchingItems.addAll(updatedItems)
|
||||
storeData = store
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (matchingItems.isNotEmpty() && storeData != null) {
|
||||
// Create initial OrderRequest object
|
||||
val orderRequest = OrderRequest(
|
||||
addressId = 0,
|
||||
paymentMethodId = 0,
|
||||
@ -126,21 +142,26 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
isNego = false,
|
||||
cartItemId = cartItemIds,
|
||||
shipEtd = "",
|
||||
// Add a list tracking which items are wholesale
|
||||
isReseller = isWholesaleMap.any { it.value } // Set true if any item is wholesale
|
||||
isReseller = isWholesaleMap.any { it.value }
|
||||
)
|
||||
|
||||
// Create checkout data
|
||||
_checkoutData.value = CheckoutData(
|
||||
orderRequest = orderRequest,
|
||||
productName = matchingItems.first().productName,
|
||||
sellerName = storeData.storeName,
|
||||
sellerId = storeData.storeId,
|
||||
isBuyNow = false,
|
||||
cartItems = matchingItems,
|
||||
cartItemWholesaleMap = isWholesaleMap // Store the wholesale map
|
||||
cartItems = matchingItems, // These now have updated wholesale prices
|
||||
cartItemWholesaleMap = isWholesaleMap
|
||||
)
|
||||
|
||||
Log.d(TAG, "CheckoutData initialized with ${matchingItems.size} items")
|
||||
matchingItems.forEachIndexed { index, item ->
|
||||
val isWholesale = isWholesaleMap[item.cartItemId] ?: false
|
||||
Log.d(TAG, "Item $index: ${item.productName}, Price: ${item.price}, IsWholesale: $isWholesale")
|
||||
}
|
||||
|
||||
// Calculate totals with updated prices
|
||||
calculateSubtotal()
|
||||
calculateTotal()
|
||||
} else {
|
||||
@ -151,6 +172,7 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_errorMessage.value = "Error: ${e.message}"
|
||||
Log.e(TAG, "Error in initializeFromCart", e)
|
||||
} finally {
|
||||
_isLoading.value = false
|
||||
}
|
||||
@ -405,8 +427,6 @@ class CheckoutViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
companion object {
|
||||
private const val TAG = "CheckoutViewModel"
|
||||
}
|
||||
|
||||
@ -34,6 +34,8 @@ class SingleCartItemAdapter(private val cartItem: CartItemsItem) :
|
||||
// Load placeholder image
|
||||
Glide.with(ivProduct.context)
|
||||
.load(R.drawable.placeholder_image)
|
||||
.placeholder(R.drawable.placeholder_image)
|
||||
.error(R.drawable.placeholder_image)
|
||||
.into(ivProduct)
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,12 +22,14 @@ 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.response.customer.order.SubdistrictsItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.VillagesItem
|
||||
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.OrderRepository
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.data.repository.UserRepository
|
||||
import com.alya.ecommerce_serang.databinding.ActivityAddAddressBinding
|
||||
import com.alya.ecommerce_serang.utils.SavedStateViewModelFactory
|
||||
@ -37,8 +39,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
|
||||
|
||||
@ -46,6 +48,8 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
private var longitude: Double? = null
|
||||
private val provinceAdapter by lazy { ProvinceAdapter(this) }
|
||||
private val cityAdapter by lazy { CityAdapter(this) }
|
||||
private val subdistrictAdapter by lazy { SubdsitrictAdapter(this)}
|
||||
private val villageAdapter by lazy { VillagesAdapter(this)}
|
||||
|
||||
private val viewModel: AddAddressViewModel by viewModels {
|
||||
SavedStateViewModelFactory(this) { savedStateHandle ->
|
||||
@ -80,11 +84,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 +102,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 {
|
||||
@ -116,6 +118,8 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
// Set adapters
|
||||
binding.autoCompleteProvinsi.setAdapter(provinceAdapter)
|
||||
binding.autoCompleteKabupaten.setAdapter(cityAdapter)
|
||||
binding.autoCompleteKecamatan.setAdapter(subdistrictAdapter)
|
||||
binding.autoCompleteDesa.setAdapter(villageAdapter)
|
||||
|
||||
// Make dropdown appear on click (not just when typing)
|
||||
binding.autoCompleteProvinsi.setOnClickListener {
|
||||
@ -134,6 +138,26 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
binding.autoCompleteKecamatan.setOnClickListener{
|
||||
if (subdistrictAdapter.count > 0){
|
||||
Log.d(TAG, "Subdistrict clicked, dropdown with ${subdistrictAdapter.count} items")
|
||||
binding.autoCompleteKecamatan.showDropDown()
|
||||
} else {
|
||||
Log.d(TAG, "No kecamatan available")
|
||||
Toast.makeText(this, "Pilih Kabupaten / Kota terlebih dahulu", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
binding.autoCompleteDesa.setOnClickListener{
|
||||
if (villageAdapter.count > 0){
|
||||
Log.d(TAG, "Village clicked, dropdown with ${villageAdapter.count} items")
|
||||
binding.autoCompleteDesa.showDropDown()
|
||||
} else {
|
||||
Log.d(TAG, "No desa available")
|
||||
Toast.makeText(this, "Pilih Kecamatan terlebih dahulu", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
// Set listeners for selection
|
||||
binding.autoCompleteProvinsi.setOnItemClickListener { _, _, position, _ ->
|
||||
val provinceId = provinceAdapter.getProvinceId(position)
|
||||
@ -152,9 +176,41 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
|
||||
cityId?.let { id ->
|
||||
Log.d(TAG, "Setting selectedCityId=$id")
|
||||
viewModel.getSubdistrict(cityId)
|
||||
viewModel.selectedCityId = id
|
||||
binding.autoCompleteKecamatan.text.clear()
|
||||
|
||||
} ?: Log.e(TAG, "Could not get cityId for position $position")
|
||||
}
|
||||
|
||||
binding.autoCompleteKecamatan.setOnItemClickListener { _, _, position, _ ->
|
||||
val subdistrictId = subdistrictAdapter.getSubdistrictId(position)
|
||||
val subdistrictName = subdistrictAdapter.getSubdistrictName(position)
|
||||
|
||||
Log.d(TAG, "Subdistrict selected at position $position, subsId=$subdistrictId")
|
||||
|
||||
subdistrictId?.let { id ->
|
||||
Log.d(TAG, "Setting subdistrict id=$id")
|
||||
viewModel.getVillages(subdistrictId)
|
||||
viewModel.selectedSubdistrictId = id
|
||||
binding.autoCompleteDesa.text.clear()
|
||||
} ?: Log.e(TAG, "Could not get subsId for position $position")
|
||||
|
||||
subdistrictName?.let { name ->
|
||||
Log.d(TAG, "Setting subdistrict=$name")
|
||||
viewModel.selectedSubdistrict = name
|
||||
} ?: Log.e(TAG, "COuldnt get subs name for position ${position}")
|
||||
}
|
||||
|
||||
binding.autoCompleteDesa.setOnItemClickListener { _, _, position, _ ->
|
||||
val villageId = villageAdapter.getVillageId(position)
|
||||
Log.d(TAG, "Village selected at position $position, villageId=$villageId")
|
||||
|
||||
villageId?.let { id ->
|
||||
Log.d(TAG, "Setting village=$id")
|
||||
viewModel.selectedVillages = id
|
||||
} ?: Log.e(TAG, "Could not get villageId for position $position")
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupButtonListeners() {
|
||||
@ -178,6 +234,16 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
handleCityState(state)
|
||||
}
|
||||
|
||||
viewModel.subdistrictState.observe(this) {state ->
|
||||
Log.d(TAG, "Received subdistrictId update: $state")
|
||||
handleSubdistrictState(state)
|
||||
}
|
||||
|
||||
viewModel.villagesState.observe(this) {state ->
|
||||
Log.d(TAG, "Received subdistrictId update: $state")
|
||||
handleVillageState(state)
|
||||
}
|
||||
|
||||
// Observe address submission
|
||||
viewModel.addressSubmissionState.observe(this) { state ->
|
||||
Log.d(TAG, "Received addressSubmissionState update: $state")
|
||||
@ -202,7 +268,7 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
}
|
||||
is ViewState.Error -> {
|
||||
// Hide loading indicator
|
||||
showError("Failed to load provinces: ${state.message}")
|
||||
// showError("Failed to load provinces: ${state.message}")
|
||||
Log.e("AddAddressActivity", "Province error: ${state.message}")
|
||||
}
|
||||
}
|
||||
@ -221,12 +287,50 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
}
|
||||
is ViewState.Error -> {
|
||||
binding.cityProgressBar.visibility = View.GONE
|
||||
showError("Failed to load cities: ${state.message}")
|
||||
// showError("Failed to load cities: ${state.message}")
|
||||
Log.e("AddAddressActivity", "City error: ${state.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleSubdistrictState(state: com.alya.ecommerce_serang.data.repository.Result<List<SubdistrictsItem>>) {
|
||||
when (state) {
|
||||
is Result.Loading -> {
|
||||
Log.d(TAG, "Loading subdistrict...")
|
||||
binding.subdistrictProgressBar.visibility = View.VISIBLE
|
||||
}
|
||||
is Result.Success -> {
|
||||
Log.d(TAG, "Subdistrict loaded: ${state.data.size}")
|
||||
binding.subdistrictProgressBar.visibility = View.GONE
|
||||
subdistrictAdapter.updateData(state.data)
|
||||
}
|
||||
is Result.Error -> {
|
||||
binding.subdistrictProgressBar.visibility = View.GONE
|
||||
// showError("Failed to load subs: ${state.message}")
|
||||
Log.e(TAG, "Subdistrict error: ${state}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleVillageState(state: Result<List<VillagesItem>>) {
|
||||
when (state) {
|
||||
is Result.Loading -> {
|
||||
Log.d(TAG, "Loading villages...")
|
||||
binding.villageProgressBar.visibility = View.VISIBLE
|
||||
}
|
||||
is Result.Success -> {
|
||||
Log.d(TAG, "Villages loaded: ${state.data.size}")
|
||||
binding.villageProgressBar.visibility = View.GONE
|
||||
villageAdapter.updateData(state.data)
|
||||
}
|
||||
is Result.Error -> {
|
||||
binding.villageProgressBar.visibility = View.GONE
|
||||
// showError("Failed to load subs: ${state.message}")
|
||||
Log.e(TAG, "Village error: ${state}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleAddressSubmissionState(state: ViewState<String>) {
|
||||
when (state) {
|
||||
is ViewState.Loading -> {
|
||||
@ -276,23 +380,20 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
val street = binding.etDetailAlamat.text.toString().trim()
|
||||
val subDistrict = binding.etKecamatan.text.toString().trim()
|
||||
// val subDistrict = binding.etKecamatan.text.toString().trim()
|
||||
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
|
||||
val cityId = viewModel.selectedCityId
|
||||
val cityId = viewModel.selectedCityId.toString()
|
||||
val subDistrict = viewModel.selectedSubdistrict.toString()
|
||||
val villageId = viewModel.selectedVillages.toString()
|
||||
|
||||
Log.d(TAG, "Form data: street=$street, subDistrict=$subDistrict, postalCode=$postalCode, " +
|
||||
"recipient=$recipient, phone=$phone, userId=$userId, provinceId=$provinceId, cityId=$cityId, " +
|
||||
"recipient=$recipient, phone=$phone, userId=$userId, provinceId=$provinceId, cityId=$cityId, subdistrict=$subDistrict, villageId=$villageId " +
|
||||
"lat=$latitude, long=$longitude")
|
||||
|
||||
// Validate required fields
|
||||
@ -333,18 +434,19 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
|
||||
// Create request with all fields
|
||||
val request = CreateAddressRequest(
|
||||
userId = userId!!,
|
||||
lat = latitude!!, // Safe to use !! as we've checked above
|
||||
long = longitude!!,
|
||||
street = street,
|
||||
subDistrict = subDistrict,
|
||||
cityId = cityId,
|
||||
cityId = cityId, // ⚠️ Make sure this is Int
|
||||
provId = provinceId,
|
||||
postCode = postalCode,
|
||||
idVillage = villageId, // Or provide a real ID if needed
|
||||
detailAddress = street,
|
||||
userId = userId,
|
||||
isStoreLocation = false,
|
||||
recipient = recipient,
|
||||
phone = phone,
|
||||
isStoreLocation = isStoreLocation
|
||||
phone = phone
|
||||
)
|
||||
|
||||
Log.d(TAG, "Form validation successful, submitting address: $request")
|
||||
@ -388,8 +490,8 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
binding.locationProgressBar.visibility = View.GONE
|
||||
binding.tvLocationStatus.text = "Provider lokasi tidak tersedia"
|
||||
isRequestingLocation = false
|
||||
Toast.makeText(this, "Provider lokasi tidak tersedia", Toast.LENGTH_SHORT).show()
|
||||
showEnableLocationDialog()
|
||||
// Toast.makeText(this, "Provider lokasi tidak tersedia", Toast.LENGTH_SHORT).show()
|
||||
// showEnableLocationDialog()
|
||||
return
|
||||
}
|
||||
|
||||
@ -416,7 +518,7 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
latitude = -6.200000
|
||||
longitude = 106.816666
|
||||
isRequestingLocation = false
|
||||
Toast.makeText(this, "Timeout lokasi, menggunakan lokasi default", Toast.LENGTH_SHORT).show()
|
||||
// Toast.makeText(this, "Timeout lokasi, menggunakan lokasi default", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}, 60000) // 15 seconds timeout
|
||||
|
||||
@ -430,7 +532,7 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
binding.locationProgressBar.visibility = View.GONE
|
||||
binding.tvLocationStatus.text = "Lokasi terdeteksi: ${lastLocation.latitude}, ${lastLocation.longitude}"
|
||||
isRequestingLocation = false
|
||||
Toast.makeText(this, "Lokasi terdeteksi", Toast.LENGTH_SHORT).show()
|
||||
// Toast.makeText(this, "Lokasi terdeteksi", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
} else {
|
||||
Log.d(TAG, "No last known location, requesting updates")
|
||||
@ -448,7 +550,7 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
binding.locationProgressBar.visibility = View.GONE
|
||||
binding.tvLocationStatus.text = "Lokasi terdeteksi: ${location.latitude}, ${location.longitude}"
|
||||
isRequestingLocation = false
|
||||
Toast.makeText(this@AddAddressActivity, "Lokasi terdeteksi", Toast.LENGTH_SHORT).show()
|
||||
// Toast.makeText(this@AddAddressActivity, "Lokasi terdeteksi", Toast.LENGTH_SHORT).show()
|
||||
|
||||
// Remove location updates after receiving a location
|
||||
try {
|
||||
@ -471,7 +573,7 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
binding.locationProgressBar.visibility = View.GONE
|
||||
binding.tvLocationStatus.text = "Provider lokasi dimatikan"
|
||||
isRequestingLocation = false
|
||||
Toast.makeText(this@AddAddressActivity, "Provider $provider dimatikan", Toast.LENGTH_SHORT).show()
|
||||
// Toast.makeText(this@AddAddressActivity, "Provider $provider dimatikan", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
@ -490,7 +592,7 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
binding.locationProgressBar.visibility = View.GONE
|
||||
binding.tvLocationStatus.text = "Error: ${e.message}"
|
||||
isRequestingLocation = false
|
||||
Toast.makeText(this, "Error mendapatkan lokasi: ${e.message}", Toast.LENGTH_SHORT).show()
|
||||
// Toast.makeText(this, "Error mendapatkan lokasi: ${e.message}", Toast.LENGTH_SHORT).show()
|
||||
|
||||
// Set default location
|
||||
latitude = -6.200000
|
||||
@ -518,12 +620,12 @@ class AddAddressActivity : AppCompatActivity() {
|
||||
// Add button to reload location (add this button to your layout)
|
||||
binding.btnReloadLocation.setOnClickListener {
|
||||
Log.d(TAG, "Reload location button clicked")
|
||||
Toast.makeText(this, "Memuat ulang lokasi...", Toast.LENGTH_SHORT).show()
|
||||
// Toast.makeText(this, "Memuat ulang lokasi...", Toast.LENGTH_SHORT).show()
|
||||
requestLocation()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "AddAddressViewModel"
|
||||
private const val TAG = "AddAddressActivity"
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,9 @@ 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.response.customer.order.SubdistrictsItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.VillagesItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.AddressDetail
|
||||
import com.alya.ecommerce_serang.data.repository.OrderRepository
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.data.repository.UserRepository
|
||||
@ -31,15 +34,31 @@ class AddAddressViewModel(private val repository: OrderRepository, private val u
|
||||
private val _citiesState = MutableLiveData<ViewState<List<CitiesItem>>>()
|
||||
val citiesState: LiveData<ViewState<List<CitiesItem>>> = _citiesState
|
||||
|
||||
private val _subdistrictState = MutableLiveData<Result<List<SubdistrictsItem>>>()
|
||||
val subdistrictState: LiveData<Result<List<SubdistrictsItem>>> = _subdistrictState
|
||||
|
||||
private val _villagesState = MutableLiveData<Result<List<VillagesItem>>>()
|
||||
val villagesState: LiveData<Result<List<VillagesItem>>> = _villagesState
|
||||
|
||||
private val _userAddress = MutableLiveData<AddressDetail>()
|
||||
val userAddress: LiveData<AddressDetail> = _userAddress
|
||||
|
||||
private val _editAddress = MutableLiveData<Boolean>()
|
||||
val editAddress: LiveData<Boolean> get() = _editAddress
|
||||
|
||||
// Stored in SavedStateHandle for configuration changes
|
||||
var selectedProvinceId: Int?
|
||||
get() = savedStateHandle.get<Int>("selectedProvinceId")
|
||||
set(value) { savedStateHandle["selectedProvinceId"] = value }
|
||||
|
||||
var selectedCityId: Int?
|
||||
get() = savedStateHandle.get<Int>("selectedCityId")
|
||||
var selectedCityId: String?
|
||||
get() = savedStateHandle.get<String>("selectedCityId")
|
||||
set(value) { savedStateHandle["selectedCityId"] = value }
|
||||
|
||||
var selectedSubdistrict: String? = null
|
||||
var selectedSubdistrictId: String? = null
|
||||
var selectedVillages: String? = null
|
||||
|
||||
init {
|
||||
// Load provinces on initialization
|
||||
getProvinces()
|
||||
@ -125,20 +144,133 @@ class AddAddressViewModel(private val repository: OrderRepository, private val u
|
||||
}
|
||||
}
|
||||
|
||||
fun getSubdistrict(cityId: String) {
|
||||
_subdistrictState.value = Result.Loading
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
|
||||
selectedSubdistrictId = cityId
|
||||
val result = repository.getListSubdistrict(cityId)
|
||||
result?.let {
|
||||
_subdistrictState.postValue(Result.Success(it.subdistricts))
|
||||
Log.d(TAG, "Subdistrict loaded for city $cityId: ${it.subdistricts.size}")
|
||||
} ?: run {
|
||||
_subdistrictState.postValue(Result.Error(Exception("Failed to load cities")))
|
||||
Log.e(TAG, "Subdistrict result was null for city $cityId")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_subdistrictState.postValue(Result.Error(Exception(e.message ?: "Error loading cities")))
|
||||
Log.e(TAG, "Error fetching subdistrict for city $cityId", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getVillages(subdistrictId: String) {
|
||||
_villagesState.value = Result.Loading
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
|
||||
selectedVillages = subdistrictId
|
||||
val result = repository.getListVillages(subdistrictId)
|
||||
result?.let {
|
||||
_villagesState.postValue(Result.Success(it.villages))
|
||||
Log.d(TAG, "Villages loaded for subdistrict $subdistrictId: ${it.villages.size}")
|
||||
} ?: run {
|
||||
_villagesState.postValue(Result.Error(Exception("Failed to load cities")))
|
||||
Log.e(TAG, "Village result was null for subdistrict $subdistrictId")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_villagesState.postValue(Result.Error(Exception(e.message ?: "Error loading cities")))
|
||||
Log.e(TAG, "Error fetching villages for subdistrict $subdistrictId", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun setSelectedProvinceId(id: Int) {
|
||||
selectedProvinceId = id
|
||||
}
|
||||
|
||||
fun setSelectedCityId(id: Int) {
|
||||
selectedCityId = id
|
||||
fun detailAddress(addressId: Int){
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response = repository.getAddressDetail(addressId)
|
||||
if (response != null){
|
||||
_userAddress.value = response.address
|
||||
} else {
|
||||
Log.e(TAG, "Failed load address detail")
|
||||
}
|
||||
|
||||
} catch (e:Exception){
|
||||
Log.e(TAG, "Error fetching address detail: $e", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun loadUserProfile(){
|
||||
fun updateAddress(oldAddress: AddressDetail, newAddress: AddressDetail) {
|
||||
val params = buildUpdateBody(oldAddress, newAddress)
|
||||
if (params.isEmpty()) {
|
||||
Log.d(TAG, "No changes detected")
|
||||
_editAddress.value = false
|
||||
return
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
when (val result = userRepo.fetchUserProfile()){
|
||||
is Result.Success -> _userProfile.postValue(result.data)
|
||||
is Result.Error -> _errorMessageUser.postValue(result.exception.message ?: "Unknown Error")
|
||||
is Result.Loading -> null
|
||||
try {
|
||||
val response = repository.updateAddress(oldAddress.id, params)
|
||||
_editAddress.value = response.isSuccessful
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error: ${e.message}")
|
||||
_editAddress.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildUpdateBody(oldAddress: AddressDetail, newAddress: AddressDetail): Map<String, Any> {
|
||||
|
||||
val params = mutableMapOf<String, Any>()
|
||||
|
||||
fun addIfChanged(key: String, oldValue: Any?, newValue: Any?) {
|
||||
if (newValue != null && newValue != oldValue) {
|
||||
params[key] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
addIfChanged("street", oldAddress.street, newAddress.street)
|
||||
addIfChanged("province_id", oldAddress.provinceId, newAddress.provinceId)
|
||||
addIfChanged("detail", oldAddress.detail, newAddress.detail)
|
||||
addIfChanged("subdistrict", oldAddress.subdistrict, newAddress.subdistrict)
|
||||
addIfChanged("city_id", oldAddress.cityId, newAddress.cityId)
|
||||
addIfChanged("village_id", oldAddress.villageId, newAddress.villageId)
|
||||
addIfChanged("postal_code", oldAddress.postalCode, newAddress.postalCode)
|
||||
addIfChanged("phone", oldAddress.phone, newAddress.phone)
|
||||
addIfChanged("recipient", oldAddress.recipient, newAddress.recipient)
|
||||
addIfChanged("latitude", oldAddress.latitude, newAddress.latitude)
|
||||
addIfChanged("longitude", oldAddress.longitude, newAddress.longitude)
|
||||
addIfChanged("is_store_location", oldAddress.isStoreLocation, newAddress.isStoreLocation)
|
||||
addIfChanged("village_name", oldAddress.villageName, newAddress.villageName)
|
||||
addIfChanged("subdsitrict_id", oldAddress.subdistrictId, newAddress.subdistrictId)
|
||||
addIfChanged("id", oldAddress.id, newAddress.id)
|
||||
addIfChanged("user_id", oldAddress.userId, newAddress.userId)
|
||||
addIfChanged("city_name", oldAddress.cityName, newAddress.cityName)
|
||||
addIfChanged("province_name", oldAddress.provinceName, newAddress.provinceName)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -74,13 +74,17 @@ class AddressActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun setupRecyclerView() {
|
||||
adapter = AddressAdapter { address ->
|
||||
// Select the address in the ViewModel
|
||||
viewModel.selectAddress(address.id)
|
||||
|
||||
// Return immediately with the selected address
|
||||
returnResultAndFinish(address.id)
|
||||
}
|
||||
adapter = AddressAdapter(
|
||||
onAddressClick = { address ->
|
||||
viewModel.selectAddress(address.id)
|
||||
returnResultAndFinish(address.id)
|
||||
},
|
||||
onEditClick = { address ->
|
||||
val intent = Intent(this, EditAddressActivity::class.java)
|
||||
intent.putExtra(EditAddressActivity.EXTRA_ADDRESS_ID, address.id)
|
||||
startActivity(intent)
|
||||
}
|
||||
)
|
||||
|
||||
binding.rvSellerOrder.apply {
|
||||
layoutManager = LinearLayoutManager(this@AddressActivity)
|
||||
@ -119,6 +123,7 @@ class AddressActivity : AppCompatActivity() {
|
||||
val intent = Intent()
|
||||
intent.putExtra(EXTRA_ADDRESS_ID, addressId)
|
||||
setResult(RESULT_OK, intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@ -3,6 +3,7 @@ package com.alya.ecommerce_serang.ui.order.address
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
@ -13,7 +14,8 @@ import com.alya.ecommerce_serang.data.api.response.customer.profile.AddressesIte
|
||||
import com.google.android.material.card.MaterialCardView
|
||||
|
||||
class AddressAdapter(
|
||||
private val onAddressClick: (AddressesItem) -> Unit
|
||||
private val onAddressClick: (AddressesItem) -> Unit,
|
||||
private val onEditClick: (AddressesItem) -> Unit
|
||||
) : ListAdapter<AddressesItem, AddressAdapter.AddressViewHolder>(DIFF_CALLBACK) {
|
||||
|
||||
private var selectedAddressId: Int? = null
|
||||
@ -47,18 +49,21 @@ class AddressAdapter(
|
||||
// Pass the whole address object to provide more context
|
||||
onAddressClick(address)
|
||||
}
|
||||
holder.editButton.setOnClickListener {
|
||||
onEditClick(address)
|
||||
}
|
||||
}
|
||||
|
||||
class AddressViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
private val tvName: TextView = itemView.findViewById(R.id.tv_name_address)
|
||||
private val tvDetail: TextView = itemView.findViewById(R.id.tv_detail_address)
|
||||
val editButton: ImageView = itemView.findViewById(R.id.iv_edit)
|
||||
private val card: MaterialCardView = itemView as MaterialCardView
|
||||
|
||||
fun bind(address: AddressesItem, isSelected: Boolean) {
|
||||
tvName.text = address.recipient
|
||||
tvDetail.text = "${address.street}, ${address.subdistrict}, ${address.phone}"
|
||||
|
||||
// Make selection more visible
|
||||
card.strokeWidth = if (isSelected) 3 else 0
|
||||
card.strokeColor = if (isSelected)
|
||||
ContextCompat.getColor(itemView.context, R.color.blue_400)
|
||||
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,21 +1,458 @@
|
||||
package com.alya.ecommerce_serang.ui.order.address
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import com.alya.ecommerce_serang.R
|
||||
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.response.customer.order.SubdistrictsItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.VillagesItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.profile.AddressDetail
|
||||
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.OrderRepository
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.data.repository.UserRepository
|
||||
import com.alya.ecommerce_serang.databinding.ActivityEditAddressBinding
|
||||
import com.alya.ecommerce_serang.utils.SavedStateViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
|
||||
class EditAddressActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContentView(R.layout.activity_edit_address)
|
||||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
|
||||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||
insets
|
||||
|
||||
private lateinit var binding: ActivityEditAddressBinding
|
||||
private lateinit var apiService: ApiService
|
||||
private lateinit var sessionManager: SessionManager
|
||||
|
||||
private var latitude: Double? = null
|
||||
private var longitude: Double? = null
|
||||
private var addressId: Int = -1
|
||||
private var currentAddress: AddressDetail? = null
|
||||
private val provinceAdapter by lazy { ProvinceAdapter(this) }
|
||||
private val cityAdapter by lazy { CityAdapter(this) }
|
||||
private val subdistrictAdapter by lazy { SubdsitrictAdapter(this)}
|
||||
private val villageAdapter by lazy { VillagesAdapter(this)}
|
||||
|
||||
private var provincesList = mutableListOf<ProvincesItem>()
|
||||
private var citiesList = mutableListOf<CitiesItem>()
|
||||
private var subdistrictsList = mutableListOf<SubdistrictsItem>()
|
||||
private var villagesList = mutableListOf<VillagesItem>()
|
||||
|
||||
private val viewModel: AddAddressViewModel by viewModels {
|
||||
SavedStateViewModelFactory(this) { savedStateHandle ->
|
||||
val apiService = ApiConfig.getApiService(sessionManager)
|
||||
val orderRepository = OrderRepository(apiService)
|
||||
val userRepository = UserRepository(apiService)
|
||||
AddAddressViewModel(orderRepository, userRepository, savedStateHandle)
|
||||
}
|
||||
}
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityEditAddressBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
sessionManager = SessionManager(this)
|
||||
apiService = ApiConfig.getApiService(sessionManager)
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
|
||||
enableEdgeToEdge()
|
||||
|
||||
// Apply insets to your root layout
|
||||
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
|
||||
val systemBars = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
view.setPadding(
|
||||
systemBars.left,
|
||||
systemBars.top,
|
||||
systemBars.right,
|
||||
systemBars.bottom
|
||||
)
|
||||
windowInsets
|
||||
}
|
||||
|
||||
addressId = intent.getIntExtra(EXTRA_ADDRESS_ID, -1)
|
||||
if (addressId == -1) {
|
||||
Toast.makeText(this, "Gagal mendapatkan alamat pengguna", Toast.LENGTH_SHORT).show()
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
setupToolbar()
|
||||
setupAdapters()
|
||||
setupObservers()
|
||||
setupListeners()
|
||||
|
||||
// Load address detail first
|
||||
Log.d(TAG, "Loading address with ID: $addressId")
|
||||
viewModel.detailAddress(addressId)
|
||||
}
|
||||
|
||||
private fun setupToolbar() {
|
||||
binding.toolbar.setNavigationOnClickListener {
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupAdapters() {
|
||||
// Set custom adapters to AutoCompleteTextViews
|
||||
binding.autoCompleteProvinsi.setAdapter(provinceAdapter)
|
||||
binding.autoCompleteKabupaten.setAdapter(cityAdapter)
|
||||
binding.autoCompleteKecamatan.setAdapter(subdistrictAdapter)
|
||||
binding.autoCompleteDesa.setAdapter(villageAdapter)
|
||||
}
|
||||
|
||||
private fun setupObservers() {
|
||||
// Observe address detail
|
||||
viewModel.userAddress.observe(this) { address ->
|
||||
currentAddress = address
|
||||
Log.d(TAG, "Address loaded: $address")
|
||||
populateAddressData(address)
|
||||
}
|
||||
|
||||
// Observe provinces
|
||||
viewModel.provincesState.observe(this) { viewState ->
|
||||
when (viewState) {
|
||||
is ViewState.Loading -> {
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
}
|
||||
is ViewState.Success -> {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
provincesList.clear()
|
||||
provincesList.addAll(viewState.data)
|
||||
provinceAdapter.updateData(viewState.data)
|
||||
|
||||
// Set selected province if address is loaded
|
||||
currentAddress?.let { address ->
|
||||
setSelectedProvince(address.provinceId.toInt())
|
||||
}
|
||||
}
|
||||
is ViewState.Error -> {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
Log.e(TAG, "Failed to load province ${viewState.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Observe cities
|
||||
viewModel.citiesState.observe(this) { viewState ->
|
||||
when (viewState) {
|
||||
is ViewState.Loading -> {
|
||||
binding.cityProgressBar.visibility = View.VISIBLE
|
||||
}
|
||||
is ViewState.Success -> {
|
||||
binding.cityProgressBar.visibility = View.GONE
|
||||
citiesList.clear()
|
||||
citiesList.addAll(viewState.data)
|
||||
cityAdapter.updateData(viewState.data)
|
||||
|
||||
// Set selected city if address is loaded
|
||||
currentAddress?.let { address ->
|
||||
setSelectedCity(address.cityId)
|
||||
}
|
||||
}
|
||||
is ViewState.Error -> {
|
||||
binding.cityProgressBar.visibility = View.GONE
|
||||
Log.e(TAG, "Failed to load cities ${viewState.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Observe subdistricts
|
||||
viewModel.subdistrictState.observe(this) { result ->
|
||||
when (result) {
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Loading -> {
|
||||
// You can add loading indicator for subdistrict if needed
|
||||
}
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Success -> {
|
||||
subdistrictsList.clear()
|
||||
subdistrictsList.addAll(result.data)
|
||||
subdistrictAdapter.updateData(result.data)
|
||||
|
||||
// Set selected subdistrict if address is loaded
|
||||
currentAddress?.let { address ->
|
||||
setSelectedSubdistrict(address.subdistrict)
|
||||
}
|
||||
}
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Error -> {
|
||||
Log.e(TAG, "Failed to load subdistricy")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Observe villages
|
||||
viewModel.villagesState.observe(this) { result ->
|
||||
when (result) {
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Loading -> {
|
||||
// You can add loading indicator for village if needed
|
||||
}
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Success -> {
|
||||
villagesList.clear()
|
||||
villagesList.addAll(result.data)
|
||||
villageAdapter.updateData(result.data)
|
||||
|
||||
// Set selected village if address is loaded
|
||||
currentAddress?.let { address ->
|
||||
setSelectedVillage(address.villageId)
|
||||
}
|
||||
}
|
||||
is Result.Error -> {
|
||||
Log.e(TAG, "Failed to load villages")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Observe update result
|
||||
viewModel.editAddress.observe(this) { isSuccess ->
|
||||
binding.submitProgressBar.visibility = View.GONE
|
||||
binding.buttonSimpan.isEnabled = true
|
||||
|
||||
if (isSuccess) {
|
||||
Log.d(TAG, "Address updated successfully")
|
||||
finish()
|
||||
} else {
|
||||
Log.d(TAG, "Failed to update address")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupListeners() {
|
||||
// Province selection
|
||||
binding.autoCompleteProvinsi.setOnItemClickListener { _, _, position, _ ->
|
||||
val provinceId = provinceAdapter.getProvinceId(position)
|
||||
provinceId?.let { id ->
|
||||
viewModel.getCities(id)
|
||||
|
||||
// Clear dependent dropdowns
|
||||
clearCitySelection()
|
||||
clearSubdistrictSelection()
|
||||
clearVillageSelection()
|
||||
}
|
||||
}
|
||||
|
||||
// City selection
|
||||
binding.autoCompleteKabupaten.setOnItemClickListener { _, _, position, _ ->
|
||||
val cityId = cityAdapter.getCityId(position)
|
||||
cityId?.let { id ->
|
||||
viewModel.getSubdistrict(id)
|
||||
|
||||
// Clear dependent dropdowns
|
||||
clearSubdistrictSelection()
|
||||
clearVillageSelection()
|
||||
}
|
||||
}
|
||||
|
||||
// Subdistrict selection
|
||||
binding.autoCompleteKecamatan.setOnItemClickListener { _, _, position, _ ->
|
||||
val subdistrictId = subdistrictAdapter.getSubdistrictId(position)
|
||||
subdistrictId?.let { id ->
|
||||
viewModel.getVillages(id)
|
||||
|
||||
// Clear dependent dropdowns
|
||||
clearVillageSelection()
|
||||
}
|
||||
}
|
||||
|
||||
// Village selection - auto-populate postal code
|
||||
binding.autoCompleteDesa.setOnItemClickListener { _, _, position, _ ->
|
||||
// val postalCode = villageAdapter.getPostalCode(position)
|
||||
// postalCode?.let {
|
||||
// binding.etKodePos.setText(it)
|
||||
// }
|
||||
}
|
||||
|
||||
// Save button
|
||||
binding.buttonSimpan.setOnClickListener {
|
||||
saveAddress()
|
||||
}
|
||||
}
|
||||
|
||||
private fun populateAddressData(address: AddressDetail) {
|
||||
binding.etNamaPenerima.setText(address.recipient ?: "")
|
||||
binding.etNomorHp.setText(address.phone ?: "")
|
||||
binding.etDetailAlamat.setText(address.detail ?: "")
|
||||
binding.etKodePos.setText(address.postalCode ?: "")
|
||||
|
||||
// Province will be set when provinces are loaded
|
||||
// City, subdistrict, village will be set in their respective observers
|
||||
}
|
||||
|
||||
private fun setSelectedProvince(provinceId: Int?) {
|
||||
provinceId?.let { id ->
|
||||
val position = provincesList.indexOfFirst { it.provinceId?.toIntOrNull() == id }
|
||||
if (position >= 0) {
|
||||
val provinceName = provincesList[position].province
|
||||
binding.autoCompleteProvinsi.setText(provinceName, false)
|
||||
viewModel.getCities(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setSelectedCity(cityId: String?) {
|
||||
cityId?.let { id ->
|
||||
val position = citiesList.indexOfFirst { it.cityId?.toString() == id }
|
||||
if (position >= 0) {
|
||||
val cityName = citiesList[position].cityName
|
||||
binding.autoCompleteKabupaten.setText(cityName, false)
|
||||
viewModel.getSubdistrict(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setSelectedSubdistrict(subdistrictId: String?) {
|
||||
subdistrictId?.let { id ->
|
||||
val position = subdistrictsList.indexOfFirst { it.subdistrictId?.toString() == id }
|
||||
if (position >= 0) {
|
||||
val subdistrictName = subdistrictsList[position].subdistrictName
|
||||
binding.autoCompleteKecamatan.setText(subdistrictName, false)
|
||||
viewModel.getVillages(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setSelectedVillage(villageId: String?) {
|
||||
villageId?.let { id ->
|
||||
val position = villagesList.indexOfFirst { it.villageId?.toString() == id }
|
||||
if (position >= 0) {
|
||||
val villageName = villagesList[position].villageName
|
||||
binding.autoCompleteDesa.setText(villageName, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// private fun populatePostalCodeFromVillage(villageId: String?) {
|
||||
// villageId?.let { id ->
|
||||
// val village = villagesList.find { it.villageId?.toString() == id }
|
||||
// village?.postalCode?.let { postalCode ->
|
||||
// if (binding.etKodePos.text.isNullOrEmpty()) {
|
||||
// binding.etKodePos.setText(postalCode)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
private fun clearCitySelection() {
|
||||
binding.autoCompleteKabupaten.setText("", false)
|
||||
citiesList.clear()
|
||||
cityAdapter.updateData(emptyList())
|
||||
}
|
||||
|
||||
private fun clearSubdistrictSelection() {
|
||||
binding.autoCompleteKecamatan.setText("", false)
|
||||
subdistrictsList.clear()
|
||||
subdistrictAdapter.updateData(emptyList())
|
||||
}
|
||||
|
||||
private fun clearVillageSelection() {
|
||||
binding.autoCompleteDesa.setText("", false)
|
||||
binding.etKodePos.setText("") // Clear postal code when village is cleared
|
||||
villagesList.clear()
|
||||
villageAdapter.updateData(emptyList())
|
||||
}
|
||||
|
||||
private fun saveAddress() {
|
||||
currentAddress?.let { oldAddress ->
|
||||
val newAddress = createNewAddressFromInputs(oldAddress)
|
||||
|
||||
binding.submitProgressBar.visibility = View.VISIBLE
|
||||
binding.buttonSimpan.isEnabled = false
|
||||
|
||||
viewModel.updateAddress(oldAddress, newAddress)
|
||||
} ?: run {
|
||||
Log.d(TAG, "Address not loaded")
|
||||
Toast.makeText(this, "Gagal mendapatkan alamat", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createNewAddressFromInputs(oldAddress: AddressDetail): AddressDetail {
|
||||
val selectedProvinceId = getSelectedProvinceId()
|
||||
val selectedCityId = getSelectedCityId()
|
||||
val selectedSubdistrictName = getSelectedSubdistrictName()
|
||||
val selectedVillageId = getSelectedVillageId()
|
||||
val selectedSubdistrictId = getSelectedSubdistrictId()
|
||||
val selectedVillageName = getSelectedVillageName()
|
||||
val selectedProvinceName = getSelectedProvinceName()
|
||||
|
||||
Log.d(TAG, "Old subdistrict: ${oldAddress.subdistrict}")
|
||||
Log.d(TAG, "Selected subdistrictId: $selectedSubdistrictName")
|
||||
|
||||
val newAddress = oldAddress.copy(
|
||||
recipient = binding.etNamaPenerima.text.toString().trim(),
|
||||
phone = binding.etNomorHp.text.toString().trim(),
|
||||
detail = binding.etDetailAlamat.text.toString().trim(),
|
||||
postalCode = binding.etKodePos.text.toString().trim(),
|
||||
provinceId = selectedProvinceId?.toString() ?: oldAddress.provinceId,
|
||||
cityId = selectedCityId ?: oldAddress.cityId,
|
||||
subdistrict = selectedSubdistrictName ?: oldAddress.subdistrict,
|
||||
villageId = selectedVillageId,
|
||||
subdistrictId = selectedSubdistrictId ?: oldAddress.subdistrictId,
|
||||
villageName = selectedVillageName ?: oldAddress.villageName,
|
||||
provinceName = selectedProvinceName ?: oldAddress.provinceName
|
||||
)
|
||||
|
||||
// 🔎 Debug logs
|
||||
Log.d(TAG, "New subdistrict: ${newAddress.subdistrict}")
|
||||
Log.d(TAG, "New village name: ${newAddress.villageName}")
|
||||
|
||||
|
||||
return newAddress
|
||||
}
|
||||
|
||||
private fun getSelectedProvinceId(): Int? {
|
||||
val selectedText = binding.autoCompleteProvinsi.text.toString()
|
||||
val position = provincesList.indexOfFirst { it.province == selectedText }
|
||||
return if (position >= 0) provinceAdapter.getProvinceId(position) else null
|
||||
}
|
||||
|
||||
private fun getSelectedProvinceName(): String? {
|
||||
val selectedText = binding.autoCompleteProvinsi.text.toString()
|
||||
val position = provincesList.indexOfFirst { it.province == selectedText }
|
||||
return if (position >= 0) provinceAdapter.getProvinceName(position) else null
|
||||
}
|
||||
|
||||
private fun getSelectedCityId(): String? {
|
||||
val selectedText = binding.autoCompleteKabupaten.text.toString()
|
||||
val position = citiesList.indexOfFirst { it.cityName == selectedText }
|
||||
return if (position >= 0) cityAdapter.getCityId(position) else null
|
||||
}
|
||||
|
||||
private fun getSelectedCityName(): String? {
|
||||
val selectedText = binding.autoCompleteKabupaten.text.toString()
|
||||
val position = citiesList.indexOfFirst { it.cityName == selectedText }
|
||||
return if (position >= 0) cityAdapter.getCityName(position) else null
|
||||
}
|
||||
|
||||
private fun getSelectedSubdistrictName(): String? {
|
||||
val selectedText = binding.autoCompleteKecamatan.text.toString()
|
||||
val position = subdistrictsList.indexOfFirst { it.subdistrictName == selectedText }
|
||||
return if (position >= 0) subdistrictAdapter.getSubdistrictName(position) else null
|
||||
}
|
||||
|
||||
private fun getSelectedSubdistrictId(): String? {
|
||||
val selectedText = binding.autoCompleteKecamatan.text.toString()
|
||||
val position = subdistrictsList.indexOfFirst { it.subdistrictName == selectedText }
|
||||
return if (position >= 0) subdistrictAdapter.getSubdistrictId(position) else null
|
||||
}
|
||||
|
||||
private fun getSelectedVillageId(): String? {
|
||||
val selectedText = binding.autoCompleteDesa.text.toString()
|
||||
val position = villagesList.indexOfFirst { it.villageName == selectedText }
|
||||
return if (position >= 0) villageAdapter.getVillageId(position) else null
|
||||
}
|
||||
|
||||
private fun getSelectedVillageName(): String? {
|
||||
val selectedText = binding.autoCompleteDesa.text.toString()
|
||||
val position = villagesList.indexOfFirst { it.villageName == selectedText }
|
||||
return if (position >= 0) villageAdapter.getVillageName(position) else null
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val EXTRA_ADDRESS_ID = "extra_address_id"
|
||||
private const val TAG = "EditAddressActivity"
|
||||
}
|
||||
|
||||
}
|
||||
@ -3,8 +3,11 @@ package com.alya.ecommerce_serang.ui.order.address
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.Spinner
|
||||
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.response.customer.order.SubdistrictsItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.VillagesItem
|
||||
|
||||
// UI adapters and helpers
|
||||
class ProvinceAdapter(
|
||||
@ -12,6 +15,7 @@ class ProvinceAdapter(
|
||||
resource: Int = android.R.layout.simple_dropdown_item_1line
|
||||
) : ArrayAdapter<String>(context, resource, ArrayList()) {
|
||||
|
||||
//call from endpoint
|
||||
private val provinces = ArrayList<ProvincesItem>()
|
||||
|
||||
fun updateData(newProvinces: List<ProvincesItem>) {
|
||||
@ -28,6 +32,10 @@ class ProvinceAdapter(
|
||||
fun getProvinceId(position: Int): Int? {
|
||||
return provinces.getOrNull(position)?.provinceId?.toIntOrNull()
|
||||
}
|
||||
|
||||
fun getProvinceName(position: Int): String? {
|
||||
return provinces.getOrNull(position)?.province?.toString()
|
||||
}
|
||||
}
|
||||
|
||||
class CityAdapter(
|
||||
@ -46,7 +54,135 @@ class CityAdapter(
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
fun getCityId(position: Int): Int? {
|
||||
return cities.getOrNull(position)?.cityId?.toIntOrNull()
|
||||
fun getCityId(position: Int): String? {
|
||||
return cities.getOrNull(position)?.cityId?.toString()
|
||||
}
|
||||
|
||||
fun getCityName(position: Int): String? {
|
||||
return cities.getOrNull(position)?.cityName?.toString()
|
||||
}
|
||||
}
|
||||
|
||||
class SubdsitrictAdapter(
|
||||
context: Context,
|
||||
resource: Int = android.R.layout.simple_dropdown_item_1line
|
||||
) : ArrayAdapter<String>(context, resource, ArrayList()) {
|
||||
|
||||
private val cities = ArrayList<SubdistrictsItem>()
|
||||
|
||||
fun updateData(newCities: List<SubdistrictsItem>) {
|
||||
cities.clear()
|
||||
cities.addAll(newCities)
|
||||
|
||||
clear()
|
||||
addAll(cities.map { it.subdistrictName })
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
fun getSubdistrictId(position: Int): String? {
|
||||
return cities.getOrNull(position)?.subdistrictId?.toString()
|
||||
}
|
||||
|
||||
fun getSubdistrictName(position: Int): String? {
|
||||
return cities.getOrNull(position)?.subdistrictName?.toString()
|
||||
}
|
||||
}
|
||||
|
||||
class VillagesAdapter(
|
||||
context: Context,
|
||||
resource: Int = android.R.layout.simple_dropdown_item_1line
|
||||
) : ArrayAdapter<String>(context, resource, ArrayList()) {
|
||||
|
||||
private val villages = ArrayList<VillagesItem>()
|
||||
|
||||
fun updateData(newCities: List<VillagesItem>) {
|
||||
villages.clear()
|
||||
villages.addAll(newCities)
|
||||
|
||||
clear()
|
||||
addAll(villages.map { it.villageName })
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
fun getVillageId(position: Int): String? {
|
||||
return villages.getOrNull(position)?.villageId?.toString()
|
||||
}
|
||||
|
||||
fun getVillageName(position: Int): String? {
|
||||
return villages.getOrNull(position)?.villageName.toString()
|
||||
}
|
||||
fun getPostalCode(position: Int): String?{
|
||||
return villages.getOrNull(position)?.postalCode
|
||||
}
|
||||
}
|
||||
|
||||
class BankAdapter(
|
||||
context: Context,
|
||||
resource: Int = android.R.layout.simple_dropdown_item_1line
|
||||
) : ArrayAdapter<String>(context, resource, ArrayList()) {
|
||||
|
||||
data class BankItem(
|
||||
val bankName: String,
|
||||
val bankCode: String? = null,
|
||||
val description: String? = null
|
||||
)
|
||||
|
||||
private val banks = ArrayList<BankItem>()
|
||||
|
||||
init {
|
||||
loadHardcodedData()
|
||||
}
|
||||
|
||||
private fun loadHardcodedData() {
|
||||
val defaultBanks = listOf(
|
||||
BankItem("Bank Mandiri", "008", "PT Bank Mandiri (Persero) Tbk"),
|
||||
BankItem("Bank BRI", "002", "PT Bank Rakyat Indonesia (Persero) Tbk"),
|
||||
BankItem("Bank BCA", "014", "PT Bank Central Asia Tbk"),
|
||||
BankItem("Bank BNI", "009", "PT Bank Negara Indonesia (Persero) Tbk"),
|
||||
BankItem("Bank BTN", "200", "PT Bank Tabungan Negara (Persero) Tbk"),
|
||||
BankItem("Bank CIMB Niaga", "022", "PT Bank CIMB Niaga Tbk"),
|
||||
BankItem("Bank Danamon", "011", "PT Bank Danamon Indonesia Tbk"),
|
||||
BankItem("Bank Permata", "013", "PT Bank Permata Tbk"),
|
||||
BankItem("Bank OCBC NISP", "028", "PT Bank OCBC NISP Tbk"),
|
||||
BankItem("Bank Maybank", "016", "PT Bank Maybank Indonesia Tbk"),
|
||||
BankItem("Bank Panin", "019", "PT Bank Panin Dubai Syariah Tbk"),
|
||||
BankItem("Bank UOB", "023", "PT Bank UOB Indonesia"),
|
||||
BankItem("Bank Mega", "426", "PT Bank Mega Tbk"),
|
||||
BankItem("Bank Bukopin", "441", "PT Bank Bukopin Tbk"),
|
||||
BankItem("Bank BJB", "110", "PT Bank Pembangunan Daerah Jawa Barat dan Banten Tbk")
|
||||
)
|
||||
updateData(defaultBanks)
|
||||
}
|
||||
|
||||
fun updateData(newBanks: List<BankItem>) {
|
||||
banks.clear()
|
||||
banks.addAll(newBanks)
|
||||
|
||||
clear()
|
||||
addAll(banks.map { it.bankName })
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
fun getBankName(position: Int): String? {
|
||||
return banks.getOrNull(position)?.bankName
|
||||
}
|
||||
|
||||
fun getBankItem(position: Int): BankItem? {
|
||||
return banks.getOrNull(position)
|
||||
}
|
||||
|
||||
fun getBankCode(position: Int): String? {
|
||||
return banks.getOrNull(position)?.bankCode
|
||||
}
|
||||
|
||||
fun findPositionByName(bankName: String): Int {
|
||||
return banks.indexOfFirst { it.bankName == bankName }
|
||||
}
|
||||
|
||||
fun setDefaultSelection(spinner: Spinner, defaultBankName: String) {
|
||||
val position = findPositionByName(defaultBankName)
|
||||
if (position >= 0) {
|
||||
spinner.setSelection(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -39,7 +39,6 @@ import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import java.io.File
|
||||
import java.text.NumberFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Locale
|
||||
@ -63,7 +62,6 @@ class AddEvidencePaymentActivity : AppCompatActivity() {
|
||||
|
||||
private val paymentMethods = arrayOf(
|
||||
"Transfer Bank",
|
||||
"E-Wallet",
|
||||
"QRIS",
|
||||
)
|
||||
|
||||
@ -129,7 +127,7 @@ class AddEvidencePaymentActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun setupUI() {
|
||||
val paymentMethods = listOf("Transfer Bank", "COD", "QRIS")
|
||||
val paymentMethods = listOf("Transfer Bank", "QRIS")
|
||||
val adapter = SpinnerCardAdapter(this, paymentMethods)
|
||||
binding.spinnerPaymentMethod.adapter = adapter
|
||||
}
|
||||
@ -320,11 +318,12 @@ class AddEvidencePaymentActivity : AppCompatActivity() {
|
||||
Toast.makeText(this, "Silahkan pilih metode pembayaran", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
binding.etAccountNumber.visibility = View.GONE
|
||||
|
||||
if (binding.etAccountNumber.text.toString().trim().isEmpty()) {
|
||||
Toast.makeText(this, "Silahkan isi nomor rekening/HP", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
// if (binding.etAccountNumber.text.toString().trim().isEmpty()) {
|
||||
// Toast.makeText(this, "Silahkan isi nomor rekening/HP", Toast.LENGTH_SHORT).show()
|
||||
// return
|
||||
// }
|
||||
|
||||
if (binding.tvPaymentDate.text.toString() == "Pilih tanggal") {
|
||||
Toast.makeText(this, "Silahkan pilih tanggal pembayaran", Toast.LENGTH_SHORT).show()
|
||||
|
||||
@ -10,6 +10,7 @@ import com.alya.ecommerce_serang.data.api.dto.CompletedOrderRequest
|
||||
import com.alya.ecommerce_serang.data.api.dto.OrdersItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.CancelOrderResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.OrderListItemsItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.OrderListResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.Orders
|
||||
import com.alya.ecommerce_serang.data.api.response.order.CompletedOrderResponse
|
||||
import com.alya.ecommerce_serang.data.repository.OrderRepository
|
||||
@ -18,6 +19,13 @@ import com.alya.ecommerce_serang.ui.order.address.ViewState
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
import java.text.SimpleDateFormat
|
||||
@ -29,8 +37,8 @@ class HistoryViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
private const val TAG = "HistoryViewModel"
|
||||
}
|
||||
|
||||
private val _orders = MutableLiveData<ViewState<List<OrdersItem>>>()
|
||||
val orders: LiveData<ViewState<List<OrdersItem>>> = _orders
|
||||
// private val _orders = MutableLiveData<ViewState<List<OrdersItem>>>()
|
||||
// val orders: LiveData<ViewState<List<OrdersItem>>> = _orders
|
||||
|
||||
private val _orderCompletionStatus = MutableLiveData<Result<CompletedOrderResponse>>()
|
||||
val orderCompletionStatus: LiveData<Result<CompletedOrderResponse>> = _orderCompletionStatus
|
||||
@ -59,81 +67,156 @@ class HistoryViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
private val _error = MutableLiveData<String>()
|
||||
val error: LiveData<String> get() = _error
|
||||
|
||||
fun getOrderList(status: String) {
|
||||
_orders.value = ViewState.Loading
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
if (status == "all") {
|
||||
// Get all orders by combining all statuses
|
||||
getAllOrdersCombined()
|
||||
} else {
|
||||
// Get orders for specific status
|
||||
when (val result = repository.getOrderList(status)) {
|
||||
is Result.Success -> {
|
||||
_orders.value = ViewState.Success(result.data.orders)
|
||||
Log.d(TAG, "Orders loaded successfully: ${result.data.orders.size} items")
|
||||
private val _selectedStatus = MutableStateFlow("all")
|
||||
val selectedStatus: StateFlow<String> = _selectedStatus.asStateFlow()
|
||||
|
||||
val orders: StateFlow<ViewState<List<OrdersItem>>> =
|
||||
_selectedStatus
|
||||
.flatMapLatest { status ->
|
||||
flow<ViewState<List<OrdersItem>>> {
|
||||
Log.d(TAG, "⏳ Loading orders for status = $status")
|
||||
emit(ViewState.Loading)
|
||||
|
||||
val viewState =
|
||||
if (status == "all") {
|
||||
getAllOrdersCombined().also {
|
||||
Log.d(TAG, "✅ Combined orders size = ${(it as? ViewState.Success)?.data?.size}")
|
||||
}
|
||||
} else {
|
||||
when (val r = repository.getOrderList(status)) {
|
||||
|
||||
is Result.Loading -> {
|
||||
Log.d(TAG, " repository.getOrderList($status) → Loading")
|
||||
ViewState.Loading
|
||||
}
|
||||
|
||||
is Result.Success -> {
|
||||
Log.d(TAG, "✅ repository.getOrderList($status) success, size = ${r.data.orders.size}")
|
||||
// Tag each order so the fragment’s filter works
|
||||
val tagged = r.data.orders.onEach { it.displayStatus = status }
|
||||
ViewState.Success(tagged)
|
||||
}
|
||||
|
||||
is Result.Error -> {
|
||||
Log.e(TAG, "❌ repository.getOrderList($status) error = ${r.exception.message}")
|
||||
ViewState.Error(r.exception.message ?: "Unknown error")
|
||||
}
|
||||
}
|
||||
}
|
||||
is Result.Error -> {
|
||||
_orders.value = ViewState.Error(result.exception.message ?: "Unknown error occurred")
|
||||
Log.e(TAG, "Error loading orders", result.exception)
|
||||
}
|
||||
is Result.Loading -> {
|
||||
// Keep loading state
|
||||
}
|
||||
}
|
||||
|
||||
emit(viewState)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_orders.value = ViewState.Error("An unexpected error occurred: ${e.message}")
|
||||
Log.e(TAG, "Exception in getOrderList", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
.stateIn(
|
||||
viewModelScope,
|
||||
SharingStarted.WhileSubscribed(5_000),
|
||||
ViewState.Loading // ② initial value, still fine
|
||||
)
|
||||
|
||||
private suspend fun getAllOrdersCombined() {
|
||||
try {
|
||||
val allStatuses = listOf("unpaid", "paid", "processed", "shipped", "completed", "canceled")
|
||||
val allOrders = mutableListOf<OrdersItem>()
|
||||
|
||||
// Use coroutineScope to allow launching async blocks
|
||||
coroutineScope {
|
||||
val deferreds = allStatuses.map { status ->
|
||||
// fun getOrderList(status: String) {
|
||||
// _orders.value = ViewState.Loading
|
||||
// viewModelScope.launch {
|
||||
// try {
|
||||
// if (status == "all") {
|
||||
// // Get all orders by combining all statuses
|
||||
// getAllOrdersCombined()
|
||||
// } else {
|
||||
// // Get orders for specific status
|
||||
// when (val result = repository.getOrderList(status)) {
|
||||
// is Result.Success -> {
|
||||
// _orders.value = ViewState.Success(result.data.orders)
|
||||
// Log.d(TAG, "Orders loaded successfully: ${result.data.orders.size} items")
|
||||
// }
|
||||
// is Result.Error -> {
|
||||
// _orders.value = ViewState.Error(result.exception.message ?: "Unknown error occurred")
|
||||
// Log.e(TAG, "Error loading orders", result.exception)
|
||||
// }
|
||||
// is Result.Loading -> {
|
||||
// // Keep loading state
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// _orders.value = ViewState.Error("An unexpected error occurred: ${e.message}")
|
||||
// Log.e(TAG, "Exception in getOrderList", e)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// private suspend fun getAllOrdersCombined() {
|
||||
// try {
|
||||
// val allStatuses = listOf("unpaid", "paid", "processed", "shipped", "completed", "canceled")
|
||||
// val allOrders = mutableListOf<OrdersItem>()
|
||||
//
|
||||
// // Use coroutineScope to allow launching async blocks
|
||||
// coroutineScope {
|
||||
// val deferreds = allStatuses.map { status ->
|
||||
// async {
|
||||
// when (val result = repository.getOrderList(status)) {
|
||||
// is Result.Success -> {
|
||||
// // Tag each order with the status it was fetched from
|
||||
// result.data.orders.onEach { it.displayStatus = status }
|
||||
// }
|
||||
// is Result.Error -> {
|
||||
// Log.e(TAG, "Error loading orders for status $status", result.exception)
|
||||
// emptyList<OrdersItem>()
|
||||
// }
|
||||
// is Result.Loading -> emptyList<OrdersItem>()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Await all results and combine
|
||||
// deferreds.awaitAll().forEach { orders ->
|
||||
// allOrders.addAll(orders)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Sort orders
|
||||
// val sortedOrders = allOrders.sortedByDescending { order ->
|
||||
// try {
|
||||
// SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).parse(order.createdAt)
|
||||
// } catch (e: Exception) {
|
||||
// null
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// _orders.value = ViewState.Success(sortedOrders)
|
||||
// Log.d(TAG, "All orders loaded successfully: ${sortedOrders.size} items")
|
||||
//
|
||||
// } catch (e: Exception) {
|
||||
// _orders.value = ViewState.Error("An unexpected error occurred: ${e.message}")
|
||||
// Log.e(TAG, "Exception in getAllOrdersCombined", e)
|
||||
// }
|
||||
// }
|
||||
private suspend fun getAllOrdersCombined(): ViewState<List<OrdersItem>> = try {
|
||||
val statuses = listOf("unpaid", "paid", "processed", "shipped", "completed", "canceled")
|
||||
|
||||
val all = coroutineScope {
|
||||
statuses
|
||||
.map { status ->
|
||||
async {
|
||||
when (val result = repository.getOrderList(status)) {
|
||||
is Result.Success -> {
|
||||
// Tag each order with the status it was fetched from
|
||||
result.data.orders.onEach { it.displayStatus = status }
|
||||
}
|
||||
is Result.Error -> {
|
||||
Log.e(TAG, "Error loading orders for status $status", result.exception)
|
||||
emptyList<OrdersItem>()
|
||||
}
|
||||
is Result.Loading -> emptyList<OrdersItem>()
|
||||
when (val r = repository.getOrderList(status)) {
|
||||
is Result.Success -> r.data.orders.onEach { it.displayStatus = status }
|
||||
else -> emptyList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Await all results and combine
|
||||
deferreds.awaitAll().forEach { orders ->
|
||||
allOrders.addAll(orders)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort orders
|
||||
val sortedOrders = allOrders.sortedByDescending { order ->
|
||||
try {
|
||||
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).parse(order.createdAt)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
_orders.value = ViewState.Success(sortedOrders)
|
||||
Log.d(TAG, "All orders loaded successfully: ${sortedOrders.size} items")
|
||||
|
||||
} catch (e: Exception) {
|
||||
_orders.value = ViewState.Error("An unexpected error occurred: ${e.message}")
|
||||
Log.e(TAG, "Exception in getAllOrdersCombined", e)
|
||||
.awaitAll()
|
||||
.flatten()
|
||||
}
|
||||
|
||||
val sorted = all.sortedByDescending { order ->
|
||||
try {
|
||||
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
|
||||
.parse(order.createdAt)
|
||||
} catch (_: Exception) { null }
|
||||
}
|
||||
|
||||
ViewState.Success(sorted)
|
||||
} catch (e: Exception) {
|
||||
ViewState.Error("Failed to load orders: ${e.message}")
|
||||
}
|
||||
|
||||
fun confirmOrderCompleted(orderId: Int, status: String) {
|
||||
@ -209,9 +292,52 @@ class HistoryViewModel(private val repository: OrderRepository) : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
fun refreshOrders(status: String = "all") {
|
||||
Log.d(TAG, "Refreshing orders with status: $status")
|
||||
// Don't set Loading here if you want to show current data while refreshing
|
||||
getOrderList(status)
|
||||
// fun refreshOrders(status: String = "all") {
|
||||
// Log.d(TAG, "Refreshing orders with status: $status")
|
||||
// // Don't set Loading here if you want to show current data while refreshing
|
||||
// getOrderList(status)
|
||||
// }
|
||||
|
||||
fun updateStatus(status: String, forceRefresh: Boolean = false) {
|
||||
Log.d(TAG, "↪️ updateStatus(status = $status, forceRefresh = $forceRefresh)")
|
||||
|
||||
// No‑op guard (optional): skip if user re‑selects same tab and no refresh asked
|
||||
if (_selectedStatus.value == status && !forceRefresh) {
|
||||
Log.d(TAG, "🔸 Status unchanged & forceRefresh = false → skip update")
|
||||
return
|
||||
}
|
||||
|
||||
_selectedStatus.value = status
|
||||
Log.d(TAG, "✅ _selectedStatus set to \"$status\"")
|
||||
|
||||
if (forceRefresh) {
|
||||
Log.d(TAG, "🔄 forceRefresh = true → launching refresh()")
|
||||
viewModelScope.launch { refresh(status) }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun refresh(status: String) {
|
||||
Log.d(TAG, "⏳ refresh(\"$status\") started")
|
||||
|
||||
try {
|
||||
if (status == "all") {
|
||||
Log.d(TAG, "🌐 Calling getAllOrdersCombined()")
|
||||
getAllOrdersCombined() // network → cache
|
||||
} else {
|
||||
Log.d(TAG, "🌐 repository.getOrderList(\"$status\")")
|
||||
repository.getOrderList(status) // network → cache
|
||||
}
|
||||
Log.d(TAG, "✅ refresh(\"$status\") completed (repository updated)")
|
||||
// Flow that watches DB/cache will emit automatically
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "❌ refresh(\"$status\") failed: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Result<OrderListResponse>.toViewState(): ViewState<List<OrdersItem>> =
|
||||
when (this) {
|
||||
is Result.Success -> ViewState.Success(data.orders)
|
||||
is Result.Error -> ViewState.Error(exception.message ?: "Unknown error")
|
||||
is Result.Loading -> ViewState.Loading // should rarely reach UI
|
||||
}
|
||||
}
|
||||
@ -32,6 +32,7 @@ import com.google.android.material.button.MaterialButton
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import com.google.gson.Gson
|
||||
import java.io.File
|
||||
import java.text.NumberFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Locale
|
||||
@ -88,7 +89,8 @@ class OrderHistoryAdapter(
|
||||
tvStoreName.text = storeName
|
||||
|
||||
// Set total amount
|
||||
tvTotalAmount.text = order.totalAmount
|
||||
tvTotalAmount.text = formatCurrency(order.totalAmount.toDouble())
|
||||
|
||||
|
||||
// Set item count
|
||||
val itemCount = order.orderItems.size
|
||||
@ -195,7 +197,7 @@ class OrderHistoryAdapter(
|
||||
text = itemView.context.getString(R.string.canceled_order_btn)
|
||||
setOnClickListener {
|
||||
showCancelOrderDialog(order.orderId.toString())
|
||||
viewModel.refreshOrders()
|
||||
// viewModel.refreshOrders()
|
||||
}
|
||||
}
|
||||
// deadlineDate.apply {
|
||||
@ -213,14 +215,15 @@ class OrderHistoryAdapter(
|
||||
visibility = View.VISIBLE
|
||||
text = itemView.context.getString(R.string.dl_processed)
|
||||
}
|
||||
btnLeft.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = itemView.context.getString(R.string.canceled_order_btn)
|
||||
setOnClickListener {
|
||||
showCancelOrderDialog(order.orderId.toString())
|
||||
viewModel.refreshOrders()
|
||||
}
|
||||
}
|
||||
// gabisa complaint
|
||||
// btnLeft.apply {
|
||||
// visibility = View.VISIBLE
|
||||
// text = itemView.context.getString(R.string.canceled_order_btn)
|
||||
// setOnClickListener {
|
||||
// showCancelOrderDialog(order.orderId.toString())
|
||||
// viewModel.refreshOrders()
|
||||
// }
|
||||
// }
|
||||
}
|
||||
"shipped" -> {
|
||||
// Untuk status shipped, tampilkan "Lacak Pengiriman" dan "Terima Barang"
|
||||
@ -237,7 +240,7 @@ class OrderHistoryAdapter(
|
||||
text = itemView.context.getString(R.string.claim_complaint)
|
||||
setOnClickListener {
|
||||
showCancelOrderDialog(order.orderId.toString())
|
||||
viewModel.refreshOrders()
|
||||
// viewModel.refreshOrders()
|
||||
}
|
||||
}
|
||||
btnRight.apply {
|
||||
@ -248,7 +251,7 @@ class OrderHistoryAdapter(
|
||||
|
||||
// Call ViewModel
|
||||
viewModel.confirmOrderCompleted(order.orderId, "completed")
|
||||
viewModel.refreshOrders()
|
||||
// viewModel.refreshOrders()
|
||||
|
||||
}
|
||||
|
||||
@ -268,13 +271,21 @@ class OrderHistoryAdapter(
|
||||
text = itemView.context.getString(R.string.dl_shipped)
|
||||
}
|
||||
btnRight.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = itemView.context.getString(R.string.add_review)
|
||||
setOnClickListener {
|
||||
addReviewProduct(order)
|
||||
viewModel.refreshOrders()
|
||||
// Handle click event
|
||||
val checkReview = order.orderItems[0].reviewId
|
||||
if (checkReview > 0){
|
||||
visibility = View.VISIBLE
|
||||
text = itemView.context.getString(R.string.add_review)
|
||||
setOnClickListener {
|
||||
|
||||
addReviewProduct(order)
|
||||
// viewModel.refreshOrders()
|
||||
// Handle click event
|
||||
}
|
||||
} else {
|
||||
visibility = View.GONE
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
deadlineDate.apply {
|
||||
visibility = View.VISIBLE
|
||||
@ -518,7 +529,7 @@ class OrderHistoryAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
// Create and show the bottom sheet using the obtained FragmentManager
|
||||
// cancel sebelum bayar
|
||||
val bottomSheet = CancelOrderBottomSheet(
|
||||
orderId = orderId,
|
||||
onOrderCancelled = {
|
||||
@ -531,6 +542,7 @@ class OrderHistoryAdapter(
|
||||
bottomSheet.show(fragmentActivity.supportFragmentManager, CancelOrderBottomSheet.TAG)
|
||||
}
|
||||
|
||||
// tambah review / ulasan
|
||||
private fun addReviewProduct(order: OrdersItem) {
|
||||
// Use ViewModel to fetch order details
|
||||
viewModel.getOrderDetails(order.orderId)
|
||||
@ -550,7 +562,7 @@ class OrderHistoryAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
// Observe the order details result
|
||||
// Observe order items
|
||||
viewModel.orderItems.observe(itemView.findViewTreeLifecycleOwner()!!) { orderItems ->
|
||||
if (orderItems != null && orderItems.isNotEmpty()) {
|
||||
// For single item review
|
||||
@ -589,6 +601,11 @@ class OrderHistoryAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatCurrency(amount: Double): String {
|
||||
val formatter = NumberFormat.getCurrencyInstance(Locale("in", "ID"))
|
||||
return formatter.format(amount).replace(",00", "")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@ -5,8 +5,13 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.viewpager2.widget.ViewPager2
|
||||
import com.alya.ecommerce_serang.R
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
import com.alya.ecommerce_serang.data.repository.OrderRepository
|
||||
import com.alya.ecommerce_serang.databinding.FragmentOrderHistoryBinding
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.google.android.material.tabs.TabLayoutMediator
|
||||
|
||||
@ -16,6 +21,12 @@ class OrderHistoryFragment : Fragment() {
|
||||
private val binding get() = _binding!!
|
||||
private lateinit var sessionManager: SessionManager
|
||||
|
||||
private val historyVm: HistoryViewModel by activityViewModels {
|
||||
BaseViewModelFactory {
|
||||
val api = ApiConfig.getApiService(SessionManager(requireContext()))
|
||||
HistoryViewModel(OrderRepository(api))
|
||||
}
|
||||
}
|
||||
|
||||
private lateinit var viewPagerAdapter: OrderViewPagerAdapter
|
||||
|
||||
@ -33,6 +44,8 @@ class OrderHistoryFragment : Fragment() {
|
||||
sessionManager = SessionManager(requireContext())
|
||||
|
||||
setupViewPager()
|
||||
|
||||
|
||||
}
|
||||
|
||||
private fun setupViewPager() {
|
||||
@ -53,6 +66,16 @@ class OrderHistoryFragment : Fragment() {
|
||||
else -> "Tab $position"
|
||||
}
|
||||
}.attach()
|
||||
|
||||
binding.viewPager.registerOnPageChangeCallback(
|
||||
object : ViewPager2.OnPageChangeCallback() {
|
||||
override fun onPageSelected(position: Int) {
|
||||
val status = viewPagerAdapter.orderStatuses[position]
|
||||
/* setStatus() is the API we added earlier; TRUE → always re‑query */
|
||||
historyVm.updateStatus(status, forceRefresh = true)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
|
||||
@ -8,8 +8,10 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.alya.ecommerce_serang.data.api.dto.OrdersItem
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
@ -27,17 +29,26 @@ class OrderListFragment : Fragment(), OrderHistoryAdapter.OrderActionCallbacks {
|
||||
private val binding get() = _binding!!
|
||||
private lateinit var sessionManager: SessionManager
|
||||
|
||||
private val viewModel: HistoryViewModel by viewModels {
|
||||
private val viewModel: HistoryViewModel by activityViewModels {
|
||||
BaseViewModelFactory {
|
||||
val apiService = ApiConfig.getApiService(sessionManager)
|
||||
val orderRepository = OrderRepository(apiService)
|
||||
HistoryViewModel(orderRepository)
|
||||
val api = ApiConfig.getApiService(SessionManager(requireContext()))
|
||||
HistoryViewModel(OrderRepository(api))
|
||||
}
|
||||
}
|
||||
|
||||
private lateinit var orderAdapter: OrderHistoryAdapter
|
||||
|
||||
private var status: String = "all"
|
||||
|
||||
private val detailOrderLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
/* force‑refresh the current tab */
|
||||
viewModel.updateStatus(status, forceRefresh = true)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ARG_STATUS = "status"
|
||||
|
||||
@ -73,8 +84,8 @@ class OrderListFragment : Fragment(), OrderHistoryAdapter.OrderActionCallbacks {
|
||||
setupRecyclerView()
|
||||
observeOrderList()
|
||||
observeViewModel()
|
||||
observeOrderCompletionStatus()
|
||||
loadOrders()
|
||||
// observeOrderCompletionStatus()
|
||||
// loadOrders()
|
||||
}
|
||||
|
||||
private fun setupRecyclerView() {
|
||||
@ -96,27 +107,50 @@ class OrderListFragment : Fragment(), OrderHistoryAdapter.OrderActionCallbacks {
|
||||
|
||||
private fun observeOrderList() {
|
||||
// Now we only need to observe one LiveData for all cases
|
||||
viewModel.orders.observe(viewLifecycleOwner) { result ->
|
||||
when (result) {
|
||||
is ViewState.Success -> {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
|
||||
if (result.data.isNullOrEmpty()) {
|
||||
binding.tvEmptyState.visibility = View.VISIBLE
|
||||
binding.rvOrders.visibility = View.GONE
|
||||
} else {
|
||||
binding.tvEmptyState.visibility = View.GONE
|
||||
binding.rvOrders.visibility = View.VISIBLE
|
||||
orderAdapter.submitList(result.data)
|
||||
// viewModel.orders.observe(viewLifecycleOwner) { result ->
|
||||
// when (result) {
|
||||
// is ViewState.Success -> {
|
||||
// binding.progressBar.visibility = View.GONE
|
||||
//
|
||||
// if (result.data.isNullOrEmpty()) {
|
||||
// binding.tvEmptyState.visibility = View.VISIBLE
|
||||
// binding.rvOrders.visibility = View.GONE
|
||||
// } else {
|
||||
// binding.tvEmptyState.visibility = View.GONE
|
||||
// binding.rvOrders.visibility = View.VISIBLE
|
||||
// orderAdapter.submitList(result.data)
|
||||
// }
|
||||
// }
|
||||
// is ViewState.Error -> {
|
||||
// binding.progressBar.visibility = View.GONE
|
||||
// binding.tvEmptyState.visibility = View.VISIBLE
|
||||
// Toast.makeText(requireContext(), result.message, Toast.LENGTH_SHORT).show()
|
||||
// }
|
||||
// is ViewState.Loading -> {
|
||||
// binding.progressBar.visibility = View.VISIBLE
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
|
||||
viewModel.orders.collect { state ->
|
||||
when (state) {
|
||||
is ViewState.Loading -> {
|
||||
binding.progressBar.isVisible = true
|
||||
}
|
||||
is ViewState.Error -> {
|
||||
binding.progressBar.isVisible = false
|
||||
binding.tvEmptyState.isVisible = true
|
||||
binding.rvOrders.isVisible = false
|
||||
Toast.makeText(requireContext(), state.message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
is ViewState.Success -> {
|
||||
binding.progressBar.isVisible = false
|
||||
val list = state.data
|
||||
.filter { status == "all" || it.displayStatus == status }
|
||||
binding.tvEmptyState.isVisible = list.isEmpty()
|
||||
binding.rvOrders.isVisible = list.isNotEmpty()
|
||||
orderAdapter.submitList(list)
|
||||
}
|
||||
}
|
||||
is ViewState.Error -> {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
binding.tvEmptyState.visibility = View.VISIBLE
|
||||
Toast.makeText(requireContext(), result.message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
is ViewState.Loading -> {
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -124,51 +158,78 @@ class OrderListFragment : Fragment(), OrderHistoryAdapter.OrderActionCallbacks {
|
||||
|
||||
private fun observeViewModel() {
|
||||
// Observe order completion
|
||||
// viewModel.orderCompletionStatus.observe(viewLifecycleOwner) { result ->
|
||||
// when (result) {
|
||||
// is Result.Success -> {
|
||||
// Toast.makeText(requireContext(), "Order completed successfully!", Toast.LENGTH_SHORT).show()
|
||||
//// loadOrders() // Refresh here
|
||||
// }
|
||||
// is Result.Error -> {
|
||||
// Toast.makeText(requireContext(), "Failed: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
// }
|
||||
// is Result.Loading -> {
|
||||
// // Show loading if needed
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Observe cancel order status
|
||||
// viewModel.cancelOrderStatus.observe(viewLifecycleOwner) { result ->
|
||||
// when (result) {
|
||||
// is Result.Success -> {
|
||||
// Toast.makeText(requireContext(), "Order cancelled successfully!", Toast.LENGTH_SHORT).show()
|
||||
// loadOrders() // Refresh here
|
||||
// }
|
||||
// is Result.Error -> {
|
||||
// Toast.makeText(requireContext(), "Failed to cancel: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
// }
|
||||
// is Result.Loading -> {
|
||||
// // Show loading if needed
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
viewModel.orderCompletionStatus.observe(viewLifecycleOwner) { result ->
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
Toast.makeText(requireContext(), "Order completed successfully!", Toast.LENGTH_SHORT).show()
|
||||
loadOrders() // Refresh here
|
||||
}
|
||||
is Result.Error -> {
|
||||
Toast.makeText(requireContext(), "Failed: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
is Result.Loading -> {
|
||||
// Show loading if needed
|
||||
Toast.makeText(requireContext(),
|
||||
"Order completed!", Toast.LENGTH_SHORT).show()
|
||||
viewModel.updateStatus(status, forceRefresh = true)
|
||||
}
|
||||
is Result.Error ->
|
||||
Toast.makeText(requireContext(),
|
||||
"Failed: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
else -> { /* Loading → no UI change */ }
|
||||
}
|
||||
}
|
||||
|
||||
// Observe cancel order status
|
||||
viewModel.cancelOrderStatus.observe(viewLifecycleOwner) { result ->
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
Toast.makeText(requireContext(), "Order cancelled successfully!", Toast.LENGTH_SHORT).show()
|
||||
loadOrders() // Refresh here
|
||||
}
|
||||
is Result.Error -> {
|
||||
Toast.makeText(requireContext(), "Failed to cancel: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
is Result.Loading -> {
|
||||
// Show loading if needed
|
||||
Toast.makeText(requireContext(),
|
||||
"Order cancelled!", Toast.LENGTH_SHORT).show()
|
||||
viewModel.updateStatus(status, forceRefresh = true)
|
||||
}
|
||||
is Result.Error ->
|
||||
Toast.makeText(requireContext(),
|
||||
"Failed: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
else -> { /* Loading */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadOrders() {
|
||||
// Simple - just call getOrderList for any status including "all"
|
||||
viewModel.getOrderList(status)
|
||||
}
|
||||
// private fun loadOrders() {
|
||||
// // Simple - just call getOrderList for any status including "all"
|
||||
// viewModel.getOrderList(status)
|
||||
// }
|
||||
|
||||
private val detailOrderLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
// Refresh order list when returning with OK result
|
||||
loadOrders()
|
||||
}
|
||||
}
|
||||
// private val detailOrderLauncher = registerForActivityResult(
|
||||
// ActivityResultContracts.StartActivityForResult()
|
||||
// ) { result ->
|
||||
// if (result.resultCode == Activity.RESULT_OK) {
|
||||
// // Refresh order list when returning with OK result
|
||||
//// loadOrders()
|
||||
// }
|
||||
// }
|
||||
|
||||
private fun navigateToOrderDetail(order: OrdersItem) {
|
||||
val intent = Intent(requireContext(), DetailOrderStatusActivity::class.java).apply {
|
||||
@ -183,7 +244,9 @@ class OrderListFragment : Fragment(), OrderHistoryAdapter.OrderActionCallbacks {
|
||||
override fun onOrderCancelled(orderId: String, success: Boolean, message: String) {
|
||||
if (success) {
|
||||
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
|
||||
loadOrders() // Refresh the list
|
||||
// loadOrders() // Refresh the list
|
||||
if (success) viewModel.updateStatus(status, forceRefresh = true)
|
||||
|
||||
} else {
|
||||
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
@ -192,7 +255,8 @@ class OrderListFragment : Fragment(), OrderHistoryAdapter.OrderActionCallbacks {
|
||||
override fun onOrderCompleted(orderId: Int, success: Boolean, message: String) {
|
||||
if (success) {
|
||||
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
|
||||
loadOrders() // Refresh the list
|
||||
// loadOrders() // Refresh the list
|
||||
if (success) viewModel.updateStatus(status, forceRefresh = true)
|
||||
} else {
|
||||
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
@ -207,20 +271,20 @@ class OrderListFragment : Fragment(), OrderHistoryAdapter.OrderActionCallbacks {
|
||||
_binding = null
|
||||
}
|
||||
|
||||
private fun observeOrderCompletionStatus() {
|
||||
viewModel.orderCompletionStatus.observe(viewLifecycleOwner) { result ->
|
||||
when (result) {
|
||||
is Result.Loading -> {
|
||||
// Handle loading state if needed
|
||||
}
|
||||
is Result.Success -> {
|
||||
Toast.makeText(requireContext(), "Order completed successfully!", Toast.LENGTH_SHORT).show()
|
||||
loadOrders()
|
||||
}
|
||||
is Result.Error -> {
|
||||
Toast.makeText(requireContext(), "Failed to complete order: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// private fun observeOrderCompletionStatus() {
|
||||
// viewModel.orderCompletionStatus.observe(viewLifecycleOwner) { result ->
|
||||
// when (result) {
|
||||
// is Result.Loading -> {
|
||||
// // Handle loading state if needed
|
||||
// }
|
||||
// is Result.Success -> {
|
||||
// Toast.makeText(requireContext(), "Order completed successfully!", Toast.LENGTH_SHORT).show()
|
||||
//// loadOrders()
|
||||
// }
|
||||
// is Result.Error -> {
|
||||
// Toast.makeText(requireContext(), "Failed to complete order: ${result.exception.message}", Toast.LENGTH_SHORT).show()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@ -10,6 +10,8 @@ import com.alya.ecommerce_serang.BuildConfig.BASE_URL
|
||||
import com.alya.ecommerce_serang.R
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.OrderItemsItem
|
||||
import com.bumptech.glide.Glide
|
||||
import java.text.NumberFormat
|
||||
import java.util.Locale
|
||||
|
||||
class OrderProductAdapter : RecyclerView.Adapter<OrderProductAdapter.ProductViewHolder>() {
|
||||
|
||||
@ -46,7 +48,7 @@ class OrderProductAdapter : RecyclerView.Adapter<OrderProductAdapter.ProductView
|
||||
tvQuantity.text = "${product.quantity} buah"
|
||||
|
||||
// Set price with currency format
|
||||
tvProductPrice.text = formatCurrency(product.price)
|
||||
tvProductPrice.text = formatCurrency(product.price.toDouble())
|
||||
|
||||
val fullImageUrl = when (val img = product.productImage) {
|
||||
is String -> {
|
||||
@ -65,10 +67,9 @@ class OrderProductAdapter : RecyclerView.Adapter<OrderProductAdapter.ProductView
|
||||
|
||||
|
||||
|
||||
private fun formatCurrency(amount: Int): String {
|
||||
// In a real app, you would use NumberFormat for proper currency formatting
|
||||
// For simplicity, just return a basic formatted string
|
||||
return "Rp${amount}"
|
||||
private fun formatCurrency(amount: Double): String {
|
||||
val formatter = NumberFormat.getCurrencyInstance(Locale("in", "ID"))
|
||||
return formatter.format(amount).replace(",00", "")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -9,7 +9,7 @@ class OrderViewPagerAdapter(
|
||||
) : FragmentStateAdapter(fragmentActivity) {
|
||||
|
||||
// Define all possible order statuses
|
||||
private val orderStatuses = listOf(
|
||||
val orderStatuses = listOf(
|
||||
"all", // All orders
|
||||
"unpaid", // Menunggu Tagihan
|
||||
"paid", // Belum Dibayar
|
||||
|
||||
@ -55,7 +55,7 @@ class CancelOrderBottomSheet(
|
||||
val btnConfirm = view.findViewById<Button>(R.id.btn_confirm)
|
||||
|
||||
// Set the title
|
||||
tvTitle.text = "Cancel Order #$orderId"
|
||||
tvTitle.text = "Batalkan Pesanan #$orderId"
|
||||
|
||||
// Set up the spinner with cancellation reasons
|
||||
setupReasonSpinner(spinnerReason)
|
||||
@ -94,11 +94,11 @@ class CancelOrderBottomSheet(
|
||||
private fun getCancellationReasons(): List<CancelOrderReq> {
|
||||
// These should ideally come from the server or a configuration
|
||||
return listOf(
|
||||
CancelOrderReq(1, "Changed my mind"),
|
||||
CancelOrderReq(2, "Found a better option"),
|
||||
CancelOrderReq(3, "Ordered by mistake"),
|
||||
CancelOrderReq(4, "Delivery time too long"),
|
||||
CancelOrderReq(5, "Other reason")
|
||||
CancelOrderReq(1, "Berubah pikiran"),
|
||||
CancelOrderReq(2, "Menemukan pilihan yang lebih baik"),
|
||||
CancelOrderReq(3, "Kesalahan pemesanan"),
|
||||
CancelOrderReq(4, "Waktu pengiriman lama"),
|
||||
CancelOrderReq(5, "Lainnya")
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -44,6 +44,9 @@ import com.google.gson.Gson
|
||||
import java.io.File
|
||||
import java.text.NumberFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.Calendar
|
||||
import java.util.Locale
|
||||
import java.util.TimeZone
|
||||
@ -197,12 +200,12 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
Log.d(TAG, "populateOrderDetails: Payment method=${orders.payInfoName ?: "Tidak tersedia"}")
|
||||
|
||||
// Set subtotal, shipping cost, and total
|
||||
val subtotal = orders.totalAmount?.minus(orders.shipmentPrice.toIntOrNull() ?: 0) ?: 0
|
||||
binding.tvSubtotal.text = formatCurrency(subtotal.toDouble())
|
||||
// val subtotal = orders.totalAmount?.minus(orders.shipmentPrice.toDouble() ?: 0) ?: 0
|
||||
// binding.tvSubtotal.text = formatCurrency(subtotal.toDouble())
|
||||
binding.tvShippingCost.text = formatCurrency(orders.shipmentPrice.toDouble())
|
||||
binding.tvTotal.text = formatCurrency(orders.totalAmount?.toDouble() ?: 0.00)
|
||||
|
||||
Log.d(TAG, "populateOrderDetails: Subtotal=$subtotal, Shipping=${orders.shipmentPrice}, Total=${orders.totalAmount}")
|
||||
Log.d(TAG, "populateOrderDetails: Subtotal=, Shipping=${orders.shipmentPrice}, Total=${orders.totalAmount}")
|
||||
|
||||
// Adjust buttons based on order status
|
||||
Log.d(TAG, "populateOrderDetails: Adjusting buttons for status=$orderStatus")
|
||||
@ -223,6 +226,11 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
this.adapter = adapter
|
||||
}
|
||||
adapter.submitList(orderItems)
|
||||
|
||||
// get data from ordetlistitemsitem untuk ambil subtotal nya dan dijumlahkan
|
||||
val subtotalSum = orderItems.sumOf { it.subtotal }
|
||||
binding.tvSubtotal.text = formatCurrency(subtotalSum.toDouble())
|
||||
|
||||
}
|
||||
|
||||
private fun adjustButtonsBasedOnStatus(orders: Orders, status: String) {
|
||||
@ -287,20 +295,20 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
// Show status note
|
||||
binding.tvStatusHeader.text = "Sudah Dibayar"
|
||||
binding.tvStatusNote.visibility = View.VISIBLE
|
||||
binding.tvStatusNote.text = "Menunggu pesanan dikonfirmasi penjual ${formatDatePay(orders.updatedAt)}"
|
||||
binding.tvStatusNote.text = "Menunggu pesanan dikonfirmasi penjual ${formatDatePaid(orders.updatedAt)}"
|
||||
binding.tvPaymentDeadlineLabel.text = "Batas konfirmasi penjual:"
|
||||
binding.tvPaymentDeadline.text = formatDatePay(orders.updatedAt)
|
||||
binding.tvPaymentDeadline.text = formatDatePaid(orders.updatedAt)
|
||||
|
||||
// Set buttons
|
||||
binding.btnSecondary.apply {
|
||||
visibility = View.VISIBLE
|
||||
text = "Batalkan Pesanan"
|
||||
setOnClickListener {
|
||||
Log.d(TAG, "Cancel Order button clicked")
|
||||
showCancelOrderDialog(orders.orderId.toString())
|
||||
viewModel.getOrderDetails(orders.orderId)
|
||||
}
|
||||
}
|
||||
// cancel pesanan
|
||||
// binding.btnSecondary.apply {
|
||||
// visibility = View.VISIBLE
|
||||
// text = "Batalkan Pesanan"
|
||||
// setOnClickListener {
|
||||
// Log.d(TAG, "Cancel Order button clicked")
|
||||
// showCancelOrderDialog(orders.orderId.toString())
|
||||
// viewModel.getOrderDetails(orders.orderId)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
"processed" -> {
|
||||
Log.d(TAG, "adjustButtonsBasedOnStatus: Setting up UI for processed order")
|
||||
@ -309,7 +317,7 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
binding.tvStatusNote.visibility = View.VISIBLE
|
||||
binding.tvStatusNote.text = "Penjual sedang memproses pesanan Anda"
|
||||
binding.tvPaymentDeadlineLabel.text = "Batas diproses penjual:"
|
||||
binding.tvPaymentDeadline.text = formatDatePay(orders.updatedAt)
|
||||
binding.tvPaymentDeadline.text = formatDateProcessed(orders.updatedAt)
|
||||
|
||||
binding.btnSecondary.apply {
|
||||
visibility = View.VISIBLE
|
||||
@ -333,7 +341,7 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
binding.tvStatusNote.visibility = View.VISIBLE
|
||||
binding.tvStatusNote.text = "Pesanan Anda sedang dalam perjalanan. Akan sampai sekitar ${formatShipmentDate(orders.updatedAt, orders.etd ?: "0")}"
|
||||
binding.tvPaymentDeadlineLabel.text = "Estimasi pesanan sampai:"
|
||||
binding.tvPaymentDeadline.text = formatShipmentDate(orders.updatedAt, orders.etd ?: "0")
|
||||
binding.tvPaymentDeadline.text = formatShipmentDate(orders.autoCompletedAt, orders.etd ?: "0")
|
||||
|
||||
binding.btnSecondary.apply {
|
||||
visibility = View.VISIBLE
|
||||
@ -367,7 +375,7 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
binding.tvStatusHeader.text = "Pesanan Selesai"
|
||||
binding.tvStatusNote.visibility = View.GONE
|
||||
binding.tvPaymentDeadlineLabel.text = "Pesanan selesai:"
|
||||
binding.tvPaymentDeadline.text = formatDate(orders.autoCompletedAt.toString())
|
||||
binding.tvPaymentDeadline.text = formatDate(orders.updatedAt.toString())
|
||||
|
||||
binding.btnPrimary.apply {
|
||||
visibility = View.VISIBLE
|
||||
@ -386,7 +394,7 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
"canceled" -> {
|
||||
Log.d(TAG, "adjustButtonsBasedOnStatus: Setting up UI for canceled order")
|
||||
|
||||
binding.tvStatusHeader.text = "Pesanan Selesai"
|
||||
binding.tvStatusHeader.text = "Pesanan Dibatalkan"
|
||||
binding.tvStatusNote.visibility = View.VISIBLE
|
||||
binding.tvStatusNote.text = "Pesanan dibatalkan: ${orders.cancelReason ?: "Alasan tidak diberikan"}"
|
||||
binding.tvPaymentDeadlineLabel.text = "Tanggal dibatalkan: "
|
||||
@ -598,10 +606,6 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
val bottomSheet = CancelOrderBottomSheet(
|
||||
orderId = orderId,
|
||||
onOrderCancelled = {
|
||||
// Handle the successful cancellation
|
||||
// Refresh the data
|
||||
|
||||
// Show a success message
|
||||
Toast.makeText(this, "Order cancelled successfully", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
)
|
||||
@ -610,32 +614,17 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun formatDate(dateString: String): String {
|
||||
Log.d(TAG, "formatDate: Formatting date: $dateString")
|
||||
|
||||
return try {
|
||||
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
|
||||
inputFormat.timeZone = TimeZone.getTimeZone("UTC")
|
||||
val jakarta = ZoneId.of("Asia/Jakarta")
|
||||
val instant = Instant.parse(dateString) // parses ISO‑8601 with ‘Z’
|
||||
val zoned = instant.atZone(jakarta)
|
||||
|
||||
val timeFormat = SimpleDateFormat("HH:mm", Locale("id", "ID"))
|
||||
val dateFormat = SimpleDateFormat("dd MMMM yyyy", Locale("id", "ID"))
|
||||
val time = DateTimeFormatter.ofPattern("HH:mm", Locale("id", "ID")).format(zoned)
|
||||
val date = DateTimeFormatter.ofPattern("dd MMMM yyyy",Locale("id", "ID")).format(zoned)
|
||||
|
||||
val date = inputFormat.parse(dateString)
|
||||
|
||||
date?.let {
|
||||
val calendar = Calendar.getInstance()
|
||||
calendar.time = it
|
||||
calendar.set(Calendar.HOUR_OF_DAY, 23)
|
||||
calendar.set(Calendar.MINUTE, 59)
|
||||
|
||||
val timePart = timeFormat.format(calendar.time)
|
||||
val datePart = dateFormat.format(calendar.time)
|
||||
|
||||
val formatted = "$timePart\n$datePart"
|
||||
Log.d(TAG, "formatDate: Formatted date: $formatted")
|
||||
formatted
|
||||
} ?: dateString
|
||||
"$time\n$date"
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "formatDate: Error formatting date: ${e.message}", e)
|
||||
Log.e(TAG, "formatDate: $e")
|
||||
dateString
|
||||
}
|
||||
}
|
||||
@ -673,6 +662,73 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatDatePaid(dateString: String): String {
|
||||
Log.d(TAG, "formatDatePay: Formatting payment date: $dateString")
|
||||
|
||||
return try {
|
||||
// Parse the ISO 8601 date
|
||||
val isoDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
|
||||
isoDateFormat.timeZone = TimeZone.getTimeZone("UTC")
|
||||
|
||||
val createdDate = isoDateFormat.parse(dateString)
|
||||
|
||||
// Add 24 hours to get due date
|
||||
val calendar = Calendar.getInstance()
|
||||
calendar.time = createdDate
|
||||
calendar.add(Calendar.HOUR, 120)
|
||||
val dueDate = calendar.time
|
||||
|
||||
val timeFormat = SimpleDateFormat("HH:mm", Locale("id", "ID"))
|
||||
val dateFormat = SimpleDateFormat("dd MMM yyyy", Locale("id", "ID"))
|
||||
|
||||
val timePart = timeFormat.format(dueDate)
|
||||
val datePart = dateFormat.format(dueDate)
|
||||
|
||||
val formatted = "$timePart\n$datePart"
|
||||
|
||||
Log.d(TAG, "formatDatePay: Formatted payment date: $formatted")
|
||||
formatted
|
||||
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "formatDatePay: Error formatting date: ${e.message}", e)
|
||||
dateString
|
||||
}
|
||||
}
|
||||
|
||||
//format batas tgl diproses
|
||||
private fun formatDateProcessed(dateString: String): String {
|
||||
Log.d(TAG, "formatDatePay: Formatting payment date: $dateString")
|
||||
|
||||
return try {
|
||||
// Parse the ISO 8601 date
|
||||
val isoDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
|
||||
isoDateFormat.timeZone = TimeZone.getTimeZone("UTC")
|
||||
|
||||
val createdDate = isoDateFormat.parse(dateString)
|
||||
|
||||
// Add 24 hours to get due date
|
||||
val calendar = Calendar.getInstance()
|
||||
calendar.time = createdDate
|
||||
calendar.add(Calendar.HOUR, 72)
|
||||
val dueDate = calendar.time
|
||||
|
||||
val timeFormat = SimpleDateFormat("HH:mm", Locale("id", "ID"))
|
||||
val dateFormat = SimpleDateFormat("dd MMM yyyy", Locale("id", "ID"))
|
||||
|
||||
val timePart = timeFormat.format(dueDate)
|
||||
val datePart = dateFormat.format(dueDate)
|
||||
|
||||
val formatted = "$timePart\n$datePart"
|
||||
|
||||
Log.d(TAG, "formatDatePay: Formatted payment date: $formatted")
|
||||
formatted
|
||||
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "formatDatePay: Error formatting date: ${e.message}", e)
|
||||
dateString
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatShipmentDate(dateString: String, estimateString: String): String {
|
||||
Log.d(TAG, "formatShipmentDate: Formatting shipment date: $dateString with ETD: $estimateString")
|
||||
|
||||
@ -696,7 +752,6 @@ class DetailOrderStatusActivity : AppCompatActivity() {
|
||||
calendar.time = it
|
||||
|
||||
// Add estimated days
|
||||
calendar.add(Calendar.DAY_OF_MONTH, estimate)
|
||||
val formatted = outputFormat.format(calendar.time)
|
||||
|
||||
Log.d(TAG, "formatShipmentDate: Estimated arrival date: $formatted")
|
||||
|
||||
@ -75,6 +75,7 @@ class DetailOrderViewModel(private val orderRepository: OrderRepository): ViewMo
|
||||
orderRepository.submitComplaint(orderId.toString(), reason, imageFile)
|
||||
_isSuccess.value = true
|
||||
_message.value = "Order canceled successfully"
|
||||
Log.d("DetailOrderViewModel", "Complaint order success")
|
||||
|
||||
} catch (e: Exception) {
|
||||
_isSuccess.value = false
|
||||
|
||||
@ -164,6 +164,7 @@ class DetailProductActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
//info toko
|
||||
private fun updateStoreInfo(store: StoreItem?) {
|
||||
store?.let {
|
||||
binding.tvSellerName.text = it.storeName
|
||||
@ -230,9 +231,8 @@ class DetailProductActivity : AppCompatActivity() {
|
||||
|
||||
private fun updateUI(product: Product){
|
||||
binding.tvProductName.text = product.productName
|
||||
binding.tvPrice.text = "Rp${formatCurrency(product.price.toDouble())}"
|
||||
binding.tvPrice.text = formatCurrency(product.price.toDouble())
|
||||
binding.tvSold.text = "Terjual ${product.totalSold} buah"
|
||||
binding.tvRating.text = product.rating
|
||||
binding.tvWeight.text = "${product.weight} gram"
|
||||
binding.tvStock.text = "${product.stock} buah"
|
||||
binding.tvCategory.text = product.productCategory
|
||||
@ -243,7 +243,7 @@ class DetailProductActivity : AppCompatActivity() {
|
||||
isWholesaleSelected = false // Default to regular pricing
|
||||
if (isWholesaleAvailable) {
|
||||
binding.containerWholesale.visibility = View.VISIBLE
|
||||
binding.tvPriceWholesale.text = "Rp${formatCurrency(product.wholesalePrice!!.toDouble())}"
|
||||
binding.tvPriceWholesale.text = formatCurrency(product.wholesalePrice!!.toDouble())
|
||||
binding.descMinOrder.text = "Minimal pembelian ${minOrder}"
|
||||
} else {
|
||||
binding.containerWholesale.visibility = View.GONE
|
||||
@ -281,6 +281,17 @@ class DetailProductActivity : AppCompatActivity() {
|
||||
.load(fullImageUrl)
|
||||
.placeholder(R.drawable.placeholder_image)
|
||||
.into(binding.ivProductImage)
|
||||
|
||||
val ratingStr = product.rating
|
||||
val ratingValue = ratingStr?.toFloatOrNull()
|
||||
|
||||
if (ratingValue != null && ratingValue > 0f) {
|
||||
binding.tvRating.text = String.format("%.1f", ratingValue)
|
||||
binding.tvRating.visibility = View.VISIBLE
|
||||
} else {
|
||||
binding.tvRating.text = "Belum ada rating"
|
||||
binding.tvRating.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleAllReviewsClick(productId: Int) {
|
||||
@ -347,6 +358,7 @@ class DetailProductActivity : AppCompatActivity() {
|
||||
|
||||
}
|
||||
|
||||
//dialog tambah quantity dan harga grosir
|
||||
private fun showQuantityDialog(productId: Int, isBuyNow: Boolean) {
|
||||
val bottomSheetDialog = BottomSheetDialog(this)
|
||||
val view = layoutInflater.inflate(R.layout.dialog_count_buy, null)
|
||||
@ -377,10 +389,9 @@ class DetailProductActivity : AppCompatActivity() {
|
||||
switchWholesale.visibility = View.VISIBLE
|
||||
Toast.makeText(this, "Minimal pembelian grosir $currentQuantity produk", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
titleWholesale.visibility = View.GONE
|
||||
switchWholesale.visibility = View.GONE
|
||||
}
|
||||
// Set initial quantity based on current selection
|
||||
|
||||
|
||||
switchWholesale.setOnCheckedChangeListener { _, isChecked ->
|
||||
isWholesaleSelected = isChecked
|
||||
|
||||
@ -2,6 +2,7 @@ package com.alya.ecommerce_serang.ui.product
|
||||
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
@ -35,7 +36,16 @@ class OtherProductAdapter (
|
||||
|
||||
tvProductName.text = product.name
|
||||
tvProductPrice.text = formatCurrency(product.price.toDouble())
|
||||
rating.text = product.rating
|
||||
val ratingStr = product.rating
|
||||
val ratingValue = ratingStr?.toFloatOrNull()
|
||||
|
||||
if (ratingValue != null && ratingValue > 0f) {
|
||||
binding.rating.text = String.format("%.1f", ratingValue)
|
||||
binding.rating.visibility = View.VISIBLE
|
||||
} else {
|
||||
binding.rating.text = "Belum ada rating"
|
||||
binding.rating.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
|
||||
}
|
||||
|
||||
// Load image using Glide
|
||||
Glide.with(itemView)
|
||||
|
||||
@ -128,7 +128,6 @@ class StoreDetailActivity : AppCompatActivity() {
|
||||
private fun updateStoreInfo(store: StoreItem?) {
|
||||
store?.let {
|
||||
binding.tvStoreName.text = it.storeName
|
||||
binding.tvStoreRating.text = it.storeRating
|
||||
binding.tvStoreLocation.text = it.storeLocation
|
||||
binding.tvStoreType.text = it.storeType
|
||||
binding.tvActiveStatus.text = it.status
|
||||
@ -145,6 +144,17 @@ class StoreDetailActivity : AppCompatActivity() {
|
||||
.load(fullImageUrl)
|
||||
.placeholder(R.drawable.placeholder_image)
|
||||
.into(binding.ivStoreImage)
|
||||
|
||||
val ratingStr = it.storeRating
|
||||
val ratingValue = ratingStr?.toFloatOrNull()
|
||||
|
||||
if (ratingValue != null && ratingValue > 0f) {
|
||||
binding.tvStoreRating.text = String.format("%.1f", ratingValue)
|
||||
binding.tvStoreRating.visibility = View.VISIBLE
|
||||
} else {
|
||||
binding.tvStoreRating.text = "Belum ada rating"
|
||||
binding.tvStoreRating.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -25,6 +25,7 @@ import com.alya.ecommerce_serang.ui.order.address.AddressActivity
|
||||
import com.alya.ecommerce_serang.ui.order.history.HistoryActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.MyStoreActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.StoreOnReviewActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.StoreSuspendedActivity
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.MyStoreViewModel
|
||||
@ -73,13 +74,11 @@ class ProfileFragment : Fragment() {
|
||||
|
||||
observeUserProfile()
|
||||
|
||||
observeStoreStatus()
|
||||
|
||||
viewModel.loadUserProfile()
|
||||
viewModel.checkStoreUser()
|
||||
|
||||
val hasStore = viewModel.checkStore.value
|
||||
Log.d("Profile Fragment", "Check store $hasStore")
|
||||
binding.tvBukaToko.text = if (hasStore == true) "Toko Saya" else "Buka Toko"
|
||||
|
||||
binding.cardBukaToko.setOnClickListener{
|
||||
// if (hasStore == true) startActivity(Intent(requireContext(), MyStoreActivity::class.java))
|
||||
// else startActivity(Intent(requireContext(), RegisterStoreActivity::class.java))
|
||||
@ -88,8 +87,11 @@ class ProfileFragment : Fragment() {
|
||||
myStoreViewModel.myStoreProfile.observe(viewLifecycleOwner) { store ->
|
||||
store?.let {
|
||||
when (store.storeStatus) {
|
||||
"process" -> startActivity(Intent(requireContext(), StoreOnReviewActivity::class.java))
|
||||
"active" -> startActivity(Intent(requireContext(), MyStoreActivity::class.java))
|
||||
else -> startActivity(Intent(requireContext(), StoreOnReviewActivity::class.java))
|
||||
"inactive" -> startActivity(Intent(requireContext(), MyStoreActivity::class.java))
|
||||
"suspended" -> startActivity(Intent(requireContext(), StoreSuspendedActivity::class.java))
|
||||
else -> startActivity(Intent(requireContext(), RegisterStoreActivity::class.java))
|
||||
}
|
||||
} ?: run {
|
||||
Toast.makeText(requireContext(), "Gagal memuat data toko", Toast.LENGTH_SHORT).show()
|
||||
@ -132,6 +134,12 @@ class ProfileFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun observeStoreStatus() {
|
||||
viewModel.checkStore.observe(viewLifecycleOwner) { hasStore ->
|
||||
binding.tvBukaToko.text = if (hasStore) "Toko Saya" else "Buka Toko"
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateUI(user: UserProfile) = with(binding){
|
||||
val fullImageUrl = when (val img = user.image) {
|
||||
is String -> {
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
package com.alya.ecommerce_serang.ui.profile.editprofile
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.app.DatePickerDialog
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
@ -159,17 +161,14 @@ class EditProfileCustActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun openImagePicker() {
|
||||
// Check for permission first
|
||||
if (ContextCompat.checkSelfPermission(
|
||||
this,
|
||||
android.Manifest.permission.READ_EXTERNAL_STORAGE
|
||||
) != PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),
|
||||
REQUEST_STORAGE_PERMISSION
|
||||
)
|
||||
val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
Manifest.permission.READ_MEDIA_IMAGES
|
||||
} else {
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE
|
||||
}
|
||||
|
||||
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(this, arrayOf(permission), REQUEST_STORAGE_PERMISSION)
|
||||
} else {
|
||||
launchImagePicker()
|
||||
}
|
||||
|
||||
@ -7,24 +7,26 @@ import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.alya.ecommerce_serang.BuildConfig.BASE_URL
|
||||
import com.alya.ecommerce_serang.R
|
||||
import com.alya.ecommerce_serang.data.api.dto.Store
|
||||
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.MyStoreRepository
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.databinding.ActivityMyStoreBinding
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.balance.BalanceActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.chat.ChatListStoreActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.product.ProductActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.profile.DetailStoreProfileActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.review.ReviewActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.review.ReviewFragment
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.sells.SellsActivity
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.MyStoreViewModel
|
||||
import com.bumptech.glide.Glide
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class MyStoreActivity : AppCompatActivity() {
|
||||
private lateinit var binding: ActivityMyStoreBinding
|
||||
@ -49,14 +51,16 @@ class MyStoreActivity : AppCompatActivity() {
|
||||
|
||||
enableEdgeToEdge()
|
||||
|
||||
binding.header.headerTitle.text = "Toko Saya"
|
||||
|
||||
binding.header.headerLeftIcon.setOnClickListener {
|
||||
binding.headerMyStore.headerTitle.text = "Toko Saya"
|
||||
|
||||
binding.headerMyStore.headerLeftIcon.setOnClickListener {
|
||||
onBackPressed()
|
||||
finish()
|
||||
}
|
||||
|
||||
viewModel.loadMyStore()
|
||||
viewModel.loadMyStoreProducts()
|
||||
|
||||
viewModel.myStoreProfile.observe(this){ user ->
|
||||
user?.let { myStoreProfileOverview(it) }
|
||||
@ -65,8 +69,11 @@ class MyStoreActivity : AppCompatActivity() {
|
||||
viewModel.errorMessage.observe(this) { error ->
|
||||
Toast.makeText(this, error, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
setUpClickListeners()
|
||||
getCountOrder()
|
||||
observeViewModel()
|
||||
viewModel.fetchBalance()
|
||||
fetchBalance()
|
||||
}
|
||||
|
||||
private fun myStoreProfileOverview(store: Store){
|
||||
@ -140,15 +147,67 @@ class MyStoreActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCountOrder(){
|
||||
lifecycleScope.launch {
|
||||
try {
|
||||
val allCounts = viewModel.getAllStatusCounts()
|
||||
val totalUnpaid = allCounts["unpaid"]
|
||||
val totalPaid = allCounts["paid"]
|
||||
val totalProcessed = allCounts["processed"]
|
||||
Log.d("MyStoreActivity",
|
||||
"Total orders: unpaid=$totalUnpaid, processed=$totalProcessed, paid=$totalPaid")
|
||||
|
||||
binding.tvNumPesananMasuk.text = totalUnpaid.toString()
|
||||
binding.tvNumPembayaran.text = totalPaid.toString()
|
||||
binding.tvNumPerluDikirim.text = totalProcessed.toString()
|
||||
} catch (e:Exception){
|
||||
Log.e("MyStoreActivity", "Error getting order counts: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun fetchBalance(){
|
||||
viewModel.balanceResult.observe(this){result ->
|
||||
when (result) {
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Loading ->
|
||||
null
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Success ->
|
||||
viewModel.formattedBalance.observe(this) {
|
||||
binding.tvBalance.text = it
|
||||
}
|
||||
is Result.Error -> {
|
||||
Log.e(
|
||||
"MyStoreActivity",
|
||||
"Gagal memuat saldo: ${result.exception.localizedMessage}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun observeViewModel() {
|
||||
viewModel.productList.observe(this) { result ->
|
||||
when (result) {
|
||||
is Result.Loading -> {
|
||||
null
|
||||
}
|
||||
is Result.Success -> {
|
||||
val productList = result.data
|
||||
val count = productList.size
|
||||
Log.d("MyStoreActivty", "You have $count products")
|
||||
|
||||
// Example: update UI
|
||||
binding.tvNumProduct.text = "$count produk"
|
||||
}
|
||||
is Result.Error -> {
|
||||
Log.e("MyStoreActivity", "Failed load product : ${result.exception.message}" )
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PROFILE_REQUEST_CODE = 100
|
||||
}
|
||||
|
||||
// private fun navigateToSellsFragment(status: String) {
|
||||
// val sellsFragment = SellsListFragment.newInstance(status)
|
||||
// supportFragmentManager.beginTransaction()
|
||||
// .replace(android.R.id.content, sellsFragment)
|
||||
// .addToBackStack(null)
|
||||
// .commit()
|
||||
// }
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
package com.alya.ecommerce_serang.ui.profile.mystore
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
@ -20,8 +19,8 @@ import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.drawable.toDrawable
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
@ -31,13 +30,13 @@ import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.data.repository.UserRepository
|
||||
import com.alya.ecommerce_serang.databinding.ActivityRegisterStoreBinding
|
||||
import com.alya.ecommerce_serang.ui.order.address.BankAdapter
|
||||
import com.alya.ecommerce_serang.ui.order.address.CityAdapter
|
||||
import com.alya.ecommerce_serang.ui.order.address.ProvinceAdapter
|
||||
import com.alya.ecommerce_serang.ui.order.address.SubdsitrictAdapter
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.RegisterStoreViewModel
|
||||
import androidx.core.graphics.drawable.toDrawable
|
||||
import androidx.core.widget.ImageViewCompat
|
||||
|
||||
class RegisterStoreActivity : AppCompatActivity() {
|
||||
|
||||
@ -46,6 +45,9 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var provinceAdapter: ProvinceAdapter
|
||||
private lateinit var cityAdapter: CityAdapter
|
||||
private lateinit var subdistrictAdapter: SubdsitrictAdapter
|
||||
private lateinit var bankAdapter: BankAdapter
|
||||
|
||||
// Request codes for file picking
|
||||
private val PICK_STORE_IMAGE_REQUEST = 1001
|
||||
private val PICK_KTP_REQUEST = 1002
|
||||
@ -89,6 +91,8 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
|
||||
provinceAdapter = ProvinceAdapter(this)
|
||||
cityAdapter = CityAdapter(this)
|
||||
subdistrictAdapter = SubdsitrictAdapter(this)
|
||||
bankAdapter = BankAdapter(this)
|
||||
Log.d(TAG, "onCreate: Adapters initialized")
|
||||
|
||||
setupDataBinding()
|
||||
@ -102,8 +106,12 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
setupObservers()
|
||||
Log.d(TAG, "onCreate: Observers setup completed")
|
||||
|
||||
setupMap()
|
||||
Log.d(TAG, "onCreate: Map setup completed")
|
||||
viewModel.latitude.value = "-6.2088"
|
||||
viewModel.longitude.value = "106.8456"
|
||||
Log.d(TAG, "Location permission granted, setting default location")
|
||||
// Toast.makeText(this, "Lokasi dipilih", Toast.LENGTH_SHORT).show()
|
||||
Log.d(TAG, "Default location set - Lat: ${viewModel.latitude.value}, Long: ${viewModel.longitude.value}")
|
||||
// Toast.makeText(this, "Lokasi dipilih", Toast.LENGTH_SHORT).show()
|
||||
|
||||
setupDocumentUploads()
|
||||
Log.d(TAG, "onCreate: Document uploads setup completed")
|
||||
@ -157,21 +165,22 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
!viewModel.bankName.value.isNullOrBlank() &&
|
||||
(viewModel.bankNumber.value ?: 0) > 0 &&
|
||||
(viewModel.provinceId.value ?: 0) > 0 &&
|
||||
(viewModel.cityId.value ?: 0) > 0 &&
|
||||
!viewModel.cityId.value.isNullOrBlank() &&
|
||||
(viewModel.storeTypeId.value ?: 0) > 0 &&
|
||||
viewModel.ktpUri != null &&
|
||||
viewModel.nibUri != null &&
|
||||
viewModel.npwpUri != null &&
|
||||
viewModel.selectedCouriers.isNotEmpty()
|
||||
|
||||
binding.btnRegister.isEnabled = isFormValid
|
||||
viewModel.selectedCouriers.isNotEmpty() &&
|
||||
!viewModel.accountName.value.isNullOrBlank()
|
||||
|
||||
binding.btnRegister.isEnabled = true
|
||||
if (isFormValid) {
|
||||
binding.btnRegister.setBackgroundResource(R.drawable.bg_button_active)
|
||||
binding.btnRegister.setTextColor(ContextCompat.getColor(this, R.color.white))
|
||||
} else {
|
||||
binding.btnRegister.setBackgroundResource(R.drawable.bg_button_disabled)
|
||||
binding.btnRegister.setTextColor(ContextCompat.getColor(this, R.color.black_300))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -226,6 +235,28 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.subdistrictState.observe(this) { state ->
|
||||
when (state) {
|
||||
is Result.Loading -> {
|
||||
Log.d(TAG, "setupobservers: Loading Subdistrict...")
|
||||
binding.subdistrictProgressBar.visibility = View.VISIBLE
|
||||
binding.spinnerSubdistrict.isEnabled = false
|
||||
}
|
||||
is Result.Success -> {
|
||||
Log.d(TAG, "setupobservers: Subdistrict loaded successfullti: ${state.data.size} subdistrict")
|
||||
binding.subdistrictProgressBar.visibility = View.GONE
|
||||
binding.spinnerSubdistrict.isEnabled = true
|
||||
|
||||
subdistrictAdapter.updateData(state.data)
|
||||
}
|
||||
is Result.Error -> {
|
||||
Log.e(TAG, "setupObservers: Error loading subdistrict: ${state.exception.message}")
|
||||
binding.subdistrictProgressBar.visibility = View.GONE
|
||||
binding.spinnerCity.isEnabled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Observe registration state
|
||||
viewModel.registerState.observe(this) { result ->
|
||||
when (result) {
|
||||
@ -396,6 +427,11 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
if (cityId != null) {
|
||||
Log.d(TAG, "Setting city ID: $cityId")
|
||||
viewModel.cityId.value = cityId
|
||||
Log.d(TAG, "Fetching subdistrict for city ID: $cityId")
|
||||
viewModel.getSubdistrict(cityId)
|
||||
|
||||
subdistrictAdapter.clear()
|
||||
binding.spinnerSubdistrict.setSelection(0)
|
||||
viewModel.selectedCityId = cityId
|
||||
} else {
|
||||
Log.e(TAG, "Invalid city ID for position: $position")
|
||||
@ -407,6 +443,61 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
//Setup Subdistrict spinner
|
||||
binding.spinnerSubdistrict.adapter = subdistrictAdapter
|
||||
binding.spinnerSubdistrict.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
Log.d(TAG, "Subdistrict selected at position: $position")
|
||||
val subdistrictId = subdistrictAdapter.getSubdistrictId(position)
|
||||
if (subdistrictId != null) {
|
||||
Log.d(TAG, "Setting subdistrict ID: $subdistrictId")
|
||||
viewModel.subdistrict.value = subdistrictId
|
||||
viewModel.selectedSubdistrict = subdistrictId
|
||||
} else {
|
||||
Log.e(TAG, "Invalid subdistrict ID for position: $position")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {
|
||||
Log.d(TAG, "No city selected")
|
||||
}
|
||||
}
|
||||
|
||||
binding.spinnerBankName.adapter = bankAdapter
|
||||
binding.spinnerBankName.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(
|
||||
parent: AdapterView<*>?,
|
||||
view: View?,
|
||||
position: Int,
|
||||
id: Long
|
||||
) {
|
||||
Log.d(TAG, "Bank selected at position: $position")
|
||||
val bankName = bankAdapter.getBankName(position)
|
||||
if (bankName != null) {
|
||||
Log.d(TAG, "Setting bank name: $bankName")
|
||||
viewModel.bankName.value = bankName
|
||||
viewModel.selectedBankName = bankName
|
||||
|
||||
// Optional: Log the selected bank details
|
||||
val selectedBank = bankAdapter.getBankItem(position)
|
||||
selectedBank?.let {
|
||||
Log.d(TAG, "Selected bank: ${it.bankName} (Code: ${it.bankCode})")
|
||||
}
|
||||
|
||||
// Hide progress bar if it was showing
|
||||
binding.bankNameProgressBar.visibility = View.GONE
|
||||
|
||||
} else {
|
||||
Log.e(TAG, "Invalid bank name for position: $position")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {
|
||||
Log.d(TAG, "No bank selected")
|
||||
viewModel.selectedBankName = null
|
||||
}
|
||||
}
|
||||
|
||||
// Add initial hints to the spinners
|
||||
if (provinceAdapter.isEmpty) {
|
||||
Log.d(TAG, "Adding default province hint")
|
||||
@ -418,6 +509,16 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
cityAdapter.add("Pilih Kabupaten/Kota")
|
||||
}
|
||||
|
||||
if (subdistrictAdapter.isEmpty) {
|
||||
Log.d(TAG, "Adding default kecamatan hint")
|
||||
subdistrictAdapter.add("Pilih Kecamatan")
|
||||
}
|
||||
|
||||
if (bankAdapter.isEmpty) {
|
||||
Log.d(TAG, "Adding default bank hint")
|
||||
bankAdapter.add("Pilih Bank")
|
||||
}
|
||||
|
||||
Log.d(TAG, "setupSpinners: Province and city spinners setup completed")
|
||||
}
|
||||
|
||||
@ -501,44 +602,44 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
validateRequiredFields()
|
||||
}
|
||||
|
||||
private fun setupMap() {
|
||||
Log.d(TAG, "setupMap: Setting up map container")
|
||||
// This would typically integrate with Google Maps SDK
|
||||
// For simplicity, we're just using a placeholder
|
||||
binding.mapContainer.setOnClickListener {
|
||||
Log.d(TAG, "Map container clicked, checking location permission")
|
||||
// Request location permission if not granted
|
||||
if (ContextCompat.checkSelfPermission(
|
||||
this,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
) != PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
Log.d(TAG, "Location permission not granted, requesting permission")
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
|
||||
LOCATION_PERMISSION_REQUEST
|
||||
)
|
||||
viewModel.latitude.value = "-6.2088"
|
||||
viewModel.longitude.value = "106.8456"
|
||||
Log.d(TAG, "Location permission granted, setting default location")
|
||||
Toast.makeText(this, "Lokasi dipilih", Toast.LENGTH_SHORT).show()
|
||||
Log.d(TAG, "Default location set - Lat: ${viewModel.latitude.value}, Long: ${viewModel.longitude.value}")
|
||||
Toast.makeText(this, "Lokasi dipilih", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Log.d(TAG, "Location permission already granted, setting location")
|
||||
// Show map selection UI
|
||||
// This would typically launch Maps UI for location selection
|
||||
// For now, we'll just set some dummy coordinates
|
||||
viewModel.latitude.value = "-6.2088"
|
||||
viewModel.longitude.value = "106.8456"
|
||||
Log.d(TAG, "Location set - Lat: ${viewModel.latitude.value}, Long: ${viewModel.longitude.value}")
|
||||
Toast.makeText(this, "Lokasi dipilih", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
Log.d(TAG, "setupMap: Map container setup completed")
|
||||
}
|
||||
// private fun setupMap() {
|
||||
// Log.d(TAG, "setupMap: Setting up map container")
|
||||
// // This would typically integrate with Google Maps SDK
|
||||
// // For simplicity, we're just using a placeholder
|
||||
// binding.mapContainer.setOnClickListener {
|
||||
// Log.d(TAG, "Map container clicked, checking location permission")
|
||||
// // Request location permission if not granted
|
||||
// if (ContextCompat.checkSelfPermission(
|
||||
// this,
|
||||
// Manifest.permission.ACCESS_FINE_LOCATION
|
||||
// ) != PackageManager.PERMISSION_GRANTED
|
||||
// ) {
|
||||
// Log.d(TAG, "Location permission not granted, requesting permission")
|
||||
// ActivityCompat.requestPermissions(
|
||||
// this,
|
||||
// arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
|
||||
// LOCATION_PERMISSION_REQUEST
|
||||
// )
|
||||
// viewModel.latitude.value = "-6.2088"
|
||||
// viewModel.longitude.value = "106.8456"
|
||||
// Log.d(TAG, "Location permission granted, setting default location")
|
||||
// Toast.makeText(this, "Lokasi dipilih", Toast.LENGTH_SHORT).show()
|
||||
// Log.d(TAG, "Default location set - Lat: ${viewModel.latitude.value}, Long: ${viewModel.longitude.value}")
|
||||
// Toast.makeText(this, "Lokasi dipilih", Toast.LENGTH_SHORT).show()
|
||||
// } else {
|
||||
// Log.d(TAG, "Location permission already granted, setting location")
|
||||
// // Show map selection UI
|
||||
// // This would typically launch Maps UI for location selection
|
||||
// // For now, we'll just set some dummy coordinates
|
||||
// viewModel.latitude.value = "-6.2088"
|
||||
// viewModel.longitude.value = "106.8456"
|
||||
// Log.d(TAG, "Location set - Lat: ${viewModel.latitude.value}, Long: ${viewModel.longitude.value}")
|
||||
// Toast.makeText(this, "Lokasi dipilih", Toast.LENGTH_SHORT).show()
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Log.d(TAG, "setupMap: Map container setup completed")
|
||||
// }
|
||||
|
||||
private fun setupDataBinding() {
|
||||
Log.d(TAG, "setupDataBinding: Setting up two-way data binding for text fields")
|
||||
@ -618,25 +719,36 @@ class RegisterStoreActivity : AppCompatActivity() {
|
||||
validateRequiredFields()
|
||||
}
|
||||
})
|
||||
//
|
||||
// binding.etSubdistrict.addTextChangedListener(object : TextWatcher {
|
||||
// override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
// override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||||
// override fun afterTextChanged(s: Editable?) {
|
||||
// viewModel.subdistrict.value = s.toString()
|
||||
// Log.d(TAG, "Subdistrict updated: ${s.toString()}")
|
||||
// validateRequiredFields()
|
||||
// }
|
||||
// })
|
||||
|
||||
binding.etSubdistrict.addTextChangedListener(object : TextWatcher {
|
||||
// binding.etBankName.addTextChangedListener(object: TextWatcher {
|
||||
// override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
// override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||||
// override fun afterTextChanged(s: Editable?) {
|
||||
// viewModel.bankName.value = s.toString()
|
||||
// Log.d(TAG, "Bank name updated: ${s.toString()}")
|
||||
// validateRequiredFields()
|
||||
// }
|
||||
// })
|
||||
|
||||
binding.etAccountName.addTextChangedListener(object : TextWatcher {
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
viewModel.subdistrict.value = s.toString()
|
||||
Log.d(TAG, "Subdistrict updated: ${s.toString()}")
|
||||
viewModel.accountName.value = s.toString()
|
||||
Log.d(TAG, "Account Name updated: ${s.toString()}")
|
||||
validateRequiredFields()
|
||||
}
|
||||
})
|
||||
|
||||
binding.etBankName.addTextChangedListener(object: TextWatcher {
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
viewModel.bankName.value = s.toString()
|
||||
Log.d(TAG, "Bank name updated: ${s.toString()}")
|
||||
validateRequiredFields()
|
||||
}
|
||||
})
|
||||
|
||||
Log.d(TAG, "setupDataBinding: Text field data binding setup completed")
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
package com.alya.ecommerce_serang.ui.profile.mystore
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import com.alya.ecommerce_serang.R
|
||||
import com.alya.ecommerce_serang.databinding.ActivityStoreSuspendedBinding
|
||||
|
||||
class StoreSuspendedActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityStoreSuspendedBinding
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityStoreSuspendedBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
binding.header.headerTitle.text = "Toko Dinonaktifkan"
|
||||
binding.header.headerLeftIcon.setOnClickListener {
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -408,6 +408,10 @@ class BalanceActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun navigateTotalBalance(){
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TOP_UP_REQUEST_CODE = 101
|
||||
}
|
||||
|
||||
@ -15,11 +15,15 @@ import android.widget.Spinner
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.widget.doAfterTextChanged
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.alya.ecommerce_serang.R
|
||||
import com.alya.ecommerce_serang.data.api.response.store.profile.Payment
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
import com.alya.ecommerce_serang.utils.ImageUtils.compressImage
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import kotlinx.coroutines.launch
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
@ -38,6 +42,8 @@ class BalanceTopUpActivity : AppCompatActivity() {
|
||||
private lateinit var spinnerPaymentMethod: Spinner
|
||||
private lateinit var edtTransactionDate: EditText
|
||||
private lateinit var datePickerIcon: ImageView
|
||||
private lateinit var layoutMBankingInstructions: View
|
||||
private lateinit var layoutATMInstructions: View
|
||||
private lateinit var btnSend: Button
|
||||
private lateinit var sessionManager: SessionManager
|
||||
|
||||
@ -52,7 +58,22 @@ class BalanceTopUpActivity : AppCompatActivity() {
|
||||
val imageUri = result.data?.data
|
||||
imageUri?.let {
|
||||
selectedImageUri = it
|
||||
imgPreview.setImageURI(it)
|
||||
|
||||
// Compress the image before displaying it
|
||||
val compressedFile = compressImage(
|
||||
context = this,
|
||||
uri = it,
|
||||
filename = "topup_img",
|
||||
maxWidth = 1024,
|
||||
maxHeight = 1024,
|
||||
quality = 80
|
||||
)
|
||||
|
||||
// Display the compressed image
|
||||
selectedImageUri = Uri.fromFile(compressedFile)
|
||||
imgPreview.setImageURI(Uri.fromFile(compressedFile))
|
||||
|
||||
validateForm()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -71,6 +92,8 @@ class BalanceTopUpActivity : AppCompatActivity() {
|
||||
spinnerPaymentMethod = findViewById(R.id.spinner_metode_bayar)
|
||||
edtTransactionDate = findViewById(R.id.edt_tgl_transaksi)
|
||||
datePickerIcon = findViewById(R.id.img_date_picker)
|
||||
layoutMBankingInstructions = findViewById(R.id.layout_mbanking_instructions)
|
||||
layoutATMInstructions = findViewById(R.id.layout_atm_instructions)
|
||||
btnSend = findViewById(R.id.btn_send)
|
||||
|
||||
// Setup header title
|
||||
@ -98,10 +121,27 @@ class BalanceTopUpActivity : AppCompatActivity() {
|
||||
// Fetch payment methods
|
||||
fetchPaymentMethods()
|
||||
|
||||
setupClickListeners("1234567890")
|
||||
|
||||
// Setup submit button
|
||||
btnSend.setOnClickListener {
|
||||
submitForm()
|
||||
}
|
||||
|
||||
// Validate form when any input changes
|
||||
edtNominal.doAfterTextChanged { validateForm() }
|
||||
edtTransactionDate.doAfterTextChanged { validateForm() }
|
||||
spinnerPaymentMethod.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
selectedPaymentId = paymentMethods[position].id
|
||||
validateForm()
|
||||
}
|
||||
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {
|
||||
selectedPaymentId = -1
|
||||
validateForm()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun openGallery() {
|
||||
@ -200,6 +240,24 @@ class BalanceTopUpActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateForm() {
|
||||
val isNominalFilled = edtNominal.text.toString().trim().isNotEmpty()
|
||||
val isPaymentMethodSelected = selectedPaymentId != -1
|
||||
val isTransactionDateFilled = edtTransactionDate.text.toString().trim().isNotEmpty()
|
||||
val isImageSelected = selectedImageUri != null
|
||||
|
||||
val valid = isNominalFilled && isPaymentMethodSelected && isTransactionDateFilled && isImageSelected
|
||||
btnSend.isEnabled = valid
|
||||
btnSend.setTextColor(
|
||||
if (valid) ContextCompat.getColor(this, R.color.white)
|
||||
else ContextCompat.getColor(this, R.color.black_300)
|
||||
)
|
||||
btnSend.setBackgroundResource(
|
||||
if (valid) R.drawable.bg_button_active
|
||||
else R.drawable.bg_button_disabled
|
||||
)
|
||||
}
|
||||
|
||||
private fun submitForm() {
|
||||
// Prevent multiple clicks
|
||||
if (!btnSend.isEnabled) {
|
||||
@ -316,7 +374,7 @@ class BalanceTopUpActivity : AppCompatActivity() {
|
||||
|
||||
// Show a dialog with the success message
|
||||
runOnUiThread {
|
||||
androidx.appcompat.app.AlertDialog.Builder(this@BalanceTopUpActivity)
|
||||
AlertDialog.Builder(this@BalanceTopUpActivity)
|
||||
.setTitle("Berhasil")
|
||||
.setMessage(successMessage)
|
||||
.setPositiveButton("OK") { dialog, _ ->
|
||||
@ -350,7 +408,7 @@ class BalanceTopUpActivity : AppCompatActivity() {
|
||||
|
||||
// Show a dialog with the error message
|
||||
runOnUiThread {
|
||||
androidx.appcompat.app.AlertDialog.Builder(this@BalanceTopUpActivity)
|
||||
AlertDialog.Builder(this@BalanceTopUpActivity)
|
||||
.setTitle("Error Response")
|
||||
.setMessage(errorMessage)
|
||||
.setPositiveButton("OK") { dialog, _ ->
|
||||
@ -392,4 +450,46 @@ class BalanceTopUpActivity : AppCompatActivity() {
|
||||
|
||||
return tempFile
|
||||
}
|
||||
|
||||
private fun setupClickListeners(bankAccountNumber: String) {
|
||||
// Instructions clicks
|
||||
layoutMBankingInstructions.setOnClickListener {
|
||||
showInstructions("mBanking", bankAccountNumber)
|
||||
}
|
||||
|
||||
layoutATMInstructions.setOnClickListener {
|
||||
showInstructions("ATM", bankAccountNumber)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showInstructions(type: String, bankAccountNumber: String) {
|
||||
// Implementasi tampilkan instruksi
|
||||
val instructions = when (type) {
|
||||
"mBanking" -> listOf(
|
||||
"1. Login ke aplikasi mobile banking",
|
||||
"2. Pilih menu Transfer",
|
||||
"3. Pilih menu Antar Rekening",
|
||||
"4. Masukkan nomor rekening tujuan: $bankAccountNumber",
|
||||
"5. Masukkan nominal saldo yang ingin diisi",
|
||||
"6. Konfirmasi dan selesaikan transfer"
|
||||
)
|
||||
"ATM" -> listOf(
|
||||
"1. Masukkan kartu ATM dan PIN",
|
||||
"2. Pilih menu Transfer",
|
||||
"3. Pilih menu Antar Rekening",
|
||||
"4. Masukkan kode bank dan nomor rekening tujuan: $bankAccountNumber",
|
||||
"5. Masukkan nominal saldo yang ingin diisi",
|
||||
"6. Konfirmasi dan selesaikan transfer"
|
||||
)
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
// Tampilkan instruksi dalam dialog
|
||||
val dialog = AlertDialog.Builder(this)
|
||||
.setTitle("Petunjuk Transfer $type")
|
||||
.setItems(instructions.toTypedArray(), null)
|
||||
.setPositiveButton("Tutup", null)
|
||||
.create()
|
||||
dialog.show()
|
||||
}
|
||||
}
|
||||
@ -25,6 +25,7 @@ import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsAnimationCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.alya.ecommerce_serang.BuildConfig.BASE_URL
|
||||
import com.alya.ecommerce_serang.R
|
||||
@ -373,7 +374,8 @@ class ChatStoreActivity : AppCompatActivity() {
|
||||
}
|
||||
})
|
||||
|
||||
viewModel.state.observe(this, Observer { state ->
|
||||
lifecycleScope.launchWhenStarted {
|
||||
viewModel.state.collect { state ->
|
||||
Log.d(TAG, "State updated - Messages: ${state.messages.size}")
|
||||
|
||||
// Update messages
|
||||
@ -426,15 +428,16 @@ class ChatStoreActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
// Show typing indicator
|
||||
binding.tvTypingIndicator.visibility =
|
||||
if (state.isOtherUserTyping) View.VISIBLE else View.GONE
|
||||
// binding.tvTypingIndicator.visibility =
|
||||
// if (state.isOtherUserTyping) View.VISIBLE else View.GONE
|
||||
|
||||
// Show error if any
|
||||
state.error?.let { error ->
|
||||
Toast.makeText(this@ChatStoreActivity, error, Toast.LENGTH_SHORT).show()
|
||||
viewModel.clearError()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showOptionsMenu() {
|
||||
@ -520,6 +523,19 @@ class ChatStoreActivity : AppCompatActivity() {
|
||||
private fun handleSelectedImage(uri: Uri) {
|
||||
try {
|
||||
Log.d(TAG, "Processing selected image: $uri")
|
||||
binding.layoutAttachImage.visibility = View.VISIBLE
|
||||
val fullImageUrl = when (val img = uri.toString()) {
|
||||
is String -> {
|
||||
if (img.startsWith("/")) BASE_URL + img.substring(1) else img
|
||||
}
|
||||
else -> R.drawable.placeholder_image
|
||||
}
|
||||
|
||||
Glide.with(this)
|
||||
.load(fullImageUrl)
|
||||
.placeholder(R.drawable.placeholder_image)
|
||||
.into(binding.ivAttach)
|
||||
Log.d(TAG, "Display attach image: $uri")
|
||||
|
||||
// Always use the copy-to-cache approach for reliability
|
||||
contentResolver.openInputStream(uri)?.use { inputStream ->
|
||||
|
||||
@ -12,36 +12,37 @@ import android.view.View
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.Toast
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import com.alya.ecommerce_serang.R
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.net.toUri
|
||||
import androidx.core.widget.doAfterTextChanged
|
||||
import com.alya.ecommerce_serang.BuildConfig.BASE_URL
|
||||
import com.alya.ecommerce_serang.R
|
||||
import com.alya.ecommerce_serang.data.api.dto.CategoryItem
|
||||
import com.alya.ecommerce_serang.data.api.dto.Preorder
|
||||
import com.alya.ecommerce_serang.data.api.dto.Wholesale
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
import com.alya.ecommerce_serang.data.repository.ProductRepository
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.databinding.ActivityDetailStoreProductBinding
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.ProductViewModel
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.FileUtils.compressFile
|
||||
import com.alya.ecommerce_serang.utils.ImageUtils.compressImage
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.ProductViewModel
|
||||
import com.bumptech.glide.Glide
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import kotlin.getValue
|
||||
import androidx.core.net.toUri
|
||||
import androidx.core.widget.doAfterTextChanged
|
||||
import com.alya.ecommerce_serang.BuildConfig.BASE_URL
|
||||
import com.alya.ecommerce_serang.data.api.dto.Wholesale
|
||||
|
||||
class DetailStoreProductActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityDetailStoreProductBinding
|
||||
private lateinit var sessionManager: SessionManager
|
||||
private lateinit var categoryList: List<CategoryItem>
|
||||
private var categoryList: List<CategoryItem> = emptyList()
|
||||
private var imageUri: Uri? = null
|
||||
private var sppirtUri: Uri? = null
|
||||
private var halalUri: Uri? = null
|
||||
@ -61,7 +62,10 @@ class DetailStoreProductActivity : AppCompatActivity() {
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
imageUri = result.data?.data
|
||||
imageUri?.let {
|
||||
binding.ivPreviewFoto.setImageURI(it)
|
||||
compressImage(this, it, "productimg").let { compressedImageFile ->
|
||||
binding.ivPreviewFoto.setImageURI(Uri.fromFile(compressedImageFile))
|
||||
imageUri = Uri.fromFile(compressedImageFile)
|
||||
}
|
||||
binding.switcherFotoProduk.showNext()
|
||||
hasImage = true
|
||||
}
|
||||
@ -71,17 +75,21 @@ class DetailStoreProductActivity : AppCompatActivity() {
|
||||
|
||||
private val sppirtLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
if (uri != null && isValidFile(uri)) {
|
||||
sppirtUri = uri
|
||||
binding.tvSppirtName.text = getFileName(uri)
|
||||
binding.switcherSppirt.showNext()
|
||||
compressFile(this, uri).let { compressedFile ->
|
||||
sppirtUri = compressedFile?.toUri()
|
||||
binding.tvSppirtName.text = getFileName(sppirtUri!!)
|
||||
binding.switcherSppirt.showNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val halalLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||
if (uri != null && isValidFile(uri)) {
|
||||
halalUri = uri
|
||||
binding.tvHalalName.text = getFileName(uri)
|
||||
binding.switcherHalal.showNext()
|
||||
compressFile(this, uri).let { compressedFile ->
|
||||
halalUri = compressedFile?.toUri()
|
||||
binding.tvHalalName.text = getFileName(halalUri!!)
|
||||
binding.switcherHalal.showNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,7 +101,7 @@ class DetailStoreProductActivity : AppCompatActivity() {
|
||||
val isEditing = intent.getBooleanExtra("is_editing", false)
|
||||
productId = intent.getIntExtra("product_id", -1)
|
||||
|
||||
binding.header.headerTitle.text = if (isEditing) "Ubah Produk" else "Tambah Produk"
|
||||
binding.headerStoreProduct.headerTitle.text = if (isEditing) "Ubah Produk" else "Tambah Produk"
|
||||
|
||||
if (isEditing && productId != null && productId != -1) {
|
||||
viewModel.loadProductDetail(productId!!)
|
||||
@ -140,7 +148,7 @@ class DetailStoreProductActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
binding.header.headerLeftIcon.setOnClickListener {
|
||||
binding.headerStoreProduct.headerLeftIcon.setOnClickListener {
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,9 +11,9 @@ import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
import com.alya.ecommerce_serang.data.repository.ProductRepository
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.databinding.ActivityProductBinding
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.ProductViewModel
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.ProductViewModel
|
||||
|
||||
class ProductActivity : AppCompatActivity() {
|
||||
|
||||
@ -94,14 +94,14 @@ class ProductActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun setupHeader() {
|
||||
binding.header.headerTitle.text = "Produk Saya"
|
||||
binding.header.headerRightText.visibility = View.VISIBLE
|
||||
binding.headerListProduct.headerTitle.text = "Produk Saya"
|
||||
binding.headerListProduct.headerRightText.visibility = View.VISIBLE
|
||||
|
||||
binding.header.headerLeftIcon.setOnClickListener {
|
||||
binding.headerListProduct.headerLeftIcon.setOnClickListener {
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
|
||||
binding.header.headerRightText.setOnClickListener {
|
||||
binding.headerListProduct.headerRightText.setOnClickListener {
|
||||
val intent = Intent(this, DetailStoreProductActivity::class.java)
|
||||
intent.putExtra("is_editing", false)
|
||||
startActivity(intent)
|
||||
@ -111,4 +111,6 @@ class ProductActivity : AppCompatActivity() {
|
||||
private fun setupRecyclerView() {
|
||||
binding.rvStoreProduct.layoutManager = LinearLayoutManager(this)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
package com.alya.ecommerce_serang.ui.profile.mystore.profile.address
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
@ -14,13 +13,16 @@ import com.alya.ecommerce_serang.BuildConfig
|
||||
import com.alya.ecommerce_serang.R
|
||||
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.api.retrofit.ApiConfig
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiService
|
||||
import com.alya.ecommerce_serang.data.repository.AddressRepository
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.databinding.ActivityDetailStoreAddressBinding
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.AddressViewModel
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.AddressViewModel
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
|
||||
class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
@ -30,10 +32,15 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
|
||||
private var selectedProvinceId: String? = null
|
||||
private var selectedCityId: String? = null
|
||||
private var selectedSubdistrict: String? = null
|
||||
private var provinces: List<Province> = emptyList()
|
||||
private var cities: List<City> = emptyList()
|
||||
private var subdistrict: List<SubdistrictsItem> = emptyList()
|
||||
private var currentAddress: AddressesItem? = null
|
||||
|
||||
private val TAG = "StoreAddressActivity"
|
||||
// private lateinit var subdistrictAdapter: SubdsitrictAdapter
|
||||
|
||||
private val TAG = "DetailStoreAddressActivity"
|
||||
|
||||
private val viewModel: AddressViewModel by viewModels {
|
||||
BaseViewModelFactory {
|
||||
@ -58,13 +65,15 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
binding.tvError.visibility = View.GONE
|
||||
|
||||
// Set up header title
|
||||
binding.header.headerTitle.text = "Atur Alamat Toko"
|
||||
binding.headerAddressStore.headerTitle.text = "Atur Alamat Toko"
|
||||
|
||||
// Set up back button
|
||||
binding.header.headerLeftIcon.setOnClickListener {
|
||||
binding.headerAddressStore.headerLeftIcon.setOnClickListener {
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
|
||||
// subdistrictAdapter = SubdsitrictAdapter(this)
|
||||
|
||||
setupSpinners()
|
||||
setupObservers()
|
||||
setupSaveButton()
|
||||
@ -113,11 +122,26 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
binding.spinnerCity.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
selectedCityId = if (position > 0) cities[position - 1].cityId else null
|
||||
|
||||
viewModel.getSubdistrict(selectedCityId.toString())
|
||||
|
||||
checkAllFieldsFilled()
|
||||
|
||||
}
|
||||
|
||||
override fun onNothingSelected(p0: AdapterView<*>?) {}
|
||||
}
|
||||
|
||||
binding.spinnerSubdistrict.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
selectedSubdistrict = if (position > 0) subdistrict[position - 1].subdistrictName else null
|
||||
|
||||
checkAllFieldsFilled()
|
||||
|
||||
}
|
||||
override fun onNothingSelected(p0: AdapterView<*>?) {}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupObservers() {
|
||||
@ -176,19 +200,58 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.subdistrictState.observe(this) { result ->
|
||||
when (result) {
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Loading -> {
|
||||
showSubLoading(true)
|
||||
}
|
||||
|
||||
is com.alya.ecommerce_serang.data.repository.Result.Success -> {
|
||||
showSubLoading(false)
|
||||
|
||||
subdistrict = result.data
|
||||
val subdistrictNames = mutableListOf("Pilih Kecamatan")
|
||||
subdistrictNames.addAll(result.data.map { it.subdistrictName })
|
||||
|
||||
val adapter = ArrayAdapter(
|
||||
this,
|
||||
android.R.layout.simple_spinner_item,
|
||||
subdistrictNames
|
||||
)
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
binding.spinnerSubdistrict.adapter = adapter
|
||||
|
||||
// Compare by name, since stored value is the subdistrict name
|
||||
viewModel.storeAddress.value?.let { address ->
|
||||
val index = subdistrict.indexOfFirst { it.subdistrictName == address.subdistrict }
|
||||
if (index != -1) {
|
||||
binding.spinnerSubdistrict.setSelection(index + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is Result.Error -> {
|
||||
showSubLoading(false)
|
||||
Log.e(TAG, "Error: ${result.exception.message}", result.exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Observe store address data
|
||||
viewModel.storeAddress.observe(this) { address ->
|
||||
currentAddress = address
|
||||
Log.d(TAG, "Received store address: $address")
|
||||
address?.let {
|
||||
// Set the fields
|
||||
binding.edtStreet.setText(it.street)
|
||||
binding.edtSubdistrict.setText(it.subdistrict)
|
||||
// binding.edtSubdistrict.setText(it.subdistrict)
|
||||
binding.edtDetailAddress.setText(it.detail ?: "")
|
||||
binding.edtPostalCode.setText(it.postalCode)
|
||||
binding.edtLatitude.setText(it.latitude.toString())
|
||||
binding.edtLongitude.setText(it.longitude.toString())
|
||||
selectedProvinceId = it.provinceId
|
||||
selectedCityId = it.cityId
|
||||
selectedSubdistrict = it.subdistrict
|
||||
|
||||
// Find province index and select it after provinces are loaded
|
||||
if (provinces.isNotEmpty()) {
|
||||
@ -214,11 +277,12 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
// Observe save success
|
||||
viewModel.saveSuccess.observe(this) {
|
||||
if (it) {
|
||||
Toast.makeText(this, "Alamat berhasil disimpan", Toast.LENGTH_SHORT).show()
|
||||
setResult(Activity.RESULT_OK)
|
||||
viewModel.saveSuccess.observe(this) { success ->
|
||||
if (success) {
|
||||
Log.d(TAG, "Address updated successfully")
|
||||
finish()
|
||||
} else {
|
||||
Log.e(TAG, "Failed to update address")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -226,14 +290,15 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
private fun setupSaveButton() {
|
||||
binding.btnSaveAddress.setOnClickListener {
|
||||
val street = binding.edtStreet.text.toString()
|
||||
val subdistrict = binding.edtSubdistrict.text.toString()
|
||||
val detail = binding.edtDetailAddress.text.toString()
|
||||
val postalCode = binding.edtPostalCode.text.toString()
|
||||
val latitude = binding.edtLatitude.text.toString().toDoubleOrNull() ?: 0.0
|
||||
val longitude = binding.edtLongitude.text.toString().toDoubleOrNull() ?: 0.0
|
||||
val latitude = binding.edtLatitude.text.toString()
|
||||
val longitude = binding.edtLongitude.text.toString()
|
||||
|
||||
val city = cities.find { it.cityId == selectedCityId }
|
||||
val province = provinces.find { it.provinceId == selectedProvinceId }
|
||||
val subdistrictName = subdistrict.find { it.subdistrictName == selectedSubdistrict }?.subdistrictName.toString()
|
||||
Log.d(TAG, "Subdistrict name: $subdistrictName")
|
||||
|
||||
// Validate required fields
|
||||
if (selectedProvinceId.isNullOrEmpty() || city == null || street.isEmpty() || subdistrict.isEmpty() || postalCode.isEmpty()) {
|
||||
@ -241,19 +306,35 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
// Save address
|
||||
viewModel.saveStoreAddress(
|
||||
val oldAddress = currentAddress ?: return@setOnClickListener
|
||||
val newAddress = oldAddress.copy(
|
||||
provinceId = selectedProvinceId!!,
|
||||
provinceName = province?.provinceName ?: "",
|
||||
cityId = city.cityId,
|
||||
cityName = city.cityName,
|
||||
street = street,
|
||||
subdistrict = subdistrict,
|
||||
subdistrict = subdistrictName,
|
||||
detail = detail,
|
||||
postalCode = postalCode,
|
||||
latitude = latitude,
|
||||
longitude = longitude
|
||||
longitude = longitude,
|
||||
phone = oldAddress.phone,
|
||||
recipient = oldAddress.recipient ?: "",
|
||||
isStoreLocation = oldAddress.isStoreLocation,
|
||||
villageId = oldAddress.villageId
|
||||
)
|
||||
viewModel.saveStoreAddress(oldAddress, newAddress)
|
||||
// Save address
|
||||
// viewModel.saveStoreAddress(
|
||||
// provinceId = selectedProvinceId!!,
|
||||
// provinceName = province?.provinceName ?: "",
|
||||
// cityId = city.cityId,
|
||||
// cityName = city.cityName,
|
||||
// street = street,
|
||||
// subdistrict = subdistrict,
|
||||
// detail = detail,
|
||||
// postalCode = postalCode,
|
||||
// latitude = latitude,
|
||||
// longitude = longitude
|
||||
// )
|
||||
}
|
||||
}
|
||||
|
||||
@ -264,15 +345,14 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||||
}
|
||||
binding.edtStreet.addTextChangedListener(watcher)
|
||||
binding.edtSubdistrict.addTextChangedListener(watcher)
|
||||
binding.edtPostalCode.addTextChangedListener(watcher)
|
||||
}
|
||||
|
||||
private fun checkAllFieldsFilled() {
|
||||
val allValid = !selectedProvinceId.isNullOrEmpty()
|
||||
&& !selectedCityId.isNullOrEmpty()
|
||||
&& !selectedSubdistrict.isNullOrEmpty()
|
||||
&& binding.edtStreet.text.isNotBlank()
|
||||
&& binding.edtSubdistrict.text.isNotBlank()
|
||||
&& binding.edtPostalCode.text.isNotBlank()
|
||||
|
||||
binding.btnSaveAddress.let {
|
||||
@ -284,6 +364,7 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
it.isEnabled = false
|
||||
it.setBackgroundResource(R.drawable.bg_button_disabled)
|
||||
it.setTextColor(getColor(R.color.black_300))
|
||||
Toast.makeText(this, "Periksa dan lenkapi alamat anda", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -298,6 +379,12 @@ class DetailStoreAddressActivity : AppCompatActivity() {
|
||||
binding.spinnerCity.visibility = if (isLoading) View.GONE else View.VISIBLE
|
||||
}
|
||||
|
||||
private fun showSubLoading(isLoading: Boolean) {
|
||||
binding.subdistrictProgressBar.visibility = if (isLoading) View.VISIBLE else View.GONE
|
||||
binding.spinnerSubdistrict.visibility = if (isLoading) View.GONE else View.VISIBLE
|
||||
}
|
||||
|
||||
|
||||
private fun showError(message: String) {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
binding.tvError.visibility = View.VISIBLE
|
||||
|
||||
@ -6,9 +6,12 @@ import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.AdapterView
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.ImageView
|
||||
import android.widget.ProgressBar
|
||||
import android.widget.Spinner
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
@ -18,6 +21,7 @@ import com.alya.ecommerce_serang.data.api.dto.PaymentInfo
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
import com.alya.ecommerce_serang.data.repository.PaymentInfoRepository
|
||||
import com.alya.ecommerce_serang.databinding.ActivityPaymentInfoBinding
|
||||
import com.alya.ecommerce_serang.ui.order.address.BankAdapter
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.alya.ecommerce_serang.utils.UriToFileConverter
|
||||
@ -32,6 +36,7 @@ class PaymentInfoActivity : AppCompatActivity() {
|
||||
private lateinit var sessionManager: SessionManager
|
||||
private var selectedQrisImageUri: Uri? = null
|
||||
private var selectedQrisImageFile: File? = null
|
||||
private lateinit var bankAdapter: BankAdapter
|
||||
|
||||
// Store form data between dialog reopenings
|
||||
private var savedBankName: String = ""
|
||||
@ -95,6 +100,7 @@ class PaymentInfoActivity : AppCompatActivity() {
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
|
||||
bankAdapter = BankAdapter(this)
|
||||
setupRecyclerView()
|
||||
setupObservers()
|
||||
|
||||
@ -173,10 +179,47 @@ class PaymentInfoActivity : AppCompatActivity() {
|
||||
builder.setView(dialogView)
|
||||
|
||||
val dialog = builder.create()
|
||||
val spinnerBankName = dialogView.findViewById<Spinner>(R.id.spinner_bank_name)
|
||||
val progressBarBank = dialogView.findViewById<ProgressBar>(R.id.bank_name_progress_bar)
|
||||
|
||||
spinnerBankName.adapter = bankAdapter
|
||||
spinnerBankName.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(
|
||||
parent: AdapterView<*>?,
|
||||
view: View?,
|
||||
position: Int,
|
||||
id: Long
|
||||
) {
|
||||
Log.d(TAG, "Bank selected at position: $position")
|
||||
val bankName = bankAdapter.getBankName(position)
|
||||
if (bankName != null) {
|
||||
Log.d(TAG, "Setting bank name: $bankName")
|
||||
viewModel.bankName.value = bankName
|
||||
viewModel.selectedBankName = bankName
|
||||
|
||||
// Optional: Log the selected bank details
|
||||
val selectedBank = bankAdapter.getBankItem(position)
|
||||
selectedBank?.let {
|
||||
Log.d(TAG, "Selected bank: ${it.bankName} (Code: ${it.bankCode})")
|
||||
}
|
||||
|
||||
// Hide progress bar if it was showing
|
||||
progressBarBank.visibility = View.GONE
|
||||
|
||||
} else {
|
||||
Log.e(TAG, "Invalid bank name for position: $position")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {
|
||||
Log.d(TAG, "No bank selected")
|
||||
viewModel.selectedBankName = null
|
||||
}
|
||||
}
|
||||
|
||||
// Get references to views in the dialog
|
||||
val btnAddQris = dialogView.findViewById<Button>(R.id.btn_add_qris)
|
||||
val bankNameEditText = dialogView.findViewById<EditText>(R.id.edt_bank_name)
|
||||
// val spinnerBankName = dialogView.findViewById<Spinner>(R.id.spinner_bank_name)
|
||||
val bankNumberEditText = dialogView.findViewById<EditText>(R.id.edt_bank_number)
|
||||
val accountNameEditText = dialogView.findViewById<EditText>(R.id.edt_account_name)
|
||||
val qrisPreview = dialogView.findViewById<ImageView>(R.id.iv_qris_preview)
|
||||
@ -185,7 +228,10 @@ class PaymentInfoActivity : AppCompatActivity() {
|
||||
|
||||
// When reopening, restore the previously entered values
|
||||
if (isReopened) {
|
||||
bankNameEditText.setText(savedBankName)
|
||||
val savedPosition = bankAdapter.findPositionByName(savedBankName)
|
||||
if (savedPosition >= 0) {
|
||||
spinnerBankName.setSelection(savedPosition)
|
||||
}
|
||||
bankNumberEditText.setText(savedBankNumber)
|
||||
accountNameEditText.setText(savedAccountName)
|
||||
|
||||
@ -199,7 +245,7 @@ class PaymentInfoActivity : AppCompatActivity() {
|
||||
|
||||
btnAddQris.setOnClickListener {
|
||||
// Save the current values before dismissing
|
||||
savedBankName = bankNameEditText.text.toString().trim()
|
||||
savedBankName = viewModel.selectedBankName ?: ""
|
||||
savedBankNumber = bankNumberEditText.text.toString().trim()
|
||||
savedAccountName = accountNameEditText.text.toString().trim()
|
||||
|
||||
@ -212,13 +258,13 @@ class PaymentInfoActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
btnSave.setOnClickListener {
|
||||
val bankName = bankNameEditText.text.toString().trim()
|
||||
val bankName = viewModel.selectedBankName ?: ""
|
||||
val bankNumber = bankNumberEditText.text.toString().trim()
|
||||
val accountName = accountNameEditText.text.toString().trim()
|
||||
|
||||
// Validation
|
||||
if (bankName.isEmpty()) {
|
||||
showSnackbar("Nama bank tidak boleh kosong")
|
||||
showSnackbar("Pilih nama bank terlebih dahulu")
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
|
||||
@ -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", "")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.alya.ecommerce_serang.data.api.response.store.sells.OrdersItem
|
||||
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
|
||||
@ -16,12 +17,14 @@ import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.data.repository.SellsRepository
|
||||
import com.alya.ecommerce_serang.databinding.FragmentSellsListBinding
|
||||
import com.alya.ecommerce_serang.ui.order.address.ViewState
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.MyStoreActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.sells.payment.DetailPaymentActivity
|
||||
import com.alya.ecommerce_serang.ui.profile.mystore.sells.shipment.DetailShipmentActivity
|
||||
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
|
||||
import com.alya.ecommerce_serang.utils.SessionManager
|
||||
import com.alya.ecommerce_serang.utils.viewmodel.SellsViewModel
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class SellsListFragment : Fragment() {
|
||||
|
||||
@ -84,6 +87,7 @@ class SellsListFragment : Fragment() {
|
||||
observeSellsList()
|
||||
observePaymentConfirmation()
|
||||
loadSells()
|
||||
// getAllOrderCountsAndNavigate()
|
||||
}
|
||||
|
||||
private fun setupRecyclerView() {
|
||||
@ -183,6 +187,30 @@ class SellsListFragment : Fragment() {
|
||||
context.startActivity(intent)
|
||||
}
|
||||
|
||||
private fun getAllOrderCountsAndNavigate() {
|
||||
lifecycleScope.launch {
|
||||
try {
|
||||
// Show loading if needed
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
|
||||
val allCounts = viewModel.getAllStatusCounts()
|
||||
|
||||
binding.progressBar.visibility = View.GONE
|
||||
|
||||
val intent = Intent(requireContext(), MyStoreActivity::class.java)
|
||||
intent.putExtra("total_unpaid", allCounts["unpaid"])
|
||||
intent.putExtra("total_paid", allCounts["paid"])
|
||||
intent.putExtra("total_processed", allCounts["processed"])
|
||||
Log.d("SellsListFragment", "Total orders: unpaid=${allCounts["unpaid"]}, processed=${allCounts["processed"]}, Paid=${allCounts["paid"]}")
|
||||
|
||||
|
||||
} catch (e: Exception) {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
Log.e(TAG, "Error getting order counts: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
|
||||
@ -8,13 +8,46 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.util.zip.GZIPOutputStream
|
||||
|
||||
object FileUtils {
|
||||
private const val TAG = "FileUtils"
|
||||
|
||||
/**
|
||||
* Creates a temporary file from a URI in the app's cache directory
|
||||
* Compress a file to GZIP format to reduce its size to below 1MB
|
||||
* @param context The context
|
||||
* @param uri The URI of the file to compress
|
||||
* @param maxSize The target size limit in bytes (1MB = 1048576 bytes)
|
||||
* @return The compressed file, or null if compression failed
|
||||
*/
|
||||
fun compressFile(context: Context, uri: Uri, maxSize: Long = 1048576L): File? {
|
||||
try {
|
||||
// Create a temporary file for compressed content
|
||||
val originalFile = createTempFileFromUri(context, uri, "compressed")
|
||||
val compressedFile = File(context.cacheDir, "compressed_${System.currentTimeMillis()}.gz")
|
||||
|
||||
// Compress the original file into the GZIP file
|
||||
compressToGZIP(originalFile, compressedFile)
|
||||
|
||||
// Check if the compressed file is larger than the allowed size
|
||||
if (compressedFile.length() <= maxSize) {
|
||||
Log.d(TAG, "Compression successful. Compressed file size: ${compressedFile.length()} bytes.")
|
||||
return compressedFile
|
||||
} else {
|
||||
// If the file is still too large, you can handle it by reducing quality or adjusting compression logic
|
||||
Log.e(TAG, "Compressed file exceeds the size limit. Size: ${compressedFile.length()} bytes.")
|
||||
return null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error during file compression: ${e.message}", e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a temporary file from the URI in the app's cache directory.
|
||||
*/
|
||||
fun createTempFileFromUri(context: Context, uri: Uri, prefix: String = "temp"): File? {
|
||||
try {
|
||||
@ -41,6 +74,23 @@ object FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compress the input file into a GZIP file.
|
||||
*/
|
||||
private fun compressToGZIP(inputFile: File?, outputFile: File) {
|
||||
FileInputStream(inputFile).use { inputStream ->
|
||||
FileOutputStream(outputFile).use { fileOutputStream ->
|
||||
GZIPOutputStream(fileOutputStream).use { gzipOutputStream ->
|
||||
val buffer = ByteArray(1024)
|
||||
var bytesRead: Int
|
||||
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
|
||||
gzipOutputStream.write(buffer, 0, bytesRead)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the file extension from a URI using ContentResolver
|
||||
*/
|
||||
|
||||
@ -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.dto.StoreAddress
|
||||
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() {
|
||||
@ -21,8 +23,8 @@ class AddressViewModel(private val addressRepository: AddressRepository) : ViewM
|
||||
private val _cities = MutableLiveData<List<City>>()
|
||||
val cities: LiveData<List<City>> = _cities
|
||||
|
||||
private val _storeAddress = MutableLiveData<StoreAddress?>()
|
||||
val storeAddress: LiveData<StoreAddress?> = _storeAddress
|
||||
private val _storeAddress = MutableLiveData<AddressesItem?>()
|
||||
val storeAddress: LiveData<AddressesItem?> get() = _storeAddress
|
||||
|
||||
private val _isLoading = MutableLiveData<Boolean>()
|
||||
val isLoading: LiveData<Boolean> = _isLoading
|
||||
@ -31,99 +33,249 @@ class AddressViewModel(private val addressRepository: AddressRepository) : ViewM
|
||||
val errorMessage: LiveData<String> = _errorMessage
|
||||
|
||||
private val _saveSuccess = MutableLiveData<Boolean>()
|
||||
val saveSuccess: LiveData<Boolean> = _saveSuccess
|
||||
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() {
|
||||
Log.d(TAG, "fetchProvinces() called")
|
||||
_isLoading.value = true
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
Log.d(TAG, "Calling addressRepository.getProvinces()")
|
||||
val response = addressRepository.getProvinces()
|
||||
Log.d(TAG, "Received provinces response: ${response.size} provinces")
|
||||
_provinces.value = response
|
||||
_isLoading.value = false
|
||||
if (response.isSuccessful) {
|
||||
_provinces.value = response.body()?.data ?: emptyList()
|
||||
} else {
|
||||
Log.e("EditAddressVM", "Failed to get provinces: ${response.message()}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error fetching provinces", e)
|
||||
_errorMessage.value = "Failed to load provinces: ${e.message}"
|
||||
_isLoading.value = false
|
||||
Log.e("EditAddressVM", "Error getting provinces: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun fetchCities(provinceId: String) {
|
||||
Log.d(TAG, "fetchCities() called with provinceId: $provinceId")
|
||||
_isLoading.value = true
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
Log.d(TAG, "Calling addressRepository.getCities()")
|
||||
val response = addressRepository.getCities(provinceId)
|
||||
Log.d(TAG, "Received cities response: ${response.size} cities")
|
||||
_cities.value = response
|
||||
_isLoading.value = false
|
||||
if (response.isSuccessful) {
|
||||
_cities.value = response.body()?.cities ?: emptyList()
|
||||
} else {
|
||||
Log.e("EditAddressVM", "Failed to get cities: ${response.message()}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error fetching cities", e)
|
||||
_errorMessage.value = "Failed to load cities: ${e.message}"
|
||||
_isLoading.value = false
|
||||
Log.e("EditAddressVM", "Error getting cities: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
// viewModelScope.launch {
|
||||
// try {
|
||||
// Log.d(TAG, "Calling addressRepository.getProvinces()")
|
||||
// val response = addressRepository.getProvinces()
|
||||
// Log.d(TAG, "Received provinces response: ${response.size} provinces")
|
||||
// _provinces.value = response
|
||||
// _isLoading.value = false
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Error fetching provinces", e)
|
||||
// _errorMessage.value = "Failed to load provinces: ${e.message}"
|
||||
// _isLoading.value = false
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// fun fetchCities(provinceId: String) {
|
||||
// Log.d(TAG, "fetchCities() called with provinceId: $provinceId")
|
||||
// _isLoading.value = true
|
||||
// viewModelScope.launch {
|
||||
// try {
|
||||
// selecte
|
||||
// Log.d(TAG, "Calling addressRepository.getCities()")
|
||||
// val response = addressRepository.getCities(provinceId)
|
||||
// Log.d(TAG, "Received cities response: ${response.size} cities")
|
||||
// _cities.value = response
|
||||
// _isLoading.value = false
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Error fetching cities", e)
|
||||
// _errorMessage.value = "Failed to load cities: ${e.message}"
|
||||
// _isLoading.value = false
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// fun fetchStoreAddress() {
|
||||
// Log.d(TAG, "fetchStoreAddress() called")
|
||||
// _isLoading.value = true
|
||||
// viewModelScope.launch {
|
||||
// try {
|
||||
// Log.d(TAG, "Calling addressRepository.getStoreAddress()")
|
||||
// val response = addressRepository.getStoreAddress()
|
||||
// Log.d(TAG, "Received store address response: $response")
|
||||
// _storeAddress.value = response
|
||||
// _isLoading.value = false
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Error fetching store address", e)
|
||||
// _errorMessage.value = "Failed to load store address: ${e.message}"
|
||||
// _isLoading.value = false
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// fun fetchStoreAddress() {
|
||||
// viewModelScope.launch {
|
||||
// try {
|
||||
// val response = addressRepository.getStoreAddress()
|
||||
// if (response.isSuccessful) {
|
||||
// val storeAddress = response.body()?.addresses
|
||||
// ?.firstOrNull { it.isStoreLocation == true }
|
||||
//
|
||||
// if (storeAddress != null) {
|
||||
// _storeAddress.value = storeAddress
|
||||
// } else {
|
||||
// Log.d("EditAddressVM", "No store address found")
|
||||
// }
|
||||
// } else {
|
||||
// Log.e("EditAddressVM", "Failed to get addresses: ${response.message()}")
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// Log.e("EditAddressVM", "Error: ${e.message}")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
fun fetchStoreAddress() {
|
||||
Log.d(TAG, "fetchStoreAddress() called")
|
||||
_isLoading.value = true
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
Log.d(TAG, "Calling addressRepository.getStoreAddress()")
|
||||
val response = addressRepository.getStoreAddress()
|
||||
Log.d(TAG, "Received store address response: $response")
|
||||
_storeAddress.value = response
|
||||
_isLoading.value = false
|
||||
if (response.isSuccessful) {
|
||||
val storeAddress = response.body()?.addresses
|
||||
?.firstOrNull { it.isStoreLocation == true }
|
||||
|
||||
if (storeAddress != null) {
|
||||
_storeAddress.value = storeAddress
|
||||
} else {
|
||||
Log.d(TAG, "No store address found")
|
||||
}
|
||||
} else {
|
||||
Log.e(TAG, "Failed to get addresses: ${response.message()}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error fetching store address", e)
|
||||
_errorMessage.value = "Failed to load store address: ${e.message}"
|
||||
_isLoading.value = false
|
||||
Log.e(TAG, "Error: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun saveStoreAddress(
|
||||
provinceId: String,
|
||||
provinceName: String,
|
||||
cityId: String,
|
||||
cityName: String,
|
||||
street: String,
|
||||
subdistrict: String,
|
||||
detail: String,
|
||||
postalCode: String,
|
||||
latitude: Double,
|
||||
longitude: Double
|
||||
) {
|
||||
Log.d(TAG, "saveStoreAddress() called with provinceId: $provinceId, cityId: $cityId")
|
||||
_isLoading.value = true
|
||||
// fun saveStoreAddress(
|
||||
// provinceId: String,
|
||||
// provinceName: String,
|
||||
// cityId: String,
|
||||
// cityName: String,
|
||||
// street: String,
|
||||
// subdistrict: String,
|
||||
// detail: String,
|
||||
// postalCode: String,
|
||||
// latitude: Double,
|
||||
// longitude: Double
|
||||
// ) {
|
||||
// Log.d(TAG, "saveStoreAddress() called with provinceId: $provinceId, cityId: $cityId")
|
||||
// _isLoading.value = true
|
||||
// viewModelScope.launch {
|
||||
// try {
|
||||
// Log.d(TAG, "Calling addressRepository.saveStoreAddress()")
|
||||
// val success = addressRepository.saveStoreAddress(
|
||||
// provinceId = provinceId,
|
||||
// provinceName = provinceName,
|
||||
// cityId = cityId,
|
||||
// cityName = cityName,
|
||||
// street = street,
|
||||
// subdistrict = subdistrict,
|
||||
// detail = detail,
|
||||
// postalCode = postalCode,
|
||||
// latitude = latitude,
|
||||
// longitude = longitude
|
||||
// )
|
||||
// Log.d(TAG, "Save store address result: $success")
|
||||
// _saveSuccess.value = success
|
||||
// _isLoading.value = false
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, "Error saving store address", e)
|
||||
// _errorMessage.value = "Failed to save address: ${e.message}"
|
||||
// _isLoading.value = false
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
fun saveStoreAddress(oldAddress: AddressesItem, newAddress: AddressesItem) {
|
||||
val params = buildUpdateBody(oldAddress, newAddress)
|
||||
if (params.isEmpty()) {
|
||||
Log.d(TAG, "No changes detected")
|
||||
_saveSuccess.value = false
|
||||
return
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
Log.d(TAG, "Calling addressRepository.saveStoreAddress()")
|
||||
val success = addressRepository.saveStoreAddress(
|
||||
provinceId = provinceId,
|
||||
provinceName = provinceName,
|
||||
cityId = cityId,
|
||||
cityName = cityName,
|
||||
street = street,
|
||||
subdistrict = subdistrict,
|
||||
detail = detail,
|
||||
postalCode = postalCode,
|
||||
latitude = latitude,
|
||||
longitude = longitude
|
||||
)
|
||||
Log.d(TAG, "Save store address result: $success")
|
||||
_saveSuccess.value = success
|
||||
_isLoading.value = false
|
||||
val response = addressRepository.updateAddress(oldAddress.id, params)
|
||||
_saveSuccess.value = response.isSuccessful
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error saving store address", e)
|
||||
_errorMessage.value = "Failed to save address: ${e.message}"
|
||||
_isLoading.value = false
|
||||
Log.e(TAG, "Error: ${e.message}")
|
||||
_saveSuccess.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildUpdateBody(oldAddress: AddressesItem, newAddress: AddressesItem): Map<String, Any> {
|
||||
|
||||
val params = mutableMapOf<String, Any>()
|
||||
|
||||
fun addIfChanged(key: String, oldValue: Any?, newValue: Any?) {
|
||||
if (newValue != null && newValue != oldValue) {
|
||||
params[key] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
addIfChanged("street", oldAddress.street, newAddress.street)
|
||||
addIfChanged("province_id", oldAddress.provinceId, newAddress.provinceId)
|
||||
addIfChanged("detail", oldAddress.detail, newAddress.detail)
|
||||
addIfChanged("subdistrict", oldAddress.subdistrict, newAddress.subdistrict)
|
||||
addIfChanged("city_id", oldAddress.cityId, newAddress.cityId)
|
||||
addIfChanged("village_id", oldAddress.villageId, newAddress.villageId)
|
||||
addIfChanged("postal_code", oldAddress.postalCode, newAddress.postalCode)
|
||||
addIfChanged("phone", oldAddress.phone, newAddress.phone)
|
||||
addIfChanged("recipient", oldAddress.recipient, newAddress.recipient)
|
||||
addIfChanged("latitude", oldAddress.latitude, newAddress.latitude)
|
||||
addIfChanged("longitude", oldAddress.longitude, newAddress.longitude)
|
||||
addIfChanged("is_store_location", oldAddress.isStoreLocation, newAddress.isStoreLocation)
|
||||
|
||||
return params
|
||||
}
|
||||
}
|
||||
@ -7,8 +7,10 @@ import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.alya.ecommerce_serang.data.api.dto.FcmReq
|
||||
import com.alya.ecommerce_serang.data.api.dto.ResetPassReq
|
||||
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.ResetPassResponse
|
||||
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
|
||||
@ -27,6 +29,10 @@ class LoginViewModel(private val repository: UserRepository, private val context
|
||||
private val _message = MutableLiveData<String>()
|
||||
val message: LiveData<String> = _message
|
||||
|
||||
private val _resetPasswordState = MutableLiveData<Result<ResetPassResponse>?>()
|
||||
val resetPasswordState: LiveData<Result<ResetPassResponse>?> = _resetPasswordState
|
||||
|
||||
|
||||
private val sessionManager by lazy { SessionManager(context) }
|
||||
|
||||
private fun getAuthenticatedApiService(): ApiService {
|
||||
@ -69,4 +75,19 @@ class LoginViewModel(private val repository: UserRepository, private val context
|
||||
}
|
||||
}
|
||||
|
||||
fun resetPassword(email: String) {
|
||||
viewModelScope.launch {
|
||||
_resetPasswordState.value = Result.Loading
|
||||
|
||||
val request = ResetPassReq(emailOrPhone = email)
|
||||
val result = repository.resetPassword(request)
|
||||
|
||||
_resetPasswordState.value = result
|
||||
}
|
||||
}
|
||||
|
||||
fun clearState() {
|
||||
_resetPasswordState.value = null
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,11 +1,15 @@
|
||||
package com.alya.ecommerce_serang.utils.viewmodel
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.map
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.alya.ecommerce_serang.data.api.dto.ProductsItem
|
||||
import com.alya.ecommerce_serang.data.api.dto.Store
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.StoreTypesItem
|
||||
import com.alya.ecommerce_serang.data.api.response.store.StoreResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.store.profile.StoreDataResponse
|
||||
import com.alya.ecommerce_serang.data.repository.MyStoreRepository
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
@ -13,8 +17,12 @@ import kotlinx.coroutines.launch
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
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
|
||||
|
||||
@ -30,6 +38,12 @@ class MyStoreViewModel(private val repository: MyStoreRepository): ViewModel() {
|
||||
private val _errorMessage = MutableLiveData<String>()
|
||||
val errorMessage : LiveData<String> = _errorMessage
|
||||
|
||||
private val _balanceResult = MutableLiveData<Result<StoreResponse>>()
|
||||
val balanceResult: LiveData<Result<StoreResponse>> get() = _balanceResult
|
||||
|
||||
private val _productList = MutableLiveData<Result<List<ProductsItem>>>()
|
||||
val productList: LiveData<Result<List<ProductsItem>>> get() = _productList
|
||||
|
||||
fun loadMyStore(){
|
||||
viewModelScope.launch {
|
||||
when (val result = repository.fetchMyStoreProfile()){
|
||||
@ -72,30 +86,102 @@ 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getTotalOrdersByStatus(status: String): Int {
|
||||
return try {
|
||||
when (val result = repository.getSellList(status)) {
|
||||
is Result.Success -> {
|
||||
// Access the orders list from the response
|
||||
result.data.orders.size ?: 0
|
||||
}
|
||||
is Result.Error -> {
|
||||
Log.e(TAG, "Error getting orders count: ${result.exception.message}")
|
||||
0
|
||||
}
|
||||
is Result.Loading -> 0
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Exception getting orders count", e)
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
//count the order
|
||||
suspend fun getAllStatusCounts(): Map<String, Int> {
|
||||
val statuses = listOf( "unpaid", "paid", "processed")
|
||||
val counts = mutableMapOf<String, Int>()
|
||||
|
||||
statuses.forEach { status ->
|
||||
counts[status] = getTotalOrdersByStatus(status)
|
||||
Log.d(TAG, "Status: $status, countOrder=${counts[status]}")
|
||||
}
|
||||
|
||||
return counts
|
||||
}
|
||||
|
||||
val formattedBalance: LiveData<String> = balanceResult.map { result ->
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
val raw = result.data.store.balance.toDouble()
|
||||
NumberFormat.getCurrencyInstance(Locale("in", "ID")).format(raw)
|
||||
}
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
|
||||
/** Trigger the network call */
|
||||
fun fetchBalance() {
|
||||
viewModelScope.launch {
|
||||
_balanceResult.value = Result.Loading
|
||||
_balanceResult.value = repository.getBalance()
|
||||
}
|
||||
}
|
||||
|
||||
fun loadMyStoreProducts() {
|
||||
viewModelScope.launch {
|
||||
_productList.value = Result.Loading
|
||||
try {
|
||||
val result = repository.fetchMyStoreProducts()
|
||||
_productList.value = Result.Success(result)
|
||||
} catch (e: Exception) {
|
||||
_productList.value = Result.Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,6 +30,9 @@ class PaymentInfoViewModel(private val repository: PaymentInfoRepository) : View
|
||||
private val _deletePaymentSuccess = MutableLiveData<Boolean>()
|
||||
val deletePaymentSuccess: LiveData<Boolean> = _deletePaymentSuccess
|
||||
|
||||
var selectedBankName: String? = null
|
||||
val bankName = MutableLiveData<String>()
|
||||
|
||||
fun getPaymentInfo() {
|
||||
_isLoading.value = true
|
||||
viewModelScope.launch {
|
||||
|
||||
@ -47,15 +47,15 @@ class ProfileViewModel(private val userRepository: UserRepository) : ViewModel()
|
||||
val response: HasStoreResponse = userRepository.checkStore()
|
||||
|
||||
// Log and store success message
|
||||
Log.d("RegisterViewModel", "OTP Response: ${response.hasStore}")
|
||||
_checkStore.value = response.hasStore // Store the message for UI feedback
|
||||
Log.d("ProfileViewModel", "Has store: ${response.hasStore}")
|
||||
_checkStore.postValue(response.hasStore) // Store the message for UI feedback
|
||||
|
||||
} catch (exception: Exception) {
|
||||
// Handle any errors and update state
|
||||
_checkStore.value = false
|
||||
_checkStore.postValue(false)
|
||||
|
||||
// Log the error for debugging
|
||||
Log.e("RegisterViewModel", "Error:", exception)
|
||||
Log.e(":ProfileViewModel", "Error:", exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,6 +11,8 @@ import com.alya.ecommerce_serang.data.api.response.auth.RegisterStoreResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.StoreTypesItem
|
||||
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.response.customer.order.SubdistrictsItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.VillagesItem
|
||||
import com.alya.ecommerce_serang.data.repository.Result
|
||||
import com.alya.ecommerce_serang.data.repository.UserRepository
|
||||
import com.alya.ecommerce_serang.utils.ImageUtils
|
||||
@ -41,8 +43,17 @@ class RegisterStoreViewModel(
|
||||
private val _citiesState = MutableLiveData<Result<List<CitiesItem>>>()
|
||||
val citiesState: LiveData<Result<List<CitiesItem>>> = _citiesState
|
||||
|
||||
private val _subdistrictState = MutableLiveData<Result<List<SubdistrictsItem>>>()
|
||||
val subdistrictState: LiveData<Result<List<SubdistrictsItem>>> = _subdistrictState
|
||||
|
||||
private val _villagesState = MutableLiveData<Result<List<VillagesItem>>>()
|
||||
val villagesState: LiveData<Result<List<VillagesItem>>> = _villagesState
|
||||
|
||||
var selectedProvinceId: Int? = null
|
||||
var selectedCityId: Int? = null
|
||||
var selectedCityId: String? = null
|
||||
var selectedSubdistrict: String? = null
|
||||
var selectedVillages: String? = null
|
||||
var selectedBankName: String? = null
|
||||
|
||||
// Form fields
|
||||
val storeName = MutableLiveData<String>()
|
||||
@ -52,7 +63,7 @@ class RegisterStoreViewModel(
|
||||
val longitude = MutableLiveData<String>()
|
||||
val street = MutableLiveData<String>()
|
||||
val subdistrict = MutableLiveData<String>()
|
||||
val cityId = MutableLiveData<Int>()
|
||||
val cityId = MutableLiveData<String>()
|
||||
val provinceId = MutableLiveData<Int>()
|
||||
val postalCode = MutableLiveData<Int>()
|
||||
val addressDetail = MutableLiveData<String>()
|
||||
@ -72,47 +83,89 @@ class RegisterStoreViewModel(
|
||||
val selectedCouriers = mutableListOf<String>()
|
||||
|
||||
fun registerStore(context: Context) {
|
||||
Log.d(TAG, "Starting registerStore()")
|
||||
|
||||
val allowedFileTypes = Regex("^(jpeg|jpg|png|pdf)$", RegexOption.IGNORE_CASE)
|
||||
|
||||
// Check each file if present
|
||||
fun logFileInfo(label: String, uri: Uri?) {
|
||||
if (uri == null) {
|
||||
Log.d(TAG, "$label URI: null")
|
||||
return
|
||||
}
|
||||
Log.d(TAG, "$label URI: $uri")
|
||||
try {
|
||||
val fileSizeBytes = context.contentResolver.openFileDescriptor(uri, "r")?.use {
|
||||
it.statSize
|
||||
} ?: -1
|
||||
Log.d(TAG, "$label original size: ${fileSizeBytes / 1024} KB")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error getting size for $label", e)
|
||||
}
|
||||
}
|
||||
|
||||
// Log all file info before validation
|
||||
logFileInfo("Store Image", storeImageUri)
|
||||
logFileInfo("KTP", ktpUri)
|
||||
logFileInfo("NPWP", npwpUri)
|
||||
logFileInfo("NIB", nibUri)
|
||||
logFileInfo("Persetujuan", persetujuanUri)
|
||||
logFileInfo("QRIS", qrisUri)
|
||||
|
||||
// Check file types
|
||||
if (storeImageUri != null && !ImageUtils.isAllowedFileType(context, storeImageUri, allowedFileTypes)) {
|
||||
_errorMessage.value = "Foto toko harus berupa file JPEG, JPG, atau PNG"
|
||||
Log.e(TAG, _errorMessage.value ?: "Invalid file type for store image")
|
||||
_registerState.value = Result.Error(Exception(_errorMessage.value ?: "Invalid file type"))
|
||||
return
|
||||
}
|
||||
|
||||
if (ktpUri != null && !ImageUtils.isAllowedFileType(context, ktpUri, allowedFileTypes)) {
|
||||
_errorMessage.value = "KTP harus berupa file JPEG, JPG, atau PNG"
|
||||
Log.e(TAG, _errorMessage.value ?: "Invalid file type for KTP")
|
||||
_registerState.value = Result.Error(Exception(_errorMessage.value ?: "Invalid file type"))
|
||||
return
|
||||
}
|
||||
|
||||
if (npwpUri != null && !ImageUtils.isAllowedFileType(context, npwpUri, allowedFileTypes)) {
|
||||
_errorMessage.value = "NPWP harus berupa file JPEG, JPG, PNG, atau PDF"
|
||||
Log.e(TAG, _errorMessage.value ?: "Invalid file type for NPWP")
|
||||
_registerState.value = Result.Error(Exception(_errorMessage.value ?: "Invalid file type"))
|
||||
return
|
||||
}
|
||||
|
||||
if (nibUri != null && !ImageUtils.isAllowedFileType(context, nibUri, allowedFileTypes)) {
|
||||
_errorMessage.value = "NIB harus berupa file JPEG, JPG, PNG, atau PDF"
|
||||
Log.e(TAG, _errorMessage.value ?: "Invalid file type for NIB")
|
||||
_registerState.value = Result.Error(Exception(_errorMessage.value ?: "Invalid file type"))
|
||||
return
|
||||
}
|
||||
|
||||
if (persetujuanUri != null && !ImageUtils.isAllowedFileType(context, persetujuanUri, allowedFileTypes)) {
|
||||
_errorMessage.value = "Persetujuan harus berupa file JPEG, JPG, PNG, atau PDF"
|
||||
Log.e(TAG, _errorMessage.value ?: "Invalid file type for Persetujuan")
|
||||
_registerState.value = Result.Error(Exception(_errorMessage.value ?: "Invalid file type"))
|
||||
return
|
||||
}
|
||||
|
||||
if (qrisUri != null && !ImageUtils.isAllowedFileType(context, qrisUri, allowedFileTypes)) {
|
||||
_errorMessage.value = "QRIS harus berupa file JPEG, JPG, PNG, atau PDF"
|
||||
Log.e(TAG, _errorMessage.value ?: "Invalid file type for QRIS")
|
||||
_registerState.value = Result.Error(Exception(_errorMessage.value ?: "Invalid file type"))
|
||||
return
|
||||
}
|
||||
|
||||
Log.d(TAG, "File type checks passed. Starting repository.registerStoreUser() call.")
|
||||
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_registerState.value = Result.Loading
|
||||
Log.d(TAG, "Register store request payload: " +
|
||||
"storeName=${storeName.value}, storeTypeId=${storeTypeId.value}, " +
|
||||
"lat=${latitude.value}, long=${longitude.value}, " +
|
||||
"street=${street.value}, subdistrict=${subdistrict.value}, " +
|
||||
"cityId=${cityId.value}, provinceId=${provinceId.value}, postalCode=${postalCode.value}, " +
|
||||
"bankName=${bankName.value}, bankNum=${bankNumber.value}, accountName=${accountName.value}, " +
|
||||
"selectedCouriers=$selectedCouriers")
|
||||
|
||||
val result = repository.registerStoreUser(
|
||||
context = context,
|
||||
@ -122,7 +175,7 @@ class RegisterStoreViewModel(
|
||||
longitude = longitude.value ?: "",
|
||||
street = street.value ?: "",
|
||||
subdistrict = subdistrict.value ?: "",
|
||||
cityId = cityId.value ?: 0,
|
||||
cityId = cityId.value ?: "",
|
||||
provinceId = provinceId.value ?: 0,
|
||||
postalCode = postalCode.value ?: 0,
|
||||
detail = addressDetail.value ?: "",
|
||||
@ -139,25 +192,15 @@ class RegisterStoreViewModel(
|
||||
accountName = accountName.value ?: ""
|
||||
)
|
||||
|
||||
Log.d(TAG, "Repository returned result: $result")
|
||||
_registerState.value = result
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Exception during registerStore", e)
|
||||
_registerState.value = Result.Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// // Helper function to convert Uri to File
|
||||
// private fun getFileFromUri(context: Context, uri: Uri): File {
|
||||
// val inputStream = context.contentResolver.openInputStream(uri)
|
||||
// val tempFile = File(context.cacheDir, "temp_file_${System.currentTimeMillis()}")
|
||||
// inputStream?.use { input ->
|
||||
// tempFile.outputStream().use { output ->
|
||||
// input.copyTo(output)
|
||||
// }
|
||||
// }
|
||||
// return tempFile
|
||||
// }
|
||||
|
||||
fun validateForm(): Boolean {
|
||||
// Implement form validation logic
|
||||
return !(storeName.value.isNullOrEmpty() ||
|
||||
@ -174,8 +217,6 @@ class RegisterStoreViewModel(
|
||||
nibUri == null)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Function to fetch store types
|
||||
fun fetchStoreTypes() {
|
||||
_isLoadingType.value = true
|
||||
@ -235,6 +276,52 @@ class RegisterStoreViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
fun getSubdistrict(cityId: String) {
|
||||
_subdistrictState.value = Result.Loading
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
|
||||
selectedSubdistrict = cityId
|
||||
val result = repository.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 getVillages(subdistrictId: String) {
|
||||
_villagesState.value = Result.Loading
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
|
||||
selectedVillages = subdistrictId
|
||||
val result = repository.getListVillages(subdistrictId)
|
||||
result?.let {
|
||||
_villagesState.postValue(Result.Success(it.villages))
|
||||
Log.d(TAG, "Cities loaded for province $subdistrictId: ${it.villages.size}")
|
||||
} ?: run {
|
||||
_villagesState.postValue(Result.Error(Exception("Failed to load cities")))
|
||||
Log.e(TAG, "City result was null for province $subdistrictId")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_villagesState.postValue(Result.Error(Exception(e.message ?: "Error loading cities")))
|
||||
Log.e(TAG, "Error fetching cities for province $subdistrictId", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun isBankSelected(): Boolean {
|
||||
return !selectedBankName.isNullOrEmpty()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "RegisterStoreUserViewModel"
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.alya.ecommerce_serang.data.api.dto.CreateAddressRequest
|
||||
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.VerifRegisReq
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.LoginResponse
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.OtpResponse
|
||||
@ -16,6 +17,8 @@ import com.alya.ecommerce_serang.data.api.response.auth.User
|
||||
import com.alya.ecommerce_serang.data.api.response.auth.VerifRegisterResponse
|
||||
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.response.customer.order.SubdistrictsItem
|
||||
import com.alya.ecommerce_serang.data.api.response.customer.order.VillagesItem
|
||||
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.OrderRepository
|
||||
@ -62,17 +65,30 @@ class RegisterViewModel(private val repository: UserRepository, private val orde
|
||||
private val _registeredUser = MutableLiveData<User>()
|
||||
val registeredUser: LiveData<User> = _registeredUser
|
||||
|
||||
private val _toastMessage = MutableLiveData<com.alya.ecommerce_serang.utils.viewmodel.Event<String>>()
|
||||
val toastMessage: LiveData<com.alya.ecommerce_serang.utils.viewmodel.Event<String>> = _toastMessage
|
||||
|
||||
// For address data
|
||||
var selectedProvinceId: Int? = null
|
||||
var selectedCityId: Int? = null
|
||||
var selectedCityId: String? = null
|
||||
var selectedSubdistrict: String? = null
|
||||
var subdistrictName: String? = null
|
||||
var selectedVillages: String? = null
|
||||
var selectedPostalCode: String? = null
|
||||
|
||||
// For provinces and cities
|
||||
// For provinces and cities using raja ongkir
|
||||
private val _provincesState = MutableLiveData<ViewState<List<ProvincesItem>>>()
|
||||
val provincesState: LiveData<ViewState<List<ProvincesItem>>> = _provincesState
|
||||
|
||||
private val _citiesState = MutableLiveData<ViewState<List<CitiesItem>>>()
|
||||
val citiesState: LiveData<ViewState<List<CitiesItem>>> = _citiesState
|
||||
|
||||
private val _subdistrictState = MutableLiveData<ViewState<List<SubdistrictsItem>>>()
|
||||
val subdistrictState: LiveData<ViewState<List<SubdistrictsItem>>> = _subdistrictState
|
||||
|
||||
private val _villagesState = MutableLiveData<ViewState<List<VillagesItem>>>()
|
||||
val villagesState: LiveData<ViewState<List<VillagesItem>>> = _villagesState
|
||||
|
||||
// For address submission
|
||||
private val _addressSubmissionState = MutableLiveData<ViewState<String>>()
|
||||
val addressSubmissionState: LiveData<ViewState<String>> = _addressSubmissionState
|
||||
@ -213,16 +229,23 @@ class RegisterViewModel(private val repository: UserRepository, private val orde
|
||||
Log.d("RegisterViewModel", "OTP Response: ${response.available}")
|
||||
_checkValue.value = Result.Success(response.available)// Store the message for UI feedback
|
||||
|
||||
val msg = if (response.available)
|
||||
"${request.fieldRegis.capitalize()} dapat digunakan"
|
||||
else
|
||||
"${request.fieldRegis.capitalize()} sudah terdaftar"
|
||||
_toastMessage.value = Event(msg)
|
||||
|
||||
} catch (exception: Exception) {
|
||||
// Handle any errors and update state
|
||||
_checkValue.value = Result.Error(exception)
|
||||
_toastMessage.value = Event("Gagal memeriksa ${request.fieldRegis}")
|
||||
|
||||
// Log the error for debugging
|
||||
Log.e("RegisterViewModel", "Error:", exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//using raja ongkir
|
||||
fun getProvinces() {
|
||||
_provincesState.value = ViewState.Loading
|
||||
viewModelScope.launch {
|
||||
@ -242,6 +265,7 @@ class RegisterViewModel(private val repository: UserRepository, private val orde
|
||||
}
|
||||
}
|
||||
|
||||
//kota pake raja ongkir
|
||||
fun getCities(provinceId: Int) {
|
||||
_citiesState.value = ViewState.Loading
|
||||
viewModelScope.launch {
|
||||
@ -263,14 +287,64 @@ class RegisterViewModel(private val repository: UserRepository, private val orde
|
||||
}
|
||||
}
|
||||
|
||||
fun getSubdistrict(cityId: String) {
|
||||
_subdistrictState.value = ViewState.Loading
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
|
||||
selectedSubdistrict = cityId
|
||||
val result = repository.getListSubdistrict(cityId)
|
||||
result?.let {
|
||||
_subdistrictState.postValue(ViewState.Success(it.subdistricts))
|
||||
Log.d(TAG, "Cities loaded for province $cityId: ${it.subdistricts.size}")
|
||||
} ?: run {
|
||||
_subdistrictState.postValue(ViewState.Error("Failed to load cities"))
|
||||
Log.e(TAG, "City result was null for province $cityId")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_subdistrictState.postValue(ViewState.Error(e.message ?: "Error loading cities"))
|
||||
Log.e(TAG, "Error fetching cities for province $cityId", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getVillages(subdistrictId: String) {
|
||||
_villagesState.value = ViewState.Loading
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
|
||||
selectedVillages = subdistrictId
|
||||
val result = repository.getListVillages(subdistrictId)
|
||||
result?.let {
|
||||
_villagesState.postValue(ViewState.Success(it.villages))
|
||||
Log.d(TAG, "Cities loaded for province $subdistrictId: ${it.villages.size}")
|
||||
} ?: run {
|
||||
_villagesState.postValue(ViewState.Error("Failed to load cities"))
|
||||
Log.e(TAG, "City result was null for province $subdistrictId")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_villagesState.postValue(ViewState.Error(e.message ?: "Error loading cities"))
|
||||
Log.e(TAG, "Error fetching cities for province $subdistrictId", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun setSelectedProvinceId(id: Int) {
|
||||
selectedProvinceId = id
|
||||
}
|
||||
|
||||
fun setSelectedCityId(id: Int) {
|
||||
fun updateSelectedCityId(id: String) {
|
||||
selectedCityId = id
|
||||
}
|
||||
|
||||
fun updateSelectedSubdistrict(id: String){
|
||||
selectedSubdistrict = id
|
||||
}
|
||||
|
||||
fun updateSelectedVillages(id: String){
|
||||
selectedVillages = id
|
||||
}
|
||||
|
||||
fun addAddress(request: CreateAddressRequest) {
|
||||
Log.d(TAG, "Starting address submission process")
|
||||
_addressSubmissionState.value = ViewState.Loading
|
||||
@ -310,9 +384,17 @@ class RegisterViewModel(private val repository: UserRepository, private val orde
|
||||
}
|
||||
}
|
||||
|
||||
fun resetPass(request: ResetPassReq){
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "RegisterViewModel"
|
||||
}
|
||||
|
||||
//require auth
|
||||
}
|
||||
|
||||
class Event<out T>(private val data: T) {
|
||||
private var handled = false
|
||||
fun getContentIfNotHandled(): T? = if (handled) null else { handled = true; data }
|
||||
}
|
||||
@ -200,6 +200,38 @@ class SellsViewModel(private val repository: SellsRepository) : ViewModel() {
|
||||
|
||||
Log.d(TAG, "========== getSellList method completed ==========")
|
||||
}
|
||||
//get total order each status
|
||||
suspend fun getTotalOrdersByStatus(status: String): Int {
|
||||
return try {
|
||||
when (val result = repository.getSellList(status)) {
|
||||
is Result.Success -> {
|
||||
// Access the orders list from the response
|
||||
result.data.orders.size ?: 0
|
||||
}
|
||||
is Result.Error -> {
|
||||
Log.e("SellsViewModel", "Error getting orders count: ${result.exception.message}")
|
||||
0
|
||||
}
|
||||
is Result.Loading -> 0
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("SellsViewModel", "Exception getting orders count", e)
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
//count the order
|
||||
suspend fun getAllStatusCounts(): Map<String, Int> {
|
||||
val statuses = listOf( "unpaid", "paid", "processed")
|
||||
val counts = mutableMapOf<String, Int>()
|
||||
|
||||
statuses.forEach { status ->
|
||||
counts[status] = getTotalOrdersByStatus(status)
|
||||
Log.d("SellsViewModel", "Status: $status, countOrder=${counts[status]}")
|
||||
}
|
||||
|
||||
return counts
|
||||
}
|
||||
|
||||
fun getSellDetails(orderId: Int) {
|
||||
Log.d(TAG, "========== Starting getSellDetails ==========")
|
||||
|
||||
9
app/src/main/res/drawable/ic_close_chat.xml
Normal file
9
app/src/main/res/drawable/ic_close_chat.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="14dp"
|
||||
android:height="14dp"
|
||||
android:viewportWidth="14"
|
||||
android:viewportHeight="14">
|
||||
<path
|
||||
android:pathData="M7,8.4L2.1,13.3C1.917,13.483 1.683,13.575 1.4,13.575C1.117,13.575 0.883,13.483 0.7,13.3C0.517,13.116 0.425,12.883 0.425,12.6C0.425,12.316 0.517,12.083 0.7,11.9L5.6,7L0.7,2.1C0.517,1.916 0.425,1.683 0.425,1.4C0.425,1.116 0.517,0.883 0.7,0.7C0.883,0.516 1.117,0.425 1.4,0.425C1.683,0.425 1.917,0.516 2.1,0.7L7,5.6L11.9,0.7C12.083,0.516 12.317,0.425 12.6,0.425C12.883,0.425 13.117,0.516 13.3,0.7C13.483,0.883 13.575,1.116 13.575,1.4C13.575,1.683 13.483,1.916 13.3,2.1L8.4,7L13.3,11.9C13.483,12.083 13.575,12.316 13.575,12.6C13.575,12.883 13.483,13.116 13.3,13.3C13.117,13.483 12.883,13.575 12.6,13.575C12.317,13.575 12.083,13.483 11.9,13.3L7,8.4Z"
|
||||
android:fillColor="#000000"/>
|
||||
</vector>
|
||||
@ -1,170 +1,74 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
<vector
|
||||
android:height="108dp"
|
||||
android:width="108dp"
|
||||
android:viewportHeight="108"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path
|
||||
android:fillColor="#3DDC84"
|
||||
android:pathData="M0,0h108v108h-108z" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9,0L9,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,0L19,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,0L29,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,0L39,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,0L49,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,0L59,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,0L69,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,0L79,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M89,0L89,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M99,0L99,108"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,9L108,9"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,19L108,19"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,29L108,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,39L108,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,49L108,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,59L108,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,69L108,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,79L108,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,89L108,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,99L108,99"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,29L89,29"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,39L89,39"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,49L89,49"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,59L89,59"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,69L89,69"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,79L89,79"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,19L29,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,19L39,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,19L49,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,19L59,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,19L69,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,19L79,89"
|
||||
android:strokeWidth="0.8"
|
||||
android:strokeColor="#33FFFFFF" />
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="#3DDC84"
|
||||
android:pathData="M0,0h108v108h-108z"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M9,0L9,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M19,0L19,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M29,0L29,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M39,0L39,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M49,0L49,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M59,0L59,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M69,0L69,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M79,0L79,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M89,0L89,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M99,0L99,108"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,9L108,9"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,19L108,19"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,29L108,29"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,39L108,39"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,49L108,49"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,59L108,59"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,69L108,69"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,79L108,79"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,89L108,89"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M0,99L108,99"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M19,29L89,29"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M19,39L89,39"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M19,49L89,49"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M19,59L89,59"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M19,69L89,69"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M19,79L89,79"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M29,19L29,89"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M39,19L39,89"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M49,19L49,89"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M59,19L59,89"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M69,19L69,89"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
<path android:fillColor="#00000000" android:pathData="M79,19L79,89"
|
||||
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
|
||||
</vector>
|
||||
|
||||
@ -1,30 +1,20 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:aapt="http://schemas.android.com/aapt"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient
|
||||
android:endX="85.84757"
|
||||
android:endY="92.4963"
|
||||
android:startX="42.9492"
|
||||
android:startY="49.59793"
|
||||
android:type="linear">
|
||||
<item
|
||||
android:color="#44000000"
|
||||
android:offset="0.0" />
|
||||
<item
|
||||
android:color="#00000000"
|
||||
android:offset="1.0" />
|
||||
</gradient>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
android:viewportWidth="64"
|
||||
android:viewportHeight="64">
|
||||
<group android:scaleX="0.6722222"
|
||||
android:scaleY="0.6722222"
|
||||
android:translateX="10.488889"
|
||||
android:translateY="10.488889">
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:fillType="nonZero"
|
||||
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
|
||||
android:strokeWidth="1"
|
||||
android:strokeColor="#00000000" />
|
||||
</vector>
|
||||
android:pathData="M0,0h64v64h-64z"
|
||||
android:fillColor="#489EC6"/>
|
||||
<path
|
||||
android:pathData="M11.868,58C10.523,58 9.399,57.538 8.497,56.614C7.594,55.69 7.144,54.537 7.146,53.155V29.074C5.912,28.14 5.009,26.915 4.438,25.399C3.867,23.883 3.854,22.266 4.4,20.548L7.245,10.924C7.635,9.7 8.264,8.74 9.131,8.044C10.001,7.348 11.075,7 12.354,7H51.536C52.813,7 53.883,7.326 54.747,7.978C55.608,8.63 56.24,9.573 56.641,10.807L59.598,20.545C60.146,22.265 60.134,23.896 59.563,25.438C58.992,26.98 58.089,28.23 56.855,29.188V53.152C56.855,54.534 56.404,55.687 55.501,56.611C54.599,57.535 53.476,57.998 52.133,58H11.868ZM38.433,28C40.311,28 41.712,27.46 42.638,26.38C43.564,25.302 43.954,24.188 43.808,23.038L41.866,10H33.465V22.6C33.465,24.074 33.957,25.342 34.939,26.404C35.922,27.468 37.084,28 38.433,28ZM25.278,28C26.849,28 28.119,27.468 29.088,26.404C30.057,25.34 30.541,24.072 30.541,22.6V10H22.138L20.19,23.269C20.071,24.201 20.468,25.222 21.38,26.332C22.292,27.442 23.594,27.998 25.278,28ZM12.263,28C13.551,28 14.647,27.55 15.55,26.65C16.452,25.75 17.014,24.627 17.234,23.281L19.067,10H12.354C11.716,10 11.209,10.144 10.833,10.432C10.457,10.72 10.176,11.153 9.991,11.731L7.292,21.205C6.812,22.781 7.017,24.308 7.906,25.786C8.795,27.264 10.247,28.002 12.263,28ZM51.738,28C53.488,28 54.886,27.3 55.931,25.9C56.978,24.5 57.237,22.935 56.709,21.205L53.864,11.617C53.676,11.039 53.395,10.625 53.019,10.375C52.642,10.125 52.137,10 51.501,10H44.933L46.767,23.281C46.987,24.627 47.549,25.75 48.451,26.65C49.354,27.55 50.449,28 51.738,28Z"
|
||||
android:fillColor="#ffffff"/>
|
||||
<path
|
||||
android:pathData="M20.382,39V30.6H23.97C24.554,30.6 25.046,30.692 25.446,30.876C25.846,31.052 26.15,31.304 26.358,31.632C26.574,31.952 26.682,32.332 26.682,32.772C26.682,33.196 26.59,33.552 26.406,33.84C26.222,34.128 25.978,34.348 25.674,34.5C25.378,34.652 25.05,34.744 24.69,34.776L24.882,34.632C25.274,34.648 25.618,34.752 25.914,34.944C26.21,35.136 26.442,35.388 26.61,35.7C26.786,36.004 26.874,36.34 26.874,36.708C26.874,37.164 26.766,37.564 26.55,37.908C26.334,38.252 26.018,38.52 25.602,38.712C25.186,38.904 24.682,39 24.09,39H20.382ZM22.182,37.536H23.79C24.19,37.536 24.498,37.448 24.714,37.272C24.938,37.088 25.05,36.824 25.05,36.48C25.05,36.136 24.934,35.868 24.702,35.676C24.478,35.484 24.166,35.388 23.766,35.388H22.182V37.536ZM22.182,34.056H23.658C24.042,34.056 24.334,33.968 24.534,33.792C24.742,33.616 24.846,33.368 24.846,33.048C24.846,32.728 24.742,32.48 24.534,32.304C24.334,32.12 24.038,32.028 23.646,32.028H22.182V34.056ZM28.163,39V32.952H29.963V39H28.163ZM29.063,32.256C28.743,32.256 28.483,32.164 28.283,31.98C28.083,31.796 27.983,31.564 27.983,31.284C27.983,30.996 28.083,30.76 28.283,30.576C28.483,30.392 28.743,30.3 29.063,30.3C29.391,30.3 29.655,30.392 29.855,30.576C30.063,30.76 30.167,30.996 30.167,31.284C30.167,31.564 30.063,31.796 29.855,31.98C29.655,32.164 29.391,32.256 29.063,32.256ZM34.069,39.144C33.501,39.144 33.009,39.056 32.593,38.88C32.186,38.696 31.861,38.448 31.622,38.136C31.389,37.824 31.257,37.472 31.226,37.08H33.014C33.046,37.216 33.102,37.34 33.181,37.452C33.27,37.556 33.386,37.64 33.529,37.704C33.681,37.76 33.849,37.788 34.034,37.788C34.234,37.788 34.394,37.764 34.514,37.716C34.641,37.66 34.737,37.588 34.801,37.5C34.866,37.412 34.897,37.32 34.897,37.224C34.897,37.072 34.849,36.956 34.754,36.876C34.666,36.796 34.534,36.732 34.357,36.684C34.181,36.628 33.97,36.576 33.722,36.528C33.433,36.464 33.146,36.392 32.857,36.312C32.577,36.224 32.326,36.116 32.102,35.988C31.885,35.86 31.709,35.696 31.573,35.496C31.445,35.288 31.382,35.036 31.382,34.74C31.382,34.38 31.482,34.056 31.681,33.768C31.882,33.472 32.169,33.24 32.546,33.072C32.922,32.896 33.377,32.808 33.914,32.808C34.674,32.808 35.27,32.976 35.701,33.312C36.133,33.648 36.389,34.1 36.47,34.668H34.79C34.742,34.508 34.641,34.388 34.489,34.308C34.338,34.22 34.146,34.176 33.914,34.176C33.65,34.176 33.45,34.22 33.313,34.308C33.178,34.396 33.11,34.512 33.11,34.656C33.11,34.752 33.153,34.84 33.242,34.92C33.338,34.992 33.473,35.056 33.65,35.112C33.826,35.168 34.042,35.224 34.298,35.28C34.785,35.384 35.206,35.496 35.557,35.616C35.917,35.736 36.197,35.912 36.397,36.144C36.597,36.368 36.694,36.696 36.686,37.128C36.694,37.52 36.59,37.868 36.374,38.172C36.166,38.476 35.866,38.716 35.473,38.892C35.082,39.06 34.613,39.144 34.069,39.144ZM40.094,39.144C39.59,39.144 39.17,39.064 38.834,38.904C38.506,38.744 38.262,38.528 38.102,38.256C37.95,37.976 37.874,37.668 37.874,37.332C37.874,36.972 37.962,36.656 38.138,36.384C38.322,36.104 38.606,35.884 38.99,35.724C39.374,35.556 39.858,35.472 40.442,35.472H41.906C41.906,35.2 41.87,34.976 41.798,34.8C41.734,34.624 41.626,34.492 41.474,34.404C41.322,34.316 41.114,34.272 40.85,34.272C40.57,34.272 40.334,34.328 40.142,34.44C39.95,34.552 39.83,34.728 39.782,34.968H38.054C38.094,34.536 38.234,34.16 38.474,33.84C38.722,33.52 39.05,33.268 39.458,33.084C39.866,32.9 40.334,32.808 40.862,32.808C41.438,32.808 41.938,32.904 42.362,33.096C42.786,33.28 43.114,33.552 43.346,33.912C43.586,34.272 43.706,34.72 43.706,35.256V39H42.206L41.99,38.124C41.902,38.276 41.798,38.416 41.678,38.544C41.558,38.664 41.418,38.772 41.258,38.868C41.098,38.956 40.922,39.024 40.73,39.072C40.538,39.12 40.326,39.144 40.094,39.144ZM40.538,37.776C40.73,37.776 40.898,37.744 41.042,37.68C41.186,37.616 41.31,37.528 41.414,37.416C41.518,37.304 41.602,37.176 41.666,37.032C41.738,36.88 41.79,36.716 41.822,36.54V36.528H40.658C40.458,36.528 40.29,36.556 40.154,36.612C40.026,36.66 39.93,36.732 39.866,36.828C39.802,36.924 39.77,37.036 39.77,37.164C39.77,37.3 39.802,37.416 39.866,37.512C39.938,37.6 40.03,37.668 40.142,37.716C40.262,37.756 40.394,37.776 40.538,37.776ZM17.132,53.144C16.5,53.144 15.924,53.02 15.404,52.772C14.892,52.516 14.484,52.136 14.18,51.632C13.884,51.128 13.736,50.492 13.736,49.724V44.6H15.536V49.736C15.536,50.112 15.596,50.432 15.716,50.696C15.844,50.96 16.028,51.16 16.268,51.296C16.516,51.424 16.812,51.488 17.156,51.488C17.508,51.488 17.804,51.424 18.044,51.296C18.292,51.16 18.48,50.96 18.608,50.696C18.736,50.432 18.8,50.112 18.8,49.736V44.6H20.6V49.724C20.6,50.492 20.44,51.128 20.12,51.632C19.808,52.136 19.388,52.516 18.86,52.772C18.34,53.02 17.764,53.144 17.132,53.144ZM22.128,53V44.6H24.288L26.736,49.652L29.16,44.6H31.308V53H29.508V47.636L27.444,51.824H26.004L23.928,47.636V53H22.128ZM32.921,53V44.6H34.721V47.78L37.625,44.6H39.833L36.749,47.924L39.941,53H37.733L35.465,49.316L34.721,50.12V53H32.921ZM41.007,53V44.6H43.167L45.615,49.652L48.039,44.6H50.187V53H48.387V47.636L46.323,51.824H44.883L42.807,47.636V53H41.007Z"
|
||||
android:fillColor="#489EC6"/>
|
||||
</group>
|
||||
</vector>
|
||||
|
||||
9
app/src/main/res/drawable/ic_sent.xml
Normal file
9
app/src/main/res/drawable/ic_sent.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="19"
|
||||
android:viewportHeight="16">
|
||||
<path
|
||||
android:pathData="M0,16V10L8,8L0,6V0L19,8L0,16Z"
|
||||
android:fillColor="#000000"/>
|
||||
</vector>
|
||||
@ -168,25 +168,66 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Kecamatan / Desa"
|
||||
android:text="Kecamatan"
|
||||
android:textColor="@android:color/black"
|
||||
android:textSize="14sp" />
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:hint="Isi Kecamatan / Desa"
|
||||
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/etKecamatan"
|
||||
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
|
||||
|
||||
<AutoCompleteTextView
|
||||
android:id="@+id/autoCompleteKecamatan"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="none"
|
||||
android:focusable="false"
|
||||
android:clickable="true"
|
||||
android:padding="12dp"
|
||||
android:textSize="14sp"
|
||||
android:inputType="textCapWords" />
|
||||
|
||||
android:textSize="14sp" />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/subdistrictProgressBar"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:visibility="gone" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Desa"
|
||||
android:textColor="@android:color/black"
|
||||
android:textSize="14sp" />
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
|
||||
|
||||
<AutoCompleteTextView
|
||||
android:id="@+id/autoCompleteDesa"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="none"
|
||||
android:focusable="false"
|
||||
android:clickable="true"
|
||||
android:padding="12dp"
|
||||
android:textSize="14sp" />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/villageProgressBar"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:visibility="gone" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
|
||||
@ -133,6 +133,7 @@
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Nomor Rekening / Nomor HP *"
|
||||
android:fontFamily="@font/dmsans_semibold"
|
||||
android:visibility="gone"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<EditText
|
||||
@ -145,6 +146,7 @@
|
||||
android:inputType="text"
|
||||
android:minHeight="50dp"
|
||||
android:textSize="14sp"
|
||||
android:visibility="gone"
|
||||
android:padding="12dp" />
|
||||
|
||||
<TextView
|
||||
@ -152,6 +154,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Tanggal Pembayaran *"
|
||||
android:visibility="gone"
|
||||
android:fontFamily="@font/dmsans_semibold"
|
||||
android:textSize="16sp" />
|
||||
|
||||
@ -164,6 +167,7 @@
|
||||
android:drawableEnd="@drawable/ic_calendar"
|
||||
android:drawablePadding="8dp"
|
||||
android:hint="Pilih tanggal"
|
||||
android:visibility="gone"
|
||||
android:minHeight="50dp"
|
||||
android:padding="12dp" />
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user