Weather location added to SharedPreferences

This commit is contained in:
ottoptj 2024-07-04 02:06:06 +03:00
commit 703eab30ed
3 changed files with 49 additions and 21 deletions

View file

@ -144,6 +144,7 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap
// Detect swipe up
if (deltaY < -swipeThreshold && abs(velocityY) > swipeVelocityThreshold) {
openAppMenuActivity()
weatherSystem.getTemp(this@MainActivity)
}
// Detect swipe down

View file

@ -60,4 +60,16 @@ class SharedPreferenceManager {
editor.apply()
}
fun setWeatherLocation(cont: Context, location: String) {
val editor = cont.getSharedPreferences("weather_location", AppCompatActivity.MODE_PRIVATE).edit()
val key = "location"
editor.putString(key, location)
editor.apply()
}
fun getWeatherLocation(cont: Context) : String? {
val sharedPreferences = cont.getSharedPreferences("weather_location", AppCompatActivity.MODE_PRIVATE)
val key = "location"
return sharedPreferences.getString(key, null)
}
}

View file

@ -9,11 +9,13 @@ import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
@ -21,16 +23,14 @@ import java.util.function.Consumer
class WeatherSystem {
fun getWeather() {
getTempForLocation("latitude=60.16&longitude=24.93")
}
private val sharedPreferenceManager = SharedPreferenceManager()
fun getWeatherForCurrentLocation(activity: Activity) {
val locationManager = activity.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val locationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
println(location)
println("Location obtained")
locationManager.removeUpdates(this)
}
@ -39,30 +39,37 @@ class WeatherSystem {
override fun onProviderDisabled(provider: String) {}
}
// Check for location permissions
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Permissions are not granted
ActivityCompat.requestPermissions(
activity,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
1
)
return
}
// Request location updates from both providers
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f, locationListener)
// Try to get the last known location from both providers as a fallback
val currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
if (currentLocation != null) {
getTempForLocation("latitude=${currentLocation.latitude}&longitude=${currentLocation.longitude}")
sharedPreferenceManager.setWeatherLocation(activity, "latitude=${currentLocation.latitude}&longitude=${currentLocation.longitude}")
}
else {
Toast.makeText(activity, "Unable to get location", Toast.LENGTH_SHORT).show()
}
}
private fun getTempForLocation(location: String) {
fun getTemp(context: Context) {
CoroutineScope(Dispatchers.IO).launch {
val location = sharedPreferenceManager.getWeatherLocation(context)
if (location != null) {
val url = URL("https://api.open-meteo.com/v1/forecast?$location&current=temperature_2m")
with(url.openConnection() as HttpURLConnection) {
requestMethod = "GET"
@ -77,6 +84,14 @@ class WeatherSystem {
println("Field1: $currentWeather")
}
}}
}
}
else {
withContext(Dispatchers.Main) {
Toast.makeText(context, "No weather location set", Toast.LENGTH_SHORT).show()
}
}
}
}
}