1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-1568 App | Tech | Ensure main thread safe operations (#430)

This commit is contained in:
Evgenii Kozlov 2023-10-12 15:17:34 +02:00 committed by GitHub
parent bc90290188
commit 2ec651b657
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 38 additions and 48 deletions

View file

@ -323,7 +323,7 @@ class CreateBlockTesting : EditorTestSetup() {
) {
createBlock.stub {
onBlocking {
execute(params)
async(params)
} doReturn Resultat.success(
Pair(new.id, Payload(context = root, events = events))
)

View file

@ -364,7 +364,7 @@ class ListBlockTesting : EditorTestSetup() {
) {
createBlock.stub {
onBlocking {
execute(params)
async(params)
} doReturn Resultat.success(
Pair(new.id, Payload(context = root, events = events))
)

View file

@ -112,7 +112,7 @@ class MentionUpdateTesting : EditorTestSetup() {
stubInterceptThreadStatus()
stubUpdateText()
openPage.stub {
onBlocking { execute(any()) } doReturn Resultat.success(
onBlocking { async(any()) } doReturn Resultat.success(
Result.Success(
Payload(
context = root,

View file

@ -499,7 +499,7 @@ open class EditorTestSetup {
relations: List<Relation> = emptyList()
) {
openPage.stub {
onBlocking { execute(any()) } doReturn Resultat.success(
onBlocking { async(any()) } doReturn Resultat.success(
Result.Success(
Payload(
context = root,
@ -523,7 +523,7 @@ open class EditorTestSetup {
events: List<Event.Command>
) {
createBlock.stub {
onBlocking { execute(params) } doReturn Resultat.success(
onBlocking { async(params) } doReturn Resultat.success(
Pair(
MockDataFactory.randomUuid(),
Payload(context = root, events = events)

View file

@ -22,12 +22,13 @@ import com.anytypeio.anytype.domain.config.GetDebugSettings
import com.anytypeio.anytype.domain.config.UseCustomContextMenu
import com.anytypeio.anytype.domain.debugging.DebugLocalStore
import com.anytypeio.anytype.domain.debugging.DebugSpace
import kotlinx.coroutines.launch
import timber.log.Timber
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
import java.util.Calendar
import java.util.Locale
import javax.inject.Inject
import kotlinx.coroutines.launch
import timber.log.Timber
class DebugSettingsFragment : BaseFragment<FragmentDebugSettingsBinding>(R.layout.fragment_debug_settings) {
@ -55,7 +56,7 @@ class DebugSettingsFragment : BaseFragment<FragmentDebugSettingsBinding>(R.layou
binding.btnSync.setOnClickListener {
viewLifecycleOwner.lifecycleScope.launch {
debugSpace.execute(Unit).fold(
debugSpace.async(Unit).fold(
onSuccess = { status -> saveToFile(status) }
)
}

View file

@ -87,11 +87,6 @@ abstract class ResultInteractor<in P, R>(
suspend fun run(params: P) = doWork(params)
/*
* N.B. Executes on the caller's thread.
* */
suspend fun execute(params: P): Resultat<R> = runCatchingL { doWork(params) }
suspend fun async(params: P): Resultat<R> = withContext(context) {
runCatchingL { doWork(params) }
}

View file

@ -65,7 +65,7 @@ class GetObjectTypeTest {
runBlocking {
stubGetObjectTypes(types = listOf(type1, type2, type3))
val firstTimeResult = usecase.execute(params = defaultParams)
val firstTimeResult = usecase.async(params = defaultParams)
firstTimeResult.fold(
onFailure = { Assert.fail() },
onSuccess = { results ->
@ -75,7 +75,7 @@ class GetObjectTypeTest {
)
}
)
val secondTimeResult = usecase.execute(params = defaultParams)
val secondTimeResult = usecase.async(params = defaultParams)
assertEquals(firstTimeResult, secondTimeResult)
verify(repo, times(2)).searchObjects(

View file

@ -7,7 +7,6 @@ import androidx.lifecycle.viewModelScope
import com.anytypeio.anytype.CrashReporter
import com.anytypeio.anytype.analytics.base.Analytics
import com.anytypeio.anytype.analytics.base.EventsDictionary
import com.anytypeio.anytype.core_models.exceptions.CreateAccountException
import com.anytypeio.anytype.core_utils.common.EventWrapper
import com.anytypeio.anytype.domain.auth.interactor.CreateAccount
import com.anytypeio.anytype.domain.base.fold
@ -20,7 +19,6 @@ import com.anytypeio.anytype.presentation.extension.proceedWithAccountEvent
import com.anytypeio.anytype.presentation.navigation.AppNavigation
import com.anytypeio.anytype.presentation.navigation.SupportNavigation
import com.anytypeio.anytype.presentation.spaces.SpaceGradientProvider
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import timber.log.Timber
@ -95,7 +93,7 @@ class SetupNewAccountViewModel(
private fun setupUseCase() {
viewModelScope.launch {
setupMobileUseCaseSkip.execute(Unit).fold(
setupMobileUseCaseSkip.async(Unit).fold(
onFailure = {
Timber.e(it, "Error while importing use case")
navigateToDashboard()

View file

@ -137,7 +137,7 @@ class LibraryViewModel(
private fun proceedWithCreateDoc() {
viewModelScope.launch {
createObject.execute(CreateObject.Param(type = null))
createObject.async(CreateObject.Param(type = null))
.fold(
onSuccess = { result ->
navigate(Navigation.CreateDoc(result.objectId))
@ -231,7 +231,7 @@ class LibraryViewModel(
private fun uninstallObject(item: LibraryView, id: Id) {
viewModelScope.launch {
removeObjectsFromWorkspace.execute(
removeObjectsFromWorkspace.async(
RemoveObjectsFromWorkspace.Params(listOf(id))
).fold(
onFailure = {
@ -257,7 +257,7 @@ class LibraryViewModel(
fun uninstallObject(id: Id, type: LibraryItem, name: String) {
viewModelScope.launch {
removeObjectsFromWorkspace.execute(
removeObjectsFromWorkspace.async(
RemoveObjectsFromWorkspace.Params(listOf(id))
).fold(
onFailure = {
@ -343,7 +343,7 @@ class LibraryViewModel(
fun updateObject(id: String, name: String, icon: String?) {
viewModelScope.launch {
setObjectDetails.execute(
setObjectDetails.async(
SetObjectDetails.Params(
ctx = id,
details = mapOf(

View file

@ -143,7 +143,7 @@ class MoveToViewModel(
},
keys = ObjectSearchConstants.defaultKeysObjectType
)
getObjectTypes.execute(params).fold(
getObjectTypes.async(params).fold(
onFailure = { Timber.e(it, "Error while getting object types") },
onSuccess = {
types.value = Resultat.success(it)

View file

@ -22,7 +22,7 @@ class CreateObjectViewModel(private val createObject: CreateObject) : ViewModel(
private fun onCreatePage(type: String) {
val params = CreateObject.Param(type = type)
jobs += viewModelScope.launch {
createObject.execute(params).fold(
createObject.async(params).fold(
onFailure = { e ->
Timber.e(e, "Error while creating a new object with type:$type")
},

View file

@ -108,7 +108,7 @@ class ObjectTypeChangeViewModel(
isSetSource: Boolean
) {
viewModelScope.launch {
getDefaultPageType.execute(Unit).fold(
getDefaultPageType.async(Unit).fold(
onFailure = { e ->
Timber.e(e, "Error while getting user settings")
},

View file

@ -3,16 +3,15 @@ package com.anytypeio.anytype.presentation.objects.menu
import android.net.Uri
import androidx.lifecycle.viewModelScope
import com.anytypeio.anytype.analytics.base.Analytics
import com.anytypeio.anytype.analytics.base.EventsDictionary
import com.anytypeio.anytype.core_models.Id
import com.anytypeio.anytype.core_models.ObjectType
import com.anytypeio.anytype.core_models.Payload
import com.anytypeio.anytype.domain.`object`.DuplicateObject
import com.anytypeio.anytype.domain.base.fold
import com.anytypeio.anytype.domain.collections.AddObjectToCollection
import com.anytypeio.anytype.domain.dashboard.interactor.AddToFavorite
import com.anytypeio.anytype.domain.dashboard.interactor.RemoveFromFavorite
import com.anytypeio.anytype.domain.misc.UrlBuilder
import com.anytypeio.anytype.domain.`object`.DuplicateObject
import com.anytypeio.anytype.domain.objects.SetObjectIsArchived
import com.anytypeio.anytype.domain.page.AddBackLinkToObject
import com.anytypeio.anytype.presentation.common.Action
@ -227,7 +226,7 @@ abstract class ObjectMenuViewModelBase(
)
viewModelScope.launch {
val startTime = System.currentTimeMillis()
addObjectToCollection.execute(params).fold(
addObjectToCollection.async(params).fold(
onSuccess = { payload ->
dispatcher.send(payload)
sendAnalyticsAddToCollectionEvent(
@ -253,7 +252,7 @@ abstract class ObjectMenuViewModelBase(
Timber.d("onLinkedMyselfTo, myself:[$myself], addTo:[$addTo], fromName:[$fromName]")
jobs += viewModelScope.launch {
val startTime = System.currentTimeMillis()
addBackLinkToObject.execute(
addBackLinkToObject.async(
AddBackLinkToObject.Params(objectToLink = myself, objectToPlaceLink = addTo)
).fold(
onSuccess = { obj ->

View file

@ -1007,7 +1007,7 @@ class ObjectSetViewModel(
targets = listOf(result.objectId)
)
viewModelScope.launch {
addObjectToCollection.execute(params).fold(
addObjectToCollection.async(params).fold(
onSuccess = { payload -> dispatcher.send(payload) },
onFailure = { Timber.e(it, "Error while adding object to collection") }
)
@ -1480,7 +1480,7 @@ class ObjectSetViewModel(
ctx = context,
query = query
)
setQueryToObjectSet.execute(params).fold(
setQueryToObjectSet.async(params).fold(
onSuccess = { payload ->
logEvent(
state = stateReducer.state.value,
@ -1500,7 +1500,7 @@ class ObjectSetViewModel(
val startTime = System.currentTimeMillis()
val params = ConvertObjectToCollection.Params(ctx = context)
viewModelScope.launch {
objectToCollection.execute(params).fold(
objectToCollection.async(params).fold(
onFailure = { error -> Timber.e(error, "Error convert object to collection") },
onSuccess = {
isCustomizeViewPanelVisible.value = false

View file

@ -202,7 +202,7 @@ class MainSettingsViewModel(
fun onNameSet(name: String) {
viewModelScope.launch {
setObjectDetails.execute(
setObjectDetails.async(
SetObjectDetails.Params(
ctx = configStorage.get().workspace,
details = mapOf(

View file

@ -36,7 +36,7 @@ class OtherSettingsViewModel(
init {
viewModelScope.launch {
getDefaultPageType.execute(Unit).fold(
getDefaultPageType.async(Unit).fold(
onFailure = { e ->
Timber.e(e, "Error while getting user settings")
},

View file

@ -11,6 +11,7 @@ import com.anytypeio.anytype.analytics.props.UserProperty
import com.anytypeio.anytype.core_models.Id
import com.anytypeio.anytype.core_models.ObjectType
import com.anytypeio.anytype.core_models.exceptions.MigrationNeededException
import com.anytypeio.anytype.core_models.exceptions.NeedToUpdateApplicationException
import com.anytypeio.anytype.core_utils.tools.FeatureToggles
import com.anytypeio.anytype.domain.auth.interactor.CheckAuthorizationStatus
import com.anytypeio.anytype.domain.auth.interactor.GetLastOpenedObject
@ -19,8 +20,6 @@ import com.anytypeio.anytype.domain.auth.interactor.LaunchWallet
import com.anytypeio.anytype.domain.auth.model.AuthStatus
import com.anytypeio.anytype.domain.base.BaseUseCase
import com.anytypeio.anytype.domain.base.fold
import com.anytypeio.anytype.CrashReporter
import com.anytypeio.anytype.core_models.exceptions.NeedToUpdateApplicationException
import com.anytypeio.anytype.domain.page.CreateObject
import com.anytypeio.anytype.domain.search.ObjectTypesSubscriptionManager
import com.anytypeio.anytype.domain.search.RelationsSubscriptionManager
@ -135,7 +134,7 @@ class SplashViewModel(
fun onIntentCreateNewObject(type: Id) {
viewModelScope.launch {
createObject.execute(
createObject.async(
CreateObject.Param(type = type)
).fold(
onFailure = { e ->

View file

@ -59,7 +59,7 @@ class TypeCreationViewModel(
return
}
viewModelScope.launch {
createTypeInteractor.execute(
createTypeInteractor.async(
CreateType.Params(
name = name,
emojiUnicode = uiState.value.emojiUnicode.orNull()

View file

@ -752,7 +752,7 @@ class CollectionViewModel(
val startTime = System.currentTimeMillis()
launch {
createObject.execute(CreateObject.Param(type = null))
createObject.async(CreateObject.Param(type = null))
.fold(
onSuccess = { result ->
sendAnalyticsObjectCreateEvent(

View file

@ -2295,7 +2295,7 @@ open class EditorViewModelTest {
runBlockingTest {
verify(createBlock, never()).execute(params = any())
verify(createBlock, never()).async(params = any())
verify(updateTextStyle, times(1)).invoke(
params = eq(

View file

@ -447,7 +447,7 @@ class EditorTitleAddBlockTest : EditorPresentationTestSetup() {
params: CreateBlock.Params
) {
createBlock.stub {
onBlocking { execute(params) } doReturn Resultat.success(
onBlocking { async(params) } doReturn Resultat.success(
Pair(
MockDataFactory.randomUuid(),
Payload(

View file

@ -266,7 +266,7 @@ class HomeScreenViewModelTest {
) {
saveWidgetSession.stub {
onBlocking {
execute(SaveWidgetSession.Params(session))
async(SaveWidgetSession.Params(session))
} doReturn Resultat.Success(Unit)
}
}
@ -1082,7 +1082,7 @@ class HomeScreenViewModelTest {
getDefaultPageType.stub {
onBlocking {
execute(any())
async(any())
} doReturn Resultat.Success(
GetDefaultPageType.Response(
null, null
@ -2621,7 +2621,7 @@ class HomeScreenViewModelTest {
private fun stubGetDefaultPageType() {
getDefaultPageType.stub {
onBlocking {
execute(any())
async(any())
} doReturn Resultat.Success(
GetDefaultPageType.Response(null, null)
)

View file

@ -16,7 +16,6 @@ import com.anytypeio.anytype.domain.icon.SetDocumentImageIcon
import com.anytypeio.anytype.domain.icon.SetImageIcon
import com.anytypeio.anytype.domain.library.StoreSearchByIdsParams
import com.anytypeio.anytype.domain.library.StorelessSubscriptionContainer
import com.anytypeio.anytype.domain.library.StorelessSubscriptionContainer.Companion.SUBSCRIPTION_PROFILE
import com.anytypeio.anytype.domain.misc.UrlBuilder
import com.anytypeio.anytype.domain.`object`.SetObjectDetails
import com.anytypeio.anytype.domain.search.PROFILE_SUBSCRIPTION_ID
@ -24,7 +23,6 @@ import com.anytypeio.anytype.presentation.extension.sendScreenSettingsDeleteEven
import com.anytypeio.anytype.presentation.profile.ProfileIconView
import com.anytypeio.anytype.presentation.profile.profileIcon
import com.anytypeio.anytype.presentation.spaces.SpaceGradientProvider
import javax.inject.Named
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
@ -78,7 +76,7 @@ class ProfileViewModel(
fun onNameChange(name: String) {
Timber.d("onNameChange, name:[$name]")
viewModelScope.launch {
setObjectDetails.execute(
setObjectDetails.async(
SetObjectDetails.Params(
ctx = profileId,
details = mapOf(Relations.NAME to name)