為了完成三大法人的比重,我們需要兩個數值
import Foundation
protocol MajorInvestorsModelDelegate: AnyObject {
func didRecieve(investors: [MajorInvestor], error: Error?)
func didRecieve(dailyTradingInfo: [TwMarketTradingInfo], error: Error?)
}
class MajorInvestorsModel {
weak var delegate: MajorInvestorsModelDelegate?
var majorInvestors = [MajorInvestor]()
var dailyTradingInfo = [TwMarketTradingInfo]()
private lazy var threeMajorManager: ThreeMajorInvestorsManager = {
let manager = ThreeMajorInvestorsManager()
return manager
}()
private lazy var marketManager: TwMarketTradingInfoManager = {
let manager = TwMarketTradingInfoManager()
return manager
}()
func requestMajorInvestorsAndTwMarket() {
threeMajorManager.requestInvestorsInfo { [weak self] investors, error in
self?.majorInvestors = investors
self?.delegate?.didRecieve(investors: investors, error: error)
}
marketManager.requestTwMarketDailyTradingInfo(date: Date()) { [weak self] dailyTradingInfo, error in
self?.dailyTradingInfo = dailyTradingInfo
self?.delegate?.didRecieve(dailyTradingInfo: dailyTradingInfo, error: error)
}
}
}
接下來,在 VC 上拉出兩個 UIButton,一個發動拉取資料,另一個將下一個 vc push 進來,並把拉取的資料 array 傳進去。
這個 VC 還有個 Label 顯示當下的狀態,可以讓你知道拉下來的資料狀態。
MajorInvestorsViewController VC 的程式碼如下
import UIKit
class MajorInvestorsViewController: UIViewController {
@IBOutlet weak var stateLabel: UILabel!
private lazy var model: MajorInvestorsModel = {
let model = MajorInvestorsModel()
model.delegate = self
return model
}()
// MARK: - life cycle
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - IBAction
@IBAction func requestMajorInvestorsButtonDidTap(_ sender: Any) {
model.requestMajorInvestorsAndTwMarket()
}
@IBAction func pushToThreeMajorInvestorButtonDidTap(_ sender: Any) {
guard let storyboard = self.storyboard,
let vc = storyboard.instantiateViewController(withIdentifier: "MajorInvestorsDashboardViewController") as? MajorInvestorsDashboardViewController else {
return
}
vc.dailyTradingInfo = model.dailyTradingInfo
vc.majorInvestors = model.majorInvestors
navigationController?.pushViewController(vc, animated: true)
}
}
extension MajorInvestorsViewController: MajorInvestorsModelDelegate {
func didRecieve(investors: [MajorInvestor], error: Error?) {
if let error = error {
print("you got error during fetch investor info: \(error.localizedDescription)")
return
}
print("major investors: \(investors)")
stateLabel.text = "\(investors)"
}
func didRecieve(dailyTradingInfo: [TwMarketTradingInfo], error: Error?) {
if let error = error {
print("you got error during fetch daily trading: \(error.localizedDescription)")
return
}
print("daily trading info: \(dailyTradingInfo)")
}
}
傳下去的 VC - MajorInvestorsDashboardViewController 目前程式碼很單純,只有兩個 array。後續的計算會在下一篇開始寫。
import UIKit
class MajorInvestorsDashboardViewController: UIViewController {
var majorInvestors = [MajorInvestor]()
var dailyTradingInfo = [TwMarketTradingInfo]()
override func viewDidLoad() {
super.viewDidLoad()
}
}
台股申購日曆
IT鐵人賽Demo App
下方是這次 D1 ~ D12 的完成品,可以下載來試
App Store - 台股申購日曆