Blame view

App/commons/CellMucChupWithCamera.swift 7.28 KB
1341bf603   Trịnh Văn Quân   version 1.1
1
2
3
  import Foundation
  import UIKit
  import GeneralUtils
defd9642e   Trịnh Văn Quân   version 1.2
4
  import Alamofire
1341bf603   Trịnh Văn Quân   version 1.1
5
6
7
  
  class CellMucChupWithCamera: BaseTableViewCellUI {
      var tvTitle: UILabel!, imv: UIImageView!
defd9642e   Trịnh Văn Quân   version 1.2
8
      var shootItem: ShootItem!
1341bf603   Trịnh Văn Quân   version 1.1
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  
      static func registerClass(tableView: UITableView, forCellReuseIdentifier: String) {
          tableView.register(CellMucChupWithCamera.self, forCellReuseIdentifier: forCellReuseIdentifier)
      }
  
      override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
          super.init(style: style, reuseIdentifier: reuseIdentifier)
          initView()
      }
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          initView()
      }
  
      private func initView() {
          let stackView = UIStackView()
          stackView.axis = UILayoutConstraintAxis.horizontal
          stackView.translatesAutoresizingMaskIntoConstraints = false
          self.contentView.addSubview(stackView)
          NSLayoutConstraint.activate([stackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
                                       stackView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 8),
                                       stackView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -8),
                                       stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8)])
          tvTitle = UILabel()
          tvTitle.numberOfLines = 0
          stackView.addArrangedSubview(tvTitle)
          tvTitle.isUserInteractionEnabled = true
          let tapTitle = UITapGestureRecognizer(target: self, action: #selector(CellMucChupWithCamera.titleClick))
          tvTitle.addGestureRecognizer(tapTitle)
  
          imv = UIImageView()
          imv.contentMode = .scaleAspectFit
          imv.isUserInteractionEnabled = true
          stackView.addArrangedSubview(imv)
          imv.widthAnchor.constraint(equalToConstant: 80).isActive = true
          imv.heightAnchor.constraint(equalToConstant: 60).isActive = true
          imv.image = #imageLiteral(resourceName:"camera_black_ic").imageWithInsets(insetDimen: 10) ?? #imageLiteral(resourceName:"camera_black_ic")
          let tap = UITapGestureRecognizer(target: self, action: #selector(CellMucChupWithCamera.btnImvClick))
          imv.addGestureRecognizer(tap)
      }
  
      func titleClick() {
fbd62afcf   Trịnh Văn Quân   version 1.2.2
52
53
          guard let viewController = self.viewController as? VCShootItemList else { return }
          let cTruong = viewController.cTruong;
defd9642e   Trịnh Văn Quân   version 1.2
54
          if let imgs = shootItem.imgs, imgs.count > 0 {
fbd62afcf   Trịnh Văn Quân   version 1.2.2
55
              VCPageImgs.openController(viewController, imgObjs: imgs, cTruong: cTruong!, shootItem: self.shootItem);
defd9642e   Trịnh Văn Quân   version 1.2
56
          }
1341bf603   Trịnh Văn Quân   version 1.1
57
58
59
      }
  
      func btnImvClick() {
fbd62afcf   Trịnh Văn Quân   version 1.2.2
60
61
62
63
64
65
66
67
          guard let viewController = self.viewController as? VCShootItemList else { return }
  //        if let imgs = shootItem.imgs, imgs.count > 0 {
  //            DialogUtils.showQuestion(titleParam: <#T##String?##Swift.String?#>, messageParam: <#T##String?##Swift.String?#>, type: <#T##TypeDialog##GeneralUtils.TypeDialog#>, yesTitle: <#T##String##Swift.String#>, noTitle: <#T##String##Swift.String#>, yesAction: <#T##@escaping () -> Void##@escaping () -> Swift.Void#>)
  //        }
          let cTruong = viewController.cTruong;
          let userData = BoardObj(cTruong: cTruong, shootItem: self.shootItem);
  
          let cameraViewController = CameraViewController(croppingEnabled: false, allowsLibraryAccess: true, userData: userData) { image, asset in
defd9642e   Trịnh Văn Quân   version 1.2
68
              viewController.dismiss(animated: true, completion: nil)
1341bf603   Trịnh Văn Quân   version 1.1
69
              guard let image = image else { return }
defd9642e   Trịnh Văn Quân   version 1.2
70
71
72
              self.imv?.image = image
              guard let id = self.shootItem.id, let data = UIImageJPEGRepresentation(image, 1.0) else { return }
              let signboard = self.shootItem?.signboard ?? 1
fbd62afcf   Trịnh Văn Quân   version 1.2.2
73
              self.uploadImage(id: id, signboard: signboard, date: self.shootItem?.date, note: self.shootItem.note, data: data)
1341bf603   Trịnh Văn Quân   version 1.1
74
75
76
77
78
79
80
          }
  
          viewController.present(cameraViewController, animated: true, completion: nil)
      }
  
      override func configCellWithData(baseObj: Any, index: Int) {
          super.configCellWithData(baseObj: baseObj, index: index)
defd9642e   Trịnh Văn Quân   version 1.2
81
82
83
84
85
86
87
88
89
90
          shootItem = baseObj as! ShootItem
          tvTitle.text = shootItem.name
          if let imgs = shootItem.imgs, imgs.count > 0, let imgUrl = imgs[0].url {
              setImageUrl(imv: imv, imageUrl: imgUrl)
          } else {
              imv.image = #imageLiteral(resourceName:"camera_black_ic").imageWithInsets(insetDimen: 10) ?? #imageLiteral(resourceName:"camera_black_ic")
          }
      }
  
      //region upload =======
fbd62afcf   Trịnh Văn Quân   version 1.2.2
91
92
      //TODO: Them 2 field date: String, note: String?
      fileprivate func uploadImage(id: Int, signboard: Int, date: Double?, note: String?, data: Data) {
defd9642e   Trịnh Văn Quân   version 1.2
93
94
95
96
97
          let tvProgress = SwiftOverlays.showBlockingWaitOverlayWithText(LocalizedString("Uploading") + ".....")
          Alamofire.upload(multipartFormData: { (multipartFormData: MultipartFormData) in
              multipartFormData.append(data, withName: "file", fileName: "file_\(DataTypeUtils.getCurrentTimeSecond()).jpg", mimeType: "image/jpeg")
              multipartFormData.append("\(id)".data(using: String.Encoding.utf8)!, withName: "idShooting")
              multipartFormData.append("\(signboard)".data(using: String.Encoding.utf8)!, withName: "signboard")
fbd62afcf   Trịnh Văn Quân   version 1.2.2
98
99
100
101
102
103
104
105
              if let note = note {
                  multipartFormData.append(note.data(using: String.Encoding.utf8)!, withName: "note")
              }
              if let date = date {
                  let dateInt = Int(date)
                  println("Upload: date: \(dateInt)")
                  multipartFormData.append("\(dateInt)".data(using: String.Encoding.utf8)!, withName: "date")
              }
defd9642e   Trịnh Văn Quân   version 1.2
106
107
108
109
          }, to: Constants.PathManager.ROOT_SERVER + "api/board/store", method: .post, encodingCompletion: { (encodingResult: SessionManager.MultipartFormDataEncodingResult) in
              switch encodingResult {
              case .success(let upload, _, _):
                  upload.uploadProgress(closure: { progress in
fbd62afcf   Trịnh Văn Quân   version 1.2.2
110
                              CommonUtils.excuteOnMainThread({ tvProgress.text = (LocalizedString("Uploading") + " \(Int(progress.fractionCompleted * 100))%") })
defd9642e   Trịnh Văn Quân   version 1.2
111
112
113
114
                          })
                          .responseString { response in
                              SwiftOverlays.removeAllBlockingOverlays()
                              debugPrint("SUCCESS RESPONSE: \(response.result.value)")
fbd62afcf   Trịnh Văn Quân   version 1.2.2
115
                              if let value = response.result.value {
defd9642e   Trịnh Văn Quân   version 1.2
116
                                  let json = JSON.parse(value)
fbd62afcf   Trịnh Văn Quân   version 1.2.2
117
                                  if let isSuccess = json["isSuccess"].bool, isSuccess, let url = json["url"].string {
defd9642e   Trịnh Văn Quân   version 1.2
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                                      self.shootItem.addImg(img: ImgObj(url: url, signboard: signboard, date: nil, note: nil))
                                      CommonUtils.showToastLong(text: LocalizedString("successful"))
                                      return
                                  }
                              }
                              self.handleError()
                          }
              case .failure(let encodingError):
                  SwiftOverlays.removeAllBlockingOverlays()
                  self.handleError()
                  print("ERROR RESPONSE: \(encodingError)")
              }
          })
      }
fbd62afcf   Trịnh Văn Quân   version 1.2.2
132

defd9642e   Trịnh Văn Quân   version 1.2
133
134
      fileprivate func handleError() {
          //DialogUtils.showQuestion(messageParam: <#T##String?##Swift.String?#>, yesAction: <#T##@escaping () -> Void##@escaping () -> Swift.Void#>)
1341bf603   Trịnh Văn Quân   version 1.1
135
      }
defd9642e   Trịnh Văn Quân   version 1.2
136
      //endregion
1341bf603   Trịnh Văn Quân   version 1.1
137
  }