diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index 704ffdb..433afca 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -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 diff --git a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt index bb8d5a5..67ad565 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt @@ -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) + } } \ No newline at end of file diff --git a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt index d3c452a..4531c51 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt @@ -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,44 +39,59 @@ 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 url = URL("https://api.open-meteo.com/v1/forecast?$location¤t=temperature_2m") - with(url.openConnection() as HttpURLConnection) { - requestMethod = "GET" + val location = sharedPreferenceManager.getWeatherLocation(context) + if (location != null) { + val url = URL("https://api.open-meteo.com/v1/forecast?$location¤t=temperature_2m") + with(url.openConnection() as HttpURLConnection) { + requestMethod = "GET" - inputStream.bufferedReader().use { - val response = it.readText() + inputStream.bufferedReader().use { + val response = it.readText() - val jsonObject = JSONObject(response) + val jsonObject = JSONObject(response) - val currentData = jsonObject.getJSONObject("current") - val currentWeather = currentData.getInt("temperature_2m") + val currentData = jsonObject.getJSONObject("current") + val currentWeather = currentData.getInt("temperature_2m") - println("Field1: $currentWeather") + println("Field1: $currentWeather") + } } - }} + } + else { + withContext(Dispatchers.Main) { + Toast.makeText(context, "No weather location set", Toast.LENGTH_SHORT).show() + } + } + } + } } \ No newline at end of file