Skip to content

Commit

Permalink
[Feature] 모델, 네트워크 변경점 반영 (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
elesahich authored Jun 2, 2024
1 parent 2cffe87 commit 0edf089
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,36 @@ import Domain
import Networks

public final class PokeOnboardingRepository {
private let pokeService: PokeService
public init(pokeService: PokeService) {
self.pokeService = pokeService
}
private let pokeService: PokeService

public init(pokeService: PokeService) {
self.pokeService = pokeService
}
}

extension PokeOnboardingRepository: PokeOnboardingRepositoryInterface {
public func getRandomAcquaintances() -> AnyPublisher<[PokeUserModel], Error> {
self.pokeService
.getRandomUsers()
.map { $0.map { $0.toDomain() } }
.eraseToAnyPublisher()
}

public func getMesseageTemplates(type: PokeMessageType) -> AnyPublisher<PokeMessagesModel, Error> {
self.pokeService
.getPokeMessages(messageType: type.rawValue)
.map { $0.toDomain() }
.eraseToAnyPublisher()
}

public func poke(userId: Int, message: String) -> AnyPublisher<PokeUserModel, PokeError> {
self.pokeService
.poke(userId: userId, message: message)
.mapErrorToPokeError()
.map { $0.toDomain() }
.eraseToAnyPublisher()
}
public func getRandomAcquaintances(
randomUserType: PokeRandomUserType,
size: Int
) -> AnyPublisher<[PokeUserModel], Error> {
self.pokeService
.getRandomUsers(randomType: randomUserType.rawValue, size: size)
.map { $0.map { $0.toDomain() } }
.eraseToAnyPublisher()
}

public func getMesseageTemplates(type: PokeMessageType) -> AnyPublisher<PokeMessagesModel, Error> {
self.pokeService
.getPokeMessages(messageType: type.rawValue)
.map { $0.toDomain() }
.eraseToAnyPublisher()
}

public func poke(userId: Int, message: String) -> AnyPublisher<PokeUserModel, PokeError> {
self.pokeService
.poke(userId: userId, message: message)
.mapErrorToPokeError()
.map { $0.toDomain() }
.eraseToAnyPublisher()
}
}
18 changes: 18 additions & 0 deletions SOPT-iOS/Projects/Domain/Sources/Model/PokeRandomUserType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// PokeRandomUserType.swift
// Domain
//
// Created by Ian on 6/2/24.
// Copyright © 2024 SOPT-iOS. All rights reserved.
//

public enum PokeRandomUserType: String {
case all = "ALL"
case generation = "GENERATION"
case mbti = "MBTI"
case sojuCapacity = "SOJU_CAPACITY"
}

public enum PokeRandomUserQueryCount {
static let onboardingPage = 6
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import Combine

public protocol PokeOnboardingRepositoryInterface: PokeRepositoryInterface {
func getRandomAcquaintances() -> AnyPublisher<[PokeUserModel], Error>
func getMesseageTemplates(type: PokeMessageType) -> AnyPublisher<PokeMessagesModel, Error>
func getRandomAcquaintances(randomUserType: PokeRandomUserType, size: Int) -> AnyPublisher<[PokeUserModel], Error>
func getMesseageTemplates(type: PokeMessageType) -> AnyPublisher<PokeMessagesModel, Error>
}


Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,52 @@ import Combine
import Core

public protocol PokeOnboardingUsecase {
func getRandomAcquaintances()
func poke(userId: Int, message: PokeMessageModel)
var randomAcquaintances: PassthroughSubject<[PokeUserModel], Never> { get }
var pokedResponse: PassthroughSubject<PokeUserModel, Never> { get }
var errorMessage: PassthroughSubject<String?, Never> { get }
func getRandomAcquaintances()
func poke(userId: Int, message: PokeMessageModel)

var randomAcquaintances: PassthroughSubject<[PokeUserModel], Never> { get }
var pokedResponse: PassthroughSubject<PokeUserModel, Never> { get }
var errorMessage: PassthroughSubject<String?, Never> { get }
}

public final class DefaultPokeOnboardingUsecase {
private let repository: PokeOnboardingRepositoryInterface
private let cancelBag = CancelBag()
public let randomAcquaintances = PassthroughSubject<[PokeUserModel], Never>()
public let pokedResponse = PassthroughSubject<PokeUserModel, Never>()
public let errorMessage = PassthroughSubject<String?, Never>()
public init(repository: PokeOnboardingRepositoryInterface) {
self.repository = repository
}
private let repository: PokeOnboardingRepositoryInterface
private let cancelBag = CancelBag()

public let randomAcquaintances = PassthroughSubject<[PokeUserModel], Never>()
public let pokedResponse = PassthroughSubject<PokeUserModel, Never>()
public let errorMessage = PassthroughSubject<String?, Never>()

public init(repository: PokeOnboardingRepositoryInterface) {
self.repository = repository
}
}

extension DefaultPokeOnboardingUsecase: PokeOnboardingUsecase {
public func getRandomAcquaintances() {
self.repository
.getRandomAcquaintances()
.sink(
receiveCompletion: { _ in },
receiveValue: { [weak self] value in
self?.randomAcquaintances.send(value)
}
).store(in: self.cancelBag)
}
public func poke(userId: Int, message: PokeMessageModel) {
self.repository
.poke(userId: userId, message: message.content)
.catch { [weak self] error in
let message = error.toastMessage
self?.errorMessage.send(message)
return Empty<PokeUserModel, Never>()
}
.sink(
receiveCompletion: { _ in },
receiveValue: { [weak self] value in
self?.pokedResponse.send(value)
}
).store(in: self.cancelBag)
}
public func getRandomAcquaintances() {
self.repository
.getRandomAcquaintances(randomUserType: .all, size: PokeRandomUserQueryCount.onboardingPage)
.sink(
receiveCompletion: { _ in },
receiveValue: { [weak self] value in
self?.randomAcquaintances.send(value)
}
).store(in: self.cancelBag)
}

public func poke(userId: Int, message: PokeMessageModel) {
self.repository
.poke(userId: userId, message: message.content)
.catch { [weak self] error in
let message = error.toastMessage
self?.errorMessage.send(message)
return Empty<PokeUserModel, Never>()
}
.sink(
receiveCompletion: { _ in },
receiveValue: { [weak self] value in
self?.pokedResponse.send(value)
}
).store(in: self.cancelBag)
}
}

114 changes: 58 additions & 56 deletions SOPT-iOS/Projects/Modules/Networks/Sources/API/PokeAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,66 +13,68 @@ import Moya
import Core

public enum PokeAPI {
case isNewUser
case getWhoPokedToMe
case getWhoPokedToMeList(pageIndex: String)
case getFriend
case getFriendRandomUser
case getFriendList
case getFriendListWithRelation(relation: String, page: Int)
case getRandomUsers
case getPokeMessages(messageType: String)
case poke(userId: String, params: Parameters)
case isNewUser
case getWhoPokedToMe
case getWhoPokedToMeList(pageIndex: String)
case getFriend
case getFriendRandomUser
case getFriendList
case getFriendListWithRelation(relation: String, page: Int)
case getRandomUsers(params: Parameters)
case getPokeMessages(messageType: String)
case poke(userId: String, params: Parameters)
}

extension PokeAPI: BaseAPI {
public static var apiType: APIType = .poke

public var path: String {
switch self {
case .isNewUser:
return "/new"
case .getWhoPokedToMe:
return "/to/me"
case .getWhoPokedToMeList:
return "/to/me/list"
case .getFriend:
return "/friend"
case .getFriendRandomUser:
return "/friend/random-user"
case .getFriendList, .getFriendListWithRelation:
return "/friend/list"
case .getRandomUsers:
return "/random-user"
case .getPokeMessages:
return "/message"
case .poke(let userId, _):
return "/\(userId)"
}
public static var apiType: APIType = .poke

public var path: String {
switch self {
case .isNewUser:
return "/new"
case .getWhoPokedToMe:
return "/to/me"
case .getWhoPokedToMeList:
return "/to/me/list"
case .getFriend:
return "/friend"
case .getFriendRandomUser:
return "/friend/random-user"
case .getFriendList, .getFriendListWithRelation:
return "/friend/list"
case .getRandomUsers:
return "/random-user"
case .getPokeMessages:
return "/message"
case .poke(let userId, _):
return "/\(userId)"
}
public var method: Moya.Method {
switch self {
case .isNewUser, .getWhoPokedToMe, .getWhoPokedToMeList, .getFriend, .getFriendListWithRelation, .getFriendRandomUser,
.getFriendList, .getRandomUsers, .getPokeMessages:
return .get
case .poke:
return .put
}
}

public var method: Moya.Method {
switch self {
case .isNewUser, .getWhoPokedToMe, .getWhoPokedToMeList, .getFriend, .getFriendListWithRelation, .getFriendRandomUser,
.getFriendList, .getRandomUsers, .getPokeMessages:
return .get
case .poke:
return .put
}

public var task: Moya.Task {
switch self {
case .getWhoPokedToMeList(let pageIndex):
return .requestParameters(parameters: ["page": pageIndex], encoding: URLEncoding.queryString)
case .getPokeMessages(let messageType):
return .requestParameters(parameters: ["messageType": messageType], encoding: URLEncoding.queryString)
case .getFriendListWithRelation(let relation, let page):
return .requestParameters(parameters: ["type": relation, "page": page], encoding: URLEncoding.queryString)
case .poke(_, let params):
return .requestParameters(parameters: params, encoding: JSONEncoding.default)
default:
return .requestPlain
}
}

public var task: Moya.Task {
switch self {
case .getWhoPokedToMeList(let pageIndex):
return .requestParameters(parameters: ["page": pageIndex], encoding: URLEncoding.queryString)
case .getPokeMessages(let messageType):
return .requestParameters(parameters: ["messageType": messageType], encoding: URLEncoding.queryString)
case .getFriendListWithRelation(let relation, let page):
return .requestParameters(parameters: ["type": relation, "page": page], encoding: URLEncoding.queryString)
case .poke(_, let params):
return .requestParameters(parameters: params, encoding: JSONEncoding.default)
case .getRandomUsers(let params):
return .requestParameters(parameters: params, encoding: URLEncoding.queryString)
default:
return .requestPlain
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,44 @@
import Foundation

public struct PokeUserEntity {
public let userId: Int
public let playgroundId: Int
public let profileImage: String
public let name: String
public let generation: Int
public let part: String
public let pokeNum: Int
public let message: String
public let relationName: String
public let mutualRelationMessage: String
public let isFirstMeet, isAlreadyPoke: Bool
public let userId: Int
public let playgroundId: Int
public let profileImage: String
public let name: String
public let generation: Int
public let part: String
public let pokeNum: Int
public let message: String
public let relationName: String
public let mutualRelationMessage: String
public let isFirstMeet, isAlreadyPoke: Bool
public let isAnonymous: Bool
public let anonymousName: String
}

extension PokeUserEntity: Codable {
public enum CodingKeys: CodingKey {
case userId, playgroundId, profileImage, name, generation, part, pokeNum,
message, relationName, mutualRelationMessage, isFirstMeet, isAlreadyPoke
}
public enum CodingKeys: CodingKey {
case userId, playgroundId, profileImage, name, generation, part, pokeNum,
message, relationName, mutualRelationMessage, isFirstMeet, isAlreadyPoke,
isAnonymous, anonymousName
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.userId = try container.decode(Int.self, forKey: .userId)
self.playgroundId = try container.decode(Int.self, forKey: .playgroundId)
self.profileImage = try container.decodeIfPresent(String.self, forKey: .profileImage) ?? ""
self.name = try container.decode(String.self, forKey: .name)
self.generation = try container.decode(Int.self, forKey: .generation)
self.part = try container.decode(String.self, forKey: .part)
self.pokeNum = try container.decode(Int.self, forKey: .pokeNum)
self.message = try container.decode(String.self, forKey: .message)
self.relationName = try container.decode(String.self, forKey: .relationName)
self.mutualRelationMessage = try container.decode(String.self, forKey: .mutualRelationMessage)
self.isFirstMeet = try container.decode(Bool.self, forKey: .isFirstMeet)
self.isAlreadyPoke = try container.decode(Bool.self, forKey: .isAlreadyPoke)
}
self.userId = try container.decode(Int.self, forKey: .userId)
self.playgroundId = try container.decode(Int.self, forKey: .playgroundId)
self.profileImage = try container.decodeIfPresent(String.self, forKey: .profileImage) ?? ""
self.name = try container.decode(String.self, forKey: .name)
self.generation = try container.decode(Int.self, forKey: .generation)
self.part = try container.decode(String.self, forKey: .part)
self.pokeNum = try container.decode(Int.self, forKey: .pokeNum)
self.message = try container.decode(String.self, forKey: .message)
self.relationName = try container.decode(String.self, forKey: .relationName)
self.mutualRelationMessage = try container.decode(String.self, forKey: .mutualRelationMessage)
self.isFirstMeet = try container.decode(Bool.self, forKey: .isFirstMeet)
self.isAlreadyPoke = try container.decode(Bool.self, forKey: .isAlreadyPoke)
self.isAnonymous = try container.decode(Bool.self, forKey: .isAnonymous)
self.anonymousName = try container.decode(String.self, forKey: .anonymousName)
}
}
Loading

0 comments on commit 0edf089

Please sign in to comment.