Add some providers

This commit is contained in:
Tommaso Berlose
2020-10-04 11:43:36 +02:00
parent c04040e242
commit 1dee4cc8e5
34 changed files with 213 additions and 3 deletions

View File

@ -36,6 +36,25 @@ object Constants {
HIGH(2)
}
enum class WeatherProvider(val value: Int) {
OPEN_WEATHER(0),
WEATHER_BIT(1),
FORECA(2),
HERE(3),
ACCUWEATHER(4),
WEATHER_GOV(5),
YR(6),
SMHI(7),
WEATHER_CA(8),
BOM(9),
METEOFRANCE(10);
companion object {
private val map = WeatherProvider.values().associateBy(WeatherProvider::value)
fun fromInt(type: Int) = map[type]
}
}
enum class WeatherIconPack(val value: Int) {
DEFAULT(0),
MINIMAL(1),

View File

@ -39,6 +39,7 @@ object Preferences : KotprefModel() {
var weatherAppName by stringPref(key = "PREF_WEATHER_APP_NAME", default = "")
var weatherAppPackage by stringPref(key = "PREF_WEATHER_APP_PACKAGE", default = "")
var weatherProviderApi by stringPref(key = "PREF_WEATHER_PROVIDER_API_KEY", default = "")
var weatherProvider by intPref(default = Constants.WeatherProvider.OPEN_WEATHER.value)
var eventAppName by stringPref(key = "PREF_EVENT_APP_NAME", default = "")
var eventAppPackage by stringPref(key = "PREF_EVENT_APP_PACKAGE", default = "")
var openEventDetails by booleanPref(default = true)

View File

@ -46,6 +46,22 @@ object WeatherHelper {
MainWidget.updateWidget(context)
}
fun getProviderName(context: Context, provider: Constants.WeatherProvider): String {
return context.getString(when(provider) {
Constants.WeatherProvider.OPEN_WEATHER -> R.string.settings_weather_provider_open_weather
Constants.WeatherProvider.WEATHER_BIT -> R.string.settings_weather_provider_weatherbit
Constants.WeatherProvider.FORECA -> R.string.settings_weather_provider_foreca
Constants.WeatherProvider.HERE -> R.string.settings_weather_provider_here
Constants.WeatherProvider.ACCUWEATHER -> R.string.settings_weather_provider_accuweather
Constants.WeatherProvider.WEATHER_GOV -> R.string.settings_weather_provider_weather_gov
Constants.WeatherProvider.YR -> R.string.settings_weather_provider_yr
Constants.WeatherProvider.SMHI -> R.string.settings_weather_provider_smhi
Constants.WeatherProvider.WEATHER_CA -> R.string.settings_weather_provider_weather_ca
Constants.WeatherProvider.BOM -> R.string.settings_weather_provider_bom
Constants.WeatherProvider.METEOFRANCE -> R.string.settings_weather_provider_meteofrance
})
}
fun getWeatherIconResource(icon: String, style: Int = Preferences.weatherIconPack): Int {
return when (icon) {
"01d" -> {

View File

@ -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)
}
}
}
}

View File

@ -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>
}

View File

@ -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()
}
}
}

View File

@ -102,6 +102,12 @@ class WeatherTabFragment : Fragment() {
checkLocationPermission()
})
viewModel.weatherProvider.observe(viewLifecycleOwner, Observer {
maintainScrollPosition {
label_weather_provider.text = WeatherHelper.getProviderName(requireContext(), Constants.WeatherProvider.fromInt(it)!!)
}
})
viewModel.weatherProviderApi.observe(viewLifecycleOwner, Observer {
maintainScrollPosition {
checkWeatherProviderConfig()
@ -198,6 +204,21 @@ class WeatherTabFragment : Fragment() {
Preferences.showWeather = enabled
}
action_weather_provider.setOnClickListener {
if (Preferences.showWeather) {
val dialog = BottomSheetMenu<Int>(requireContext(), header = getString(R.string.settings_weather_provider_api)).setSelectedValue(Preferences.weatherProvider)
(0 until 11).forEach {
val item = Constants.WeatherProvider.fromInt(it)
dialog.addItem(WeatherHelper.getProviderName(requireContext(), item!!), it)
}
dialog.addOnSelectItemListener { value ->
Preferences.weatherProvider = value
checkWeatherProviderConfig()
}.show()
}
}
action_weather_provider_api_key.setOnClickListener {
if (Preferences.showWeather) {
startActivityForResult(

View File

@ -67,6 +67,7 @@ class MainViewModel : ViewModel() {
val showWeatherWarning = Preferences.asLiveData(Preferences::showWeatherWarning)
val weatherIconPack = Preferences.asLiveData(Preferences::weatherIconPack)
val weatherProvider = Preferences.asLiveData(Preferences::weatherProvider)
// Glance
val showGlance = Preferences.asLiveData(Preferences::showGlance)