diff --git a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt
index e2d7da0..c7b900e 100644
--- a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt
+++ b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt
@@ -17,6 +17,7 @@ import android.widget.EditText
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.fragment.app.setFragmentResult
+import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope
@@ -43,7 +44,7 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
- CoroutineScope(Dispatchers.Main).launch {
+ lifecycleScope.launch {
withContext(Dispatchers.Default) {
diff --git a/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt
index 61e90ab..1b21d32 100644
--- a/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt
+++ b/app/src/main/java/eu/ottop/yamlauncher/LocationFragment.kt
@@ -12,6 +12,7 @@ import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.fragment.app.setFragmentResult
+import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView
@@ -44,7 +45,7 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
stringUtils.setLink(requireActivity().findViewById(R.id.locationLink), getString(R.string.location_link))
- CoroutineScope(Dispatchers.IO).launch {
+ lifecycleScope.launch(Dispatchers.IO) {
locationList = weatherSystem.getSearchedLocations(
searchView.text.toString()
)
@@ -75,7 +76,7 @@ class LocationFragment : Fragment(), LocationListAdapter.OnItemClickListener {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
println(searchView.text.toString())
- CoroutineScope(Dispatchers.IO).launch {
+ lifecycleScope.launch(Dispatchers.IO){
val locations = weatherSystem.getSearchedLocations(
searchView.text.toString()
)
diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt
index 34c1807..cb9ab7c 100644
--- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt
+++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt
@@ -44,6 +44,10 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.children
import androidx.core.view.marginLeft
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.LifecycleCoroutineScope
+import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.repeatOnLifecycle
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textfield.TextInputEditText
@@ -189,6 +193,28 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
}
})
+ lifecycleScope.launch(Dispatchers.IO) {
+ repeatOnLifecycle(Lifecycle.State.STARTED) {
+ while (true) {
+ updateWeather()
+ delay(600000)
+ }
+ }
+ }
+
+ }
+
+ private suspend fun updateWeather() {
+ withContext(Dispatchers.IO) {
+ if (preferences.getBoolean("weather_enabled", false)) {
+ if (preferences.getBoolean("gps_location", false)) {
+ weatherSystem.setGpsLocation(this@MainActivity)
+ } else {
+
+ updateWeatherText()
+ }
+ }
+ }
}
fun modifyDate(value: String, index: Int) {
@@ -197,22 +223,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
dateText.format24Hour = "${dateElements[1]}${stringUtils.addStartTextIfNotEmpty(dateElements[2], " | ")}${stringUtils.addStartTextIfNotEmpty(dateElements[3], " | ")}"
}
- private fun startWeatherMonitor() {
- weatherJob?.cancel()
- weatherJob = CoroutineScope(Dispatchers.IO).launch {
- while (true) {
- if (preferences.getBoolean("gps_location", false)) {
- withContext(Dispatchers.Main) {
- weatherSystem.setGpsLocation(this@MainActivity)
- }
- }
- val currentWeather = weatherSystem.getTemp(this@MainActivity)
- withContext(Dispatchers.Main) {
- modifyDate(currentWeather, 2)
- }
- delay(300000)
- }
+
+ suspend fun updateWeatherText() {
+ val temp = weatherSystem.getTemp(this@MainActivity)
+ withContext(Dispatchers.Main) {
+ modifyDate(temp, 2)
}
}
@@ -261,7 +277,9 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
"weather_enabled" -> {
if (preferences?.getBoolean(key, false) == true) {
- startWeatherMonitor()
+ lifecycleScope.launch {
+ updateWeather()
+ }
}
else {
weatherJob?.cancel()
@@ -363,9 +381,6 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
override fun onStart() {
super.onStart()
startTask()
- if (preferences.getBoolean("weather_enabled", false)) {
- startWeatherMonitor()
- }
// Keyboard is sometimes open when going back to the app, so close it.
closeKeyboard()
diff --git a/app/src/main/java/eu/ottop/yamlauncher/SettingsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/SettingsFragment.kt
index 1abbc22..8ea015d 100644
--- a/app/src/main/java/eu/ottop/yamlauncher/SettingsFragment.kt
+++ b/app/src/main/java/eu/ottop/yamlauncher/SettingsFragment.kt
@@ -36,9 +36,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
gpsLocationPref.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _, newValue ->
val isGpsEnabled = newValue as Boolean
- if (isGpsEnabled) {
- weatherSystem.setGpsLocation(requireActivity())
- }
manualLocationPref?.isEnabled = !isGpsEnabled
true // Returning true means the change is persisted
}
diff --git a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt
index 2aa21bd..16f7c95 100644
--- a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt
+++ b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt
@@ -7,32 +7,32 @@ import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
+import android.os.Handler
+import android.os.Looper
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
+import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.asExecutor
+import kotlinx.coroutines.coroutineScope
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
+import kotlin.coroutines.coroutineContext
class WeatherSystem {
private val sharedPreferenceManager = SharedPreferenceManager()
private val stringUtils = StringUtils()
- fun setGpsLocation(activity: Activity) {
+ suspend fun setGpsLocation(activity: MainActivity) {
val locationManager = activity.getSystemService(Context.LOCATION_SERVICE) as LocationManager
- val locationListener = object : LocationListener {
- override fun onLocationChanged(location: Location) {
- println("Location obtained")
- locationManager.removeUpdates(this)
- }
-
- }
-
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
activity,
@@ -42,18 +42,21 @@ class WeatherSystem {
return
}
-
- locationManager.requestLocationUpdates(LocationManager.FUSED_PROVIDER, 0, 0f, locationListener)
-
-
- val currentLocation = locationManager.getLastKnownLocation(LocationManager.FUSED_PROVIDER)
-
-
- if (currentLocation != null) {
- sharedPreferenceManager.setWeatherLocation(activity, "latitude=${currentLocation.latitude}&longitude=${currentLocation.longitude}", sharedPreferenceManager.getWeatherRegion(activity))
+ locationManager.getCurrentLocation(
+ LocationManager.GPS_PROVIDER, // Use GPS provider
+ null, // No cancellation signal
+ ContextCompat.getMainExecutor(activity)
+ )
+ { location: Location? -> // Lambda expression for the callback
+ if (location != null) {
+ CoroutineScope(Dispatchers.IO).launch {
+ val latitude = location.latitude
+ val longitude = location.longitude
+ sharedPreferenceManager.setWeatherLocation(activity, "latitude=${latitude}&longitude=${longitude}", sharedPreferenceManager.getWeatherRegion(activity))
+ activity.updateWeatherText()}
+ }
}
-
}
// Run within Dispatchers.IO from the outside (doesn't refresh properly otherwise)
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 8721ed5..1b165aa 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -127,13 +127,13 @@
+ android:layout_weight="0.23" />
Codeberg]]>
GitHub]]>
- Stripe]]>
- Liberapay]]>
+ Liberapay
(recurring)]]>
+ Stripe
(one-time)]]>
Open-Meteo.com
(CC BY 4.0)]]>
Open-Meteo.com (CC BY 4.0)]]>
diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml
index 9ceeb81..86285c3 100644
--- a/app/src/main/res/values/themes.xml
+++ b/app/src/main/res/values/themes.xml
@@ -7,7 +7,7 @@
- @android:color/transparent
- @android:color/transparent
- ?attr/isLightTheme
- - false
+ - true
- shortEdges
- false
- false
diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml
index 039733f..191d744 100644
--- a/app/src/main/res/xml/root_preferences.xml
+++ b/app/src/main/res/xml/root_preferences.xml
@@ -109,7 +109,7 @@
app:entries="@array/size_options"
app:entryValues="@array/size_values"
app:key="shortcutSize"
- app:title="Home App Size"
+ app:title="Shortcut Size"
app:useSimpleSummaryProvider="true" />