Skip to content

Commit

Permalink
fix(Chat): fixed some connect- and disconnect edge case errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasfroeller committed Jun 12, 2024
1 parent ce49f19 commit bb37c73
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
32 changes: 16 additions & 16 deletions core/features/Chat/Model/ChatInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import Foundation

struct ChatInfo: Decodable {
private(set) var monitor_hash: String
private(set) var organization_name: String
private(set) var type: String
private(set) var title: String
private(set) var short_description: String
private(set) var `public`: Bool
private(set) var created_at: String
private(set) var updated_at: String
private(set) var project_url: String
private(set) var organization_name: String?
private(set) var type: String?
private(set) var title: String?
private(set) var short_description: String?
private(set) var `public`: Bool?
private(set) var created_at: String?
private(set) var updated_at: String?
private(set) var project_url: String?

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
monitor_hash = try container.decode(String.self, forKey: .monitor_hash)
organization_name = try container.decode(String.self, forKey: .organization_name)
type = try container.decode(String.self, forKey: .type)
title = try container.decode(String.self, forKey: .title)
short_description = try container.decode(String.self, forKey: .short_description)
`public` = try container.decode(Bool.self, forKey: .public)
created_at = try container.decode(String.self, forKey: .created_at)
updated_at = try container.decode(String.self, forKey: .updated_at)
project_url = try container.decode(String.self, forKey: .project_url)
organization_name = try container.decodeIfPresent(String.self, forKey: .organization_name)
type = try container.decodeIfPresent(String.self, forKey: .type)
title = try container.decodeIfPresent(String.self, forKey: .title)
short_description = try container.decodeIfPresent(String.self, forKey: .short_description)
`public` = try container.decodeIfPresent(Bool.self, forKey: .public)
created_at = try container.decodeIfPresent(String.self, forKey: .created_at)
updated_at = try container.decodeIfPresent(String.self, forKey: .updated_at)
project_url = try container.decodeIfPresent(String.self, forKey: .project_url)
}

enum CodingKeys: String, CodingKey {
Expand Down
36 changes: 19 additions & 17 deletions core/services/ChatService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,35 @@ class ChatService {

// INFO: messages are sent in multiple chuncks and not one, meaning the chats have to be updated in .text on didReceive
webSocketManager.onConnected = {
self.webSocketManagers[chat.monitor_hash] = webSocketManager

let formatter = ISO8601DateFormatter()
let created_at = formatter.date(from: chat.created_at ?? Date().ISO8601Format())
let updated_at = formatter.date(from: chat.updated_at ?? Date().ISO8601Format())

self.chats[chat.monitor_hash] = ChatBody(
team: chat.organization_name ?? "",
type: chat.type ?? "",
title: chat.title ?? "",
description: chat.short_description ?? "",
isPublic: chat.public ?? false,
createdAt: created_at ?? Date(),
updatedAt: updated_at ?? Date(),
projectUrl: chat.project_url ?? ""
)

completion(.success([]))
}
webSocketManager.onError = { error in
let errorFallback = NSError(domain: "ChatLoginService", code: 0, userInfo: [NSLocalizedDescriptionKey: "Something went wrong."])
completion(.failure(error ?? errorFallback))
}

self.webSocketManagers[chat.monitor_hash] = webSocketManager

let formatter = ISO8601DateFormatter()
let created_at = formatter.date(from: chat.created_at)
let updated_at = formatter.date(from: chat.updated_at)

self.chats[chat.monitor_hash] = ChatBody(
team: chat.organization_name,
type: chat.type,
title: chat.title,
description: chat.short_description,
isPublic: chat.public,
createdAt: created_at ?? Date(),
updatedAt: updated_at ?? Date(),
projectUrl: chat.project_url
)
}

completion(.success([]))
} catch {
if Environment.isDebug { print("Failed to parse JSON response", error, responseString) }

let error = NSError(domain: "ChatLoginService", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to parse JSON response"])
completion(.failure(error))
}
Expand Down

0 comments on commit bb37c73

Please sign in to comment.