Skip to content

Code documentation

Alessio I edited this page Feb 24, 2023 · 10 revisions

The code is written in Swift 5 for iOS 15.0+ actually available for iPhone only but coming for iPad and Mac soon! The code was written by one person (me) . It is based on external libreries that manage the API calls to the different AI chat providers.

The app is done completely in SwiftUI and is better optimized for iOS 16 having more improvements than other versions.

Model - View - View Model (MVVM design pattern)

This project uses the MVVM design pattern. The logic is stored inside the view models classes. Every function has it's unique view model with it's own shared istance shared accross the whole project.

Group 1

AIChatViewModel

final class AIChatViewModel: ObservableObject {

    func setup() {
    ...
    }

}

This is the view model that manages the interacions with the different AI libraries. The setup function is the initializer of the viewmodel and lets you take the token from the user defaults and create the different client instances.

There are some other files in the folder of the AI Viewmodels that are extensions of AIChatViewModel class. Every extension is an implementation of a different external library like OpenAISwift or ChatGPTSwift.

AudioPlayer

class AudioPlayer {
    ...
}

This is the ViewModel that manages the AVFoundation AudioPlayer framework. It's the view model that let you hear sound in the app.

SpeechSynthesizer

class SpeechSynthesizer: ObservableObject {
    
    func readString (text: String) {
    ...
    }

    func recognizeLanguage (text: String) -> String {
    ...
    }

}

This is the ViewModel that manages the AVFoundation synthesizer and the NaturalLanguage frameworks. It is used to hear the messages. It has two functions, one to actually hear the message and another one to detect the language of the message

ThemeViewModel

This is the ViewModel that manages the accent color and chat theme in all the app.

class ThemeViewModel: ObservableObject {
   ...
}

Swiftlint

To keep the code clean and consistent, I used the swiftlint tool with the Airbnb app's swiftlint.yml setup file that can be found here.