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

DROID-1921 Tech | Integrate StackGoroutines, added to the menu (#524)

This commit is contained in:
Konstantin Ivanov 2023-11-09 12:02:58 +01:00 committed by konstantiniiv
parent 06e9e581bc
commit 2673b5bfc5
11 changed files with 189 additions and 27 deletions

View file

@ -1,9 +1,9 @@
package com.anytypeio.anytype.di.feature
import android.content.Context
import com.anytypeio.anytype.analytics.base.Analytics
import com.anytypeio.anytype.core_models.Payload
import com.anytypeio.anytype.core_utils.di.scope.PerDialog
import com.anytypeio.anytype.core_utils.di.scope.PerScreen
import com.anytypeio.anytype.core_utils.tools.FeatureToggles
import com.anytypeio.anytype.domain.`object`.DuplicateObject
import com.anytypeio.anytype.domain.auth.repo.AuthRepository
@ -30,7 +30,9 @@ import com.anytypeio.anytype.presentation.objects.menu.ObjectMenuViewModel
import com.anytypeio.anytype.presentation.objects.menu.ObjectSetMenuViewModel
import com.anytypeio.anytype.presentation.sets.state.ObjectState
import com.anytypeio.anytype.presentation.util.Dispatcher
import com.anytypeio.anytype.presentation.util.downloader.DebugGoroutinesShareDownloader
import com.anytypeio.anytype.presentation.util.downloader.DebugTreeShareDownloader
import com.anytypeio.anytype.presentation.util.downloader.UriFileProvider
import com.anytypeio.anytype.ui.editor.sheets.ObjectMenuFragment
import com.anytypeio.anytype.ui.sets.ObjectSetMenuFragment
import dagger.Module
@ -117,7 +119,8 @@ object ObjectMenuModule {
delegator: Delegator<Action>,
addObjectToCollection: AddObjectToCollection,
createTemplateFromObject: CreateTemplateFromObject,
setObjectDetails: SetObjectDetails
setObjectDetails: SetObjectDetails,
debugGoroutinesShareDownloader: DebugGoroutinesShareDownloader
): ObjectMenuViewModel.Factory = ObjectMenuViewModel.Factory(
setObjectIsArchived = setObjectIsArchived,
duplicateObject = duplicateObject,
@ -134,7 +137,8 @@ object ObjectMenuModule {
menuOptionsProvider = createMenuOptionsProvider(storage, featureToggles),
addObjectToCollection = addObjectToCollection,
createTemplateFromObject = createTemplateFromObject,
setObjectDetails = setObjectDetails
setObjectDetails = setObjectDetails,
debugGoroutinesShareDownloader = debugGoroutinesShareDownloader
)
@JvmStatic
@ -166,6 +170,21 @@ object ObjectMenuModule {
repo = repo,
dispatchers = dispatchers
)
@JvmStatic
@Provides
@PerDialog
fun debugGoRoutines(
repo: BlockRepository,
context: Context,
fileProvider: UriFileProvider,
dispatchers: AppCoroutineDispatchers
): DebugGoroutinesShareDownloader = DebugGoroutinesShareDownloader(
repo = repo,
context = context.applicationContext,
uriFileProvider = fileProvider,
dispatchers = dispatchers
)
}
@Module
@ -186,7 +205,8 @@ object ObjectSetMenuModule {
state: MutableStateFlow<ObjectState>,
featureToggles: FeatureToggles,
dispatcher: Dispatcher<Payload>,
addObjectToCollection: AddObjectToCollection
addObjectToCollection: AddObjectToCollection,
debugGoroutinesShareDownloader: DebugGoroutinesShareDownloader
): ObjectSetMenuViewModel.Factory = ObjectSetMenuViewModel.Factory(
setObjectIsArchived = setObjectIsArchived,
addToFavorite = addToFavorite,
@ -199,7 +219,8 @@ object ObjectSetMenuModule {
objectState = state,
dispatcher = dispatcher,
menuOptionsProvider = createMenuOptionsProvider(state, featureToggles),
addObjectToCollection = addObjectToCollection
addObjectToCollection = addObjectToCollection,
debugGoroutinesShareDownloader = debugGoroutinesShareDownloader
)
@JvmStatic
@ -222,6 +243,21 @@ object ObjectSetMenuModule {
dispatchers = dispatchers
)
@JvmStatic
@Provides
@PerDialog
fun debugGoRoutines(
repo: BlockRepository,
context: Context,
fileProvider: UriFileProvider,
dispatchers: AppCoroutineDispatchers
): DebugGoroutinesShareDownloader = DebugGoroutinesShareDownloader(
repo = repo,
context = context.applicationContext,
uriFileProvider = fileProvider,
dispatchers = dispatchers
)
@JvmStatic
private fun createMenuOptionsProvider(
state: StateFlow<ObjectState>,

View file

@ -94,7 +94,9 @@ import com.anytypeio.anytype.presentation.templates.ObjectTypeTemplatesContainer
import com.anytypeio.anytype.presentation.util.CopyFileToCacheDirectory
import com.anytypeio.anytype.presentation.util.DefaultCopyFileToCacheDirectory
import com.anytypeio.anytype.presentation.util.Dispatcher
import com.anytypeio.anytype.presentation.util.downloader.UriFileProvider
import com.anytypeio.anytype.providers.DefaultCoverImageHashProvider
import com.anytypeio.anytype.providers.DefaultUriFileProvider
import com.anytypeio.anytype.ui.sets.ObjectSetFragment
import dagger.Binds
import dagger.Module
@ -646,6 +648,12 @@ object ObjectSetModule {
fun bindCoverImageHashProvider(
defaultProvider: DefaultCoverImageHashProvider
): CoverImageHashProvider
@PerScreen
@Binds
fun bindUriFileProvider(
defaultProvider: DefaultUriFileProvider
): UriFileProvider
}
@JvmStatic

View file

@ -4,10 +4,12 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.os.bundleOf
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.anytypeio.anytype.R
import com.anytypeio.anytype.analytics.BuildConfig
import com.anytypeio.anytype.core_models.Id
import com.anytypeio.anytype.core_models.ObjectType
import com.anytypeio.anytype.core_ui.features.objects.ObjectActionAdapter
@ -18,16 +20,16 @@ import com.anytypeio.anytype.core_utils.ext.argOrNull
import com.anytypeio.anytype.core_utils.ext.shareFile
import com.anytypeio.anytype.core_utils.ext.throttleFirst
import com.anytypeio.anytype.core_utils.ext.toast
import com.anytypeio.anytype.core_utils.ext.visible
import com.anytypeio.anytype.core_utils.ui.BaseBottomSheetFragment
import com.anytypeio.anytype.core_utils.ui.proceed
import com.anytypeio.anytype.core_utils.ui.showActionableSnackBar
import com.anytypeio.anytype.core_utils.ui.showMessageSnackBar
import com.anytypeio.anytype.databinding.FragmentObjectMenuBinding
import com.anytypeio.anytype.presentation.navigation.AppNavigation
import com.anytypeio.anytype.presentation.objects.ObjectIcon
import com.anytypeio.anytype.presentation.objects.menu.ObjectMenuOptionsProvider
import com.anytypeio.anytype.presentation.objects.menu.ObjectMenuViewModelBase
import com.anytypeio.anytype.ui.base.navigation
import com.anytypeio.anytype.ui.editor.EditorModalFragment
import com.anytypeio.anytype.ui.editor.cover.SelectCoverObjectFragment
import com.anytypeio.anytype.ui.editor.cover.SelectCoverObjectSetFragment
import com.anytypeio.anytype.ui.editor.layout.ObjectLayoutFragment
@ -36,6 +38,7 @@ import com.anytypeio.anytype.ui.linking.BacklinkAction
import com.anytypeio.anytype.ui.linking.BacklinkOrAddToObjectFragment
import com.anytypeio.anytype.ui.moving.OnMoveToAction
import com.anytypeio.anytype.ui.relations.ObjectRelationListFragment
import com.google.android.material.snackbar.Snackbar
abstract class ObjectMenuBaseFragment :
BaseBottomSheetFragment<FragmentObjectMenuBinding>(),
@ -79,6 +82,7 @@ abstract class ObjectMenuBaseFragment :
click(binding.optionIcon) { vm.onIconClicked(ctx) }
click(binding.optionRelations) { vm.onRelationsClicked() }
click(binding.optionCover) { vm.onCoverClicked(ctx) }
click(binding.debugGoroutines) { vm.onDiagnosticsGoroutinesClicked(ctx) }
proceed(vm.actions) { actionAdapter.submitList(it) }
proceed(vm.toasts) { toast(it) }
@ -86,6 +90,11 @@ abstract class ObjectMenuBaseFragment :
proceed(vm.commands.throttleFirst()) { command -> execute(command) }
proceed(vm.options) { options -> renderOptions(options) }
if (BuildConfig.DEBUG) {
binding.debugGoroutines.visible()
binding.debugGoroutinesDivider.visible()
}
super.onStart()
vm.onStart(
ctx = ctx,
@ -134,6 +143,18 @@ abstract class ObjectMenuBaseFragment :
is ObjectMenuViewModelBase.Command.OpenSnackbar -> openSnackbar(command)
is ObjectMenuViewModelBase.Command.ShareDebugTree -> shareFile(command.uri)
is ObjectMenuViewModelBase.Command.OpenTemplate -> openTemplate(command)
is ObjectMenuViewModelBase.Command.ShareDebugGoroutines -> {
val snackbar = Snackbar.make(
dialog?.window?.decorView!!,
"Success, Path: ${command.path}",
Snackbar.LENGTH_INDEFINITE
)
snackbar.setAction("Done") {
snackbar.dismiss()
}
snackbar.anchorView = binding.anchor
snackbar.show()
}
}
}

View file

@ -2,7 +2,8 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">
<View
android:id="@+id/dragger"
@ -160,6 +161,32 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/objectDiagnostics" />
<com.anytypeio.anytype.core_ui.widgets.ObjectMenuItemWidget
android:id="@+id/debugGoroutines"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/default_ripple"
android:visibility="gone"
app:icon="@drawable/ic_object_menu_debug_goroutines"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/objectDiagnosticsDivider"
app:subtitle="Command Debug.StackGoroutines"
app:title="Debug Goroutines"
tools:visibility="visible" />
<View
android:id="@+id/debugGoroutinesDivider"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:layout_marginTop="21dp"
android:visibility="gone"
android:background="@color/shape_primary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/debugGoroutines" />
<FrameLayout
android:id="@+id/rvContainer"
android:layout_width="match_parent"
@ -167,7 +194,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/objectDiagnosticsDivider">
app:layout_constraintTop_toBottomOf="@+id/debugGoroutinesDivider">
<androidx.recyclerview.widget.RecyclerView
android:layout_gravity="center_vertical"

View file

@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="44dp"
android:height="44dp"
android:viewportWidth="44"
android:viewportHeight="44">
<path
android:pathData="M22,0L22,0A22,22 0,0 1,44 22L44,22A22,22 0,0 1,22 44L22,44A22,22 0,0 1,0 22L0,22A22,22 0,0 1,22 0z"
android:strokeAlpha="0.2"
android:fillColor="@color/palette_dark_red"
android:fillAlpha="0.2"/>
<path
android:pathData="M24.76,34C24.517,34 24.306,33.823 24.247,33.569L19.37,12.889L17.189,22.14C17.129,22.394 16.917,22.571 16.675,22.571L10.53,22.571C10.237,22.571 10,22.315 10,22C10,21.684 10.237,21.428 10.53,21.428H16.263L18.857,10.431C18.917,10.177 19.128,10 19.37,10C19.612,10 19.824,10.177 19.883,10.431L24.76,31.111L26.941,21.86C27.001,21.606 27.212,21.429 27.454,21.429H33.47C33.763,21.429 34,21.685 34,22C34,22.316 33.763,22.572 33.47,22.572L27.866,22.572L25.273,33.569C25.213,33.823 25.002,34 24.76,34H24.76Z"
android:strokeWidth="0.6"
android:fillColor="@color/palette_system_red"
android:strokeColor="@color/palette_system_red"/>
</vector>

View file

@ -454,8 +454,8 @@ class Orchestrator(
name = intent.name
)
).suspendFold(
onSuccess = { uri ->
intent.onDownloaded(uri)
onSuccess = { result ->
intent.onDownloaded(result.uri)
analytics.sendAnalyticsDownloadMediaEvent(intent.type)
},
onFailure = { e -> Timber.e(e, "Error while sharing a file") }

View file

@ -29,10 +29,9 @@ import com.anytypeio.anytype.presentation.editor.Editor
import com.anytypeio.anytype.presentation.extension.sendAnalyticsCreateTemplateEvent
import com.anytypeio.anytype.presentation.extension.sendAnalyticsDefaultTemplateEvent
import com.anytypeio.anytype.presentation.objects.ObjectAction
import com.anytypeio.anytype.presentation.objects.ObjectIcon
import com.anytypeio.anytype.presentation.objects.getProperName
import com.anytypeio.anytype.presentation.objects.isTemplatesAllowed
import com.anytypeio.anytype.presentation.util.Dispatcher
import com.anytypeio.anytype.presentation.util.downloader.DebugGoroutinesShareDownloader
import com.anytypeio.anytype.presentation.util.downloader.DebugTreeShareDownloader
import com.anytypeio.anytype.presentation.util.downloader.MiddlewareShareDownloader
import kotlinx.coroutines.launch
@ -54,7 +53,8 @@ class ObjectMenuViewModel(
private val updateFields: UpdateFields,
private val addObjectToCollection: AddObjectToCollection,
private val createTemplateFromObject: CreateTemplateFromObject,
private val setObjectDetails: SetObjectDetails
private val setObjectDetails: SetObjectDetails,
private val debugGoroutinesShareDownloader: DebugGoroutinesShareDownloader
) : ObjectMenuViewModelBase(
setObjectIsArchived = setObjectIsArchived,
addToFavorite = addToFavorite,
@ -66,7 +66,8 @@ class ObjectMenuViewModel(
dispatcher = dispatcher,
analytics = analytics,
menuOptionsProvider = menuOptionsProvider,
addObjectToCollection = addObjectToCollection
addObjectToCollection = addObjectToCollection,
debugGoroutinesShareDownloader = debugGoroutinesShareDownloader
) {
private val objectRestrictions = storage.objectRestrictions.current()
@ -135,8 +136,8 @@ class ObjectMenuViewModel(
MiddlewareShareDownloader.Params(hash = ctx, name = "$ctx.zip")
).collect { result ->
result.fold(
onSuccess = { uri ->
commands.emit(Command.ShareDebugTree(uri))
onSuccess = { success ->
commands.emit(Command.ShareDebugTree(success.uri))
},
onLoading = {
sendToast(
@ -410,7 +411,8 @@ class ObjectMenuViewModel(
private val menuOptionsProvider: ObjectMenuOptionsProvider,
private val addObjectToCollection: AddObjectToCollection,
private val createTemplateFromObject: CreateTemplateFromObject,
private val setObjectDetails: SetObjectDetails
private val setObjectDetails: SetObjectDetails,
private val debugGoroutinesShareDownloader: DebugGoroutinesShareDownloader
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return ObjectMenuViewModel(
@ -429,7 +431,8 @@ class ObjectMenuViewModel(
menuOptionsProvider = menuOptionsProvider,
addObjectToCollection = addObjectToCollection,
createTemplateFromObject = createTemplateFromObject,
setObjectDetails = setObjectDetails
setObjectDetails = setObjectDetails,
debugGoroutinesShareDownloader = debugGoroutinesShareDownloader
) as T
}
}

View file

@ -29,6 +29,8 @@ import com.anytypeio.anytype.presentation.objects.ObjectAction
import com.anytypeio.anytype.presentation.objects.ObjectIcon
import com.anytypeio.anytype.presentation.objects.getProperName
import com.anytypeio.anytype.presentation.util.Dispatcher
import com.anytypeio.anytype.presentation.util.downloader.DebugGoroutinesShareDownloader
import com.anytypeio.anytype.presentation.util.downloader.MiddlewareShareDownloader
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
@ -47,7 +49,8 @@ abstract class ObjectMenuViewModelBase(
private val analytics: Analytics,
private val menuOptionsProvider: ObjectMenuOptionsProvider,
private val duplicateObject: DuplicateObject,
private val addObjectToCollection: AddObjectToCollection
private val addObjectToCollection: AddObjectToCollection,
private val debugGoroutinesShareDownloader: DebugGoroutinesShareDownloader
) : BaseViewModel() {
protected val jobs = mutableListOf<Job>()
@ -329,6 +332,22 @@ abstract class ObjectMenuViewModelBase(
}
}
fun onDiagnosticsGoroutinesClicked(ctx: Id) {
jobs += viewModelScope.launch {
debugGoroutinesShareDownloader.stream(
MiddlewareShareDownloader.Params(hash = ctx, name = "goroutines")
).collect { result ->
result.fold(
onSuccess = { success -> commands.emit(Command.ShareDebugGoroutines(success.path)) },
onFailure = {
sendToast("Error while collecting goroutines diagnostics :${it.message}")
Timber.e(it, "Error while collecting goroutines diagnostics")
}
)
}
}
}
sealed class Command {
object OpenObjectIcons : Command()
object OpenSetIcons : Command()
@ -340,6 +359,7 @@ abstract class ObjectMenuViewModelBase(
object OpenSetRelations : Command()
object OpenLinkToChooser : Command()
data class ShareDebugTree(val uri: Uri) : Command()
data class ShareDebugGoroutines(val path: String) : Command()
data class OpenSnackbar(
val id: Id,
val currentObjectName: String?,

View file

@ -20,6 +20,7 @@ import com.anytypeio.anytype.presentation.objects.ObjectAction
import com.anytypeio.anytype.presentation.sets.dataViewState
import com.anytypeio.anytype.presentation.sets.state.ObjectState
import com.anytypeio.anytype.presentation.util.Dispatcher
import com.anytypeio.anytype.presentation.util.downloader.DebugGoroutinesShareDownloader
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
@ -35,7 +36,8 @@ class ObjectSetMenuViewModel(
menuOptionsProvider: ObjectMenuOptionsProvider,
private val objectState: StateFlow<ObjectState>,
private val analytics: Analytics,
private val addObjectToCollection: AddObjectToCollection
private val addObjectToCollection: AddObjectToCollection,
private val debugGoroutinesShareDownloader: DebugGoroutinesShareDownloader
) : ObjectMenuViewModelBase(
setObjectIsArchived = setObjectIsArchived,
addToFavorite = addToFavorite,
@ -47,7 +49,8 @@ class ObjectSetMenuViewModel(
dispatcher = dispatcher,
analytics = analytics,
menuOptionsProvider = menuOptionsProvider,
addObjectToCollection = addObjectToCollection
addObjectToCollection = addObjectToCollection,
debugGoroutinesShareDownloader = debugGoroutinesShareDownloader
) {
@Suppress("UNCHECKED_CAST")
@ -63,7 +66,8 @@ class ObjectSetMenuViewModel(
private val analytics: Analytics,
private val objectState: StateFlow<ObjectState>,
private val menuOptionsProvider: ObjectMenuOptionsProvider,
private val addObjectToCollection: AddObjectToCollection
private val addObjectToCollection: AddObjectToCollection,
private val debugGoroutinesShareDownloader: DebugGoroutinesShareDownloader
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return ObjectSetMenuViewModel(
@ -78,7 +82,8 @@ class ObjectSetMenuViewModel(
objectState = objectState,
dispatcher = dispatcher,
menuOptionsProvider = menuOptionsProvider,
addObjectToCollection = addObjectToCollection
addObjectToCollection = addObjectToCollection,
debugGoroutinesShareDownloader = debugGoroutinesShareDownloader
) as T
}
}

View file

@ -0,0 +1,18 @@
package com.anytypeio.anytype.presentation.util.downloader
import android.content.Context
import com.anytypeio.anytype.domain.base.AppCoroutineDispatchers
import com.anytypeio.anytype.domain.block.repo.BlockRepository
class DebugGoroutinesShareDownloader(
private val repo: BlockRepository,
context: Context,
uriFileProvider: UriFileProvider,
dispatchers: AppCoroutineDispatchers
) : MiddlewareShareDownloader(context, uriFileProvider, dispatchers) {
override suspend fun downloadFile(hash: String, path: String): String {
repo.debugStackGoroutines(path = path)
return path
}
}

View file

@ -15,13 +15,18 @@ abstract class MiddlewareShareDownloader(
private val context: Context,
private val uriFileProvider: UriFileProvider,
dispatchers: AppCoroutineDispatchers
) : ResultInteractor<MiddlewareShareDownloader.Params, Uri>(dispatchers.io) {
) : ResultInteractor<MiddlewareShareDownloader.Params, MiddlewareShareDownloader.Response>(dispatchers.io) {
data class Params(
val hash: Hash,
val name: String
)
data class Response(
val uri: Uri,
val path: String
)
/**
* @param hash is a some middleware id
* @param path is local storage path to the file created
@ -29,7 +34,7 @@ abstract class MiddlewareShareDownloader(
* */
abstract suspend fun downloadFile(hash: String, path: String): String
override suspend fun doWork(params: Params): Uri {
override suspend fun doWork(params: Params): Response {
val cacheDir = context.cacheDir
require(cacheDir != null) { "Impossible to cache files!" }
@ -49,7 +54,10 @@ abstract class MiddlewareShareDownloader(
tempResult.renameTo(resultFile)
}
return uriFileProvider.getUriForFile(resultFile)
return Response(
uri = uriFileProvider.getUriForFile(resultFile),
path = resultFilePath
)
}
}