Skip to content

Commit

Permalink
Add here api's
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaailashsharma committed Dec 27, 2023
1 parent 61c62e0 commit ca05f3a
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ fun WasteItemCard(

}

private fun distance(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
fun distance(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
val theta = lon1 - lon2
var dist = (sin(deg2rad(lat1))
* sin(deg2rad(lat2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ object ApiRoutes {

const val routing = "https://router.hereapi.com/v8/routes"

const val weather = "https://weather.cc.api.here.com/weather/1.0/report.json"

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import app.waste2wealth.com.ktorClient.here.dto.HerePlaces
import app.waste2wealth.com.ktorClient.hereSearch.HereSearchResponse
import app.waste2wealth.com.ktorClient.placesAPI.dto.Places
import app.waste2wealth.com.ktorClient.routing.dto.HereRoutes
import app.waste2wealth.com.ktorClient.weather.dto.HereWeather
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.headers
import io.ktor.client.request.url
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.net.URLEncoder

class PlacesRepoImpl(private val client: HttpClient) : PlacesRepository {
Expand Down Expand Up @@ -110,5 +113,41 @@ class PlacesRepoImpl(private val client: HttpClient) : PlacesRepository {
}
}

override suspend fun hereWeather(latitude: String, longitude: String): HereWeather {
return try {
val ok = ApiRoutes.weather
val encodeLat = URLEncoder.encode(latitude, "UTF-8")
val encodeLong = URLEncoder.encode(longitude, "UTF-8")
val a = client.get<HereWeather> {
url(
"https://api.open-meteo.com/v1/forecast" +
"?latitude=$encodeLat&longitude=$encodeLong" +
"&current=temperature_2m&temperature_unit=celsius"
)
header(HttpHeaders.ContentType, ContentType.Application.Json)
headers {
append("Accept", "*/*")
append("Content-Type", "application/json")
}
}
println("weatherrrrrr 1: $latitude & $longitude")
println("weatherrrrrr: $a")
return a
} catch (e: Exception) {
Log.i("ApiException", e.message.toString())
return HereWeather(
current = null,
currentUnits = null,
elevation = null,
generationtimeMs = null,
latitude = null,
longitude = null,
timezone = null,
timezoneAbbreviation = null,
utcOffsetSeconds = null,
)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import app.waste2wealth.com.ktorClient.here.dto.HerePlaces
import app.waste2wealth.com.ktorClient.hereSearch.HereSearchResponse
import app.waste2wealth.com.ktorClient.placesAPI.dto.Places
import app.waste2wealth.com.ktorClient.routing.dto.HereRoutes
import app.waste2wealth.com.ktorClient.weather.dto.HereWeather


interface PlacesRepository {
Expand All @@ -26,5 +27,10 @@ interface PlacesRepository {
destination:String
): HereRoutes

suspend fun hereWeather(
latitude: String,
longitude: String
): HereWeather


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package app.waste2wealth.com.ktorClient.weather.dto


import com.google.gson.annotations.SerializedName
import kotlinx.serialization.Serializable

@Serializable
data class Current(
@SerializedName("interval")
val interval: Int?,
@SerializedName("temperature_2m")
val temperature: Double?,
@SerializedName("time")
val time: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package app.waste2wealth.com.ktorClient.weather.dto


import com.google.gson.annotations.SerializedName
import kotlinx.serialization.Serializable

@Serializable
data class CurrentUnits(
@SerializedName("interval")
val interval: String?,
@SerializedName("temperature_2m")
val temperature_2m: String?,
@SerializedName("time")
val time: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app.waste2wealth.com.ktorClient.weather.dto


import com.google.gson.annotations.SerializedName
import kotlinx.serialization.Serializable

@Serializable
data class HereWeather(
@SerializedName("current")
val current: Current?,
@SerializedName("current_units")
val currentUnits: CurrentUnits?,
@SerializedName("elevation")
val elevation: Double?,
@SerializedName("generationtime_ms")
val generationtimeMs: Double?,
@SerializedName("latitude")
val latitude: Double?,
@SerializedName("longitude")
val longitude: Double?,
@SerializedName("timezone")
val timezone: String?,
@SerializedName("timezone_abbreviation")
val timezoneAbbreviation: String?,
@SerializedName("utc_offset_seconds")
val utcOffsetSeconds: Int?
)
3 changes: 2 additions & 1 deletion app/src/main/java/app/waste2wealth/com/maps/MapsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ fun MapScreen(
onTrailingClick = {
mapsSearchViewModel.setQuery(TextFieldValue(""))
},
navController = navController
navController = navController,
locationViewModel = viewModel
)
Spacer(modifier = Modifier.height(10.dp))
AnimatedVisibility(
Expand Down
Loading

0 comments on commit ca05f3a

Please sign in to comment.