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] 4주차 과제 #8

Merged
merged 9 commits into from
Dec 24, 2021
1 change: 1 addition & 0 deletions Youtube_Clone/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ target 'Youtube_Clone' do
# Pods for Youtube_Clone
pod 'SnapKit', '~> 5.0.0'
pod 'Then'
pod 'Alamofire'
end
6 changes: 5 additions & 1 deletion Youtube_Clone/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
PODS:
- Alamofire (5.4.3)
- SnapKit (5.0.1)
- Then (2.7.0)

DEPENDENCIES:
- Alamofire
- SnapKit (~> 5.0.0)
- Then

SPEC REPOS:
trunk:
- Alamofire
- SnapKit
- Then

SPEC CHECKSUMS:
Alamofire: e447a2774a40c996748296fa2c55112fdbbc42f9
SnapKit: 97b92857e3df3a0c71833cce143274bf6ef8e5eb
Then: acfe0be7e98221c6204e12f8161459606d5d822d

PODFILE CHECKSUM: e873bea5c112d4efff91bbd242fa10e7ce41d5de
PODFILE CHECKSUM: 31a913186a28117b589147ea2d6e26920ca6a7a9

COCOAPODS: 1.11.2
32 changes: 32 additions & 0 deletions Youtube_Clone/Youtube_Clone.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
362F42FB273CF3FA0033E580 /* NetworkResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 362F42FA273CF3FA0033E580 /* NetworkResult.swift */; };
362F42FD273CF40E0033E580 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 362F42FC273CF40E0033E580 /* Constants.swift */; };
362F42FF273CF5BA0033E580 /* LoginDataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 362F42FE273CF5BA0033E580 /* LoginDataModel.swift */; };
362F4301273CF9320033E580 /* LoginService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 362F4300273CF9320033E580 /* LoginService.swift */; };
362F4303273CFD530033E580 /* UIViewController++.swift in Sources */ = {isa = PBXBuildFile; fileRef = 362F4302273CFD530033E580 /* UIViewController++.swift */; };
362F4305273D0AC70033E580 /* RegisterService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 362F4304273D0AC70033E580 /* RegisterService.swift */; };
36631BF8272BD2B4001BEC5B /* UIView++.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36631BF7272BD2B4001BEC5B /* UIView++.swift */; };
36631C01272BDF31001BEC5B /* UITableView++.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36631C00272BDF31001BEC5B /* UITableView++.swift */; };
36631C04272BF533001BEC5B /* ShortsTVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36631C02272BF533001BEC5B /* ShortsTVC.swift */; };
Expand Down Expand Up @@ -44,6 +50,12 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
362F42FA273CF3FA0033E580 /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = "<group>"; };
362F42FC273CF40E0033E580 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
362F42FE273CF5BA0033E580 /* LoginDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginDataModel.swift; sourceTree = "<group>"; };
362F4300273CF9320033E580 /* LoginService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginService.swift; sourceTree = "<group>"; };
362F4302273CFD530033E580 /* UIViewController++.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController++.swift"; sourceTree = "<group>"; };
362F4304273D0AC70033E580 /* RegisterService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegisterService.swift; sourceTree = "<group>"; };
36631BF7272BD2B4001BEC5B /* UIView++.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView++.swift"; sourceTree = "<group>"; };
36631C00272BDF31001BEC5B /* UITableView++.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableView++.swift"; sourceTree = "<group>"; };
36631C02272BF533001BEC5B /* ShortsTVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortsTVC.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -105,12 +117,24 @@
path = Pods;
sourceTree = "<group>";
};
362F42F9273CF3E00033E580 /* Service */ = {
isa = PBXGroup;
children = (
362F42FA273CF3FA0033E580 /* NetworkResult.swift */,
362F42FC273CF40E0033E580 /* Constants.swift */,
362F4300273CF9320033E580 /* LoginService.swift */,
362F4304273D0AC70033E580 /* RegisterService.swift */,
);
path = Service;
sourceTree = "<group>";
};
36631BF6272BD2A8001BEC5B /* Extension */ = {
isa = PBXGroup;
children = (
36631BF7272BD2B4001BEC5B /* UIView++.swift */,
36631C00272BDF31001BEC5B /* UITableView++.swift */,
36631C0C272BF9F9001BEC5B /* UICollectionView++.swift */,
362F4302273CFD530033E580 /* UIViewController++.swift */,
);
path = Extension;
sourceTree = "<group>";
Expand Down Expand Up @@ -161,6 +185,7 @@
36631C16272C0E1A001BEC5B /* Models */ = {
isa = PBXGroup;
children = (
362F42FE273CF5BA0033E580 /* LoginDataModel.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -195,6 +220,7 @@
36A6022F271007A5002BBF40 /* Source */ = {
isa = PBXGroup;
children = (
362F42F9273CF3E00033E580 /* Service */,
36631C16272C0E1A001BEC5B /* Models */,
36631C06272BF674001BEC5B /* CVCs */,
36631BF9272BDE73001BEC5B /* TVCs */,
Expand Down Expand Up @@ -513,18 +539,24 @@
36631C04272BF533001BEC5B /* ShortsTVC.swift in Sources */,
36F8F6E72722DDE600092338 /* AddsVC.swift in Sources */,
366D23FA27106D6200652491 /* CompleteLoginVC.swift in Sources */,
362F4305273D0AC70033E580 /* RegisterService.swift in Sources */,
36F8F6C72721464E00092338 /* TabbarVC.swift in Sources */,
36631BF8272BD2B4001BEC5B /* UIView++.swift in Sources */,
36A6021C2710078F002BBF40 /* AppDelegate.swift in Sources */,
36631C09272BF68C001BEC5B /* ShortsCVC.swift in Sources */,
362F42FF273CF5BA0033E580 /* LoginDataModel.swift in Sources */,
36A60237271008DE002BBF40 /* LoginVC.swift in Sources */,
36F8F6EB2722DE0500092338 /* SavesVC.swift in Sources */,
36A6021E2710078F002BBF40 /* SceneDelegate.swift in Sources */,
36631C0D272BF9F9001BEC5B /* UICollectionView++.swift in Sources */,
36F8F6E02722DDB100092338 /* ShortsVC.swift in Sources */,
362F4303273CFD530033E580 /* UIViewController++.swift in Sources */,
362F42FD273CF40E0033E580 /* Constants.swift in Sources */,
362F4301273CF9320033E580 /* LoginService.swift in Sources */,
366D23F62710628800652491 /* RegisterVC.swift in Sources */,
36F8F6E92722DDF800092338 /* SubscribeVC.swift in Sources */,
36631C10272C04F2001BEC5B /* VideoTVC.swift in Sources */,
362F42FB273CF3FA0033E580 /* NetworkResult.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion Youtube_Clone/Youtube_Clone/Resource/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Login</string>
<string>Tabbar</string>
</dict>
</array>
</dict>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19162" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19162" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="Y6W-OH-hqX">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19144"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// UIViewController++.swift
// Youtube_Clone
//
// Created by 박익범 on 2021/11/11.
//

import Foundation
import UIKit

extension UIViewController{
func makeRequestAlert(title : String,
message : String,
okAction : ((UIAlertAction) -> Void)?,
cancelAction : ((UIAlertAction) -> Void)? = nil,
completion : (() -> Void)? = nil){

let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()

let alertViewController = UIAlertController(title: title, message: message,
preferredStyle: .alert)

let okAction = UIAlertAction(title: "예", style: .default, handler: okAction)
alertViewController.addAction(okAction)


let cancelAction = UIAlertAction(title: "아니오", style: .default, handler: cancelAction)
alertViewController.addAction(cancelAction)


self.present(alertViewController, animated: true, completion: completion)
}


func makeAlert(title : String,
message : String,
okAction : ((UIAlertAction) -> Void)? = nil,
completion : (() -> Void)? = nil){
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()

let alertViewController = UIAlertController(title: title, message: message,
preferredStyle: .alert)

let okAction = UIAlertAction(title: "확인", style: .default, handler: okAction)
alertViewController.addAction(okAction)


self.present(alertViewController, animated: true, completion: completion)
}

func makeAlert(title : String?,
message : String?,
okAction : ((UIAlertAction) -> Void)? = nil,
completion : (() -> Void)? = nil){
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()

let alertViewController = UIAlertController(title: "", message: message,
preferredStyle: .alert)



let okAction = UIAlertAction(title: "확인", style: .default, handler: okAction)
alertViewController.addAction(okAction)


self.present(alertViewController, animated: true, completion: completion)
}
}
23 changes: 23 additions & 0 deletions Youtube_Clone/Youtube_Clone/Source/Models/LoginDataModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// LoginDataModel.swift
// Youtube_Clone
//
// Created by 박익범 on 2021/11/11.
//


import Foundation

// MARK: - Login
struct LoginDataModel: Codable {
let status: Int
let success: Bool
let message: String
let data: loginData?
}

// MARK: - DataClass
struct loginData: Codable {
let id: Int
let name, email: String
}
10 changes: 10 additions & 0 deletions Youtube_Clone/Youtube_Clone/Source/Service/Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation

struct Constants{

static let baseURL: String = "https://asia-northeast3-we-sopt-29.cloudfunctions.net"

static let login: String = baseURL + "/api/user/login"
static let register: String = baseURL + "/api/user/signup"

}
67 changes: 67 additions & 0 deletions Youtube_Clone/Youtube_Clone/Source/Service/LoginService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Foundation
import Alamofire

struct LoginService {
static let shared = LoginService()

private func makeParameter(email : String, password : String) -> Parameters
{
return ["email" : email,
"password" : password ]
}

func Login(email : String,
password : String,
completion : @escaping (NetworkResult<Any>) -> Void)
{
let header : HTTPHeaders = ["Content-Type": "application/json"]
let dataRequest = AF.request(Constants.login,
method: .post,
parameters: makeParameter(email: email, password: password),
encoding: JSONEncoding.default,
headers: header)


dataRequest.responseData { dataResponse in

switch dataResponse.result {
case .success:

print("----- 로그인 데이터 요청 성공")
guard let statusCode = dataResponse.response?.statusCode else {return}
print(dataRequest.response?.statusCode)
guard let value = dataResponse.value else {return}
let networkResult = self.judgeStatus(by: statusCode, value)
completion(networkResult)

case .failure: completion(.pathErr)

}
}

}

private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {

let decoder = JSONDecoder()

guard let decodedData = try? decoder.decode(LoginDataModel.self, from: data)
else {
print("패쓰에러")
return .pathErr

}

switch statusCode {

case 200:
print("--- 로그인 데이터 받기 성공")
return .success(decodedData)
case 400: return .requestErr(decodedData)
case 500: return .serverErr
default: return .networkFail
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

enum NetworkResult<T> {
case success(T)
case requestErr(T)
case pathErr
case serverErr
case networkFail
}
70 changes: 70 additions & 0 deletions Youtube_Clone/Youtube_Clone/Source/Service/RegisterService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Foundation
import Alamofire

struct RegisterService {
static let shared = RegisterService()

private func makeParameter(email: String, password: String, name: String) -> Parameters
{
return ["email": email,
"password": password,
"name": name]
}

func Register(email: String,
password: String,
name: String,
completion : @escaping (NetworkResult<Any>) -> Void)
{
let header : HTTPHeaders = ["Content-Type": "application/json"]
let dataRequest = AF.request(Constants.register,
method: .post,
parameters: makeParameter(email: email, password: password, name: name),
encoding: JSONEncoding.default,
headers: header)


dataRequest.responseData { dataResponse in

switch dataResponse.result {
case .success:

print("----- 회원가입 데이터 요청 성공")
guard let statusCode = dataResponse.response?.statusCode else {return}
print(dataRequest.response?.statusCode)
guard let value = dataResponse.value else {return}
let networkResult = self.judgeStatus(by: statusCode, value)
completion(networkResult)

case .failure: completion(.pathErr)

}
}

}

private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {

let decoder = JSONDecoder()


guard let decodedData = try? decoder.decode(LoginDataModel.self, from: data)
else {
print("패쓰에러")
return .pathErr

}

switch statusCode {

case 200:
print("--- 회원가입 데이터 받기 성공")
return .success(decodedData)
case 400: return .requestErr(decodedData)
case 500: return .serverErr
default: return .networkFail
}
}


}
Loading