@@ -33,6 +33,7 @@ import android.widget.TextView
3333import androidx.appcompat.app.AppCompatActivity
3434import androidx.appcompat.widget.AppCompatImageView
3535import androidx.core.content.res.ResourcesCompat
36+ import androidx.core.text.isDigitsOnly
3637import androidx.leanback.widget.BaseGridView
3738import androidx.leanback.widget.OnChildViewHolderSelectedListener
3839import androidx.leanback.widget.VerticalGridView
@@ -587,20 +588,38 @@ class MainActivity : AppCompatActivity(), OnEditModeChangedListener,
587588 lw.useCurrentLocation = true
588589 lw.updateCurrentLocation = true
589590 } else {
591+ lw.useCurrentLocation = false
590592 RowPreferences .getUserLocation(this )?.let { loc ->
591593 if (loc.isNotEmpty())
592- lw.fetchCurrentWeatherByCityName(loc)
594+ if (loc.isDigitsOnly()) { // assume city id, ex. 524901
595+ if (BuildConfig .DEBUG ) Log .d(TAG , " fetchCurrentWeatherByCityId($loc )" )
596+ lw.fetchCurrentWeatherByCityId(loc)
597+ } else if (loc.split(" , " ).size == 2 &&
598+ loc.split(" , " ).first().toDoubleOrNull() != null &&
599+ loc.split(" , " ).last().toDoubleOrNull() != null
600+ ) { // assume coordinates, ex. 45.75 47.61
601+ val lat = loc.split(" , " ).first().toDouble()
602+ val lon = loc.split(" , " ).last().toDouble()
603+ if (BuildConfig .DEBUG ) Log .d(TAG , " fetchCurrentWeatherByLocation($lat ,$lon )" )
604+ lw.fetchCurrentWeatherByLocation(lat, lon)
605+ } else {
606+ if (BuildConfig .DEBUG ) Log .d(TAG , " fetchCurrentWeatherByCityName($loc )" )
607+ lw.fetchCurrentWeatherByCityName(loc)
608+ }
609+ else
610+ LauncherApplication .Toast (R .string.user_location_warning, true )
593611 }
594612 }
595613
596614 lw.weatherCallback = object : LocalWeather .WeatherCallback {
597615 override fun onSuccess (weather : Weather ) {
598- if (BuildConfig .DEBUG ) Log .d(TAG , " LocalWeather onSuccess()" )
616+ if (BuildConfig .DEBUG ) Log .d(TAG , " LocalWeather onSuccess() -> updateWeatherDetails " )
599617 updateWeatherDetails(weather)
600618 }
601619
602620 override fun onFailure (exception : Throwable ? ) {
603621 Log .e(TAG , " Weather fetching exception ${exception!! .message!! } " )
622+ LauncherApplication .Toast (" Weather error: ${exception!! .message!! } " , true )
604623 }
605624 }
606625
0 commit comments