This is the 11th day of my participation in the August More Text Challenge. For details, see:August is more challenging

preface

Listen to the story (audio playback), but this can only play a small part, because the url to get the audio playback needs to be signed, do not know the signature rules, so only those that do not need to be signed.

Story list

1, create a new RecAudioListViewController, exposed three properties

var atype: String = ""
var id: String = ""
var outerid: String = ""
Copy the code

2, the above introduction is fixed in the navigation bar, a new RecAudioHeaderView, then RecAudioListViewController initialization

private lazy var headerView: RecAudioHeaderView = {
    RecAudioHeaderView()
}()
Copy the code

3. Below is tableView

private lazy var tableView: UITableView = {
    let view = UITableView(frame: .zero, style: .plain)
    view.separator(left: space, right: 0)
    view.plainFooterView()
    view.rowHeight = 55.fit
    view.delegate = self
    view.dataSource = self
    view.register(cellWithClass: RecAudioListCell.self)
    return view
}()

extension RecAudioListViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: RecAudioListCell.self)
        cell.countLabel.text = "\(indexPath.row + 1)"
        cell.update(dataSource[indexPath.row])
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = AudioPlayerViewController.share
        vc.atype = atype
        vc.audioModel = audioModel
        vc.dataSource = dataSource
        vc.playerIndex = indexPath.row
        vc.didAudioItemModeChange = { [weak self] in
            guard let `self` = self else { return }
            self.refreshSeletedData()
        }
        vc.playerURL()
        push(vc)
    }
}
Copy the code

4. Request data

The extension RecAudioListViewController {/ / / if atype is 2, only need to request this one interface, Private func requestData() {network.rec.audioalbum (id: id, page: page) .request() .responseData(AudioModel.self) { [weak self] (model) in guard let `self` = self else { return } /// End of mj footer refresh self. TableView. EndFooterRefresh () / / / update details information self. The headerView. Update (model. The result. The album) self. AudioModel = model.result if self.atype == "2" { if self.tableView.isHeaderRefresh { self.dataSource.removeAll() } self.tableView.endHeaderRefresh() self.dataSource.append(contentsOf: model.result.items) self.refreshSeletedData() } } failure: { [weak self] (error) in guard let `self` = self else { return } self.tableView.endHeaderRefresh() self.tableView.endFooterRefresh() Toast.show(info: }} private func requestAudioList() {net.rec.audiolist (id: id)}} private func requestAudioList() {net.rec.audiolist (id: id) outerid, page: page) .request() .responseAudioData([AudioItemModel].self) { [weak self] (model) in guard let `self` = self else { return } if self.tableView.isHeaderRefresh { self.dataSource.removeAll() } self.tableView.endHeaderRefresh() self.tableView.endFooterRefresh() self.dataSource.append(contentsOf: model.data) self.refreshSeletedData() } failure: { [weak self] (error) in guard let `self` = self else { return } self.tableView.endHeaderRefresh() self.tableView.endFooterRefresh() Toast.show(info: error.errorMessage) } } }Copy the code

5. Processing the selected data status

/// Refresh the data being played, because the audio playback page can be played back to the home page, so use the singleton /// compare the playback audio with the list data, if the ID is the same, Set up an isSelected = false private func refreshSeletedData () {let item = AudioPlayerViewController. Share. PlayerItemModel dataSource = dataSource.map { model -> AudioItemModel in var m = model if m.id == item.id { m.isSelected = true } else {  m.isSelected = false } return m } tableView.reloadData() }Copy the code

The list is relatively simple, and in the next article we’ll start to encapsulate an audio playback capability