Add some providers
This commit is contained in:
@ -1,19 +1,41 @@
|
||||
package com.tommasoberlose.anotherwidget.network
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.google.gson.internal.LinkedTreeMap
|
||||
import com.kwabenaberko.openweathermaplib.constants.Units
|
||||
import com.kwabenaberko.openweathermaplib.implementation.OpenWeatherMapHelper
|
||||
import com.kwabenaberko.openweathermaplib.implementation.callbacks.CurrentWeatherCallback
|
||||
import com.kwabenaberko.openweathermaplib.models.currentweather.CurrentWeather
|
||||
import com.tommasoberlose.anotherwidget.global.Constants
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.helpers.WeatherHelper
|
||||
import com.tommasoberlose.anotherwidget.network.repository.WeatherGovRepository
|
||||
import com.tommasoberlose.anotherwidget.ui.fragments.MainFragment
|
||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import java.lang.Exception
|
||||
|
||||
class WeatherNetworkApi(val context: Context) {
|
||||
fun updateWeather() {
|
||||
if (Preferences.showWeather && Preferences.weatherProviderApi != "" && Preferences.customLocationLat != "" && Preferences.customLocationLon != "") {
|
||||
if (Preferences.showWeather && Preferences.customLocationLat != "" && Preferences.customLocationLon != "") {
|
||||
when (Constants.WeatherProvider.fromInt(Preferences.weatherProvider)) {
|
||||
Constants.WeatherProvider.OPEN_WEATHER -> useOpenWeatherMap(context)
|
||||
Constants.WeatherProvider.WEATHER_GOV -> useWeatherGov(context)
|
||||
}
|
||||
} else {
|
||||
WeatherHelper.removeWeather(
|
||||
context
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun useOpenWeatherMap(context: Context) {
|
||||
if (Preferences.weatherProviderApi != "" ) {
|
||||
val helper = OpenWeatherMapHelper(Preferences.weatherProviderApi)
|
||||
helper.setUnits(if (Preferences.weatherTempUnit == "F") Units.IMPERIAL else Units.METRIC)
|
||||
helper.getCurrentWeatherByGeoCoordinates(Preferences.customLocationLat.toDouble(), Preferences.customLocationLon.toDouble(), object :
|
||||
@ -39,4 +61,27 @@ class WeatherNetworkApi(val context: Context) {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun useWeatherGov(context: Context) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val repository = WeatherGovRepository()
|
||||
try {
|
||||
val points = repository.getGridPoints(
|
||||
Preferences.customLocationLat,
|
||||
Preferences.customLocationLon
|
||||
)
|
||||
|
||||
val pp = points["properties"] as LinkedTreeMap<*,*>
|
||||
val gridId = pp["gridId"] as String
|
||||
val gridX = pp["gridX"] as Double
|
||||
val gridY = pp["gridY"] as Double
|
||||
|
||||
val weather = repository.getWeather(gridId, gridX, gridY)
|
||||
|
||||
Log.d("ciao", weather.toString())
|
||||
} catch (ex: Exception) {
|
||||
Log.d("ciao", ex.localizedMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.tommasoberlose.anotherwidget.network.api
|
||||
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Path
|
||||
|
||||
interface WeatherGovApiService {
|
||||
@GET("points/{latitude},{longitude}")
|
||||
suspend fun getGridPoints(@Path("latitude") latitude: String, @Path("longitude") longitude: String): HashMap<String, Any>
|
||||
|
||||
@GET("gridpoints/{gridId}/{gridX},{gridY}/forecast")
|
||||
suspend fun getWeather(@Path("gridId") gridId: String, @Path("gridX") gridX: Int, @Path("gridY") gridY: Int): HashMap<String, Any>
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.tommasoberlose.anotherwidget.network.repository
|
||||
|
||||
import com.tommasoberlose.anotherwidget.network.api.WeatherGovApiService
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
class WeatherGovRepository() {
|
||||
|
||||
private val apiService: WeatherGovApiService = getRetrofit().create(WeatherGovApiService::class.java)
|
||||
|
||||
suspend fun getGridPoints(latitude: String, longitude: String) = apiService.getGridPoints(latitude, longitude)
|
||||
suspend fun getWeather(gridId: String, gridX: Double, gridY: Double) = apiService.getWeather(gridId, gridX.toInt(), gridY.toInt())
|
||||
|
||||
companion object {
|
||||
private const val BASE_URL = "https://api.weather.gov/"
|
||||
private fun getRetrofit(): Retrofit {
|
||||
return Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user