D26 - 用 Swift 和公开资讯,打造投资理财的 Apps { 三大法人成交比重实作.1 }

为了完成三大法人的比重,我们需要两个数值

  • 三大法人成交金额
  • 台股日成交金额 - 这一项在前面已经完成了,直接使用 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


<<:  Day21 - 预览页加入按纽

>>:  javascript(DOM调整属性与样式&计时器)(DAY23)

Day 16 JavaScript boxing vs unboxing

boxing: 封装可以让原始型态的资料暂时转成物件,这样他才可以使用属性或方法。 遇到使用字面值(...

[Day 2] 一个非同步案例 httpServer

前言 或许有些人会有所困惑, 同步非同步的实践难在哪里, 为甚麽要一直巴拉巴拉, 但事实上, 非同步...

27. Tech leader的重要战略

前言 这篇的讲者很nice,直接讲了这篇演讲很适合给这几种人看 刚成为TL 还不是TL但你觉得你会...

[LeetCode30] Day26 - 1106. Parsing A Boolean Expression

题目 给一个型别为string的boolean expression,回传结果(true or fa...

30-11 之Domain Layer - Transaction Script

接下来这一篇文章开始,我们要进入所谓 3-Tier 的『 Domain 』的部份,这个层级基本上就是...