Clean up logic

Fixed randomly found timeunit error
This commit is contained in:
ThomasSession 2024-07-30 13:30:58 +10:00
parent d3990572a0
commit 42733c910d
3 changed files with 16 additions and 23 deletions

View File

@ -250,7 +250,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
resubmitProfilePictureIfNeeded();
loadEmojiSearchIndexIfNeeded();
EmojiSource.refresh();
versionUtil = new VersionUtil(this, textSecurePreferences);
versionUtil = new VersionUtil(textSecurePreferences);
NetworkConstraint networkConstraint = new NetworkConstraint.Factory(this).create();
HTTP.INSTANCE.setConnectedToNetwork(networkConstraint::isMet);
@ -278,8 +278,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
OpenGroupManager.INSTANCE.startPolling();
});
// try to fetch last version now and start the version polling
versionUtil.fetchVersionData();
// fetch last version data
versionUtil.startTimedVersionCheck();
}

View File

@ -18,7 +18,7 @@ fun showMuteDialog(
private enum class Option(@StringRes val stringRes: Int, val getTime: () -> Long) {
ONE_HOUR(R.string.arrays__mute_for_one_hour, duration = TimeUnit.HOURS.toMillis(1)),
TWO_HOURS(R.string.arrays__mute_for_two_hours, duration = TimeUnit.DAYS.toMillis(2)),
TWO_HOURS(R.string.arrays__mute_for_two_hours, duration = TimeUnit.HOURS.toMillis(2)),
ONE_DAY(R.string.arrays__mute_for_one_day, duration = TimeUnit.DAYS.toMillis(1)),
SEVEN_DAYS(R.string.arrays__mute_for_seven_days, duration = TimeUnit.DAYS.toMillis(7)),
FOREVER(R.string.arrays__mute_forever, getTime = { Long.MAX_VALUE });

View File

@ -1,20 +1,19 @@
package org.thoughtcrime.securesms.util
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.session.libsession.messaging.file_server.FileServerApi
import org.session.libsession.utilities.TextSecurePreferences
import java.util.concurrent.TimeUnit
class VersionUtil(
private val context: Context,
private val prefs: TextSecurePreferences
) {
private val FOUR_HOURS: Long = TimeUnit.HOURS.toMillis(4)
private val handler = Handler(Looper.getMainLooper())
private val runnable: Runnable
@ -24,12 +23,8 @@ class VersionUtil(
init {
runnable = Runnable {
// Task to be executed every 4 hours
fetchVersionData()
fetchAndScheduleNextVersionCheck()
}
// Re-schedule the task
handler.postDelayed(runnable, FOUR_HOURS)
}
fun startTimedVersionCheck() {
@ -45,26 +40,25 @@ class VersionUtil(
stopTimedVersionCheck()
}
fun fetchVersionData() {
Log.d("", "***** Trying to fetch version. Last check: ${prefs.getLastVersionCheck()}")
// only perform this if at least 4h has elapsed since th last successful check
if(prefs.getLastVersionCheck() < FOUR_HOURS) return
private fun fetchAndScheduleNextVersionCheck() {
fetchVersionData()
handler.postDelayed(runnable, FOUR_HOURS)
}
private fun fetchVersionData() {
// only perform this if at least 4h has elapsed since th last successful check
val lastCheck = System.currentTimeMillis() - prefs.getLastVersionCheck()
if(lastCheck < FOUR_HOURS) return
job?.cancel()
job = scope.launch {
try {
// perform the version check
Log.d("", "***** Fetching last version")
val clientVersion = FileServerApi.getClientVersion()
Log.d("", "***** Got version: $clientVersion")
prefs.setLastVersionCheck()
} catch (e: Exception) {
// we can silently ignore the error
Log.e("", "***** Error fetching version", e)
}
}
}
companion object {
private const val FOUR_HOURS = 4 * 60 * 60 * 1000L // 4 hours in milliseconds
}
}