Refactor: Changed the time for checking updates and weather.

Signed-off-by: HeCodes2Much <wayne6324@gmail.com>
This commit is contained in:
HeCodes2Much
2024-07-19 21:04:24 +01:00
parent c6189c01ca
commit ad2f907b33
3 changed files with 22 additions and 5 deletions

View File

@@ -320,16 +320,16 @@ class AppHelper @Inject constructor() {
// Save cached data to SharedPreferences
val sharedPreferences = getSharedPreferences(Constants.WEATHER_PREFS, Context.MODE_PRIVATE)
sharedPreferences.edit()
.putLong("cachedDataTimestamp", cachedData.timestamp)
.putString("weatherResponse", Gson().toJson(cachedData.weatherResponse))
.putLong(Constants.CACHED_DATA_TIMESTAMP, cachedData.timestamp)
.putString(Constants.WEATHER_RESPONSE, Gson().toJson(cachedData.weatherResponse))
.apply()
}
// Function to retrieve weather data from cache
private fun Context.getWeatherDataFromCache(): CachedWeatherData? {
val sharedPreferences = getSharedPreferences(Constants.WEATHER_PREFS, Context.MODE_PRIVATE)
val timestamp = sharedPreferences.getLong("cachedDataTimestamp", -1)
val weatherResponseJson = sharedPreferences.getString("weatherResponse", null)
val sharedPreferences = getSharedPreferences(Constants.TIMERS_PREFS, Context.MODE_PRIVATE)
val timestamp = sharedPreferences.getLong(Constants.CACHED_DATA_TIMESTAMP, -1)
val weatherResponseJson = sharedPreferences.getString(Constants.WEATHER_RESPONSE, null)
if (timestamp != -1L && weatherResponseJson != null) {
val weatherResponse = Gson().fromJson(weatherResponseJson, WeatherResponse::class.java)
return CachedWeatherData(timestamp, weatherResponse)

View File

@@ -30,6 +30,16 @@ class UpdateManagerHelper(private val fragment: Fragment) {
fun checkForUpdates() {
val currentVersion = BuildConfig.VERSION_NAME
val url = "https://api.github.com/repos/DroidWorksStudio/EasyLauncher/releases/latest"
Log.d("UpdateManager", "URL: $url | Current version: $currentVersion")
val sharedPreferences = activity.getSharedPreferences(Constants.TIMERS_PREFS, Context.MODE_PRIVATE)
val lastCheckTime = sharedPreferences.getLong(Constants.LAST_CHECK_TIME, 0)
val currentTime = System.currentTimeMillis()
if (currentTime - lastCheckTime < 30 * 60 * 1000) {
Log.d("UpdateManager", "Checked for updates recently, skipping")
return
}
val request = Request.Builder().url(url).build()
OkHttpClient().newCall(request).enqueue(object : Callback {
@@ -67,6 +77,8 @@ class UpdateManagerHelper(private val fragment: Fragment) {
} else {
Log.e("UpdateManager", "Assets array is empty")
}
// Update the last check time
sharedPreferences.edit().putLong(Constants.LAST_CHECK_TIME, currentTime).apply()
}
}
})

View File

@@ -10,12 +10,17 @@ object Constants {
const val PACKAGE_NAME_DEBUG = "$PACKAGE_NAME.debug"
const val PACKAGE_PREFS = "EasyLauncher.pref"
const val TIMERS_PREFS = "Timers.pref"
const val LAST_CHECK_TIME = "LAST_CHECK_TIME"
const val CACHED_DATA_TIMESTAMP = "CACHED_DATA_TIMESTAMP"
const val WIDGETS_COUNT = 2
const val WIDGET_WEATHER = "WIDGET_WEATHER"
const val WIDGET_BATTERY = "WIDGET_BATTERY"
const val WEATHER_PREFS = "EasyWeather.pref"
const val WEATHER_RESPONSE = "WEATHER_RESPONSE"
const val WEATHER_UNITS = "WEATHER_UNITS"
const val LATITUDE = "LATITUDE"
const val LONGITUDE = "LONGITUDE"