Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] iOS 4차 과제 #13

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions InstagramClone/InstagramClone.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
8A3BF8AB28117D8D00F3D110 /* ProfileViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A3BF8AA28117D8D00F3D110 /* ProfileViewController.swift */; };
8A3BF8B228126AD000F3D110 /* LoginUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A3BF8B128126AD000F3D110 /* LoginUseCase.swift */; };
8A3BF8B428126ADF00F3D110 /* SignUpUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A3BF8B328126ADF00F3D110 /* SignUpUseCase.swift */; };
8A3BF8BF28127E9E00F3D110 /* MockUserInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A3BF8BE28127E9E00F3D110 /* MockUserInfo.swift */; };
8A3BF8C328128B0A00F3D110 /* LoginViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A3BF8C228128B0A00F3D110 /* LoginViewModel.swift */; };
8A3BF8C62812A63B00F3D110 /* SignUpPasswordViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A3BF8C52812A63B00F3D110 /* SignUpPasswordViewModel.swift */; };
8A3BF8CC2812B2C200F3D110 /* SignUpIDViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A3BF8CB2812B2C200F3D110 /* SignUpIDViewModel.swift */; };
8A3BF8CF2812DD3B00F3D110 /* Asset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A3BF8CE2812DD3B00F3D110 /* Asset.swift */; };
8A5881FE282EAA1E005747C4 /* APIConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A5881FD282EAA1E005747C4 /* APIConstants.swift */; };
8A588203282EADE4005747C4 /* LoginResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A588202282EADE4005747C4 /* LoginResponse.swift */; };
8A588207282EAE51005747C4 /* SignUpResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A588206282EAE51005747C4 /* SignUpResponse.swift */; };
8A588209282EAEC5005747C4 /* NetworkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A588208282EAEC5005747C4 /* NetworkService.swift */; };
8A65EA37281AF83C00900A2A /* StoryCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A65EA36281AF83C00900A2A /* StoryCollectionViewCell.swift */; };
8A65EA39281AF86700900A2A /* PostTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A65EA38281AF86600900A2A /* PostTableViewCell.swift */; };
8A65EA3B281B354C00900A2A /* ToggleImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A65EA3A281B354C00900A2A /* ToggleImageView.swift */; };
Expand Down Expand Up @@ -52,11 +55,14 @@
8A3BF8AA28117D8D00F3D110 /* ProfileViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileViewController.swift; sourceTree = "<group>"; };
8A3BF8B128126AD000F3D110 /* LoginUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginUseCase.swift; sourceTree = "<group>"; };
8A3BF8B328126ADF00F3D110 /* SignUpUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpUseCase.swift; sourceTree = "<group>"; };
8A3BF8BE28127E9E00F3D110 /* MockUserInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockUserInfo.swift; sourceTree = "<group>"; };
8A3BF8C228128B0A00F3D110 /* LoginViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewModel.swift; sourceTree = "<group>"; };
8A3BF8C52812A63B00F3D110 /* SignUpPasswordViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpPasswordViewModel.swift; sourceTree = "<group>"; };
8A3BF8CB2812B2C200F3D110 /* SignUpIDViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpIDViewModel.swift; sourceTree = "<group>"; };
8A3BF8CE2812DD3B00F3D110 /* Asset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Asset.swift; sourceTree = "<group>"; };
8A5881FD282EAA1E005747C4 /* APIConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIConstants.swift; sourceTree = "<group>"; };
8A588202282EADE4005747C4 /* LoginResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginResponse.swift; sourceTree = "<group>"; };
8A588206282EAE51005747C4 /* SignUpResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpResponse.swift; sourceTree = "<group>"; };
8A588208282EAEC5005747C4 /* NetworkService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkService.swift; sourceTree = "<group>"; };
8A65EA36281AF83C00900A2A /* StoryCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoryCollectionViewCell.swift; sourceTree = "<group>"; };
8A65EA38281AF86600900A2A /* PostTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostTableViewCell.swift; sourceTree = "<group>"; };
8A65EA3A281B354C00900A2A /* ToggleImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToggleImageView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -173,14 +179,6 @@
name = UseCase;
sourceTree = "<group>";
};
8A3BF8BD28127E7C00F3D110 /* Mock */ = {
isa = PBXGroup;
children = (
8A3BF8BE28127E9E00F3D110 /* MockUserInfo.swift */,
);
name = Mock;
sourceTree = "<group>";
};
8A3BF8C42812A57200F3D110 /* SignUpID */ = {
isa = PBXGroup;
children = (
Expand All @@ -198,6 +196,25 @@
path = Resource;
sourceTree = "<group>";
};
8A5881FC282EAA0B005747C4 /* Netwrok */ = {
isa = PBXGroup;
children = (
8A588201282EAD5B005747C4 /* Data Model */,
8A5881FD282EAA1E005747C4 /* APIConstants.swift */,
8A588208282EAEC5005747C4 /* NetworkService.swift */,
);
path = Netwrok;
sourceTree = "<group>";
};
8A588201282EAD5B005747C4 /* Data Model */ = {
isa = PBXGroup;
children = (
8A588202282EADE4005747C4 /* LoginResponse.swift */,
8A588206282EAE51005747C4 /* SignUpResponse.swift */,
);
path = "Data Model";
sourceTree = "<group>";
};
8A8EA860280271EF004DB3EF /* Extension */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -242,6 +259,7 @@
8AC6466927FD547A00377BB2 /* InstagramClone */ = {
isa = PBXGroup;
children = (
8A5881FC282EAA0B005747C4 /* Netwrok */,
8A3BF8CD2812DD1500F3D110 /* Resource */,
8AC6469027FE9F4400377BB2 /* Domain */,
8AC6467E27FD552300377BB2 /* Presentation */,
Expand Down Expand Up @@ -308,7 +326,6 @@
8AC6469027FE9F4400377BB2 /* Domain */ = {
isa = PBXGroup;
children = (
8A3BF8BD28127E7C00F3D110 /* Mock */,
8A3BF8AE281269C600F3D110 /* UseCase */,
8A3BF8AD281269BF00F3D110 /* Model */,
);
Expand Down Expand Up @@ -404,11 +421,15 @@
8A65EA3B281B354C00900A2A /* ToggleImageView.swift in Sources */,
8A3BF8CF2812DD3B00F3D110 /* Asset.swift in Sources */,
8A3BF8A728117D7D00F3D110 /* ReelsViewController.swift in Sources */,
8A5881FE282EAA1E005747C4 /* APIConstants.swift in Sources */,
8A3BF8AB28117D8D00F3D110 /* ProfileViewController.swift in Sources */,
8A588209282EAEC5005747C4 /* NetworkService.swift in Sources */,
8A65EA3D281B364E00900A2A /* StoryTableViewCell.swift in Sources */,
8A3BF8C328128B0A00F3D110 /* LoginViewModel.swift in Sources */,
8A65EA37281AF83C00900A2A /* StoryCollectionViewCell.swift in Sources */,
8A588207282EAE51005747C4 /* SignUpResponse.swift in Sources */,
8AC6466B27FD547A00377BB2 /* AppDelegate.swift in Sources */,
8A588203282EADE4005747C4 /* LoginResponse.swift in Sources */,
8A65EA39281AF86700900A2A /* PostTableViewCell.swift in Sources */,
8AC6469327FE9F5C00377BB2 /* User.swift in Sources */,
8AC6468827FD57EE00377BB2 /* SignUpIDViewController.swift in Sources */,
Expand All @@ -421,7 +442,6 @@
8AC6466D27FD547A00377BB2 /* SceneDelegate.swift in Sources */,
8A3BF8A328117D3B00F3D110 /* HomeViewController.swift in Sources */,
8A9880622820160C00FFB84A /* Post.swift in Sources */,
8A3BF8BF28127E9E00F3D110 /* MockUserInfo.swift in Sources */,
8A3BF8B428126ADF00F3D110 /* SignUpUseCase.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
36 changes: 27 additions & 9 deletions InstagramClone/InstagramClone/Domain/LoginUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,46 @@ import RxRelay
enum LoginError: Error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 케이스마다 에러를 보기쉽게 나눌수도 있군요!!!!😮

case idNotMatch
case pwNotMatch
case networkError
}

final class LoginUseCase {
var id: String?
var password: String?
let loginError = PublishRelay<LoginError>()
let loginSuccess = PublishRelay<Void>()
let disposeBag = DisposeBag()

func execute() {
guard let id = self.id,
let user = MockUserInfo.list[id] else {
self.loginError.accept(LoginError.idNotMatch)
let password = self.password else {
return
}

if user.pw != self.password {
self.loginError.accept(LoginError.pwNotMatch)
return
do {
let response = try NetworkService.shared.loginRequest(id: id, password: password)

response
.subscribe(onNext: { [weak self] loginResponse in
guard let loginResponse = loginResponse else { return }

if loginResponse.status == 404 {
self?.loginError.accept(LoginError.idNotMatch)
return
}

if loginResponse.status == 409 {
self?.loginError.accept(LoginError.pwNotMatch)
return
}

if loginResponse.status == 200 {
self?.loginSuccess.accept(())
}
})
.disposed(by: disposeBag)
} catch {
loginError.accept(LoginError.networkError)
}

User.id = self.id
User.password = self.password
loginSuccess.accept(())
}
}
12 changes: 0 additions & 12 deletions InstagramClone/InstagramClone/Domain/MockUserInfo.swift

This file was deleted.

27 changes: 21 additions & 6 deletions InstagramClone/InstagramClone/Domain/SignUpUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ enum SignUpError: Error {
case alreadyExist
case idEmpty
case passwordEmpty
case networkError
}

final class SignUpUseCase {
let signUpError = PublishRelay<SignUpError>()
let signUpSuccess = PublishRelay<Void>()
let disposeBag = DisposeBag()

func execute() {
guard let id = User.id else {
Expand All @@ -30,12 +32,25 @@ final class SignUpUseCase {
return
}

if let _ = MockUserInfo.list[id] {
self.signUpError.accept(SignUpError.alreadyExist)
return
do {
let response = try NetworkService.shared.signUpRequest(id: id, password: pw)

response
.subscribe(onNext: { [weak self] signUpResponse in
guard let signUpResponse = signUpResponse else { return }

if signUpResponse.status == 409 {
self?.signUpError.accept(SignUpError.alreadyExist)
return
}

if signUpResponse.status == 201 {
self?.signUpSuccess.accept(())
}
})
.disposed(by: disposeBag)
} catch {
signUpError.accept(SignUpError.networkError)
}

MockUserInfo.list[id] = User(id: id, pw: pw)
self.signUpSuccess.accept(())
}
}
5 changes: 5 additions & 0 deletions InstagramClone/InstagramClone/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down
14 changes: 14 additions & 0 deletions InstagramClone/InstagramClone/Netwrok/APIConstants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// APIConstants.swift
// InstagramClone
//
// Created by Inwoo Park on 2022/05/14.
//

import Foundation

struct APIConstants {
static let baseURL = "http://13.124.62.236"
static let loginURL = baseURL + "/auth/signin"
static let signupURL = baseURL + "/auth/signup"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// LoginResponse.swift
// InstagramClone
//
// Created by Inwoo Park on 2022/05/14.
//

import Foundation

struct LoginResponse: Codable {
let status: Int
let success: Bool?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

성공, 실패 둘 다 success값이 전달되던데, 혹시 success 파라미터를 옵셔널로 선언한 이유가 따로 있나요??🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음.. 그러게요? 이유는 딱히 없고 실수인거 같습니다ㅠ
이번과제도 실수 만땅이네요 🥲 지적 감사합니다!!

let message: String
let data: LoginData?
}

struct LoginData: Codable {
let name: String
let email: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// SignUpResponse.swift
// InstagramClone
//
// Created by Inwoo Park on 2022/05/14.
//

import Foundation

struct SignUpResponse: Codable {
let status: Int
let success: Bool?
let message: String
let data: SignUpData?
}

struct SignUpData: Codable {
let id: Int
}
64 changes: 64 additions & 0 deletions InstagramClone/InstagramClone/Netwrok/NetworkService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// NetworkService.swift
// InstagramClone
//
// Created by Inwoo Park on 2022/05/14.
//

import Foundation

import RxSwift
import RxCocoa

class NetworkService {
static let shared = NetworkService()
private init() {}

func loginRequest(id: String, password: String) throws -> Observable<LoginResponse?> {
let body = ["email": id, "password": password]
let encoder = JSONEncoder()
let encodedBody = try encoder.encode(body)

return Observable.just(APIConstants.loginURL)
.map { URL(string: $0)! }
.map {
var request = URLRequest(url: $0)
request.httpMethod = "POST"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rx .. 멋있습니다 ..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 아직 어렵지만... 열심히해보고 있슴당..

request.httpBody = encodedBody
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
return request
}
.flatMap { URLSession.shared.rx.response(request: $0) }
.map { _, data in
do {
return try JSONDecoder().decode(LoginResponse.self, from: data)
} catch {
return nil
}
}
}

func signUpRequest(id: String, password: String) throws -> Observable<SignUpResponse?> {
let body = ["name": "", "email": id, "password": password]
let encoder = JSONEncoder()
let encodedBody = try encoder.encode(body)

return Observable.just(APIConstants.signupURL)
.map { URL(string: $0)! }
.map {
var request = URLRequest(url: $0)
request.httpMethod = "POST"
request.httpBody = encodedBody
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
return request
}
.flatMap { URLSession.shared.rx.response(request: $0) }
.map { _, data in
do {
return try JSONDecoder().decode(SignUpResponse.self, from: data)
} catch {
return nil
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,11 @@ private extension LoginViewController {

output.errorMessage
.asDriver(onErrorJustReturn: "")
.drive(onNext: { error in
// TODO: 에러처리
print(error)
.drive(onNext: { [weak self] errorMessage in
let alert = UIAlertController(title: nil, message: errorMessage, preferredStyle: UIAlertController.Style.alert)
let defaultAction = UIAlertAction(title: "확인", style: UIAlertAction.Style.default)
alert.addAction(defaultAction)
self?.present(alert, animated: false)
})
.disposed(by: disposeBag)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ final class LoginViewModel {
output.errorMessage.accept("존재하지 않는 ID입니다.")
case .pwNotMatch:
output.errorMessage.accept("비밀번호가 일치하지 않습니다.")
case .networkError:
output.errorMessage.accept("네트워크 에러입니다.")
}
})
.disposed(by: disposeBag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ private extension SignUpPasswordViewController {

output.errorMessage
.asDriver(onErrorJustReturn: "")
.drive(onNext: { error in
// TODO: 에러처리
print(error)
.drive(onNext: { [weak self] errorMessage in
let alert = UIAlertController(title: nil, message: errorMessage, preferredStyle: UIAlertController.Style.alert)
let defaultAction = UIAlertAction(title: "확인", style: UIAlertAction.Style.default)
alert.addAction(defaultAction)
self?.present(alert, animated: false)
})
.disposed(by: disposeBag)
}
Expand Down
Loading