iT邦幫忙

2021 iThome 鐵人賽

DAY 26
0
Mobile Development

使用 Swift 和公開資訊,打造投資理財的 Apps系列 第 27

D26 - 用 Swift 和公開資訊,打造投資理財的 Apps { 三大法人成交比重實作.1 }

  • 分享至 

  • xImage
  •  

為了完成三大法人的比重,我們需要兩個數值

  • 三大法人成交金額
  • 台股日成交金額 - 這一項在前面已經完成了,直接使用 TwMarketTradingInfoManager 即可。
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 顯示當下的狀態,可以讓你知道拉下來的資料狀態。

https://ithelp.ithome.com.tw/upload/images/20211005/20140622bRCLrR2diP.png

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 - 台股申購日曆

https://ithelp.ithome.com.tw/upload/images/20210924/20140622ypOBM0tgrZ.png


上一篇
D25 - 用 Swift 和公開資訊,打造投資理財的 Apps { 三大法人成交比重 資料分析 }
下一篇
D27 - 用 Swift 和公開資訊,打造投資理財的 Apps { 三大法人成交比重實作.2 }
系列文
使用 Swift 和公開資訊,打造投資理財的 Apps37
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言