fix: tests and main thread blocking db fetch for path status view

This commit is contained in:
0x330a 2023-06-26 17:38:47 +10:00
parent 1676e97bc6
commit eb173d72e0
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
4 changed files with 35 additions and 11 deletions

View File

@ -57,7 +57,7 @@ class LibSessionTests {
private fun fakePollNewConfig(configBase: ConfigBase, toMerge: ByteArray) { private fun fakePollNewConfig(configBase: ConfigBase, toMerge: ByteArray) {
configBase.merge(nextFakeHash to toMerge) configBase.merge(nextFakeHash to toMerge)
MessagingModuleConfiguration.shared.configFactory.persist(configBase) MessagingModuleConfiguration.shared.configFactory.persist(configBase, System.currentTimeMillis())
} }
@Before @Before

View File

@ -60,6 +60,7 @@ import org.thoughtcrime.securesms.database.LokiAPIDatabase;
import org.thoughtcrime.securesms.database.Storage; import org.thoughtcrime.securesms.database.Storage;
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper; import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
import org.thoughtcrime.securesms.database.model.EmojiSearchData; import org.thoughtcrime.securesms.database.model.EmojiSearchData;
import org.thoughtcrime.securesms.dependencies.AppComponent;
import org.thoughtcrime.securesms.dependencies.ConfigFactory; import org.thoughtcrime.securesms.dependencies.ConfigFactory;
import org.thoughtcrime.securesms.dependencies.DatabaseComponent; import org.thoughtcrime.securesms.dependencies.DatabaseComponent;
import org.thoughtcrime.securesms.dependencies.DatabaseModule; import org.thoughtcrime.securesms.dependencies.DatabaseModule;
@ -162,6 +163,10 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
return (ApplicationContext) context.getApplicationContext(); return (ApplicationContext) context.getApplicationContext();
} }
public TextSecurePreferences getPrefs() {
return EntryPoints.get(getApplicationContext(), AppComponent.class).getPrefs();
}
public DatabaseComponent getDatabaseComponent() { public DatabaseComponent getDatabaseComponent() {
return EntryPoints.get(getApplicationContext(), DatabaseComponent.class); return EntryPoints.get(getApplicationContext(), DatabaseComponent.class);
} }

View File

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.dependencies
import dagger.Binds import dagger.Binds
import dagger.Module import dagger.Module
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import org.session.libsession.utilities.AppTextSecurePreferences import org.session.libsession.utilities.AppTextSecurePreferences
@ -19,4 +20,10 @@ abstract class AppModule {
@Binds @Binds
abstract fun bindConversationRepository(repository: DefaultConversationRepository): ConversationRepository abstract fun bindConversationRepository(repository: DefaultConversationRepository): ConversationRepository
}
@EntryPoint
@InstallIn(SingletonComponent::class)
interface AppComponent {
fun getPrefs(): TextSecurePreferences
} }

View File

@ -9,9 +9,14 @@ import android.graphics.Paint
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.lifecycle.coroutineScope
import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.localbroadcastmanager.content.LocalBroadcastManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.withContext
import network.loki.messenger.R import network.loki.messenger.R
import org.session.libsession.snode.OnionRequestAPI import org.session.libsession.snode.OnionRequestAPI
import org.thoughtcrime.securesms.conversation.v2.ViewUtil
import org.thoughtcrime.securesms.util.getColorWithID import org.thoughtcrime.securesms.util.getColorWithID
import org.thoughtcrime.securesms.util.toPx import org.thoughtcrime.securesms.util.toPx
@ -29,6 +34,8 @@ class PathStatusView : View {
result result
} }
private var updateJob: Job? = null
constructor(context: Context) : super(context) { constructor(context: Context) : super(context) {
initialize() initialize()
} }
@ -87,16 +94,21 @@ class PathStatusView : View {
private fun handlePathsBuiltEvent() { update() } private fun handlePathsBuiltEvent() { update() }
private fun update() { private fun update() {
if (OnionRequestAPI.paths.isNotEmpty()) { if (updateJob?.isActive != true) { // false or null
setBackgroundResource(R.drawable.accent_dot) updateJob = ViewUtil.getActivityLifecycle(this)?.coroutineScope?.launchWhenStarted {
val hasPathsColor = context.getColor(R.color.accent_green) val paths = withContext(Dispatchers.IO) { OnionRequestAPI.paths }
mainColor = hasPathsColor if (paths.isNotEmpty()) {
sessionShadowColor = hasPathsColor setBackgroundResource(R.drawable.accent_dot)
} else { val hasPathsColor = context.getColor(R.color.accent_green)
setBackgroundResource(R.drawable.paths_building_dot) mainColor = hasPathsColor
val pathsBuildingColor = resources.getColorWithID(R.color.paths_building, context.theme) sessionShadowColor = hasPathsColor
mainColor = pathsBuildingColor } else {
sessionShadowColor = pathsBuildingColor setBackgroundResource(R.drawable.paths_building_dot)
val pathsBuildingColor = resources.getColorWithID(R.color.paths_building, context.theme)
mainColor = pathsBuildingColor
sessionShadowColor = pathsBuildingColor
}
}
} }
} }