| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- //
- // LNJoinUsVoiceExamplePanel.swift
- // Gami
- //
- // Created by OneeChan on 2026/1/22.
- //
- import Foundation
- import UIKit
- import SnapKit
- class LNJoinUsVoiceExamplePanel: LNPopupView {
- private let titleLabel = UILabel()
- private let descLabel = UILabel()
-
- private let playIcon = UIImageView()
- private let voiceWaveView = LNVoiceWaveView()
- private let playDurationLabel = UILabel()
-
- private var curItem: LNSkillFieldExample?
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- setupViews()
-
- LNEventDeliver.addObserver(self)
- }
-
- func update(_ example: LNSkillFieldExample) {
- curItem = example
-
- updateDuration()
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- extension LNJoinUsVoiceExamplePanel: LNVoicePlayerNotify {
- func onAudioUpdateDuration(path: String, cur: TimeInterval, total: TimeInterval) {
- guard curItem?.value == path else { return }
-
- let remain = Int(total - cur)
- playDurationLabel.text = "\(remain)“"
- }
-
- func onAudioStopPlay(path: String) {
- guard curItem?.value == path else { return }
-
- updateDuration()
- playIcon.image = .icVoicePlay
- voiceWaveView.stopAnimate()
- }
-
- func onAudioStartPlay(path: String) {
- guard curItem?.value == path else { return }
-
- playIcon.image = .icVoicePause
- voiceWaveView.startAnimate()
- }
- }
- extension LNJoinUsVoiceExamplePanel {
- private func updateDuration() {
- guard let url = curItem?.value else { return }
- LNVoiceResourceManager.shared.getRemoteAudioDuration(urlStr: url) { [weak self] duration, _ in
- guard let self else { return }
- guard let duration else { return }
- let intDuration = Int(duration)
- playDurationLabel.text = "\(intDuration)“"
- }
- }
-
- private func setupViews() {
- let headerView = buildHeader()
- container.addSubview(headerView)
- headerView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(32)
- make.top.equalToSuperview()
- }
-
- let playView = buildPlayView()
- container.addSubview(playView)
- playView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalTo(headerView.snp.bottom)
- make.bottom.equalToSuperview().offset(commonBottomInset)
- }
- }
-
- private func buildHeader() -> UIView {
- let container = UIView()
-
- titleLabel.font = .heading_h3
- titleLabel.textColor = .text_5
- titleLabel.numberOfLines = 0
- titleLabel.textAlignment = .center
- container.addSubview(titleLabel)
- titleLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(32)
- make.top.equalToSuperview().offset(13)
- }
-
- descLabel.font = .body_xs
- descLabel.textColor = .text_3
- descLabel.numberOfLines = 0
- descLabel.textAlignment = .center
- container.addSubview(descLabel)
- descLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(32)
- make.bottom.equalToSuperview().offset(-13)
- make.top.equalTo(titleLabel.snp.bottom).offset(2)
- }
-
- return container
- }
-
- private func buildPlayView() -> UIView {
- let playView = UIView()
-
- let button = UIButton()
- button.setBackgroundImage(.primary_7, for: .normal)
- button.layer.cornerRadius = 16
- button.clipsToBounds = true
- button.addAction(UIAction(handler: { [weak self] _ in
- guard let self else { return }
- if LNVoicePlayer.shared.isPlaying {
- LNVoicePlayer.shared.stop()
- } else if let url = curItem?.value {
- LNVoicePlayer.shared.play(url)
- }
- }), for: .touchUpInside)
- playView.addSubview(button)
- button.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.top.equalToSuperview().offset(20)
- make.bottom.equalToSuperview().offset(-50)
- make.width.equalTo(163)
- make.height.equalTo(32)
- }
-
- playIcon.image = .icVoicePlay
- button.addSubview(playIcon)
- playIcon.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalToSuperview().offset(3)
- make.width.height.equalTo(22)
- }
-
- voiceWaveView.build()
- button.addSubview(voiceWaveView)
- voiceWaveView.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalTo(playIcon.snp.trailing).offset(7)
- make.width.equalTo(18)
- make.height.equalTo(11)
- }
-
- playDurationLabel.font = .heading_h5
- playDurationLabel.textColor = .text_1
- button.addSubview(playDurationLabel)
- playDurationLabel.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.trailing.equalToSuperview().offset(-8)
- }
-
- return playView
- }
- }
|