mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-11 17:27:42 +00:00
Getting rid of .get call on promise
This commit is contained in:
@@ -115,6 +115,7 @@ import javax.inject.Inject;
|
||||
|
||||
import dagger.hilt.EntryPoints;
|
||||
import dagger.hilt.android.HiltAndroidApp;
|
||||
import kotlin.Unit;
|
||||
import network.loki.messenger.BuildConfig;
|
||||
import network.loki.messenger.R;
|
||||
|
||||
@@ -307,6 +308,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
|
||||
startPollingIfNeeded();
|
||||
|
||||
OpenGroupManager.INSTANCE.startPolling();
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
|
||||
// fetch last version data
|
||||
|
@@ -6,7 +6,6 @@ import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import java.util.concurrent.TimeoutException
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
@@ -14,12 +13,12 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import network.loki.messenger.R
|
||||
import org.session.libsession.snode.SnodeAPI
|
||||
import org.session.libsession.snode.utilities.await
|
||||
import org.session.libsignal.utilities.PublicKeyValidation
|
||||
import org.session.libsignal.utilities.timeout
|
||||
import org.thoughtcrime.securesms.ui.GetString
|
||||
|
||||
@HiltViewModel
|
||||
@@ -68,12 +67,14 @@ internal class NewMessageViewModel @Inject constructor(
|
||||
// This could be an ONS name
|
||||
_state.update { it.copy(isTextErrorColor = false, error = null, loading = true) }
|
||||
|
||||
loadOnsJob = viewModelScope.launch(Dispatchers.IO) {
|
||||
loadOnsJob = viewModelScope.launch {
|
||||
try {
|
||||
val publicKey = SnodeAPI.getAccountID(ons).timeout(30_000).get()
|
||||
if (isActive) onPublicKey(publicKey)
|
||||
val publicKey = withTimeout(30_000L, {
|
||||
SnodeAPI.getAccountID(ons).await()
|
||||
})
|
||||
onPublicKey(publicKey)
|
||||
} catch (e: Exception) {
|
||||
if (isActive) onError(e)
|
||||
onError(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -9,7 +9,11 @@ import android.text.SpannableStringBuilder
|
||||
import android.text.style.StyleSpan
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.squareup.phrase.Phrase
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import network.loki.messenger.R
|
||||
import org.session.libsession.database.StorageProtocol
|
||||
import org.session.libsession.utilities.OpenGroupUrlParser
|
||||
@@ -43,14 +47,23 @@ class JoinOpenGroupDialog(private val name: String, private val url: String) : D
|
||||
private fun join() {
|
||||
val openGroup = OpenGroupUrlParser.parseUrl(url)
|
||||
val activity = requireActivity()
|
||||
ThreadUtils.queue {
|
||||
lifecycleScope.launch {
|
||||
try {
|
||||
openGroup.apply { OpenGroupManager.add(server, room, serverPublicKey, activity) }
|
||||
storage.onOpenGroupAdded(openGroup.server, openGroup.room)
|
||||
withContext(Dispatchers.Default) {
|
||||
OpenGroupManager.add(
|
||||
server = openGroup.server,
|
||||
room = openGroup.room,
|
||||
publicKey = openGroup.serverPublicKey,
|
||||
context = activity
|
||||
)
|
||||
|
||||
storage.onOpenGroupAdded(openGroup.server, openGroup.room)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Toast.makeText(activity, R.string.communityErrorDescription, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
@@ -328,7 +328,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
||||
contentValues.put(HAS_MENTION, 0)
|
||||
database.update(TABLE_NAME, contentValues, ID_WHERE, arrayOf(messageId.toString()))
|
||||
val attachmentDatabase = get(context).attachmentDatabase()
|
||||
queue(Runnable { attachmentDatabase.deleteAttachmentsForMessage(messageId) })
|
||||
queue { attachmentDatabase.deleteAttachmentsForMessage(messageId) }
|
||||
val threadId = getThreadIdForMessage(messageId)
|
||||
|
||||
markAs(messageId, MmsSmsColumns.Types.BASE_DELETED_TYPE, threadId)
|
||||
@@ -889,7 +889,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
||||
}
|
||||
val idsAsString = queryBuilder.toString()
|
||||
val attachmentDatabase = get(context).attachmentDatabase()
|
||||
queue(Runnable { attachmentDatabase.deleteAttachmentsForMessages(messageIds) })
|
||||
queue { attachmentDatabase.deleteAttachmentsForMessages(messageIds) }
|
||||
val groupReceiptDatabase = get(context).groupReceiptDatabase()
|
||||
groupReceiptDatabase.deleteRowsForMessages(messageIds)
|
||||
val database = databaseHelper.writableDatabase
|
||||
@@ -906,7 +906,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
||||
override fun deleteMessage(messageId: Long): Boolean {
|
||||
val threadId = getThreadIdForMessage(messageId)
|
||||
val attachmentDatabase = get(context).attachmentDatabase()
|
||||
queue(Runnable { attachmentDatabase.deleteAttachmentsForMessage(messageId) })
|
||||
queue { attachmentDatabase.deleteAttachmentsForMessage(messageId) }
|
||||
val groupReceiptDatabase = get(context).groupReceiptDatabase()
|
||||
groupReceiptDatabase.deleteRowsForMessage(messageId)
|
||||
val database = databaseHelper.writableDatabase
|
||||
@@ -925,7 +925,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
||||
val attachmentDatabase = get(context).attachmentDatabase()
|
||||
val groupReceiptDatabase = get(context).groupReceiptDatabase()
|
||||
|
||||
queue(Runnable { attachmentDatabase.deleteAttachmentsForMessages(messageIds) })
|
||||
queue { attachmentDatabase.deleteAttachmentsForMessages(messageIds) }
|
||||
groupReceiptDatabase.deleteRowsForMessages(messageIds)
|
||||
|
||||
val db = databaseHelper.writableDatabase
|
||||
|
@@ -222,7 +222,7 @@ class ConfigFactory @Inject constructor(
|
||||
private val groupConfigs = ConcurrentHashMap<AccountId, GroupConfigsImpl>()
|
||||
|
||||
private val _configUpdateNotifications = MutableSharedFlow<ConfigUpdateNotification>(
|
||||
extraBufferCapacity = 1,
|
||||
extraBufferCapacity = 5, // The notifications are normally important so we can afford to buffer a few
|
||||
onBufferOverflow = BufferOverflow.SUSPEND
|
||||
)
|
||||
override val configUpdateNotifications get() = _configUpdateNotifications
|
||||
@@ -260,15 +260,15 @@ class ConfigFactory @Inject constructor(
|
||||
* @param cb A function that takes a [UserConfigsImpl] and returns a pair of the result of the operation and a boolean indicating if the configs were changed.
|
||||
*/
|
||||
private fun <T> doWithMutableUserConfigs(cb: (UserConfigsImpl) -> Pair<T, Boolean>): T {
|
||||
return withUserConfigs { configs ->
|
||||
val (result, changed) = cb(configs as UserConfigsImpl)
|
||||
|
||||
if (changed) {
|
||||
_configUpdateNotifications.tryEmit(ConfigUpdateNotification.UserConfigs)
|
||||
}
|
||||
|
||||
result
|
||||
val (result, changed) = withUserConfigs { configs ->
|
||||
cb(configs as UserConfigsImpl)
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
_configUpdateNotifications.tryEmit(ConfigUpdateNotification.UserConfigs)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
override fun mergeUserConfigs(
|
||||
@@ -319,19 +319,19 @@ class ConfigFactory @Inject constructor(
|
||||
}
|
||||
|
||||
private fun <T> doWithMutableGroupConfigs(groupId: AccountId, cb: (GroupConfigsImpl) -> Pair<T, Boolean>): T {
|
||||
return withGroupConfigs(groupId) { configs ->
|
||||
val (result, changed) = cb(configs as GroupConfigsImpl)
|
||||
|
||||
Log.d("ConfigFactory", "Group updated? $groupId: $changed")
|
||||
|
||||
if (changed) {
|
||||
if (!_configUpdateNotifications.tryEmit(ConfigUpdateNotification.GroupConfigsUpdated(groupId))) {
|
||||
Log.e("ConfigFactory", "Unable to deliver group update notification")
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
val (result, changed) = withGroupConfigs(groupId) { configs ->
|
||||
cb(configs as GroupConfigsImpl)
|
||||
}
|
||||
|
||||
Log.d("ConfigFactory", "Group updated? $groupId: $changed")
|
||||
|
||||
if (changed) {
|
||||
if (!_configUpdateNotifications.tryEmit(ConfigUpdateNotification.GroupConfigsUpdated(groupId))) {
|
||||
Log.e("ConfigFactory", "Unable to deliver group update notification")
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
override fun <T> withMutableGroupConfigs(
|
||||
|
@@ -240,7 +240,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
||||
}
|
||||
|
||||
lifecycleScope.launchWhenStarted {
|
||||
launch(Dispatchers.IO) {
|
||||
launch(Dispatchers.Default) {
|
||||
// Double check that the long poller is up
|
||||
(applicationContext as ApplicationContext).startPollingIfNeeded()
|
||||
// update things based on TextSecurePrefs (profile info etc)
|
||||
@@ -516,7 +516,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
||||
.put(NAME_KEY, thread.recipient.name)
|
||||
.format())
|
||||
dangerButton(R.string.block, R.string.AccessibilityId_blockConfirm) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
lifecycleScope.launch(Dispatchers.Default) {
|
||||
storage.setBlocked(listOf(thread.recipient), true)
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
@@ -536,7 +536,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
||||
title(R.string.blockUnblock)
|
||||
text(Phrase.from(context, R.string.blockUnblockName).put(NAME_KEY, thread.recipient.name).format())
|
||||
dangerButton(R.string.blockUnblock, R.string.AccessibilityId_unblockConfirm) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
lifecycleScope.launch(Dispatchers.Default) {
|
||||
storage.setBlocked(listOf(thread.recipient), false)
|
||||
withContext(Dispatchers.Main) {
|
||||
binding.recyclerView.adapter!!.notifyDataSetChanged()
|
||||
@@ -549,7 +549,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
||||
|
||||
private fun setConversationMuted(thread: ThreadRecord, isMuted: Boolean) {
|
||||
if (!isMuted) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
lifecycleScope.launch(Dispatchers.Default) {
|
||||
recipientDatabase.setMuted(thread.recipient, 0)
|
||||
withContext(Dispatchers.Main) {
|
||||
binding.recyclerView.adapter!!.notifyDataSetChanged()
|
||||
@@ -557,7 +557,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
||||
}
|
||||
} else {
|
||||
showMuteDialog(this) { until ->
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
lifecycleScope.launch(Dispatchers.Default) {
|
||||
Log.d("", "**** until: $until")
|
||||
recipientDatabase.setMuted(thread.recipient, until)
|
||||
withContext(Dispatchers.Main) {
|
||||
@@ -569,7 +569,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
||||
}
|
||||
|
||||
private fun setNotifyType(thread: ThreadRecord, newNotifyType: Int) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
lifecycleScope.launch(Dispatchers.Default) {
|
||||
recipientDatabase.setNotifyType(thread.recipient, newNotifyType)
|
||||
withContext(Dispatchers.Main) {
|
||||
binding.recyclerView.adapter!!.notifyDataSetChanged()
|
||||
@@ -578,14 +578,14 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
||||
}
|
||||
|
||||
private fun setConversationPinned(threadId: Long, pinned: Boolean) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
lifecycleScope.launch(Dispatchers.Default) {
|
||||
storage.setPinned(threadId, pinned)
|
||||
homeViewModel.tryReload()
|
||||
}
|
||||
}
|
||||
|
||||
private fun markAllAsRead(thread: ThreadRecord) {
|
||||
ThreadUtils.queue {
|
||||
lifecycleScope.launch(Dispatchers.Default) {
|
||||
MessagingModuleConfiguration.shared.storage.markConversationAsRead(thread.threadId, SnodeAPI.nowWithOffset)
|
||||
}
|
||||
}
|
||||
@@ -656,7 +656,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
||||
context
|
||||
)
|
||||
} else {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
lifecycleScope.launch(Dispatchers.Default) {
|
||||
threadDb.deleteConversation(threadID)
|
||||
}
|
||||
}
|
||||
|
@@ -6,12 +6,15 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.AsyncTask
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.session.libsession.database.userAuth
|
||||
import org.session.libsession.messaging.MessagingModuleConfiguration.Companion.shared
|
||||
import org.session.libsession.messaging.messages.control.ReadReceipt
|
||||
import org.session.libsession.messaging.sending_receiving.MessageSender.send
|
||||
import org.session.libsession.snode.SnodeAPI
|
||||
import org.session.libsession.snode.SnodeAPI.nowWithOffset
|
||||
import org.session.libsession.snode.utilities.await
|
||||
import org.session.libsession.utilities.SSKEnvironment
|
||||
import org.session.libsession.utilities.TextSecurePreferences.Companion.isReadReceiptsEnabled
|
||||
import org.session.libsession.utilities.associateByNotNull
|
||||
@@ -72,9 +75,11 @@ class MarkReadReceiver : BroadcastReceiver() {
|
||||
}
|
||||
.forEach { messageExpirationManager.startDisappearAfterRead(it.timetamp, it.address.serialize()) }
|
||||
|
||||
hashToDisappearAfterReadMessage(context, markedReadMessages)?.let {
|
||||
fetchUpdatedExpiriesAndScheduleDeletion(context, it)
|
||||
shortenExpiryOfDisappearingAfterRead(context, it)
|
||||
hashToDisappearAfterReadMessage(context, markedReadMessages)?.let { hashToMessages ->
|
||||
GlobalScope.launch {
|
||||
fetchUpdatedExpiriesAndScheduleDeletion(context, hashToMessages)
|
||||
shortenExpiryOfDisappearingAfterRead(hashToMessages)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +96,6 @@ class MarkReadReceiver : BroadcastReceiver() {
|
||||
}
|
||||
|
||||
private fun shortenExpiryOfDisappearingAfterRead(
|
||||
context: Context,
|
||||
hashToMessage: Map<String, MarkedMessageInfo>
|
||||
) {
|
||||
hashToMessage.entries
|
||||
@@ -125,12 +129,12 @@ class MarkReadReceiver : BroadcastReceiver() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun fetchUpdatedExpiriesAndScheduleDeletion(
|
||||
private suspend fun fetchUpdatedExpiriesAndScheduleDeletion(
|
||||
context: Context,
|
||||
hashToMessage: Map<String, MarkedMessageInfo>
|
||||
) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val expiries = SnodeAPI.getExpiries(hashToMessage.keys.toList(), shared.storage.userAuth!!).get()["expiries"] as Map<String, Long>
|
||||
val expiries = SnodeAPI.getExpiries(hashToMessage.keys.toList(), shared.storage.userAuth!!).await()["expiries"] as Map<String, Long>
|
||||
hashToMessage.forEach { (hash, info) -> expiries[hash]?.let { scheduleDeletion(context, info.expirationInfo, it - info.expirationInfo.expireStarted) } }
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,8 @@ import org.thoughtcrime.securesms.groups.OpenGroupManager;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import kotlin.Unit;
|
||||
|
||||
public class OptimizedMessageNotifier implements MessageNotifier {
|
||||
private final MessageNotifier wrapped;
|
||||
private final Debouncer debouncer;
|
||||
@@ -118,7 +120,10 @@ public class OptimizedMessageNotifier implements MessageNotifier {
|
||||
|
||||
private void performOnBackgroundThreadIfNeeded(Runnable r) {
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
ThreadUtils.queue(r);
|
||||
ThreadUtils.queue(() -> {
|
||||
r.run();
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
} else {
|
||||
r.run();
|
||||
}
|
||||
|
@@ -6,6 +6,8 @@ import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import com.opencsv.CSVReader
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.session.libsession.snode.OnionRequestAPI
|
||||
import org.session.libsignal.utilities.Log
|
||||
import org.session.libsignal.utilities.ThreadUtils
|
||||
@@ -133,7 +135,7 @@ class IP2Country private constructor(private val context: Context) {
|
||||
}
|
||||
|
||||
private fun populateCacheIfNeeded() {
|
||||
ThreadUtils.queue {
|
||||
GlobalScope.launch {
|
||||
OnionRequestAPI.paths.iterator().forEach { path ->
|
||||
path.iterator().forEach { snode ->
|
||||
cacheCountryForIP(snode.ip) // Preload if needed
|
||||
|
Reference in New Issue
Block a user