Skip to content

Commit

Permalink
Initialize initial messages inline, add some helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kinoroy committed Jul 18, 2020
1 parent f481519 commit dbc7de1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
15 changes: 15 additions & 0 deletions Example/Sources/Data Generation/SampleData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ final internal class SampleData {
completion(messages)
}

func getMessages(count: Int) -> [MockMessage] {
var messages: [MockMessage] = []
// Disable Custom Messages
UserDefaults.standard.set(false, forKey: "Custom Messages")
for _ in 0..<count {
let uniqueID = UUID().uuidString
let user = senders.random()!
let date = dateAddingRandomTime()
let randomSentence = Lorem.sentence()
let message = MockMessage(text: randomSentence, user: user, messageId: uniqueID, date: date)
messages.append(message)
}
return messages
}

func getAdvancedMessages(count: Int, completion: ([MockMessage]) -> Void) {
var messages: [MockMessage] = []
// Enable Custom Messages
Expand Down
32 changes: 24 additions & 8 deletions Example/Sources/Views/SwiftUI/SwiftUIExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,36 @@ import MessageKit

@available(iOS 13.0, *)
struct SwiftUIExampleView: View {
@State var messages = [MessageType]()

@State var messages: [MessageType] = SampleData.shared.getMessages(count: 20)

var body: some View {
MessagesView(messages: $messages).onAppear {
SampleData.shared.getMessages(count: 20) { messages in
self.messages.append(contentsOf: messages)
}
MockSocket.shared.connect(with: [SampleData.shared.nathan, SampleData.shared.wu]).onNewMessage { message in
self.messages.append(message)
}
self.connectToMessageSocket()
}.onDisappear {
MockSocket.shared.disconnect()
self.cleanupSocket()
}
.navigationBarTitle("SwiftUI Example", displayMode: .inline)
}

private func getInitialMessages() {
SampleData.shared.getMessages(count: 20) { messages in
DispatchQueue.main.async {
self.messages.append(contentsOf: messages)
}
}
}

private func connectToMessageSocket() {
MockSocket.shared.connect(with: [SampleData.shared.nathan, SampleData.shared.wu]).onNewMessage { message in
self.messages.append(message)
}
}

private func cleanupSocket() {
MockSocket.shared.disconnect()
}

}

@available(iOS 13.0.0, *)
Expand Down

0 comments on commit dbc7de1

Please sign in to comment.