Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
6.5 kB
1
Indexable
Never
  suspend fun getSectionIdsFromApi()  : MutableSet<Int> = withContext(Dispatchers.IO){
        Log.d(TAG, "callled: ")
        val client = OkHttpClient()
        uniquesectionIds.clear()
        val uniqueidslist = mutableSetOf<Int>()

        val url = "https://stagegeospatial.digital.lntecc.com/dg-360-api/api/v1/modules"
        // Append the ID as a query parameter in the URL
        val ss = database.userDao().getallprojectdata().get(0).projectId
        val requestBody = FormBody.Builder()
            .add("projectId", ss)
            .build()
        val pd = ProgressDialog(this@MainActivity)
        pd.setMessage("Loading... ")
        pd.setCancelable(false)
        pd.show()
        val request = Request.Builder()
            .url(url)
            .post(requestBody)
            .build()

        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                pd.dismiss()
                Log.d(ContentValues.TAG, "onFailure:outdoor " + e.message)
//                progressBar.visibility = View.GONE
            }

            override fun onResponse(call: Call, response: Response) {
                runOnUiThread {
                    // Stuff that updates the UI

                    try {
                        if (pd.isShowing) {
                            pd.dismiss()
                        }
                        val json = response.body!!.string()
                        val jsonObject = JSONObject(json)
                        val msg = jsonObject.get("message")
                        val sts = jsonObject.get("status")
                        val dataobj = jsonObject.getJSONObject("data")
//                        Log.d(ContentValues.TAG, "onResp allcluster apiresp=" + json)
                        if (sts == false) {
                            Toast.makeText(this@MainActivity, msg.toString(), Toast.LENGTH_SHORT).show()

                        } else if (sts == true) {
                            val project_id = dataobj.getString("project_id")
                            val selectedProId = database.userDao().getallprojectdata().get(0).projectId
                            var stringis = "is_indoor"
                            if(inorout == 1){
                                stringis = "is_outdoor"
                            }else{
                                stringis = "is_indoor"
                            }
                            val isindoor = dataobj.get(stringis)
                            if (isindoor == true && selectedProId.equals(project_id)) {

                                val imagesArray = dataobj.getJSONArray("images")
                                if (imagesArray != null && imagesArray.length() > 0) {
                                    for (i in 0 until imagesArray.length()) {
                                        // Level id and project id condtin == or !
                                            val datesArray = imagesArray.getJSONObject(i).getJSONArray("data")
                                            // checking data points empty or not
                                            if (datesArray != null && datesArray.length() > 0) {
//                                                  geoPoints.clear()
                                                for (j in 0 until datesArray.length()) {
                                                    val dates = database.userDao().getallprojectdata().get(0).createdDate

                                                    val insidedatesArray = datesArray.getJSONObject(j).getJSONArray("data")
                                                    if (insidedatesArray != null && insidedatesArray.length() > 0) {

                                                        for (k in 0 until insidedatesArray.length()) {
                                                            val mid = insidedatesArray.getJSONObject(k).get("mid")

                                                            val latitude = insidedatesArray.getJSONObject(k).get("latitude")


                                                            if (latitude != null) {

                                                                val xval = latitude

                                                                if (!xval.toString().equals("null")) {

                                                                    if (dates.equals(datesArray.getJSONObject(j).getString("date"))) {
                                                                        // uploaded green marker
//                                                                        Log.d(TAG, "dateeuwal:545= "+imagesArray.getJSONObject(i).get("name"))
                                                                      if(imagesArray.getJSONObject(i).get("id").toString().length>0){
                                                                          uniquesectionIds.add(
                                                                              imagesArray.getJSONObject(i).get("id") as Int)
                                                                          uniqueidslist.add( imagesArray.getJSONObject(i).get("id") as Int)
                                                                      }
                                                                        Log.d(TAG, "onResponse: "+uniqueidslist)
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }

                                                }
                                            }

                                    }
                                }
                            }
                        }

                    } catch (ex: JSONException) {
                        Log.d("err", "onResponse:outdoor " + ex.message)
                    }
                }

            }
        })
        Log.d(TAG, "callled2=: "+uniqueidslist)

        uniquesectionIds.clear()
        uniquesectionIds = uniqueidslist
        Log.d(TAG, "callled3: "+uniquesectionIds)
       uniquesectionIds
    }