From 6ebca679715d6b657d7d8ac2cc7f2bcbe3a29d75 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Mon, 10 Jul 2023 09:29:40 +0200 Subject: [PATCH] Simplify weather logic Now everytime the user taps the update button, it will try to update the weather, no matter if it kills battery. The user is responsible enough of its usage. It simplifies the code a lot. It also makes more sense. The user at least see a visual feedback with the location icon showing up. --- .../features/weather/WeatherInfoView.java | 2 +- .../weather/WeatherUpdateService.java | 2 +- .../features/weather/WeatherUpdater.java | 45 +++---------------- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherInfoView.java b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherInfoView.java index adf4d32a8f..54d8927ca1 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherInfoView.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherInfoView.java @@ -62,7 +62,7 @@ public class WeatherInfoView extends LinearLayout { }); findViewById(R.id.weather_setting_imageview).setOnClickListener(v -> startWeatherPreferences()); findViewById(R.id.weather_refresh_imageview).setOnClickListener(v -> { - WeatherUpdater.getInstance(getContext().getApplicationContext()).forceWeatherRequest(); + WeatherUpdater.getInstance(getContext().getApplicationContext()).updateWeather(); }); } diff --git a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdateService.java b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdateService.java index 500d2f034a..5668135c8a 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdateService.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdateService.java @@ -42,7 +42,7 @@ public class WeatherUpdateService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null && ACTION_FORCE_UPDATE.equals(intent.getAction())) { - WeatherUpdater.getInstance(this).forceWeatherRequest(); + WeatherUpdater.getInstance(this).updateWeather(); } return START_STICKY; diff --git a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java index 31266e0af8..1f68471460 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java @@ -44,13 +44,10 @@ import timber.log.Timber; public class WeatherUpdater { private static final String TAG = "WeatherUpdater"; - private static final long DEFAULT_FORCE_REQUEST_PERIOD_IN_MS = 10L * 1000L; private final WeakReference mWeakContext; - private long mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; - private long mLastWeatherUpdateTimeStamp = 0; - private long mForceRequestLastTry = 0; + private long mLastWeatherUpdateInMs = 0; private static WeatherUpdater mInstance = null; @@ -68,36 +65,18 @@ public class WeatherUpdater { public void checkWeatherRequest() { Context context = mWeakContext.get(); - long refreshPeriod = Preferences.weatherRefreshIntervalInMs(context); - long elapsedTime = Math.abs(SystemClock.elapsedRealtime() - mLastWeatherUpdateTimeStamp); + long refreshPeriodInMs = Preferences.weatherRefreshIntervalInMs(context); + long systemTimeInMs = SystemClock.elapsedRealtime(); + long elapsedTimeInMs = Math.abs(systemTimeInMs - mLastWeatherUpdateInMs); - boolean isPeriodicRequestAllowed = refreshPeriod != 0 && elapsedTime >= refreshPeriod; + boolean isPeriodicRequestAllowed = refreshPeriodInMs != 0 && elapsedTimeInMs >= refreshPeriodInMs; if (isPeriodicRequestAllowed) { - forceWeatherRequest(); - } - } - - public void forceWeatherRequest() { - if (canForceWeatherRequest()) { updateWeather(); - increaseForceRequestPeriod(); + mLastWeatherUpdateInMs = systemTimeInMs; } } - private boolean canForceWeatherRequest() { - - long systemTime = SystemClock.elapsedRealtime(); - long elapsedTime = Math.abs(systemTime - mForceRequestLastTry); - boolean isRequestAllowed = elapsedTime >= mForceRequestPeriodInMs; - - if (isRequestAllowed) { - mForceRequestLastTry = systemTime; - } - - return isRequestAllowed; - } - - private void updateWeather() { + public void updateWeather() { Timber.tag(TAG).i("Updating weather"); Context context = mWeakContext.get(); @@ -108,14 +87,6 @@ public class WeatherUpdater { } } - private void increaseForceRequestPeriod() { - mForceRequestPeriodInMs = mForceRequestPeriodInMs * 2; - - if (mForceRequestPeriodInMs > Preferences.weatherRefreshIntervalInMs(mWeakContext.get())) { - mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; - } - } - @SuppressLint("MissingPermission") private void fetchNewLocation() { if (hasMissingPermissions()) { @@ -190,10 +161,8 @@ public class WeatherUpdater { long now = SystemClock.elapsedRealtime(); Preferences.setCachedWeatherInfo(context, now, weatherInfo); - mLastWeatherUpdateTimeStamp = now; Intent updateIntent = new Intent(WeatherUpdateService.ACTION_UPDATE_FINISHED); LocalBroadcastManager.getInstance(context).sendBroadcast(updateIntent); - mForceRequestPeriodInMs = DEFAULT_FORCE_REQUEST_PERIOD_IN_MS; } private void reverseGeocodeLocation(@NonNull Location location) { -- GitLab