From 097f8155f2fc9cc508921eb169fe21f4b7fe13e6 Mon Sep 17 00:00:00 2001 From: mmaybei Date: Mon, 26 Aug 2024 20:22:05 +0900 Subject: [PATCH] =?UTF-8?q?refactor/#336=20=EB=AF=B8=ED=8C=85=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KkuMulKum.xcodeproj/project.pbxproj | 8 ++ .../Network/Service/MeetingListService.swift | 89 +++++++++++++++++++ .../Network/Service/MeetingService.swift | 27 ------ .../TargetType/MeetingListTargetType.swift | 54 +++++++++++ .../Source/Core/MainTabBarController.swift | 2 +- .../MeetingListServiceProtocol.swift | 1 + 6 files changed, 153 insertions(+), 28 deletions(-) create mode 100644 KkuMulKum/Network/Service/MeetingListService.swift create mode 100644 KkuMulKum/Network/TargetType/MeetingListTargetType.swift diff --git a/KkuMulKum.xcodeproj/project.pbxproj b/KkuMulKum.xcodeproj/project.pbxproj index ea556104..6a284e95 100644 --- a/KkuMulKum.xcodeproj/project.pbxproj +++ b/KkuMulKum.xcodeproj/project.pbxproj @@ -100,6 +100,8 @@ A3DD9C412C41BAD000E58A13 /* MeetingListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C3A2C41BAD000E58A13 /* MeetingListViewModel.swift */; }; A3DD9C682C45C78300E58A13 /* HomeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C672C45C78300E58A13 /* HomeService.swift */; }; A3DF8EC72C7C93CD00207BCC /* MeetingListServiceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DF8EC62C7C93CD00207BCC /* MeetingListServiceProtocol.swift */; }; + A3DF8EC92C7C987200207BCC /* MeetingListService.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DF8EC82C7C987200207BCC /* MeetingListService.swift */; }; + A3DF8ECB2C7C98A200207BCC /* MeetingListTargetType.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DF8ECA2C7C98A200207BCC /* MeetingListTargetType.swift */; }; A3FB02442C788A790026DE0E /* HomeServiceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3FB02432C788A790026DE0E /* HomeServiceProtocol.swift */; }; A3FB184D2C3BF45F001483E5 /* MakeMeetingsRequestModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3FB184C2C3BF45F001483E5 /* MakeMeetingsRequestModel.swift */; }; A3FB184F2C3BF4BC001483E5 /* MakeMeetingsResponseModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3FB184E2C3BF4BB001483E5 /* MakeMeetingsResponseModel.swift */; }; @@ -308,6 +310,8 @@ A3DD9C3A2C41BAD000E58A13 /* MeetingListViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeetingListViewModel.swift; sourceTree = ""; }; A3DD9C672C45C78300E58A13 /* HomeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeService.swift; sourceTree = ""; }; A3DF8EC62C7C93CD00207BCC /* MeetingListServiceProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingListServiceProtocol.swift; sourceTree = ""; }; + A3DF8EC82C7C987200207BCC /* MeetingListService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingListService.swift; sourceTree = ""; }; + A3DF8ECA2C7C98A200207BCC /* MeetingListTargetType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeetingListTargetType.swift; sourceTree = ""; }; A3FB02432C788A790026DE0E /* HomeServiceProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeServiceProtocol.swift; sourceTree = ""; }; A3FB184C2C3BF45F001483E5 /* MakeMeetingsRequestModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MakeMeetingsRequestModel.swift; sourceTree = ""; }; A3FB184E2C3BF4BB001483E5 /* MakeMeetingsResponseModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MakeMeetingsResponseModel.swift; sourceTree = ""; }; @@ -751,6 +755,7 @@ isa = PBXGroup; children = ( DDD62FFA2C5FA4FE00174B57 /* MeetingService.swift */, + A3DF8EC82C7C987200207BCC /* MeetingListService.swift */, 78BD61332C45B4A7005752FD /* AuthService.swift */, A3DD9C672C45C78300E58A13 /* HomeService.swift */, DD3F9DC52C484DEB008E1FF7 /* PromiseService.swift */, @@ -1199,6 +1204,7 @@ children = ( 789196392C49697F00FF8CDF /* Auth */, DDE7D2C52C47D2BB005A921F /* MeetingTargetType.swift */, + A3DF8ECA2C7C98A200207BCC /* MeetingListTargetType.swift */, DDE7D2C92C47EE81005A921F /* PromiseTargetType.swift */, A39F2B1C2C47F3D0008DA5F5 /* HomeTargetType.swift */, DD3F9DD32C4858A3008E1FF7 /* UtilTargetType.swift */, @@ -1881,6 +1887,7 @@ DE32D1D42C3BFB56006848DF /* UpdateProfileNameModel.swift in Sources */, A39F2B212C499CE5008DA5F5 /* SetReadyStatusInfoServiceType.swift in Sources */, DE6D4D132C3F14D80005584B /* MeetingMemberCell.swift in Sources */, + A3DF8ECB2C7C98A200207BCC /* MeetingListTargetType.swift in Sources */, DD3F9DD82C49C25D008E1FF7 /* PromiseServiceProtocol.swift in Sources */, 784825072C77215B00FE07A0 /* MyPageTermsViewController.swift in Sources */, DDAF1C932C3D6E3D008A37D3 /* PromiseViewController.swift in Sources */, @@ -1945,6 +1952,7 @@ DECB84582C43FBEB0022A003 /* AddPromiseViewController.swift in Sources */, DDE7D2C62C47D2BB005A921F /* MeetingTargetType.swift in Sources */, A39F2B1D2C47F3D0008DA5F5 /* HomeTargetType.swift in Sources */, + A3DF8EC92C7C987200207BCC /* MeetingListService.swift in Sources */, DDFA507F2C4693BD000A62E2 /* ProfileSetupViewModel.swift in Sources */, A3DD9C3D2C41BAD000E58A13 /* MeetingTableViewCell.swift in Sources */, DE558C5B2C4595A7008DAC4A /* SelectMemberView.swift in Sources */, diff --git a/KkuMulKum/Network/Service/MeetingListService.swift b/KkuMulKum/Network/Service/MeetingListService.swift new file mode 100644 index 00000000..ebea1757 --- /dev/null +++ b/KkuMulKum/Network/Service/MeetingListService.swift @@ -0,0 +1,89 @@ +// +// MeetingListService.swift +// KkuMulKum +// +// Created by 예삐 on 8/26/24. +// + +import Foundation + +import Moya + +final class MeetingListService { + let provider: MoyaProvider + + init(provider: MoyaProvider = MoyaProvider(plugins: [MoyaLoggingPlugin()])) { + self.provider = provider + } + + func request( + with request: MeetingListTargetType + ) async throws -> ResponseBodyDTO? { + return try await withCheckedThrowingContinuation { continuation in + provider.request(request) { result in + switch result { + case .success(let response): + do { + let decodedData = try JSONDecoder().decode( + ResponseBodyDTO.self, + from: response.data + ) + continuation.resume(returning: decodedData) + } catch { + continuation.resume(throwing: error) + } + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } +} + +extension MeetingListService: MeetingListServiceProtocol { + func fetchLoginUser() async throws -> ResponseBodyDTO? { + return try await request(with: .fetchLoginUser) + } + + func fetchMeetingList() async throws -> ResponseBodyDTO? { + return try await request(with: .fetchMeetingList) + } +} + +final class MockMeetingListService: MeetingListServiceProtocol { + func fetchLoginUser() async throws -> ResponseBodyDTO? { + let mockData = ResponseBodyDTO( + success: true, + data: LoginUserModel( + userID: 1, + name: "꾸물리안", + level: 4, + promiseCount: 8, + tardyCount: 2, + tardySum: 72, + profileImageURL: "" + ), + error: nil + ) + return mockData + } + + func fetchMeetingList() async throws -> ResponseBodyDTO? { + let mockData = ResponseBodyDTO( + success: true, + data: MeetingListModel( + count: 6, + meetings: [ + Meeting(meetingID: 1, name: "꾸물이들", memberCount: 14), + Meeting(meetingID: 2, name: "아요레디", memberCount: 28), + Meeting(meetingID: 3, name: "안드가자", memberCount: 26), + Meeting(meetingID: 4, name: "난이서버", memberCount: 30), + Meeting(meetingID: 5, name: "캔디팟", memberCount: 24), + Meeting(meetingID: 6, name: "열기팟", memberCount: 24) + ] + ), + error: nil + ) + return mockData + } +} diff --git a/KkuMulKum/Network/Service/MeetingService.swift b/KkuMulKum/Network/Service/MeetingService.swift index d5137970..65494b4a 100644 --- a/KkuMulKum/Network/Service/MeetingService.swift +++ b/KkuMulKum/Network/Service/MeetingService.swift @@ -42,12 +42,6 @@ final class MeetingService { } } -extension MeetingService: MeetingListServiceProtocol { - func fetchMeetingList() async throws -> ResponseBodyDTO? { - return try await request(with: .fetchMeetingList) - } -} - extension MeetingService: MeetingInfoServiceProtocol { func fetchMeetingInfo(with meetingID: Int) async throws -> ResponseBodyDTO? { return try await request(with: .fetchMeetingInfo(meetingID: meetingID)) @@ -88,27 +82,6 @@ extension MeetingService: InviteCodeServiceProtocol { } } -final class MockMeetingListService: MeetingListServiceProtocol { - func fetchMeetingList() async throws -> ResponseBodyDTO? { - let mockData = ResponseBodyDTO( - success: true, - data: MeetingListModel( - count: 6, - meetings: [ - Meeting(meetingID: 1, name: "꾸물이들", memberCount: 14), - Meeting(meetingID: 2, name: "아요레디", memberCount: 28), - Meeting(meetingID: 3, name: "안드가자", memberCount: 26), - Meeting(meetingID: 4, name: "난이서버", memberCount: 30), - Meeting(meetingID: 5, name: "캔디팟", memberCount: 24), - Meeting(meetingID: 6, name: "열기팟", memberCount: 24) - ] - ), - error: nil - ) - return mockData - } -} - final class MockMeetingInfoService: MeetingInfoServiceProtocol { func fetchMeetingInfo(with meetingID: Int) -> ResponseBodyDTO? { diff --git a/KkuMulKum/Network/TargetType/MeetingListTargetType.swift b/KkuMulKum/Network/TargetType/MeetingListTargetType.swift new file mode 100644 index 00000000..3f54b2f2 --- /dev/null +++ b/KkuMulKum/Network/TargetType/MeetingListTargetType.swift @@ -0,0 +1,54 @@ +// +// MeetingListTargetType.swift +// KkuMulKum +// +// Created by 예삐 on 8/26/24. +// + +import Foundation + +import Moya + +enum MeetingListTargetType { + case fetchLoginUser + case fetchMeetingList +} + +extension MeetingListTargetType: TargetType { + var baseURL: URL { + guard let privacyInfo = Bundle.main.privacyInfo, + let urlString = privacyInfo["BASE_URL"] as? String, + let url = URL(string: urlString) else { + fatalError("Invalid BASE_URL in PrivacyInfo.plist") + } + return url + } + + var path: String { + switch self { + case .fetchLoginUser: + return "/api/v1/users/me" + case .fetchMeetingList: + return "/api/v1/meetings" + } + } + + var method: Moya.Method { + return .get + } + + var task: Task { + return .requestPlain + } + + var headers: [String : String]? { + guard let token = DefaultKeychainService.shared.accessToken else { + return ["Content-Type" : "application/json"] + } + + return [ + "Content-Type" : "application/json", + "Authorization" : "Bearer \(token)" + ] + } +} diff --git a/KkuMulKum/Source/Core/MainTabBarController.swift b/KkuMulKum/Source/Core/MainTabBarController.swift index f35f541e..fc5186d0 100644 --- a/KkuMulKum/Source/Core/MainTabBarController.swift +++ b/KkuMulKum/Source/Core/MainTabBarController.swift @@ -33,7 +33,7 @@ final class MainTabBarController: UITabBarController { let meetingListViewController = MeetingListViewController( viewModel: MeetingListViewModel( - service: MeetingService() + service: MeetingListService() )).then { $0.tabBarItem.title = "내 모임" $0.tabBarItem.image = .iconGroup diff --git a/KkuMulKum/Source/MeetingList/ServiceProtocol/MeetingListServiceProtocol.swift b/KkuMulKum/Source/MeetingList/ServiceProtocol/MeetingListServiceProtocol.swift index 6f047c64..f257dd48 100644 --- a/KkuMulKum/Source/MeetingList/ServiceProtocol/MeetingListServiceProtocol.swift +++ b/KkuMulKum/Source/MeetingList/ServiceProtocol/MeetingListServiceProtocol.swift @@ -10,5 +10,6 @@ import Foundation import Moya protocol MeetingListServiceProtocol { + func fetchLoginUser() async throws -> ResponseBodyDTO? func fetchMeetingList() async throws -> ResponseBodyDTO? }