Skip to content

Commit

Permalink
feat: 소켓 Json 에러 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraMincho committed Dec 7, 2023
1 parent e33b3dd commit c9ece19
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
//

import Combine
import CommonNetworkingKeyManager
import Foundation
import Keychain
import Log
import Trinet
import CommonNetworkingKeyManager
import Keychain

// MARK: - WorkoutSocketRepositoryDependency

Expand All @@ -34,17 +34,31 @@ struct WorkoutSocketRepository {
session: session,
endPoint: .init(headers: [
.init(key: "roomId", value: dependency.roomID),
.authorization(bearer: String(data: Keychain.shared.load(key: Tokens.accessToken)!, encoding: .utf8)!)
.authorization(bearer: String(data: Keychain.shared.load(key: Tokens.accessToken)!, encoding: .utf8)!),
])
)
task = receiveParticipantsData()
}

private func stringToWorkoutRealTimeModel(rawString: String) throws -> WorkoutRealTimeModel {
guard let jsonData = rawString.data(using: .utf8) else {
Log.make().debug("StringToWorkoutRealTimeModel에서 Decode에러 ")
throw WorkoutSocketRepositoryError.invalidStringForConversion
}
return try jsonDecoder.decode(WorkoutRealTimeModel.self, from: jsonData)
guard let workoutSessionModel = try? jsonDecoder.decode(WorkoutSession.self, from: jsonData) else {
Log.make().debug("StringToWorkoutRealTimeModel에서 Decode에러 ")
throw WorkoutSocketRepositoryError.invalidStringForConversion
}
return .init(
id: workoutSessionModel.data.id,
roomID: workoutSessionModel.data.roomID,
nickname: workoutSessionModel.data.nickname,
health: .init(
distance: workoutSessionModel.data.health.distance,
calories: workoutSessionModel.data.health.calories,
heartRate: nil
)
)
}

private func receiveParticipantsData() -> Task<Void, Error> {
Expand All @@ -54,7 +68,7 @@ struct WorkoutSocketRepository {
do {
switch try await provider.receive() {
case let .string(string):
Log.make(with: .network).debug("received \(string)")
Log.make(with: .network).debug("소켓: received \(string)")
try subject.send(stringToWorkoutRealTimeModel(rawString: string))
default:
Log.make().error("소켓: You can't enter this line")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@

import Foundation

// MARK: - WorkoutSession

/// 소켓을 통해 받은 데이터 형식 입니다.
struct WorkoutSession: Decodable {
let event: String
let data: SessionData
}

// MARK: - SessionData

struct SessionData: Decodable {
let nickname: String
let health: HealthData
let id: String
let roomID: String

enum CodingKeys: String, CodingKey {
case nickname
case health
case id
case roomID = "roomId"
}
}

// MARK: - HealthData

struct HealthData: Decodable {
let calories: Double
let distance: Double
}

// MARK: - WorkoutRealTimeModel

/// 운동 데이터를 송수신하는 모델입니다. 소켓 통신 시 사용합니다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ extension WorkoutPeerRandomMatchingViewModel: WorkoutPeerRandomMatchingViewModel
Log.make().debug("\(startDate)")

let workoutSessionComponents = WorkoutSessionComponents(
participants: [sessionPeerTypeOfMe] + peers,
participants: peers,
startDate: startDate,
roomID: roomID,
id: id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public final class WorkoutSessionViewController: UIViewController {
init(viewModel: WorkoutSessionViewModelRepresentable, dependency: WorkoutSessionViewControllerDependency) {
self.viewModel = viewModel
for participant in dependency.participants {
Log.make().debug("사람 id는 \(participant.id), nickName: \(participant.nickname)")
userInfoByID[participant.id] = participant
realTimeModelByID[participant.id] = .init(distance: 0, calories: 0, heartRate: 0)
}
Expand Down Expand Up @@ -132,8 +133,11 @@ public final class WorkoutSessionViewController: UIViewController {
Log.make().debug("\(model)")
self?.realTimeModelByID[model.id] = model.health
var snapshot = self?.participantsDataSource?.snapshot()

snapshot?.reconfigureItems([model.id])
if let snapshot {
let temp = snapshot.itemIdentifiers
Log.make().debug("현재 snpahot의 아이템은 다음과 같습니다. \(temp)")
self?.participantsDataSource?.apply(snapshot)
} else {
Log.make().error("snapshot이 생성되지 못했습니다. 헬스 데이터로 UI를 그리지 못합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public extension UserInformationManager {
private extension UserInformationManager {
/// 만약 userDefaults에 값이 존재한다면 fakeData를 설정합니다.
func setDefaultsData() {
guard
data(.userNickName) == nil,
data(.birthDayDate) == nil,
data(.userProfileImage) == nil,
data(.userProfileImageURL) == nil
else {
return
}
// guard
// data(.userNickName) == nil,
// data(.birthDayDate) == nil,
// data(.userProfileImage) == nil,
// data(.userProfileImageURL) == nil
// else {
// return
// }

let date = Date.now
let formatter = DateFormatter()
Expand All @@ -76,7 +76,7 @@ private extension UserInformationManager {
let dateData = Data(dateString.utf8)
defaults.setValue(dateData, forKey: UserInformation.birthDayDate.rawValue)

let name = Data("김무디".utf8)
let name = Data("슈퍼맨".utf8)
defaults.setValue(name, forKey: UserInformation.userNickName.rawValue)

let imageURLString = "https://www.catster.com/wp-content/uploads/2017/08/Pixiebob-cat.jpg"
Expand Down

0 comments on commit c9ece19

Please sign in to comment.