宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
1
2
3
4
5
6
7
8
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
//  CarDetailCarCommentVC.swift
//  OKProject
//
//  Created by 无故事王国 on 2022/5/10.
//  Copyright © 2022 yangwang. All rights reserved.
//
 
import UIKit
import DZNEmptyDataSet
 
class CarDetailCarCommentVC: YYViewController{
 
    @IBOutlet weak var commentView: UIView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var profileImg: UIImageView!
    @IBOutlet weak var commentBottomCons: NSLayoutConstraint!
    var scrollView:UIScrollView?
 
    let viewModel = CommentOrderViewModel()
 
    var orderId:Int?
    var type:CommonStyle = .sell //类型(1=租车,2=卖车,3=招聘)
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
        guard orderId != nil else {return}
 
        viewModel.orderId.accept(orderId!)
        viewModel.type.accept(type.rawValue)
 
        viewModel.configure(tableView: tableView)
        tableView.beginRefreshing()
 
        commentView.isUserInteractionEnabled = true
        profileImg.load(url: readUser().avatar, placeHolder: UIImage(named: "logo")!)
 
        scrollView?.delegate = self
 
        let a = Double((scrollView?.contentSize.height ?? 0)) - Double(ScreenHeight) - Double((scrollView?.contentOffset.y ?? 0))
        commentBottomCons.constant = a
        print("--->1 \(a)")
 
        assert(scrollView != nil, "需要传adapterVC.scrollView")
    }
 
    override func bindRx() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(openCommentAction))
        commentView.addGestureRecognizer(tap)
    }
 
    @objc func openCommentAction(){
 
        guard orderId != nil else { alert(text: "数据错误");return}
 
        PublishCommentView.show({[weak self]text in
            guard let weakSelf = self else { return }
 
            let type = weakSelf.type.rawValue
            let orderId = weakSelf.orderId!
 
            APIManager.shared.provider.rx.request(.insertComment(commentId: nil, content: text, orderId: orderId, type: type, replyUserId: nil)).map(YYModel<Nothing>.self).validate().subscribe(onSuccess: {data in
                weakSelf.tableView.beginRefreshing()
            }) { error in
                alert(text: "评论失败")
            }.disposed(by: weakSelf.disposeBag)
        })
    }
 
    override func setupViews() {
        super.setupViews()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(CommentContentView.self, forHeaderFooterViewReuseIdentifier: "_CommentContentView")
        tableView.register(UINib(nibName: "CommentReplyTCell", bundle: nil), forCellReuseIdentifier: "_CommentReplyTCell")
    }
 
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        commentView.addShadow(ofColor: UIColor(hexString: "#E6E6E6")!.withAlphaComponent(0.50), radius: 4, offset: CGSize(width: 0, height: -2), opacity: 1)
    }
}
 
extension CarDetailCarCommentVC:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if tableView == scrollView{return}
        let a = scrollView.contentSize.height - ScreenHeight - scrollView.contentOffset.y
        print("--->2 \(a)")
        if a < 0{return}
        commentBottomCons.constant = a
    }
}
 
extension CarDetailCarCommentVC:UITableViewDelegate{
 
}
 
extension CarDetailCarCommentVC:UITableViewDataSource{
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let m = viewModel.dataSource.value[section]
        var headView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "_CommentContentView") as? CommentContentView
        if headView == nil{
            headView = CommentContentView(reuseIdentifier: "_CommentContentView")
        }
        headView?.section = section
        headView?.setCommentModel(m)
        headView?.showAllDelegate.delegate(on: self, block: {_,_ in
            m.showAll = true
            tableView.reloadData()
        })
 
        headView?.replyDelegate.delegate(on: self, block: {_,_ in
            tableView.mj_header?.beginRefreshing()
        })
 
        headView?.replyDelegate.delegate(on: self, block: {_,_ in
            tableView.mj_header?.beginRefreshing()
        })
 
        headView?.showMoreDelegate.delegate(on: self) { (self, arge) in
            let indexSet = IndexSet.Element(bitPattern: UInt(bitPattern: arge))
            tableView.reloadSections(IndexSet(integer: indexSet), with: .fade)
        }
 
        return headView!
    }
 
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
 
        var h:Double = 55
        let m = viewModel.dataSource.value[section]
 
        if m.replyCommentList.count > 10{
            h += 32
        }
        if m.showAll{
            h += m.contentHeight
        }else{
            h += 51
        }
        return h
    }
 
 
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.dataSource.value.count
    }
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let m = viewModel.dataSource.value[section]
        if m.showOther{
            return m.replyCommentList.count
        }
        return 0
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let m = viewModel.dataSource.value[indexPath.section].replyCommentList[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "_CommentReplyTCell") as! CommentReplyTCell
        cell.setCommentModel(m)
        cell.replyDelegate.delegate(on: self) {_,_ in
            tableView.mj_header?.beginRefreshing()
        }
        return cell
    }
}