Swiftgram / TDLibKit

Native Swift wrapper for Telegram's TDLib. Available on iOS, macOS, watchOS, tvOS and visionOS.
MIT License
106 stars 23 forks source link

Need for mutable properties on structs #22

Closed ibengeu closed 1 year ago

ibengeu commented 1 year ago

Hello, I am making use of an updatehandler to handle updates such as updateLastMessage and unable to update my list with the chat update due to "let" variable.

Here is an example of what I would like to do

 for chatPosition in chat.positions {

      if(chatPosition.list == ChatList.chatListMain){
        mainChatList.remove(OrderedChat(chatId: chat.id, position: chatPosition))
      }

    }

     chat.positions = positions // throws "Cannot assign to property: 'positions' is a 'let' constant"

    for chatPosition in chat.positions {

      if(chatPosition.list == ChatList.chatListMain){

        mainChatList.insert(OrderedChat(chatId: chat.id, position: chatPosition))
//        assert isRemoved;
      }

Is there a better way to resolve or do we make a pull request?

ibengeu commented 1 year ago

Do take a look @Kylmakalle

Kylmakalle commented 1 year ago

You're trying to change objects which were returned by the server. I don't know why you need it (since you can't return the same abject back to server to make changes) Instead, make a copy of object you need and do any related changes.

var customChatPositions: [ChatPosition] = chat.positions

for chatPosition in customChatPositions {
    /* ... */
}
ibengeu commented 1 year ago

First of thanks for you time, and apologize for any noob questions or cases.

You're trying to change objects which were returned by the server

Not exactly, the response is stored in a dictionary(cache) of which i retrieve and potentially want to update

Instead, make a copy of object you need and do any related changes.

Agreed, worked with a similar structure but faced with the same issues of let constants. see an example below

case .updateChatTitle(let updateChat):
              var chat = ChatHandler.getChat(chatId: updateChat.chatId)
              chat?.title = updateChat.title // throws error here
              break
              // other cases with update
Kylmakalle commented 1 year ago

You're trying to change objects which were returned by the server

Not exactly

potentially want to update

So you are

Kylmakalle commented 1 year ago

response is stored in a dictionary(cache)

TDLib has its own cache, so whenever you will request a new chat object, it should return you a cached one. If you want to make sure that all the fields are up to date, use methods like openChat to enforce the update mechanism.

If you're still not satisfied with this, you can create an extension or child class for objects you need to make changes and add additional fields through them. Structs are not final.

ibengeu commented 1 year ago

Alright, although not clear - I would take a step back to think of the suggestions plus reason about the parser.

levochkaa commented 1 year ago

Because of immutable properties in struct's I made Custom<original name> structs, where I had to update some data, like chat positions and ended with this for the moment:

struct CustomChat: Identifiable, Equatable {
    let id = UUID()
    var chat: Chat
    var user: User
    var positions: [ChatPosition]
    var lastMessage: Message?
    var draftMessage: DraftMessage?
}

Just create some func, that will create this struct, using TDLibKit methods

Hope that helps

ibengeu commented 1 year ago

Thanks, made use of the repo directly but your method seems less cumbersome or heavy.