From 17cf58846d860097ed43bd6ca6794085b7ceba9a Mon Sep 17 00:00:00 2001 From: ottoptj Date: Tue, 4 Jun 2024 03:12:09 +0300 Subject: [PATCH] The app can now pull weather from an api for a specific url. No location functionality yet. --- app/src/main/AndroidManifest.xml | 1 + .../java/eu/ottop/yamlauncher/MainActivity.kt | 3 ++- .../eu/ottop/yamlauncher/WeatherSystem.kt | 27 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8f87ec6..5cd76b5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ + diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index 5764a08..a25456d 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -60,6 +60,7 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap private val appMenuLinearLayoutManager = AppMenuLinearLayoutManager(this@MainActivity) private val appMenuEdgeFactory = AppMenuEdgeFactory(this@MainActivity) private val animations = Animations() + private val weatherSystem = WeatherSystem() private val swipeThreshold = 100 private val swipeVelocityThreshold = 100 @@ -149,7 +150,7 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap val statusBarManager: Class<*> = Class.forName("android.app.StatusBarManager") val expandMethod: Method = statusBarManager.getMethod("expandNotificationsPanel") expandMethod.invoke(statusBarService) - + weatherSystem.getWeather() } // Detect swipe left diff --git a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt index 8d8e316..e96a801 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt @@ -1,4 +1,31 @@ package eu.ottop.yamlauncher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import org.json.JSONObject +import java.net.HttpURLConnection +import java.net.URL + class WeatherSystem { + fun getWeather() { + CoroutineScope(Dispatchers.IO).launch { + val url = URL("https://api.open-meteo.com/v1/forecast?latitude=60.16&longitude=24.93¤t=temperature_2m") + with(url.openConnection() as HttpURLConnection) { + requestMethod = "GET" + + inputStream.bufferedReader().use { + val response = it.readText() + + // Parse the JSON response + val jsonObject = JSONObject(response) + + // Access specific fields or nested objects + val currentData = jsonObject.getJSONObject("current") + val currentWeather = currentData.getInt("temperature_2m") + + println("Field1: $currentWeather") + } + }} + } } \ No newline at end of file