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

Ringo-67 친구목록 불러오기 기능 #28

Merged
merged 2 commits into from
Feb 8, 2024
Merged
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
6 changes: 5 additions & 1 deletion iOS/Ringo/Ringo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -30,9 +31,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
940C25632B73D20900E069D0 /* ConnectionCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionCollectionViewCell.swift; sourceTree = "<group>"; };
940C25652B74EF4D00E069D0 /* FriendService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FriendService.swift; sourceTree = "<group>"; };
94470A922B7163E900F0A942 /* Model.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Model.swift; sourceTree = "<group>"; };
94470A942B71680100F0A942 /* SigninService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SigninService.swift; sourceTree = "<group>"; };
940C25632B73D20900E069D0 /* ConnectionCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionCollectionViewCell.swift; sourceTree = "<group>"; };
94470A962B717DC300F0A942 /* ConnectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionViewController.swift; sourceTree = "<group>"; };
945603122B6AC07D002F4B33 /* TabBarViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarViewController.swift; sourceTree = "<group>"; };
945603142B6AC22A002F4B33 /* ContactsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
Binary file not shown.
66 changes: 66 additions & 0 deletions iOS/Ringo/Ringo/FriendService.swift
Original file line number Diff line number Diff line change
@@ -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<Any>) -> 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<Any> {
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<Any> {
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)
}
}
37 changes: 35 additions & 2 deletions iOS/Ringo/Ringo/Screens/ContactsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ContactsViewController: UIViewController {

let contactsTableViewCell = ContactsTableViewCell.identifier

let randomNames = ["민준", "서준", "예준", "도윤", "시우", "주원", "하준", "지호", "지후", "준서", "준우", "현우", "도현", "지훈", "건우", "우진", "선우", "서진", "민재", "현준", "연우", "유준", "정우", "승우", "승현", "시윤", "준혁", "은우", "지환", "승민", "지우", "유찬", "윤우", "민성", "준영", "시후", "진우", "지수", "서연", "서윤", "지우", "서현", "민서", "하은", "하윤", "윤서", "지유", "지민", "채원", "지윤", "은서", "수아", "다은", "예은", "지아", "수빈", "소율", "예린", "예원", "지원", "소윤", "지안", "하린", "시은", "유진", "채은"]
var randomNames = [String]()

let searchController = UISearchController()
var tableView: UITableView!
Expand All @@ -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(){
Expand Down Expand Up @@ -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, *)
Expand Down