@@ -26,6 +26,14 @@ import androidx.compose.ui.text.font.FontWeight
2626import androidx.compose.ui.unit.dp
2727import androidx.compose.ui.unit.sp
2828import com.example.ui.theme.*
29+ import kotlinx.coroutines.Dispatchers
30+ import kotlinx.coroutines.launch
31+ import kotlinx.coroutines.withContext
32+ import okhttp3.MediaType.Companion.toMediaTypeOrNull
33+ import okhttp3.RequestBody.Companion.toRequestBody
34+ import org.json.JSONArray
35+ import org.json.JSONObject
36+ import androidx.compose.runtime.rememberCoroutineScope
2937
3038data class EquipmentItem (
3139 val id : String = java.util.UUID .randomUUID().toString(),
@@ -48,52 +56,50 @@ fun EquipmentRentalScreen(
4856 val scrollState = rememberScrollState()
4957 var showAddDialog by remember { mutableStateOf(false ) }
5058
51- var equipmentList by remember {
52- mutableStateOf(
53- listOf (
54- EquipmentItem (
55- name = " Mahindra 575 DI Tractor (45 HP)" ,
56- category = " Tractor & Tillage" ,
57- rate = " ₹450 / Hour" ,
58- owner = " Verma Farms (Rajesh Verma)" ,
59- distance = " 3.2 km away" ,
60- phone = " 9876543210" ,
61- isAvailable = true ,
62- icon = " 🚜"
63- ),
64- EquipmentItem (
65- name = " DJI Agras T40 Spraying Drone" ,
66- category = " Pesticide Drone Spraying" ,
67- rate = " ₹350 / Acre" ,
68- owner = " AgriTech Kisan Co-op" ,
69- distance = " 5.0 km away" ,
70- phone = " 9812345678" ,
71- isAvailable = true ,
72- icon = " 🛸"
73- ),
74- EquipmentItem (
75- name = " CLAAS Crop Combine Harvester" ,
76- category = " Harvester" ,
77- rate = " ₹1,200 / Hour" ,
78- owner = " Suresh Patel" ,
79- distance = " 8.5 km away" ,
80- phone = " 9765432109" ,
81- isAvailable = false ,
82- icon = " ⚙️"
83- ),
84- EquipmentItem (
85- name = " Solar Drip Irrigation Pump (5 HP)" ,
86- category = " Irrigation Pump" ,
87- rate = " ₹150 / Day" ,
88- owner = " Ramesh Singh" ,
89- distance = " 2.1 km away" ,
90- phone = " 9988776655" ,
91- isAvailable = true ,
92- icon = " 💧"
93- )
94- )
59+ val seedEquipment = remember {
60+ listOf (
61+ EquipmentItem (name = " Mahindra 575 DI Tractor (45 HP)" , category = " Tractor & Tillage" , rate = " ₹450 / Hour" , owner = " Verma Farms" , distance = " Nearby" , phone = " 9876543210" , isAvailable = true , icon = " 🚜" ),
62+ EquipmentItem (name = " DJI Agras T40 Spraying Drone" , category = " Pesticide Drone" , rate = " ₹350 / Acre" , owner = " AgriTech Co-op" , distance = " Nearby" , phone = " 9812345678" , isAvailable = true , icon = " 🛸" ),
63+ EquipmentItem (name = " Solar Drip Irrigation Pump" , category = " Irrigation" , rate = " ₹150 / Day" , owner = " Ramesh Singh" , distance = " Nearby" , phone = " 9988776655" , isAvailable = true , icon = " 💧" )
9564 )
9665 }
66+ var equipmentList by remember { mutableStateOf<List <EquipmentItem >>(seedEquipment) }
67+ var isLoadingEquipment by remember { mutableStateOf(true ) }
68+ val scope = rememberCoroutineScope()
69+ val httpClient2 = remember { okhttp3.OkHttpClient .Builder ().connectTimeout(10 , java.util.concurrent.TimeUnit .SECONDS ).readTimeout(10 , java.util.concurrent.TimeUnit .SECONDS ).build() }
70+
71+ LaunchedEffect (Unit ) {
72+ withContext(kotlinx.coroutines.Dispatchers .IO ) {
73+ try {
74+ val url = " $SUPABASE_URL /rest/v1/equipment_listings?select=*&order=created_at.desc&limit=50"
75+ val req = okhttp3.Request .Builder ().url(url)
76+ .addHeader(" apikey" , SUPABASE_ANON_KEY )
77+ .addHeader(" Authorization" , " Bearer $SUPABASE_ANON_KEY " )
78+ .addHeader(" Accept" , " application/json" ).get().build()
79+ val body = httpClient2.newCall(req).execute().body?.string() ? : " "
80+ val arr = org.json.JSONArray (body)
81+ if (arr.length() > 0 ) {
82+ val list = mutableListOf<EquipmentItem >()
83+ for (i in 0 until arr.length()) {
84+ val obj = arr.getJSONObject(i)
85+ list.add(EquipmentItem (
86+ id = obj.optString(" id" , java.util.UUID .randomUUID().toString()),
87+ name = obj.optString(" name" , " Equipment" ),
88+ category = obj.optString(" category" , " General" ),
89+ rate = obj.optString(" rate" , " Contact Owner" ),
90+ owner = obj.optString(" owner_name" , " Farmer" ),
91+ distance = obj.optString(" location" , " Nearby" ),
92+ phone = obj.optString(" phone" , " 0000000000" ),
93+ isAvailable = obj.optBoolean(" is_available" , true ),
94+ icon = obj.optString(" icon" , " 🚜" )
95+ ))
96+ }
97+ equipmentList = list
98+ }
99+ } catch (_: Exception ) {}
100+ }
101+ isLoadingEquipment = false
102+ }
97103
98104 Column (
99105 modifier = Modifier
@@ -229,6 +235,30 @@ fun EquipmentRentalScreen(
229235 icon = if (categoryInput.contains(" Drone" , ignoreCase = true )) " 🛸" else " 🚜"
230236 )
231237 equipmentList = listOf (newItem) + equipmentList
238+
239+ scope.launch {
240+ withContext(kotlinx.coroutines.Dispatchers .IO ) {
241+ try {
242+ val payload = org.json.JSONObject ().apply {
243+ put(" name" , nameInput.trim())
244+ put(" category" , categoryInput)
245+ put(" rate" , rateInput.trim())
246+ put(" owner_name" , " My Farm" )
247+ put(" location" , locationInput.trim())
248+ put(" phone" , if (phoneInput.isBlank()) " 9876543210" else phoneInput.trim())
249+ put(" is_available" , true )
250+ put(" icon" , if (categoryInput.contains(" Drone" , ignoreCase = true )) " 🛸" else " 🚜" )
251+ }.toString()
252+ val reqBody = payload.toRequestBody(" application/json" .toMediaTypeOrNull())
253+ okhttp3.Request .Builder ().url(" $SUPABASE_URL /rest/v1/equipment_listings" )
254+ .addHeader(" apikey" , SUPABASE_ANON_KEY )
255+ .addHeader(" Authorization" , " Bearer $SUPABASE_ANON_KEY " )
256+ .addHeader(" Content-Type" , " application/json" )
257+ .addHeader(" Prefer" , " return=minimal" )
258+ .post(reqBody).build().let { httpClient2.newCall(it).execute() }
259+ } catch (_: Exception ) {}
260+ }
261+ }
232262 }
233263 showAddDialog = false
234264 },
0 commit comments