From c9ece19af43dd81b778cf96016b27780e907db04 Mon Sep 17 00:00:00 2001 From: MaraMincho <103064352+MaraMincho@users.noreply.github.com> Date: Fri, 8 Dec 2023 03:49:07 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=86=8C=EC=BC=93=20Json=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WorkoutSocketRepository.swift | 24 +++++++++++--- .../Entities/WorkoutRealTimeModel.swift | 31 +++++++++++++++++++ .../WorkoutPeerRandomMatchingViewModel.swift | 2 +- .../WorkoutSessionViewController.swift | 4 +++ .../Sources/UserInformationManager.swift | 18 +++++------ 5 files changed, 64 insertions(+), 15 deletions(-) diff --git a/iOS/Projects/Features/Record/Sources/Data/Repositories/WorkoutSocketRepository.swift b/iOS/Projects/Features/Record/Sources/Data/Repositories/WorkoutSocketRepository.swift index ac24ea00..41c57830 100644 --- a/iOS/Projects/Features/Record/Sources/Data/Repositories/WorkoutSocketRepository.swift +++ b/iOS/Projects/Features/Record/Sources/Data/Repositories/WorkoutSocketRepository.swift @@ -7,11 +7,11 @@ // import Combine +import CommonNetworkingKeyManager import Foundation +import Keychain import Log import Trinet -import CommonNetworkingKeyManager -import Keychain // MARK: - WorkoutSocketRepositoryDependency @@ -34,7 +34,7 @@ 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() @@ -42,9 +42,23 @@ struct WorkoutSocketRepository { 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 { @@ -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") diff --git a/iOS/Projects/Features/Record/Sources/Domain/Entities/WorkoutRealTimeModel.swift b/iOS/Projects/Features/Record/Sources/Domain/Entities/WorkoutRealTimeModel.swift index b48eee1b..7a344dca 100644 --- a/iOS/Projects/Features/Record/Sources/Domain/Entities/WorkoutRealTimeModel.swift +++ b/iOS/Projects/Features/Record/Sources/Domain/Entities/WorkoutRealTimeModel.swift @@ -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 /// 운동 데이터를 송수신하는 모델입니다. 소켓 통신 시 사용합니다. diff --git a/iOS/Projects/Features/Record/Sources/Presentation/WorkoutPeerMatchingScene/ViewModel/WorkoutPeerRandomMatchingViewModel.swift b/iOS/Projects/Features/Record/Sources/Presentation/WorkoutPeerMatchingScene/ViewModel/WorkoutPeerRandomMatchingViewModel.swift index 6c271822..fae36ce4 100644 --- a/iOS/Projects/Features/Record/Sources/Presentation/WorkoutPeerMatchingScene/ViewModel/WorkoutPeerRandomMatchingViewModel.swift +++ b/iOS/Projects/Features/Record/Sources/Presentation/WorkoutPeerMatchingScene/ViewModel/WorkoutPeerRandomMatchingViewModel.swift @@ -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, diff --git a/iOS/Projects/Features/Record/Sources/Presentation/WorkoutSessionGroupScene/SessionScene/WorkoutSessionViewController.swift b/iOS/Projects/Features/Record/Sources/Presentation/WorkoutSessionGroupScene/SessionScene/WorkoutSessionViewController.swift index fe159fbc..a8555f9d 100644 --- a/iOS/Projects/Features/Record/Sources/Presentation/WorkoutSessionGroupScene/SessionScene/WorkoutSessionViewController.swift +++ b/iOS/Projects/Features/Record/Sources/Presentation/WorkoutSessionGroupScene/SessionScene/WorkoutSessionViewController.swift @@ -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) } @@ -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를 그리지 못합니다.") diff --git a/iOS/Projects/Shared/UserInformationManager/Sources/UserInformationManager.swift b/iOS/Projects/Shared/UserInformationManager/Sources/UserInformationManager.swift index f5ea2bea..0007bce2 100644 --- a/iOS/Projects/Shared/UserInformationManager/Sources/UserInformationManager.swift +++ b/iOS/Projects/Shared/UserInformationManager/Sources/UserInformationManager.swift @@ -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() @@ -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"