mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-08 05:47:05 +09:00
DROID-849 Relations | Fix | Read-only-value Relations should not be editable (#2835)
This commit is contained in:
parent
5c5b21221f
commit
6d11d1ab1e
3 changed files with 103 additions and 114 deletions
|
@ -510,96 +510,92 @@ class ObjectSetViewModel(
|
|||
|
||||
fun onGridCellClicked(cell: CellView) {
|
||||
Timber.d("onGridCellClicked, cell:[$cell]")
|
||||
|
||||
if (cell.relationKey == Relations.NAME) return
|
||||
viewModelScope.launch {
|
||||
val state = reducer.state.value
|
||||
|
||||
val state = reducer.state.value
|
||||
|
||||
if (!state.isInitialized) {
|
||||
Timber.e("State was not initialized or cleared when cell is clicked")
|
||||
return
|
||||
}
|
||||
|
||||
val block = state.dataview
|
||||
val dv = block.content as DV
|
||||
val viewer =
|
||||
dv.viewers.find { it.id == session.currentViewerId.value }?.id ?: dv.viewers.first().id
|
||||
|
||||
if (dv.isRelationReadOnly(relationKey = cell.relationKey)) {
|
||||
val relation = dv.relations.first { it.key == cell.relationKey }
|
||||
if (relation.format == Relation.Format.OBJECT) {
|
||||
// TODO terrible workaround, which must be removed in the future!
|
||||
if (cell is CellView.Object && cell.objects.isNotEmpty()) {
|
||||
val obj = cell.objects.first()
|
||||
onRelationObjectClicked(target = obj.id)
|
||||
return
|
||||
} else {
|
||||
toast(NOT_ALLOWED_CELL)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
Timber.d("onGridCellClicked, relation is ReadOnly")
|
||||
toast(NOT_ALLOWED_CELL)
|
||||
return
|
||||
if (!state.isInitialized) {
|
||||
Timber.e("State was not initialized or cleared when cell is clicked")
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
|
||||
when (cell) {
|
||||
is CellView.Description,
|
||||
is CellView.Number,
|
||||
is CellView.Email,
|
||||
is CellView.Url,
|
||||
is CellView.Phone -> {
|
||||
dispatch(
|
||||
ObjectSetCommand.Modal.EditGridTextCell(
|
||||
ctx = context,
|
||||
relationKey = cell.relationKey,
|
||||
recordId = cell.id
|
||||
)
|
||||
)
|
||||
val block = state.dataview
|
||||
val viewer = state.viewerById(session.currentViewerId.value)
|
||||
val relation = storeOfRelations.getByKey(cell.relationKey)
|
||||
|
||||
if (relation == null) {
|
||||
toast("Could not found this relation. Please, try again later.")
|
||||
return@launch
|
||||
}
|
||||
is CellView.Date -> {
|
||||
dispatch(
|
||||
ObjectSetCommand.Modal.EditGridDateCell(
|
||||
ctx = context,
|
||||
objectId = cell.id,
|
||||
relationKey = cell.relationKey
|
||||
)
|
||||
)
|
||||
}
|
||||
is CellView.Tag, is CellView.Status, is CellView.File -> {
|
||||
dispatch(
|
||||
ObjectSetCommand.Modal.EditRelationCell(
|
||||
ctx = context,
|
||||
target = cell.id,
|
||||
dataview = block.id,
|
||||
relationKey = cell.relationKey,
|
||||
viewer = viewer,
|
||||
targetObjectTypes = emptyList()
|
||||
)
|
||||
)
|
||||
}
|
||||
is CellView.Object -> {
|
||||
viewModelScope.launch {
|
||||
val targetObjectTypes = mutableListOf<String>()
|
||||
val relation = storeOfRelations.getByKey(cell.relationKey)
|
||||
if (relation != null) {
|
||||
targetObjectTypes.addAll(relation.relationFormatObjectTypes)
|
||||
|
||||
if (relation.isReadonlyValue) {
|
||||
if (relation.format == Relation.Format.OBJECT) {
|
||||
// TODO terrible workaround, which must be removed in the future!
|
||||
if (cell is CellView.Object && cell.objects.isNotEmpty()) {
|
||||
val obj = cell.objects.first()
|
||||
onRelationObjectClicked(target = obj.id)
|
||||
return@launch
|
||||
} else {
|
||||
toast(NOT_ALLOWED_CELL)
|
||||
return@launch
|
||||
}
|
||||
} else {
|
||||
Timber.d("onGridCellClicked, relation is ReadOnly")
|
||||
toast(NOT_ALLOWED_CELL)
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
|
||||
when (cell) {
|
||||
is CellView.Description,
|
||||
is CellView.Number,
|
||||
is CellView.Email,
|
||||
is CellView.Url,
|
||||
is CellView.Phone -> {
|
||||
dispatch(
|
||||
ObjectSetCommand.Modal.EditGridTextCell(
|
||||
ctx = context,
|
||||
relationKey = cell.relationKey,
|
||||
recordId = cell.id
|
||||
)
|
||||
)
|
||||
}
|
||||
is CellView.Date -> {
|
||||
dispatch(
|
||||
ObjectSetCommand.Modal.EditGridDateCell(
|
||||
ctx = context,
|
||||
objectId = cell.id,
|
||||
relationKey = cell.relationKey
|
||||
)
|
||||
)
|
||||
}
|
||||
is CellView.Tag, is CellView.Status, is CellView.File -> {
|
||||
dispatch(
|
||||
ObjectSetCommand.Modal.EditRelationCell(
|
||||
ctx = context,
|
||||
target = cell.id,
|
||||
dataview = block.id,
|
||||
relationKey = cell.relationKey,
|
||||
viewer = viewer,
|
||||
viewer = viewer.id,
|
||||
targetObjectTypes = emptyList()
|
||||
)
|
||||
)
|
||||
}
|
||||
is CellView.Object -> {
|
||||
val targetObjectTypes = mutableListOf<String>()
|
||||
targetObjectTypes.addAll(relation.relationFormatObjectTypes)
|
||||
dispatch(
|
||||
ObjectSetCommand.Modal.EditRelationCell(
|
||||
ctx = context,
|
||||
target = cell.id,
|
||||
dataview = block.id,
|
||||
relationKey = cell.relationKey,
|
||||
viewer = viewer.id,
|
||||
targetObjectTypes = targetObjectTypes
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
is CellView.Checkbox -> {
|
||||
viewModelScope.launch {
|
||||
is CellView.Checkbox -> {
|
||||
setObjectDetails(
|
||||
UpdateDetail.Params(
|
||||
ctx = cell.id,
|
||||
|
|
|
@ -8,11 +8,18 @@ import com.anytypeio.anytype.core_models.DVViewer
|
|||
import com.anytypeio.anytype.core_models.DVViewerRelation
|
||||
import com.anytypeio.anytype.core_models.ObjectWrapper
|
||||
import com.anytypeio.anytype.core_models.Relation
|
||||
import com.anytypeio.anytype.core_models.RelationLink
|
||||
import com.anytypeio.anytype.core_models.Relations
|
||||
import com.anytypeio.anytype.core_models.SearchResult
|
||||
import com.anytypeio.anytype.core_models.StubHeader
|
||||
import com.anytypeio.anytype.core_models.StubRelation
|
||||
import com.anytypeio.anytype.core_models.StubRelationObject
|
||||
import com.anytypeio.anytype.core_models.StubTitle
|
||||
import com.anytypeio.anytype.core_models.ext.content
|
||||
import com.anytypeio.anytype.core_models.restrictions.DataViewRestriction
|
||||
import com.anytypeio.anytype.core_models.restrictions.DataViewRestrictions
|
||||
import com.anytypeio.anytype.presentation.relations.ObjectSetConfig
|
||||
import com.anytypeio.anytype.presentation.search.ObjectSearchConstants.defaultDataViewKeys
|
||||
import com.anytypeio.anytype.presentation.sets.ObjectSetViewModel
|
||||
import com.anytypeio.anytype.presentation.sets.model.CellView
|
||||
import com.anytypeio.anytype.presentation.util.CoroutinesTestRule
|
||||
|
@ -43,48 +50,19 @@ class ObjectSetCellTest : ObjectSetViewModelTestSetup() {
|
|||
.onlyLogWhenTestFails(true)
|
||||
.build()
|
||||
|
||||
private val title = Block(
|
||||
id = MockDataFactory.randomUuid(),
|
||||
content = Block.Content.Text(
|
||||
style = Block.Content.Text.Style.TITLE,
|
||||
text = MockDataFactory.randomString(),
|
||||
marks = emptyList()
|
||||
),
|
||||
children = emptyList(),
|
||||
fields = Block.Fields.empty()
|
||||
)
|
||||
|
||||
private val header = Block(
|
||||
id = MockDataFactory.randomUuid(),
|
||||
content = Block.Content.Layout(
|
||||
type = Block.Content.Layout.Type.HEADER
|
||||
),
|
||||
fields = Block.Fields.empty(),
|
||||
children = listOf(title.id)
|
||||
)
|
||||
private val title = StubTitle()
|
||||
private val header = StubHeader(children = listOf(title.id))
|
||||
|
||||
private val relations = listOf(
|
||||
Relation(
|
||||
key = MockDataFactory.randomString(),
|
||||
name = MockDataFactory.randomString(),
|
||||
source = Relation.Source.DETAILS,
|
||||
defaultValue = null,
|
||||
StubRelationObject(
|
||||
key = MockDataFactory.randomUuid(),
|
||||
format = Relation.Format.LONG_TEXT,
|
||||
isHidden = false,
|
||||
isMulti = false,
|
||||
isReadOnly = false,
|
||||
selections = emptyList()
|
||||
isReadOnlyValue = false
|
||||
),
|
||||
Relation(
|
||||
key = MockDataFactory.randomString(),
|
||||
name = MockDataFactory.randomString(),
|
||||
source = Relation.Source.DETAILS,
|
||||
defaultValue = null,
|
||||
StubRelationObject(
|
||||
key = MockDataFactory.randomUuid(),
|
||||
format = Relation.Format.LONG_TEXT,
|
||||
isHidden = false,
|
||||
isMulti = false,
|
||||
isReadOnly = true,
|
||||
selections = emptyList()
|
||||
isReadOnlyValue = true
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -151,8 +129,14 @@ class ObjectSetCellTest : ObjectSetViewModelTestSetup() {
|
|||
id = MockDataFactory.randomUuid(),
|
||||
content = DV(
|
||||
sources = listOf(MockDataFactory.randomString()),
|
||||
relations = relations,
|
||||
viewers = listOf(viewer1, viewer2)
|
||||
relationsIndex = relations.map {
|
||||
RelationLink(
|
||||
key = it.key,
|
||||
format = it.format
|
||||
)
|
||||
},
|
||||
viewers = listOf(viewer1, viewer2),
|
||||
relations = emptyList()
|
||||
),
|
||||
children = emptyList(),
|
||||
fields = Block.Fields.empty()
|
||||
|
@ -188,7 +172,7 @@ class ObjectSetCellTest : ObjectSetViewModelTestSetup() {
|
|||
afterId = null,
|
||||
beforeId = null,
|
||||
sources = dv.content<DV>().sources,
|
||||
keys = dv.content<DV>().relations.map { it.key },
|
||||
keys = dv.content<DV>().relationsIndex.map { it.key } + defaultDataViewKeys,
|
||||
limit = ObjectSetConfig.DEFAULT_LIMIT,
|
||||
offset = 0,
|
||||
result = SearchResult(
|
||||
|
@ -214,6 +198,13 @@ class ObjectSetCellTest : ObjectSetViewModelTestSetup() {
|
|||
dataViewRestrictions = dvRestrictions
|
||||
)
|
||||
|
||||
relations.forEach {
|
||||
storeOfRelations.set(
|
||||
target = it.id,
|
||||
data = it.map
|
||||
)
|
||||
}
|
||||
|
||||
val vm = givenViewModel()
|
||||
|
||||
// TESTING
|
||||
|
|
|
@ -9,6 +9,7 @@ fun StubRelationObject(
|
|||
format: Relation.Format = Relation.Format.SHORT_TEXT,
|
||||
isHidden: Boolean = false,
|
||||
isReadOnly: Boolean = false,
|
||||
isReadOnlyValue: Boolean = false,
|
||||
objectTypes: List<Id> = emptyList(),
|
||||
relationOptionsDict: List<Id> = emptyList(),
|
||||
sourceObject: Id = MockDataFactory.randomUuid(),
|
||||
|
@ -20,6 +21,7 @@ fun StubRelationObject(
|
|||
Relations.NAME to name,
|
||||
Relations.IS_HIDDEN to isHidden,
|
||||
Relations.IS_READ_ONLY to isReadOnly,
|
||||
Relations.RELATION_READ_ONLY_VALUE to isReadOnlyValue,
|
||||
Relations.RELATION_FORMAT_OBJECT_TYPES to objectTypes,
|
||||
Relations.RELATION_FORMAT to format.code.toDouble(),
|
||||
Relations.RELATION_OPTION_DICT to relationOptionsDict,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue