Просмотр исходного кода

very basic error handling if playback of an audio file failed

cyberta 4 лет назад
Родитель
Сommit
6ceade5508

+ 10 - 3
deltachat-ios/Chat/AudioController.swift

@@ -15,10 +15,16 @@ public enum PlayerState {
     case stopped
 }
 
+public protocol AudioControllerDelegate: AnyObject {
+    func onAudioPlayFailed()
+}
+
 /// The `AudioController` update UI for current audio cell that is playing a sound
 /// and also creates and manage an `AVAudioPlayer` states, play, pause and stop.
 open class AudioController: NSObject, AVAudioPlayerDelegate, AudioMessageCellDelegate {
 
+    open weak var delegate: AudioControllerDelegate?
+
     lazy var audioSession: AVAudioSession = {
         let audioSession = AVAudioSession.sharedInstance()
         _ = try? audioSession.setCategory(AVAudioSession.Category.playback, options: [.defaultToSpeaker])
@@ -46,10 +52,11 @@ open class AudioController: NSObject, AVAudioPlayerDelegate, AudioMessageCellDel
 
     // MARK: - Init Methods
 
-    public init(dcContext: DcContext, chatId: Int) {
+    public init(dcContext: DcContext, chatId: Int, delegate: AudioControllerDelegate? = nil) {
         self.dcContext = dcContext
         self.chatId = chatId
         self.chat = dcContext.getChat(chatId: chatId)
+        self.delegate = delegate
         super.init()
         NotificationCenter.default.addObserver(self,
                                                selector: #selector(audioRouteChanged),
@@ -118,9 +125,9 @@ open class AudioController: NSObject, AVAudioPlayerDelegate, AudioMessageCellDel
                 state = .playing
                 audioCell.audioPlayerView.showPlayLayout(true)  // show pause button on audio cell
                 startProgressTimer()
+            } else {
+                delegate?.onAudioPlayFailed()
             }
-
-            print("NewAudioController failed play sound becasue given message kind is not Audio")
         }
     }
 

+ 9 - 1
deltachat-ios/Chat/ChatViewController.swift

@@ -198,7 +198,7 @@ class ChatViewController: UITableViewController {
     }()
 
     /// The `BasicAudioController` controll the AVAudioPlayer state (play, pause, stop) and update audio cell UI accordingly.
-    private lazy var audioController = AudioController(dcContext: dcContext, chatId: chatId)
+    private lazy var audioController = AudioController(dcContext: dcContext, chatId: chatId, delegate: self)
 
     private var disableWriting: Bool
     private var showNamesAboveMessage: Bool
@@ -1468,3 +1468,11 @@ extension ChatViewController: QLPreviewControllerDelegate {
         }
     }
 }
+
+extension ChatViewController: AudioControllerDelegate {
+    func onAudioPlayFailed() {
+        let alert = UIAlertController(title: String.localized("error"), message: String.localized("cannot_play_unsupported_file_type"), preferredStyle: .safeActionSheet)
+        alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
+        self.present(alert, animated: true, completion: nil)
+    }
+}

+ 2 - 0
deltachat-ios/en.lproj/Localizable.strings

@@ -755,3 +755,5 @@
 "a11y_voice_message_hint_ios" = "After recording double-tap to send. To discard the recording scrub left-right with two fingers.";
 "login_error_no_internet_connection" = "No internet connection, can\'t log in to your server.";
 "share_account_not_configured" = "Account is not configured.";
+"cannot_play_unsupported_file_type" = "The audio file cannot be played.";
+

+ 1 - 0
scripts/untranslated.xml

@@ -8,4 +8,5 @@
     <string name="a11y_voice_message_hint_ios">After recording double-tap to send. To discard the recording scrub left-right with two fingers.</string>
     <string name="login_error_no_internet_connection">No internet connection, can\'t log in to your server.</string>
     <string name="share_account_not_configured">Account is not configured.</string>
+    <string name="cannot_play_unsupported_file_type">The audio file cannot be played.</string>
 </resources>