diff --git a/iOS_assignment3/iOS_assignment3.xcodeproj/project.pbxproj b/iOS_assignment3/iOS_assignment3.xcodeproj/project.pbxproj index 2de94fd..5dd3539 100644 --- a/iOS_assignment3/iOS_assignment3.xcodeproj/project.pbxproj +++ b/iOS_assignment3/iOS_assignment3.xcodeproj/project.pbxproj @@ -10,6 +10,8 @@ 0CCB6F6955A11F843A56F9EB /* Pods_iOS_assignment3_iOS_assignment3UITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 901190AF570D2766E1FBB84F /* Pods_iOS_assignment3_iOS_assignment3UITests.framework */; }; 69E8BBABF90E55BDAFDD564E /* Pods_iOS_assignment3Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1C5578EA7AA033F81743940 /* Pods_iOS_assignment3Tests.framework */; }; 730CF3D0B3B0A792F0A43F90 /* Pods_iOS_assignment3.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9AE0C4B287F3BC877BAC1802 /* Pods_iOS_assignment3.framework */; }; + E703338D282E736100ABF306 /* ImageURLModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E703338C282E736100ABF306 /* ImageURLModel.swift */; }; + E703338F282FCC4600ABF306 /* ImageService.swift in Sources */ = {isa = PBXBuildFile; fileRef = E703338E282FCC4600ABF306 /* ImageService.swift */; }; E7169963282D4C0600386B9C /* SignUpModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7169962282D4C0600386B9C /* SignUpModel.swift */; }; E7169965282E2F2400386B9C /* ImageModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7169964282E2F2400386B9C /* ImageModel.swift */; }; E75863892829E3AA00D8EC45 /* Colors.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E75863882829E3AA00D8EC45 /* Colors.xcassets */; }; @@ -75,6 +77,8 @@ 9AE0C4B287F3BC877BAC1802 /* Pods_iOS_assignment3.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_iOS_assignment3.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 9C2E190BDA3E6CC449F989CD /* Pods-iOS_assignment3.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iOS_assignment3.release.xcconfig"; path = "Target Support Files/Pods-iOS_assignment3/Pods-iOS_assignment3.release.xcconfig"; sourceTree = ""; }; D1C5578EA7AA033F81743940 /* Pods_iOS_assignment3Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_iOS_assignment3Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + E703338C282E736100ABF306 /* ImageURLModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageURLModel.swift; sourceTree = ""; }; + E703338E282FCC4600ABF306 /* ImageService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageService.swift; sourceTree = ""; }; E7169962282D4C0600386B9C /* SignUpModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpModel.swift; sourceTree = ""; }; E7169964282E2F2400386B9C /* ImageModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageModel.swift; sourceTree = ""; }; E75863882829E3AA00D8EC45 /* Colors.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Colors.xcassets; sourceTree = ""; }; @@ -185,6 +189,7 @@ E7633CBD282C8C2300F0EF04 /* LoginModel.swift */, E7169962282D4C0600386B9C /* SignUpModel.swift */, E7169964282E2F2400386B9C /* ImageModel.swift */, + E703338C282E736100ABF306 /* ImageURLModel.swift */, ); path = Model; sourceTree = ""; @@ -193,6 +198,7 @@ isa = PBXGroup; children = ( E7633CC0282C8C5B00F0EF04 /* UserService.swift */, + E703338E282FCC4600ABF306 /* ImageService.swift */, ); path = Service; sourceTree = ""; @@ -264,9 +270,9 @@ isa = PBXGroup; children = ( E7F1DF5B282426A60080199A /* Global */, - E7633CBF282C8C4D00F0EF04 /* Service */, E7633CBC282C8C1500F0EF04 /* Model */, E7633CB7282C8B6500F0EF04 /* Network */, + E7633CBF282C8C4D00F0EF04 /* Service */, E78CAD09280BD2BA00BED47D /* TabBars */, E7F1DF422823B25E0080199A /* Auth */, E7F1DF432823B3140080199A /* Resource */, @@ -692,8 +698,10 @@ E78C75B9280C11EB000CBF39 /* ReelsTabViewController.swift in Sources */, E7D2B565280A5C780095CAF8 /* LoginViewController.swift in Sources */, E7633CC1282C8C5B00F0EF04 /* UserService.swift in Sources */, + E703338D282E736100ABF306 /* ImageURLModel.swift in Sources */, E7633CB9282C8B7C00F0EF04 /* APIConstants.swift in Sources */, E7F1DF5E282426F80080199A /* HomeFeedDataModel.swift in Sources */, + E703338F282FCC4600ABF306 /* ImageService.swift in Sources */, E7169963282D4C0600386B9C /* SignUpModel.swift in Sources */, E7EA8FD7280A826D00C2A2C0 /* SignUpUserNameViewController.swift in Sources */, E7EA8FD9280A827B00C2A2C0 /* SignUpPasswordViewController.swift in Sources */, diff --git a/iOS_assignment3/iOS_assignment3/Model/ImageModel.swift b/iOS_assignment3/iOS_assignment3/Model/ImageModel.swift index 110da5b..3e4ee85 100644 --- a/iOS_assignment3/iOS_assignment3/Model/ImageModel.swift +++ b/iOS_assignment3/iOS_assignment3/Model/ImageModel.swift @@ -8,10 +8,23 @@ import Foundation struct ImageResponse: Codable { - let id: Int + let object: [ImageData] +} + +struct ImageData: Codable { + let id: String let author: String let width: Int let height: Int let url: String let downloadUrl: String + +// enum Codingkeys: String, CodingKey { +// case id, author, width, height, url +// case downloadUrl = "download_url" +// } } + + + + diff --git a/iOS_assignment3/iOS_assignment3/Model/ImageURLModel.swift b/iOS_assignment3/iOS_assignment3/Model/ImageURLModel.swift new file mode 100644 index 0000000..71ab75b --- /dev/null +++ b/iOS_assignment3/iOS_assignment3/Model/ImageURLModel.swift @@ -0,0 +1,10 @@ +// +// ImageURLModel.swift +// iOS_assignment3 +// +// Created by 김담인 on 2022/05/13. +// + +import Foundation + +var ImageURL:[String] = [""] diff --git a/iOS_assignment3/iOS_assignment3/Network/APIConstants.swift b/iOS_assignment3/iOS_assignment3/Network/APIConstants.swift index 12d8ccd..4195251 100644 --- a/iOS_assignment3/iOS_assignment3/Network/APIConstants.swift +++ b/iOS_assignment3/iOS_assignment3/Network/APIConstants.swift @@ -24,5 +24,6 @@ struct APIConstants { static let signUpURL = authBaseURL + "/auth/signup" //MARK: - Picsum Base URL - static let picsumURL = "https://picsum.photos" + static let picsumURL = " https://picsum.photos/v2/list" +// static let feedsizeURL = "/340/375" } diff --git a/iOS_assignment3/iOS_assignment3/Service/ImageService.swift b/iOS_assignment3/iOS_assignment3/Service/ImageService.swift new file mode 100644 index 0000000..41b180c --- /dev/null +++ b/iOS_assignment3/iOS_assignment3/Service/ImageService.swift @@ -0,0 +1,126 @@ +// +// ImageService.swift +// iOS_assignment3 +// +// Created by 김담인 on 2022/05/14. +// + +import Foundation +import Alamofire + +class ImageService { + // 싱글톤 변수(Singleton) + // + static let shared = ImageService() + + private init() {} + +} +/* +extension ImageService { + func getImage(id:String) { + + guard let url = URL(string: "\(APIConstants.picsumURL)/id/\(id)/info") else {return} + + var request = URLRequest.init(url: url) + request.httpMethod = "GET" + + URLSession.shared.dataTask(with: request) { + data,response,error in + + guard let data = data else {return} + + do { + //받은 json데이터 파싱 + let decoder = JSONDecoder() + decoder.keyDecodingStrategy = .convertFromSnakeCase + let result = try decoder.decode([ImageResponse].self, from: data) + print("결과:\(result)") + + } catch(let error) { + print("에러:\(error)") + } + + }.resume() //모든 task()는 일시정지 상태로 시작하기 때문에 resume()으로 task()를 시작 + } +} +*/ +extension ImageService { + +func getImage( completion: @escaping (NetworkResult) -> Void) +{ + // completion 클로저를 @escaping closure로 정의 + let url = APIConstants.picsumURL // URL + let header: HTTPHeaders = ["Content-Type" : "application/json"] // HTTP Headers + let QueryParams: Parameters = [ + "page": 1, + "limit": 4 + ] + + // 요청서 + // Request 생성 + let dataRequest = AF.request(url, + method: .get, + parameters: QueryParams, + encoding: URLEncoding.default, + headers: header) + + // 서버 통신 시작 - Request 시작 + dataRequest.responseData { response in + // 요청(Request)를 하고 넘겨받은 응답의 결과를 가지고 성공 또는 실패 분기 처리 + switch response.result { + case .success: + // 성공 시에는 상태코드(Status Code)와 값(Value) + guard let statusCode = response.response?.statusCode else { return } + guard let value = response.value else { return } + + // 해당 응답을 가지고 case 분기처리 (200, 400, 500인지 - 200: 성공을 해서 데이터를 잘 받았는지 확인 + let networkResult = self.judgeStatus(by: statusCode, value, [ImageData].self) + completion(networkResult) + + // 실패 시에는 바로 networkFail(통신 실패)라는 신호 + case .failure: + completion(.networkFail) + } + } +} + /* + private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult { + switch statusCode { + // 성공 시에는 넘겨받은 데이터를 decode(해독)하는 함수를 호출 + // statusCode가 200~400대 일때는 통신에 이상있는것이 아니라 둘다 isValidLoginData함수를 호출 + case 200..<300: return isVaildSignUpData(data: data) + case 400..<500: return isVaildSignUpData(data: data) + case 500..<600: return .serverErr + default: return .networkFail + } + } + + private func isVaildSignUpData(data: Data) -> NetworkResult { + let decoder = JSONDecoder() + guard let decodedData = try? decoder.decode(SignUpResponse.self, from: data) + else { return .pathErr } + + return .success(decodedData as Any) + } + */ + + // 상태 코드와 값(value, data, response)를 가지고 통신의 결과를 핸들링하는 함수입니다. + private func judgeStatus(by statusCode: Int, _ data: Data, _ response: T.Type) -> NetworkResult { + + let decoder = JSONDecoder() + guard let decodedData = try? decoder.decode(response.self, from: data) + else { + return .pathErr + } + + switch statusCode { + // 성공 시에는 넘겨받은 데이터를 decode(해독)하는 함수를 호출합니다. + case 200: return .success(decodedData) + case 201: return .success(decodedData) + case 400..<500: return .requestErr(decodedData) //400대에서는 .requestErr로 빠짐 + case 500: return .serverErr + default: return .networkFail + } + } +} diff --git a/iOS_assignment3/iOS_assignment3/Service/UserService.swift b/iOS_assignment3/iOS_assignment3/Service/UserService.swift index 9c27b17..45d0a1e 100644 --- a/iOS_assignment3/iOS_assignment3/Service/UserService.swift +++ b/iOS_assignment3/iOS_assignment3/Service/UserService.swift @@ -160,26 +160,3 @@ extension UserService { return .success(decodedData as Any) } } - -extension UserService { -// func getImage(){ -// var url = APIConstants.picsumURL -// var request : NSMutableURLRequest = NSMutableURLRequest() -// request.url = NSURL(string: url) as URL? -// request.httpMethod = "GET" -// -// NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue(), completionHandler:{ (response:URLResponse!, data: Data!, error: Error!) -> Void in -// var error: AutoreleasingUnsafeMutablePointer? = nil -// let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary -// -// if (jsonResult != nil) { -// // process jsonResult -// } else { -// // couldn't load JSON, look at error -// } -// -// -// }) -// } - -} diff --git a/iOS_assignment3/iOS_assignment3/TabBars/Home/Feed/TableViewCell/HomeFeedTableViewCell.swift b/iOS_assignment3/iOS_assignment3/TabBars/Home/Feed/TableViewCell/HomeFeedTableViewCell.swift index 75d211a..c3a66f3 100644 --- a/iOS_assignment3/iOS_assignment3/TabBars/Home/Feed/TableViewCell/HomeFeedTableViewCell.swift +++ b/iOS_assignment3/iOS_assignment3/TabBars/Home/Feed/TableViewCell/HomeFeedTableViewCell.swift @@ -33,7 +33,7 @@ class HomeFeedTableViewCell: UITableViewCell { func setData(_ feedData: HomeFeedDataModel) { profileImage.image = UIImage(named: feedData.profileImage) profileName.text = feedData.profileName - + //피드 이미지 feedImage.image = UIImage(named: feedData.feedImage) likeCountLabel.text = "좋아요\(feedData.likesCount)개" diff --git a/iOS_assignment3/iOS_assignment3/TabBars/Home/Main/HomeTabViewController.swift b/iOS_assignment3/iOS_assignment3/TabBars/Home/Main/HomeTabViewController.swift index f8c002c..82be133 100644 --- a/iOS_assignment3/iOS_assignment3/TabBars/Home/Main/HomeTabViewController.swift +++ b/iOS_assignment3/iOS_assignment3/TabBars/Home/Main/HomeTabViewController.swift @@ -89,5 +89,8 @@ extension HomeTabViewController: UITableViewDataSource{ } } - +// feed image 가져오는 함수 +extension HomeTabViewController { + +}