diff --git a/iOS/Ringo/Ringo.xcodeproj/project.pbxproj b/iOS/Ringo/Ringo.xcodeproj/project.pbxproj index 35c135a..1f40c9c 100644 --- a/iOS/Ringo/Ringo.xcodeproj/project.pbxproj +++ b/iOS/Ringo/Ringo.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 940C25642B73D20900E069D0 /* ConnectionCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 940C25632B73D20900E069D0 /* ConnectionCollectionViewCell.swift */; }; + 940C25662B74EF4D00E069D0 /* FriendService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 940C25652B74EF4D00E069D0 /* FriendService.swift */; }; 94470A8A2B71050700F0A942 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 94470A892B71050700F0A942 /* Alamofire */; }; 94470A8D2B710A3100F0A942 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = 94470A8C2B710A3100F0A942 /* Starscream */; }; 94470A902B710CE500F0A942 /* WebRTC in Frameworks */ = {isa = PBXBuildFile; productRef = 94470A8F2B710CE500F0A942 /* WebRTC */; }; @@ -30,9 +31,10 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 940C25632B73D20900E069D0 /* ConnectionCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionCollectionViewCell.swift; sourceTree = ""; }; + 940C25652B74EF4D00E069D0 /* FriendService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FriendService.swift; sourceTree = ""; }; 94470A922B7163E900F0A942 /* Model.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Model.swift; sourceTree = ""; }; 94470A942B71680100F0A942 /* SigninService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SigninService.swift; sourceTree = ""; }; - 940C25632B73D20900E069D0 /* ConnectionCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionCollectionViewCell.swift; sourceTree = ""; }; 94470A962B717DC300F0A942 /* ConnectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionViewController.swift; sourceTree = ""; }; 945603122B6AC07D002F4B33 /* TabBarViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarViewController.swift; sourceTree = ""; }; 945603142B6AC22A002F4B33 /* ContactsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsViewController.swift; sourceTree = ""; }; @@ -104,6 +106,7 @@ 945603222B6D31D9002F4B33 /* ContactsTableViewCell.swift */, 94470A922B7163E900F0A942 /* Model.swift */, 94470A942B71680100F0A942 /* SigninService.swift */, + 940C25652B74EF4D00E069D0 /* FriendService.swift */, 94470A962B717DC300F0A942 /* ConnectionViewController.swift */, 945603202B6D2F06002F4B33 /* Canvas.swift */, 945CF9832B67E1CE00396E4E /* Assets.xcassets */, @@ -206,6 +209,7 @@ 945603232B6D31D9002F4B33 /* ContactsTableViewCell.swift in Sources */, 945CF97D2B67E1CD00396E4E /* SceneDelegate.swift in Sources */, 94470A932B7163E900F0A942 /* Model.swift in Sources */, + 940C25662B74EF4D00E069D0 /* FriendService.swift in Sources */, 945603152B6AC22A002F4B33 /* ContactsViewController.swift in Sources */, 9456031D2B6BF44A002F4B33 /* FriendRequestViewController.swift in Sources */, 94470A952B71680100F0A942 /* SigninService.swift in Sources */, diff --git a/iOS/Ringo/Ringo.xcodeproj/project.xcworkspace/xcuserdata/jinhyuk.xcuserdatad/UserInterfaceState.xcuserstate b/iOS/Ringo/Ringo.xcodeproj/project.xcworkspace/xcuserdata/jinhyuk.xcuserdatad/UserInterfaceState.xcuserstate index b5488cf..6f8bd28 100644 Binary files a/iOS/Ringo/Ringo.xcodeproj/project.xcworkspace/xcuserdata/jinhyuk.xcuserdatad/UserInterfaceState.xcuserstate and b/iOS/Ringo/Ringo.xcodeproj/project.xcworkspace/xcuserdata/jinhyuk.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/iOS/Ringo/Ringo/FriendService.swift b/iOS/Ringo/Ringo/FriendService.swift new file mode 100644 index 0000000..aec56fb --- /dev/null +++ b/iOS/Ringo/Ringo/FriendService.swift @@ -0,0 +1,66 @@ +// +// FriendService.swift +// Ringo +// +// Created by 강진혁 on 2/8/24. +// + +import Foundation +import Alamofire + +class FriendService { + + static let shared = FriendService() + + private init() {} + + func loadFriendsList(userId: Int64, completion: @escaping(NetworkResult) -> Void) + { + let url = "http://192.168.0.7:7080/friendship/findByUserIdAndStatusOrFriendIdAndStatus" + + let header : HTTPHeaders = ["Content-Type" : "application/json"] + + let body : Parameters = [ + "userId" : userId + ] + + let dataRequest = AF.request(url, + method: .post, + parameters: body, + encoding: JSONEncoding.default, + headers: header) + + dataRequest.responseData{ + response in + switch response.result { + case .success: + guard let statusCode = response.response?.statusCode else {return} + guard let value = response.value else {return} + + let networkResult = self.judgeStatus(by: statusCode, value) + completion(networkResult) + + case .failure: + completion(.networkFail) + } + } + } + private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult { + print(statusCode) + switch statusCode { + case ..<300 : return isVaildData(data: data) + case 400..<500 : return .pathErr + case 500..<600 : return .serverErr + default : return .networkFail + } + } + //통신이 성공하고 원하는 데이터가 올바르게 들어왔을때 처리하는 함수 + private func isVaildData(data: Data) -> NetworkResult { + let decoder = JSONDecoder() //서버에서 준 데이터를 Codable을 채택, response가 json일 경우 + guard let decodedData = try? decoder.decode([String].self, from: data) + //데이터가 변환이 되게끔 Response 모델 구조체로 데이터를 변환해서 넣고, 그 데이터를 NetworkResult Success 파라미터로 전달 + else { return .pathErr } + + return .success(decodedData as Any) + } +} diff --git a/iOS/Ringo/Ringo/Screens/ContactsViewController.swift b/iOS/Ringo/Ringo/Screens/ContactsViewController.swift index b9c4000..16491bf 100644 --- a/iOS/Ringo/Ringo/Screens/ContactsViewController.swift +++ b/iOS/Ringo/Ringo/Screens/ContactsViewController.swift @@ -12,7 +12,7 @@ class ContactsViewController: UIViewController { let contactsTableViewCell = ContactsTableViewCell.identifier - let randomNames = ["민준", "서준", "예준", "도윤", "시우", "주원", "하준", "지호", "지후", "준서", "준우", "현우", "도현", "지훈", "건우", "우진", "선우", "서진", "민재", "현준", "연우", "유준", "정우", "승우", "승현", "시윤", "준혁", "은우", "지환", "승민", "지우", "유찬", "윤우", "민성", "준영", "시후", "진우", "지수", "서연", "서윤", "지우", "서현", "민서", "하은", "하윤", "윤서", "지유", "지민", "채원", "지윤", "은서", "수아", "다은", "예은", "지아", "수빈", "소율", "예린", "예원", "지원", "소윤", "지안", "하린", "시은", "유진", "채은"] + var randomNames = [String]() let searchController = UISearchController() var tableView: UITableView! @@ -32,10 +32,11 @@ class ContactsViewController: UIViewController { tableView.register(ContactsTableViewCell.self, forCellReuseIdentifier: contactsTableViewCell) tableView.delegate = self tableView.dataSource = self -// tableView.rowHeight = UITableView.automaticDimension self.view.addSubview(tableView) setConstraints() + + loadFriends() } func setConstraints(){ @@ -101,6 +102,38 @@ extension ContactsViewController: UITableViewDataSource { return cell } } +// MARK: - Sign in +extension ContactsViewController { + + func loadFriends() { + //id 3인 유저의 친구목록 + FriendService.shared.loadFriendsList(userId: 3) { response in + switch response { + case .success(let data): + + guard let data = data as? [String] else { return } + if !data.isEmpty { + self.randomNames = data + self.tableView.reloadData() + } else { + let alert = UIAlertController(title: "친구가 없습니다", message: "", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "확인", style: .cancel, handler: nil)) + self.present(alert, animated: true, completion: nil) + } + + case .requestErr(let err): + print(err) + case .pathErr: + print("pathErr") + case .serverErr: + print("serverErr") + case .networkFail: + print("networkFail") + } + } + } +} + // MARK: - canvas 이용하기 import SwiftUI @available(iOS 13.0.0, *)