Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
Refactor code for two switch case statement

I'm working on a swift project and have a question to refactor my code.

I'm writing code for checking if a user authorized both the camera and microphone authorization in the app. I wrote the following codes, but I guess (and I hope) I can refactor the code because I think the code below is unclear. I understood basic Swift grammar and gradually understood the compiled language, but let me know if there is a way to make this more readable or easy to write.

What I want to do here is...

  • check if both a camera and a microphone are authorized.

  • if both of them are authorized, show View Controller with showNextVC().

  • if one of nor neither of the item is authorized, show an alert with showConfigurationAlert

    func checkAuthStatus(){
        checkCameraStatus()
    }
    
    func checkCameraStatus() {
        switch AVCaptureDevice.authorizationStatus(for: .video) {
        case .notDetermined:
            print("not Determined")
            AVCaptureDevice.requestAccess(for: .video) { granted in
                if granted {
                    print("Now it's granted")
                }
            }
        case .restricted:
            print("restricted")
            showConfigurationAlert(for: "camera")
        case .denied:
            print("denied")
            showConfigurationAlert(for: "camera")
        case .authorized:
            checkMicrophoneStatus()
        @unknown default:
            print("unknown")
        }
    
    }
    
    func checkMicrophoneStatus() {
        switch AVCaptureDevice.authorizationStatus(for: .audio){
        case .notDetermined:
            AVCaptureDevice.requestAccess(for: .audio) { granted in
                if granted {
                    print("Now it's granted")
                }
            }
        case .restricted:
            print("restricted")
            showConfigurationAlert(for: "microphone")
        case .denied:
            print("denied")
            showConfigurationAlert(for: "microphone")
        case .authorized:
            print(("authorized"))
            showNextVC()
        @unknown default:
            print("unknown")
        }
    }
    

What I did was, first check the camera authorization in checkAuthStatus and then, if the camera is authorized, trigger checkMicrophoneStatus() to check the mic authorization. The reason why I think this code is unclear is, I only write a function to check the camera authorization in checkAuthStatus() funciton. I think if it is clear if I can write something like

func checkAuthStatus(){
    // check both cameara and microphone is authorized.
    // if both of them are authorized, show next VC with showNextVC() function.
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You could change checkCameraStatus() and checkMicrophoneStatus() to return a bool and inside checkAuthStatus() change to :

func checkAuthStatus(){
    let cameraAuthorised = checkCameraStatus()
    let micAuthorised = checkMicrophoneStatus()

    if (cameraAuthorised && micAuthorised) {
        showNextVC()
    }
 }
over 4 years ago · Santiago Trujillo Relatório

0

One way is to add an onAuthorised closure parameter to both functions. The two checkXXXStatus functions also has quite a lot in common. We don't need to duplicate the switch statement.

func checkMediaStatus(type: AVMediaType, deviceName: String, onAuthorised: (() -> Void)?) {
    switch AVCaptureDevice.authorizationStatus(for: type){
    case .notDetermined:
        AVCaptureDevice.requestAccess(for: type) { granted in
            if granted {
                print("Now it's granted")
            }
        }
    case .restricted:
        print("restricted")
        showConfigurationAlert(for: deviceName)
    case .denied:
        print("denied")
        showConfigurationAlert(for: deviceName)
    case .authorized:
        print(("authorized"))
        onAuthorised?()
    @unknown default:
        print("unknown")
    }
}

func checkMicrophoneStatus(onAuthorised: (() -> Void)?) {
    checkMediaStatus(type: .audio, deviceName: "microphone", onAuthorised: onAuthorised)
}

func checkCameraStatus(onAuthorised: (() -> Void)?) {
    checkMediaStatus(type: .video, deviceName: "camera", onAuthorised: onAuthorised)
}

Then checkAuthStatus can be written as:

func checkAuthStatus(){
    checkCameraStatus {
        self.checkMicrophoneStatus {
            self.showNextVC()
        }
    }
}

Also note that you might also want to call onAuthorised and showConfigurationAlert in the requestAccess completion handler. I think this is a better design.

if granted {
    print("Now it's granted")
    onAuthorised?()
} else {
    showConfigurationAlert(for: deviceName)
}
over 4 years ago · Santiago Trujillo Relatório

0

I think you are looking for something like this:

let videoStatus = AVCaptureDevice.authorizationStatus(for: .video)
let audioStatus = AVCaptureDevice.authorizationStatus(for: .audio)

switch (videoStatus, audioStatus) {
case (.authorized, .authorized): showNextVC()
case (.authorized, _): showConfigurationAlert(for: "microphone")
case (_, .authorized): showConfigurationAlert(for: "camera")
default: print(videoStatus, audioStatus)
}

You can check both cases at the same time and handle each of them you like

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda