import Foundation
import AVFoundation
import RxSwift

extension AVAudioSession {
    func setCategoryAsync(_ category: AVAudioSession.Category) -> Single<Void> {
        if self.category == category {
            return .just(())
        } else {
            return Single.create { observer in
                NotificationCenter.default.addObserver(forName: AVAudioSession.routeChangeNotification, object: self, queue: .main) { notification in
                    print("")
                }
                
                do {
                    try self.setCategory(category, mode: .default, options: [])
                } catch {
                    observer(.error(error))
                }
                
                return Disposables.create()
            }
        }
    }
}
