Skip to content

Instantly share code, notes, and snippets.

@halavins
Last active October 12, 2019 04:35
Show Gist options
  • Save halavins/c2bd16137c48ad6be5521186d0f5edf7 to your computer and use it in GitHub Desktop.
Save halavins/c2bd16137c48ad6be5521186d0f5edf7 to your computer and use it in GitHub Desktop.
ChatController in old Swift as the bridge with SwiftUI Views
//
// ChatController.swift
// SwiftUI Chat
//
// Created by Nick Halavins on 6/7/19. Updated 10/11/19
// Copyright © 2019 AntiLand. All rights reserved.
//
import Combine
import SwiftUI
// ChatController needs to be a ObservableObject in order
// to be accessible by SwiftUI
class ChatController : ObservableObject {
// didChange will let the SwiftUI know that some changes have happened in this object
// and we need to rebuild all the views related to that object
var didChange = PassthroughSubject<Void, Never>()
// We've relocated the messages from the main SwiftUI View. Now, if you wish, you can handle the networking part here and populate this array with any data from your database. If you do so, please share your code and let's build the first global open-source chat app in SwiftUI together
// It has to be @Published in order for the new updated values to be accessible from the ContentView Controller
@Published var messages = [
ChatMessage(message: "Hello world", avatar: "A", color: .red),
ChatMessage(message: "Hi", avatar: "B", color: .blue)
]
// this function will be accessible from SwiftUI main view
// here you can add the necessary code to send your messages not only to the SwiftUI view, but also to the database so that other users of the app would be able to see it
func sendMessage(_ chatMessage: ChatMessage) {
// here we populate the messages array
messages.append(chatMessage)
// here we let the SwiftUI know that we need to rebuild the views
didChange.send(())
}
}
@halavins
Copy link
Author

Updated Oct 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment