Skip to content

Commit

Permalink
Merge pull request #59 from devxsby/feat/#46-로그인뷰-네트워크연결
Browse files Browse the repository at this point in the history
[Feat] #46 - 로그인 뷰 Domain, Data 연결
  • Loading branch information
devxsby authored Dec 28, 2022
2 parents 47965fc + cc039e7 commit a03699f
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
//
// SignInRepository.swift
// Presentation
// Data
//
// Created by devxsby on 2022/12/01.
// Copyright © 2022 SOPT-Stamp-iOS. All rights reserved.
//

import Foundation

import Combine

import Core

import Domain
import Network

public class SignInRepository {

private let networkService: AuthService
private let cancelBag = Set<AnyCancellable>()
private let networkService: UserService
private let cancelBag = CancelBag()

public init(service: AuthService) {
public init(service: UserService) {
self.networkService = service
}
}

extension SignInRepository: SignInRepositoryInterface {

public func requestSignIn(request: SignInRequest) -> AnyPublisher<SignInModel, Error> {
networkService.requestSignIn(email: request.email, password: request.password).map { entity in
UserDefaults.standard.set(entity.userId, forKey: "userId")
return entity.toDomain()
}.eraseToAnyPublisher()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SignInTransform.swift
// Presentation
// Data
//
// Created by devxsby on 2022/12/01.
// Copyright © 2022 SOPT-Stamp-iOS. All rights reserved.
Expand All @@ -14,6 +14,6 @@ import Network
extension SignInEntity {

public func toDomain() -> SignInModel {
return SignInModel.init()
return SignInModel.init(userId: self.userId, message: self.message)
}
}
13 changes: 8 additions & 5 deletions SOPT-Stamp-iOS/Projects/Domain/Sources/Model/SignInModel.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
//
// SignInModel.swift
// Presentation
// Domain
//
// Created by devxsby on 2022/12/01.
// Created by devxsby on 2022/12/23.
// Copyright © 2022 SOPT-Stamp-iOS. All rights reserved.
//

import Foundation

public struct SignInModel {

public init() {

let userId: Int?
let message: String?

public init(userId: Int?, message: String?) {
self.userId = userId
self.message = message
}
}
21 changes: 21 additions & 0 deletions SOPT-Stamp-iOS/Projects/Domain/Sources/Model/SignInRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// SignInRequest.swift
// Domain
//
// Created by devxsby on 2022/12/01.
// Copyright © 2022 SOPT-Stamp-iOS. All rights reserved.
//

import Combine

import Core

public struct SignInRequest {
public let email: String
public let password: String

public init(email: String, password: String) {
self.email = email
self.password = password
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SignInRepositoryInterface.swift
// Presentation
// Domain
//
// Created by devxsby on 2022/12/01.
// Copyright © 2022 SOPT-Stamp-iOS. All rights reserved.
Expand All @@ -9,5 +9,5 @@
import Combine

public protocol SignInRepositoryInterface {

func requestSignIn(request: SignInRequest) -> AnyPublisher<SignInModel, Error>
}
18 changes: 14 additions & 4 deletions SOPT-Stamp-iOS/Projects/Domain/Sources/UseCase/SignInUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@

import Combine

public protocol SignInUseCase {
import Core

public protocol SignInUseCase {
func requestSignIn(signInRequest: SignInRequest)
var signInSuccess: CurrentValueSubject<Bool, Error> { get set }
}

public class DefaultSignInUseCase {

private let repository: SignInRepositoryInterface
private var cancelBag = Set<AnyCancellable>()

private var cancelBag = CancelBag()
public var signInSuccess = CurrentValueSubject<Bool, Error>(false)

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

extension DefaultSignInUseCase: SignInUseCase {

public func requestSignIn(signInRequest: SignInRequest) {
repository.requestSignIn(request: signInRequest).sink { event in
print("SignInUseCase: \(event)")
} receiveValue: { signInModel in
self.signInSuccess.send(signInModel.userId != nil)
}.store(in: self.cancelBag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Moya

public enum UserAPI {
case signUp(nickname: String, email: String, password: String)
case signIn(email: String, password: String)
}

extension UserAPI: BaseAPI {
Expand All @@ -24,13 +25,15 @@ extension UserAPI: BaseAPI {
switch self {
case .signUp:
return "signup"
case .signIn:
return "login"
}
}

// MARK: - Method
public var method: Moya.Method {
switch self {
case .signUp:
case .signUp, .signIn:
return .post
}
}
Expand All @@ -43,6 +46,9 @@ extension UserAPI: BaseAPI {
params["nickname"] = nickname
params["email"] = email
params["password"] = password
case .signIn(let email, let password):
params["email"] = email
params["password"] = password
}
return params
}
Expand All @@ -56,7 +62,7 @@ extension UserAPI: BaseAPI {

public var task: Task {
switch self {
case .signUp:
case .signUp, .signIn:
return .requestParameters(parameters: bodyParameters ?? [:], encoding: parameterEncoding)
default:
return .requestPlain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation

public struct SignInEntity {

public struct SignInEntity: Codable {
public let userId: Int?
public let message: String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ public typealias DefaultUserService = BaseService<UserAPI>

public protocol UserService {
func postSignUp(nickname: String, email: String, password: String) -> AnyPublisher<Int, Error>
func requestSignIn(email: String, password: String) -> AnyPublisher<SignInEntity, Error>
}

extension DefaultUserService: UserService {

public func postSignUp(nickname: String, email: String, password: String) -> AnyPublisher<Int, Error> {
requestObjectInCombineNoResult(.signUp(nickname: nickname,
email: email,
password: password)
)
}

public func requestSignIn(email: String, password: String) -> AnyPublisher<SignInEntity, Error> {
requestObjectInCombine(.signIn(email: email, password: password))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,18 @@ public class MissionListVC: UIViewController {
super.viewDidLoad()
self.setUI()
self.setLayout()
self.changeRootViewController()
self.setDelegate()
self.registerCells()
self.setDataSource()
self.bindViews()
self.bindViewModels()
}

public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}
}

// MARK: - UI & Layouts
Expand Down Expand Up @@ -217,6 +223,13 @@ extension MissionListVC {

extension MissionListVC {

private func changeRootViewController() {
guard let uWindow = self.view.window else { return }
uWindow.rootViewController = self
uWindow.makeKey()
UIView.transition(with: uWindow, duration: 0.5, options: [.transitionCrossDissolve], animations: {}, completion: nil)
}

private func setDelegate() {
missionListCollectionView.delegate = self
}
Expand Down Expand Up @@ -292,3 +305,9 @@ extension MissionListVC: UICollectionViewDelegate {
}
}
}

extension MissionListVC: UIGestureRecognizerDelegate {
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public class OnboardingVC: UIViewController {
self.setCollectionViewCell()
self.setOnboardingData()
}

public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}

// MARK: - @objc Function

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import Foundation

public struct SignInModel {

public init() {

public let email: String
public let password: String

public init(email: String, password: String) {
self.email = email
self.password = password
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import DSKit

import Core

import Domain

import Combine
import SnapKit
import Then
Expand All @@ -36,11 +38,13 @@ public class SignInVC: UIViewController {
.setTextFieldType(.email)
.setSubTitle(I18N.SignIn.id)
.setPlaceholder(I18N.SignIn.enterID)
.setAlertDelegate(passwordTextField)

private lazy var passwordTextField = CustomTextFieldView(type: .subTitle)
.setTextFieldType(.password)
.setSubTitle(I18N.SignIn.password)
.setPlaceholder(I18N.SignIn.enterPW)
.setAlertLabelEnabled(I18N.SignIn.checkAccount)

private lazy var findAccountButton = UIButton(type: .system).then {
$0.setTitle(I18N.SignIn.findAccount, for: .normal)
Expand All @@ -49,9 +53,7 @@ public class SignInVC: UIViewController {
$0.addTarget(self, action: #selector(findAccountButtonDidTap), for: .touchUpInside)
}

private lazy var signInButton = CustomButton(title: I18N.SignIn.signIn).setEnabled(false).then {
$0.addTarget(self, action: #selector(signInButtonDidTap), for: .touchUpInside)
}
private lazy var signInButton = CustomButton(title: I18N.SignIn.signIn).setEnabled(false)

private lazy var signUpButton = UIButton(type: .system).then {
$0.setTitle(I18N.SignIn.signUp, for: .normal)
Expand All @@ -72,6 +74,7 @@ public class SignInVC: UIViewController {

public override func viewWillAppear(_ animated: Bool) {
self.addKeyboardObserver()
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}

deinit {
Expand All @@ -83,16 +86,13 @@ public class SignInVC: UIViewController {
@objc
private func findAccountButtonDidTap() {
print("find account btn did tap")
}

@objc
private func signInButtonDidTap() {
print("sign in btn did tap")
// 화면전환
}

@objc
private func signUpButtonDidTap() {
print("sign up btn did tap")
let signUpVC = self.factory.makeSignUpVC()
self.navigationController?.pushViewController(signUpVC, animated: true)
}

}
Expand Down Expand Up @@ -151,8 +151,27 @@ extension SignInVC {
extension SignInVC {

private func bindViewModels() {
// let input = SignInViewModel.Input()
// let output = self.viewModel.transform(from: input, cancelBag: self.cancelBag)

let signInButtonTapped = signInButton.publisher(for: .touchUpInside).map { _ in
SignInRequest(email: self.emailTextField.text, password: self.passwordTextField.text)
}.asDriver()

let input = SignInViewModel.Input(emailTextChanged: emailTextField.textChanged,
passwordTextChanged: passwordTextField.textChanged,
signInButtonTapped: signInButtonTapped)
let output = self.viewModel.transform(from: input, cancelBag: self.cancelBag)

output.isFilledForm.assign(to: \.isEnabled, on: self.signInButton).store(in: self.cancelBag)

output.isSignInSuccess.sink { isSignInSuccess in
if isSignInSuccess {
let missionListVC = self.factory.makeMissionListVC(sceneType: .default)
self.navigationController?.pushViewController(missionListVC, animated: true)
} else {
self.emailTextField.alertType = .invalidInput(text: "")
self.passwordTextField.alertType = .invalidInput(text: I18N.SignIn.checkAccount)
}
}.store(in: self.cancelBag)
}

private func setTapGesture() {
Expand All @@ -173,7 +192,6 @@ extension SignInVC {
@objc func keyboardUp(notification: NSNotification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
// let safeHeight = self.view.safeAreaInsets.bottom

UIView.animate(
withDuration: 0.3,
Expand Down
Loading

0 comments on commit a03699f

Please sign in to comment.