|
| 1 | +// |
| 2 | +// DataHubViewController.swift |
| 3 | +// ios-app-bootstrap |
| 4 | +// |
| 5 | +// Created by xdf on 05/02/2018. |
| 6 | +// Copyright © 2018 open source. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import Logger |
| 11 | +import Alamofire |
| 12 | + |
| 13 | +class DataHubViewController: UIViewController { |
| 14 | + let logger = Logger() |
| 15 | + let textView = UITextView() |
| 16 | + |
| 17 | + override func viewDidLoad() { |
| 18 | + super.viewDidLoad() |
| 19 | + initView() |
| 20 | + } |
| 21 | + |
| 22 | + func initView() { |
| 23 | + navigationItem.title = Utils.Path.basename(#file) |
| 24 | + view.backgroundColor = UIColor.white |
| 25 | + |
| 26 | + let button = UIButton() |
| 27 | + button.backgroundColor = Utils.getRGB(Const.COLOR_1) |
| 28 | + button.setTitle("request", for: UIControlState()) |
| 29 | + button.translatesAutoresizingMaskIntoConstraints = false |
| 30 | + button.layer.cornerRadius = 2 |
| 31 | + button.titleLabel!.font = UIFont(name: "Helvetica",size: 16) |
| 32 | + button.addTarget(self, action: #selector(DataHubViewController.doRequest(_:)), for: .touchUpInside) |
| 33 | + view.addSubview(button) |
| 34 | + |
| 35 | + let switchbutton = UIButton() |
| 36 | + switchbutton.backgroundColor = Utils.getRGB(Const.COLOR_1) |
| 37 | + switchbutton.setTitle("swich scene", for: UIControlState()) |
| 38 | + switchbutton.translatesAutoresizingMaskIntoConstraints = false |
| 39 | + switchbutton.layer.cornerRadius = 2 |
| 40 | + switchbutton.titleLabel!.font = UIFont(name: "Helvetica",size: 16) |
| 41 | + switchbutton.addTarget(self, action: #selector(DataHubViewController.switchDataHubScene(_:)), for: .touchUpInside) |
| 42 | + view.addSubview(switchbutton) |
| 43 | + |
| 44 | + textView.text = "" |
| 45 | + textView.backgroundColor = UIColor.lightGray |
| 46 | + textView.textAlignment = .left |
| 47 | + textView.translatesAutoresizingMaskIntoConstraints = false |
| 48 | + textView.font = UIFont.systemFont(ofSize: 12) |
| 49 | + view.addSubview(textView) |
| 50 | + |
| 51 | + let views:Dictionary<String, AnyObject>=[ |
| 52 | + "textView": textView, |
| 53 | + "button": button, |
| 54 | + "switchbutton": switchbutton |
| 55 | + ] |
| 56 | + |
| 57 | + view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-100-[button(<=30)]-10-[switchbutton(<=30)]-10-[textView(<=200)]-300-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)) |
| 58 | + view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[textView]-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)) |
| 59 | + view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-120-[button]-120-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)) |
| 60 | + view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-120-[switchbutton]-120-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)) |
| 61 | + } |
| 62 | + |
| 63 | + func doRequest(_ sender: UIButton) { |
| 64 | + let parameters: Parameters = [ |
| 65 | + "foo": "bar" |
| 66 | + ] |
| 67 | + Alamofire |
| 68 | + .request(Utils.dataHubAdapter("testApi"), method: .post, parameters: parameters, encoding: URLEncoding.default) |
| 69 | + .responseJSON { response in |
| 70 | + debugPrint(response) |
| 71 | + if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) { |
| 72 | + self.textView.text = "result:\n\(utf8Text)" |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + func switchDataHubScene(_ sender: UIButton) { |
| 78 | + |
| 79 | + // TODO switch scene |
| 80 | + } |
| 81 | +} |
0 commit comments