steve228uk / MessengerKit

:speech_balloon: A UI framework for building messenger interfaces on iOS
MIT License
1.48k stars 128 forks source link

Add rawValue property in MSGMessageBody to access the raw string value #55

Closed rogermolas closed 4 years ago

rogermolas commented 4 years ago

Issue: Problem working with Firebase adding a message as MSGMessage object

1. The first step is to identify the string type before adding it to MSGMessage object.

let body: MSGMessageBody = (inputView.message.containsOnlyEmoji &&
        inputView.message.count < 5) ? .emoji(inputView.message) : .text(inputView.message)
let message = MSGMessage(id: id, body: body, user: sender!, sentAt: Date())

2. Insert the message object

insert(message) 

3. Write the message in Firebase <- Not supported MSGMessageBody object is not supported type: https://firebase.google.com/docs/firestore/manage-data/data-types

The solution is to take the raw string from MSGMessageBody

public enum MSGMessageBody {
    case text(String)
    case emoji(String)
    case image(UIImage)
    case imageFromUrl(URL)
    case video(UIImage, String)
    case custom(Any)

    var rawValue: Any { //<- add property to get the raw value
         switch self {
             case .custom(let value): return value
         case .text(let value): return value
         case .emoji(let value): return value
         case .image(let value): return value
         case .imageFromUrl(let value): return value
         case .video(_, let video): return video
        }
    }
}

or in my current case i just added an extension

extension MSGMessageBody {
    var rawValue: Any {
        switch self {
            case .custom(let value): return value
        case .text(let value): return value
        case .emoji(let value): return value
        case .image(let value): return value
        case .imageFromUrl(let value): return value
        case .video(_, let video): return video
    }
    }
}

//
let rawString  = message.body.rawValue
print(rawString)
sjoness commented 4 years ago

@rogermolas feel free to submit a PR for this change! 😄

rogermolas commented 4 years ago

@sjoness Done #60