Skip to content

Commit

Permalink
🎨 coinlist 서버 통신 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
hansol committed Jun 10, 2021
1 parent 8a8f551 commit 1df4384
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 37 deletions.
21 changes: 21 additions & 0 deletions Coinone-iOS/Coinone-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
CC3370112667C552004F067E /* NetworkResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3370102667C552004F067E /* NetworkResult.swift */; };
CC3370132667C5D4004F067E /* CoinFilterService.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3370122667C5D4004F067E /* CoinFilterService.swift */; };
CC6608BE265450B0004DD7D2 /* PopupHsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC6608BD265450B0004DD7D2 /* PopupHsViewController.swift */; };
CC710E732671F47F00BC480D /* UIImageView+.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC710E722671F47F00BC480D /* UIImageView+.swift */; };
CC710E762671F4E500BC480D /* Kingfisher in Frameworks */ = {isa = PBXBuildFile; productRef = CC710E752671F4E500BC480D /* Kingfisher */; };
CC770CF8265396CF004095A6 /* UILabel+.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC770CF7265396CF004095A6 /* UILabel+.swift */; };
CC770CFC265397BD004095A6 /* UIFont+.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC770CFB265397BD004095A6 /* UIFont+.swift */; };
CC770CFE26540903004095A6 /* TopMenuCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC770CFD26540902004095A6 /* TopMenuCollectionViewCell.swift */; };
Expand Down Expand Up @@ -83,6 +85,7 @@
CC3370102667C552004F067E /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = "<group>"; };
CC3370122667C5D4004F067E /* CoinFilterService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoinFilterService.swift; sourceTree = "<group>"; };
CC6608BD265450B0004DD7D2 /* PopupHsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PopupHsViewController.swift; sourceTree = "<group>"; };
CC710E722671F47F00BC480D /* UIImageView+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+.swift"; sourceTree = "<group>"; };
CC770CF7265396CF004095A6 /* UILabel+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UILabel+.swift"; sourceTree = "<group>"; };
CC770CFB265397BD004095A6 /* UIFont+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+.swift"; sourceTree = "<group>"; };
CC770CFD26540902004095A6 /* TopMenuCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopMenuCollectionViewCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -113,6 +116,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
CC710E762671F4E500BC480D /* Kingfisher in Frameworks */,
CCA0ECDC264FA7DB001CC563 /* Then in Frameworks */,
CCA0ECE0264FA7EE001CC563 /* SnapKit in Frameworks */,
06A1A45591145692DA4F18C7 /* Pods_Coinone_iOS.framework in Frameworks */,
Expand Down Expand Up @@ -276,6 +280,7 @@
CCAA9A33264FAF68008E50BF /* UIView+.swift */,
CC770CF7265396CF004095A6 /* UILabel+.swift */,
CC770CFB265397BD004095A6 /* UIFont+.swift */,
CC710E722671F47F00BC480D /* UIImageView+.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -318,6 +323,7 @@
packageProductDependencies = (
CCA0ECDB264FA7DB001CC563 /* Then */,
CCA0ECDF264FA7EE001CC563 /* SnapKit */,
CC710E752671F4E500BC480D /* Kingfisher */,
);
productName = "Coinone-iOS";
productReference = CCA0ECC2264FA7A4001CC563 /* Coinone-iOS.app */;
Expand Down Expand Up @@ -349,6 +355,7 @@
packageReferences = (
CCA0ECDA264FA7DB001CC563 /* XCRemoteSwiftPackageReference "Then" */,
CCA0ECDE264FA7EE001CC563 /* XCRemoteSwiftPackageReference "SnapKit" */,
CC710E742671F4E500BC480D /* XCRemoteSwiftPackageReference "Kingfisher" */,
);
productRefGroup = CCA0ECC3264FA7A4001CC563 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -449,6 +456,7 @@
CC0A1AA026564FDA000B65B9 /* CoinListModel.swift in Sources */,
CC0A1A9726564240000B65B9 /* FilterCollectionViewCell.swift in Sources */,
E8EC22B2265445210027E8A8 /* MyTableViewCell.swift in Sources */,
CC710E732671F47F00BC480D /* UIImageView+.swift in Sources */,
CC0E23272652232700B982A0 /* MyViewController.swift in Sources */,
CCAA9A2E264FAEB9008E50BF /* CALayer+.swift in Sources */,
CC3370112667C552004F067E /* NetworkResult.swift in Sources */,
Expand Down Expand Up @@ -665,6 +673,14 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
CC710E742671F4E500BC480D /* XCRemoteSwiftPackageReference "Kingfisher" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/onevcat/Kingfisher";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 6.3.0;
};
};
CCA0ECDA264FA7DB001CC563 /* XCRemoteSwiftPackageReference "Then" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/devxoul/Then";
Expand All @@ -684,6 +700,11 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
CC710E752671F4E500BC480D /* Kingfisher */ = {
isa = XCSwiftPackageProductDependency;
package = CC710E742671F4E500BC480D /* XCRemoteSwiftPackageReference "Kingfisher" */;
productName = Kingfisher;
};
CCA0ECDB264FA7DB001CC563 /* Then */ = {
isa = XCSwiftPackageProductDependency;
package = CCA0ECDA264FA7DB001CC563 /* XCRemoteSwiftPackageReference "Then" */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "Kingfisher",
"repositoryURL": "https://github.com/onevcat/Kingfisher",
"state": {
"branch": null,
"revision": "44450a8f564d7c0165f736ba2250649ff8d3e556",
"version": "6.3.0"
}
},
{
"package": "SnapKit",
"repositoryURL": "https://github.com/SnapKit/SnapKit",
Expand Down
8 changes: 4 additions & 4 deletions Coinone-iOS/Coinone-iOS/Resource/APIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import Foundation

struct APIConstants {

static let baseURL = "http://3.37.86.93:5000"
static let baseURL = "http://52.79.251.168:5000"

static let addCoinURL = baseURL + "/api/add-coin"
static let addUserURL = baseURL + "/api/add-user/"
static let myCoinURL = baseURL + "/api/my-coin/"
static let coinListURL = baseURL + "/api/coin-list/"
static let addUserURL = baseURL + "/api/add-user "
static let myCoinURL = baseURL + "/api/my-coin"
static let coinListURL = baseURL + "/api/coin-list"
}

struct APIIndex {
Expand Down
29 changes: 29 additions & 0 deletions Coinone-iOS/Coinone-iOS/Resource/Extensions/UIImageView+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// UIImageView+.swift
// Coinone-iOS
//
// Created by 노한솔 on 2021/06/10.
//

import Foundation
import UIKit
import Kingfisher

extension UIImageView {
public func imageFromUrl(_ urlString: String?, defaultImgPath : String?) {

let tmpUrl : String?
if urlString == nil {
tmpUrl = ""
} else {
tmpUrl = urlString
}
if let url = tmpUrl, let defaultURL : String = defaultImgPath {
if url.isEmpty {
self.kf.setImage(with: URL(string: defaultURL), options: [.transition(ImageTransition.fade(0.5))])
} else {
self.kf.setImage(with: URL(string: url), options: [.transition(ImageTransition.fade(0.5))])
}
}
}
}
2 changes: 1 addition & 1 deletion Coinone-iOS/Coinone-iOS/Resource/NetworkResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

enum NetworkResult<T> {
case success
case success(T)
case requestErr
case pathErr
case serverErr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ struct CoinFilterService {
static let shared = CoinFilterService()

func sortCoin(sort: String,
ascending: String,
completion: @escaping (NetworkResult<Any>) -> Void) {
ascending: String,
completion: @escaping (NetworkResult<Any>) -> Void) {
let header: HTTPHeaders = ["Content-Type": "application/json"]
let url = APIConstants.coinListURL + APIIndex.init(index: .sort(sort, ascending)).index.getIndex()
let dataRequest = AF.request(url,
method: .get,
encoding: JSONEncoding.default,
headers: header)

dataRequest.responseData { dataResponse in
dump(dataResponse)
print(dataResponse.result)
switch dataResponse.result {
case .success:
guard let statusCode = dataResponse.response?.statusCode else {return}
Expand All @@ -36,14 +36,25 @@ struct CoinFilterService {
}

private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(SortedCoin.self, from: data) else {return .pathErr}
switch statusCode {
case 200: return .success
case 200:
return isValidData(data: data)
case 400: return .requestErr
case 500: return .serverErr
default: return .networkFail
}
}

private func isValidData(data : Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()

guard let decodedData = try? decoder.decode([SortedCoin].self, from: data)
else { return .pathErr}
// 우선 PersonDataModel 형태로 decode(해독)을 한번 거칩니다. 실패하면 pathErr

// 해독에 성공하면 Person data를 success에 넣어줍니다.
return .success(decodedData)

}
}

17 changes: 7 additions & 10 deletions Coinone-iOS/Coinone-iOS/Source/Cells/CoinListTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,32 @@ extension CoinListTableViewCell {
func layoutCoinFluctuationDegreeLabel() {
self.containerView.add(coinFluctuationDegreeLabel) {
$0.snp.makeConstraints {
$0.trailing.equalTo(self.coinTotalPriceLabel.snp.leading).offset(-24)
$0.trailing.equalTo(self.coinTotalPriceLabel.snp.trailing).offset(-70)
$0.centerY.equalTo(self.containerView.snp.centerY)
}
}
}
func layoutCoinCurrentPriceLabel() {
self.containerView.add(coinCurrentPriceLabel) {
$0.snp.makeConstraints {
$0.trailing.equalTo(self.coinFluctuationDegreeLabel.snp.leading).offset(-24)
$0.trailing.equalTo(self.coinTotalPriceLabel.snp.trailing).offset(-144)
$0.centerY.equalTo(self.containerView.snp.centerY)
}
}
}

func dataBind(coinLogoImageName: String, coinEnglishTitle: String, coinKoreanTitle: String, coinCurrentPrice: Float, riseOrDescent: String, percentage: Float, coinTotalPrice: Float) {
let formatter = NumberFormatter().then {
$0.numberStyle = .decimal
}
self.coinTitleLogoImageView.image = UIImage(named: coinLogoImageName)
func dataBind(coinLogoImageName: String, coinEnglishTitle: String, coinKoreanTitle: String, coinCurrentPrice: String, riseOrDescent: String, percentage: String, coinTotalPrice: String) {
self.coinTitleLogoImageView.imageFromUrl(coinLogoImageName, defaultImgPath: "https://sopt-8-coinone.s3.ap-northeast-2.amazonaws.com/KLAY.png")
self.coinTitleEnglishLabel.setLabel(text: coinEnglishTitle, textColor: .black, font: .notoSansKRBoldFont(fontSize: 14))
self.coinTitleKoreanLabel.setLabel(text: coinKoreanTitle, textColor: .coinGray, font: .notoSansKRMediumFont(fontSize: 10))
if riseOrDescent == "+" {
self.coinCurrentPriceLabel.setLabel(text: "\(formatter.string(from: NSNumber(value: coinCurrentPrice))!)", textColor: .textRed, font: .boldSystemFont(ofSize: 14))
self.coinCurrentPriceLabel.setLabel(text: coinCurrentPrice, textColor: .textRed, font: .boldSystemFont(ofSize: 14))
self.coinFluctuationDegreeLabel.setLabel(text: "\(riseOrDescent)\(percentage)%", textColor: .textRed, font: .systemFont(ofSize: 14, weight: .regular))
}
else {
self.coinCurrentPriceLabel.setLabel(text: "\(formatter.string(from: NSNumber(value: coinCurrentPrice))!)", textColor: .mainBlue, font: .boldSystemFont(ofSize: 14))
self.coinCurrentPriceLabel.setLabel(text: coinCurrentPrice, textColor: .mainBlue, font: .boldSystemFont(ofSize: 14))
self.coinFluctuationDegreeLabel.setLabel(text: "\(riseOrDescent)\(percentage)%", textColor: .mainBlue, font: .systemFont(ofSize: 14, weight: .regular))
}
self.coinTotalPriceLabel.setLabel(text: "\(formatter.string(from: NSNumber(value:coinTotalPrice))!)", textColor: .coinGray, font: .systemFont(ofSize: 14, weight: .regular))
self.coinTotalPriceLabel.setLabel(text: "\(coinTotalPrice.dropLast(8))", textColor: .coinGray, font: .systemFont(ofSize: 14, weight: .regular))
}
}
6 changes: 3 additions & 3 deletions Coinone-iOS/Coinone-iOS/Source/Models/CoinListModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ struct CoinListModel {
var coinLogoImageName: String
var coinEnglishTitle: String
var coinKoreanTitle: String
var coinCurrentPrice: Float
var coinCurrentPrice: String
var riseOrDescent: String
var percentage: Float
var coinTotalPrice: Float
var percentage: String
var coinTotalPrice: String
}
7 changes: 1 addition & 6 deletions Coinone-iOS/Coinone-iOS/Source/Models/SortedCoinModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import Foundation

// MARK: - SortedCoin
struct SortedCoin: Codable {
let success: Bool
let data: [SortedData]
}

// MARK: - SortedData
struct SortedData: Codable {
let id: String
let coinLogoImage: String
let coinEnglishTitle, coinKoreanTitle, coinCurrentPrice, riseOrDescent: String
Expand All @@ -28,3 +22,4 @@ struct SortedData: Codable {
case v = "__v"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by 노한솔 on 2021/05/20.
//
import Alamofire
import SnapKit
import Then
import UIKit
Expand All @@ -17,6 +18,7 @@ class CoinListCollectionReusableView: UICollectionReusableView {
// MARK: - LifeCycles
override func awakeFromNib() {
super.awakeFromNib()
setInitialList()
register()
layout()
self.coinListTableView.delegate = self
Expand All @@ -28,17 +30,17 @@ class CoinListCollectionReusableView: UICollectionReusableView {
var coinModel: [CoinListModel] = [CoinListModel(coinLogoImageName: "coinLogo",
coinEnglishTitle: "XRP",
coinKoreanTitle: "리플",
coinCurrentPrice: 1625,
coinCurrentPrice: "1625",
riseOrDescent: "-",
percentage: 0.37,
coinTotalPrice: 2059),
percentage: "0.37",
coinTotalPrice: "2059"),
CoinListModel(coinLogoImageName: "coinLogo",
coinEnglishTitle: "XRP",
coinKoreanTitle: "리플",
coinCurrentPrice: 1625,
coinCurrentPrice: "1625",
riseOrDescent: "+",
percentage: 0.37,
coinTotalPrice: 2059)]
percentage: "0.37",
coinTotalPrice: "2059")]
}

// MARK: - Extensions
Expand All @@ -61,6 +63,35 @@ extension CoinListCollectionReusableView {
}
}
}
func setInitialList() {
CoinFilterService.shared.sortCoin(sort: "total-price", ascending: "-1") {
(networkResult) in
switch(networkResult) {
case .success(let data):
var tempCoinList:[CoinListModel] = []
guard let data = data as? [SortedCoin] else {return}
for i in 0..<data.count {
tempCoinList.append(CoinListModel(coinLogoImageName: data[i].coinLogoImage,
coinEnglishTitle: data[i].coinEnglishTitle,
coinKoreanTitle: data[i].coinKoreanTitle,
coinCurrentPrice: data[i].coinCurrentPrice,
riseOrDescent: data[i].riseOrDescent,
percentage: data[i].percentage,
coinTotalPrice: data[i].coinTotalPrice))
}
self.coinModel = tempCoinList
self.coinListTableView.reloadData()
case .networkFail:
print("networkFail")
case .pathErr:
print("pathErr")
case .requestErr:
print("requestErr")
case .serverErr:
print("serverErr")
}
}
}
}

extension CoinListCollectionReusableView: UITableViewDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by soyeon on 2021/05/15.
//
import Alamofire
import UIKit
import SnapKit

Expand Down Expand Up @@ -446,7 +447,8 @@ extension GeoraesoVC: UITableViewDataSource {
cell.contentView.backgroundColor = .tableViewGray

let data = stockList[indexPath.row]
cell.setData(logoPath: data.title, title: data.title, subTitle: data.subTitle, curValue: data.curValue, rate: data.rate, transPrice: data.transPrice)
// cell.setData(logoPath: data.title, title: data.title, subTitle: data.subTitle, curValue: data.curValue, rate: data.rate, transPrice: data.transPrice)
// cell.setData(coinLogoImageName: data.title, coinEnglishTitle: data.title, coinKoreanTitle: data.subTitle, coinCurrentPrice: data.curValue, riseOrDescent: data.rate, percentage: data.transPrice, coinTotalPrice: <#T##Float#>)
return cell
}
}
Expand Down

0 comments on commit 1df4384

Please sign in to comment.