@@ -23,6 +23,7 @@ import com.anytypeio.anytype.core_models.ext.EMPTY_STRING_VALUE
2323import com.anytypeio.anytype.core_models.multiplayer.SpaceAccessType
2424import com.anytypeio.anytype.core_models.multiplayer.SpaceInviteLinkAccessLevel
2525import com.anytypeio.anytype.core_models.multiplayer.SpaceMemberPermissions
26+ import com.anytypeio.anytype.core_models.multiplayer.SpaceUxType
2627import com.anytypeio.anytype.core_models.primitives.Space
2728import com.anytypeio.anytype.core_models.primitives.SpaceId
2829import com.anytypeio.anytype.core_models.syncStatus
@@ -51,6 +52,7 @@ import com.anytypeio.anytype.domain.multiplayer.SpaceViewSubscriptionContainer
5152import com.anytypeio.anytype.domain.multiplayer.UserPermissionProvider
5253import com.anytypeio.anytype.domain.notifications.NotificationBuilder
5354import com.anytypeio.anytype.domain.`object`.GetObject
55+ import com.anytypeio.anytype.domain.`object`.SetObjectDetails
5456import com.anytypeio.anytype.domain.objects.CreateObjectFromUrl
5557import com.anytypeio.anytype.domain.objects.ObjectWatcher
5658import com.anytypeio.anytype.domain.objects.SetObjectListIsArchived
@@ -129,7 +131,8 @@ class ChatViewModel @Inject constructor(
129131 private val spaceInviteLinkStore : SpaceInviteLinkStore ,
130132 private val getCurrentInviteAccessLevel : GetCurrentInviteAccessLevel ,
131133 private val pinObjectAsWidgetDelegate : PinObjectAsWidgetDelegate ,
132- private val setObjectListIsArchived : SetObjectListIsArchived
134+ private val setObjectListIsArchived : SetObjectListIsArchived ,
135+ private val setObjectDetails : SetObjectDetails
133136) : BaseViewModel(),
134137 ExitToVaultDelegate by exitToVaultDelegate,
135138 PinObjectAsWidgetDelegate by pinObjectAsWidgetDelegate {
@@ -197,15 +200,40 @@ class ChatViewModel @Inject constructor(
197200 spaceViews
198201 .observe(
199202 vmParams.space
200- ).map { view ->
203+ ).collect { view ->
201204 val isMuted = NotificationStateCalculator .calculateMutedState(view)
202- HeaderView .Default (
203- title = view.name.orEmpty(),
204- icon = view.spaceIcon(builder = urlBuilder),
205- isMuted = isMuted
206- )
207- }.collect {
208- header.value = it
205+
206+ if (view.spaceUxType == SpaceUxType .CHAT ) {
207+ // For chat spaces, use space name and icon
208+ header.value = HeaderView .Default (
209+ title = view.name.orEmpty(),
210+ icon = view.spaceIcon(builder = urlBuilder),
211+ isMuted = isMuted
212+ )
213+ } else {
214+ // For non-chat spaces, fetch the object name
215+ getObject.async(
216+ GetObject .Params (
217+ target = vmParams.ctx,
218+ space = vmParams.space
219+ )
220+ ).onSuccess { objectView ->
221+ val wrapper = ObjectWrapper .Basic (objectView.details[vmParams.ctx].orEmpty())
222+ header.value = HeaderView .Default (
223+ title = wrapper.name.orEmpty(),
224+ icon = view.spaceIcon(builder = urlBuilder),
225+ isMuted = isMuted
226+ )
227+ }.onFailure {
228+ Timber .e(it, " Failed to fetch object for chat header" )
229+ // Fallback to space name
230+ header.value = HeaderView .Default (
231+ title = view.name.orEmpty(),
232+ icon = view.spaceIcon(builder = urlBuilder),
233+ isMuted = isMuted
234+ )
235+ }
236+ }
209237 }
210238 }
211239
@@ -1559,6 +1587,43 @@ class ChatViewModel @Inject constructor(
15591587 // TODO: Implement copy link functionality
15601588 }
15611589
1590+ fun onChatInfoSaved (newName : String ) {
1591+ viewModelScope.launch {
1592+ val spaceView = spaceViews.get(vmParams.space)
1593+ if (spaceView == null ) {
1594+ Timber .e(" Space view not available" )
1595+ sendToast(" Failed to update chat name" )
1596+ return @launch
1597+ }
1598+
1599+ val targetId = if (spaceView.spaceUxType == SpaceUxType .CHAT ) {
1600+ // For chat spaces, update the space view object
1601+ spaceView.id
1602+ } else {
1603+ // For non-chat spaces, update the chat object
1604+ vmParams.ctx
1605+ }
1606+
1607+ setObjectDetails.async(
1608+ SetObjectDetails .Params (
1609+ ctx = targetId,
1610+ details = mapOf (Relations .NAME to newName)
1611+ )
1612+ ).onSuccess {
1613+ Timber .d(" Successfully updated chat name to: $newName " )
1614+ // Update local state
1615+ val currentHeader = header.value
1616+ if (currentHeader is HeaderView .Default ) {
1617+ header.value = currentHeader.copy(title = newName)
1618+ }
1619+ sendToast(" Chat name updated" )
1620+ }.onFailure { e ->
1621+ Timber .e(e, " Error while updating chat name" )
1622+ sendToast(" Failed to update chat name" )
1623+ }
1624+ }
1625+ }
1626+
15621627 fun onMoveToBin () {
15631628 showMoveToBinDialog.value = true
15641629 }
0 commit comments