HomeViewModel

class HomeViewModel(val application: Application) : AndroidViewModel, KoinComponent

A manually injected view model for demo/historical purposes, which consequently demonstrates the use of KoinComponent, which is used to inject network and notificationManager with Koin.

  • https://insert-koin.io/docs/reference/koin-core/koin-component/

I'm using AndroidViewModel here in order to have the application which is used for achievementsDao, initObjectDetector, loadAndGetUserPrefs and promptGemini

This class has some data that should survive recompositions for a good UX. These are:

It also handles some logic that should not be delegated to the UI and logic that uses both data and UI components. These operations are: handling connection losses, performing async calls, loading user preferences, launching coroutines and accessing other data components.

Note: isGeminiEnabled is also used to toggle on/off emissions of _objDetectionResults used for MediaPipe (and thus the detection outlines)

Constructors

Link copied to clipboard
constructor(application: Application)

Properties

Link copied to clipboard
private val _cameraPreviewRatio: MutableStateFlow<ResolutionSelector>
Link copied to clipboard
private val _geminiMessage: MutableStateFlow<Message>
Link copied to clipboard
private val _geminiStatus: MutableStateFlow<GeminiStatus>
Link copied to clipboard
private val _objDetectionResults: MutableStateFlow<ResultBundle?>

For objDetectionResults, toStateFlow is used instead of asStateFlow because sample returns a flow. When animations are enabled, the emissions are cut down to 1 every half a second so that AnimatedDetectionOutline doesn't have a ton of work to do, otherwise no animation would be visible since there would be too much lag when performing the animation calculations each 30fps.

Link copied to clipboard
private val _pictureTaken: MutableStateFlow<Picture?>
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val geminiMessage: StateFlow<Message>
Link copied to clipboard
val geminiStatus: StateFlow<GeminiStatus>
Link copied to clipboard
private val impl: ViewModelImpl?
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val pictureTaken: StateFlow<Picture?>
Link copied to clipboard
private var prefs: UserPreferences
Link copied to clipboard

Functions

Link copied to clipboard
open fun addCloseable(closeable: AutoCloseable)
fun addCloseable(key: String, closeable: AutoCloseable)
Link copied to clipboard
fun <T> ViewModel.async(useMainDispatcher: Boolean = false, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred<T>
Link copied to clipboard
private suspend fun checkForNewAchievements(objectsDetected: MutableList<Detection>)
Link copied to clipboard
internal fun clear()
Link copied to clipboard
open fun <T : Application> getApplication(): T
Link copied to clipboard
Link copied to clipboard
open fun getKoin(): Koin
Link copied to clipboard
private suspend fun handleGeminiOnNetworkChange(hasConnection: Boolean)
Link copied to clipboard
fun initObjectDetector(cameraImageAnalyser: ImageAnalysis, objectDetectorSettings: ObjectDetectorSettings = ObjectDetectorSettings()): ImageAnalysis

Creates a camera UseCase with MyImageAnalyser and with objectDetectorSettings The cameraImageAnalyser runs in a single thread pool executorForImgAnalysis

Link copied to clipboard
fun ViewModel.launch(useMainDispatcher: Boolean = false, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit): Job
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
protected open override fun onCleared()
Link copied to clipboard
fun promptGemini(prompt: String)

Prompts Gemini with a GeminiPrompt if the geminiApi is initialized and pictureTaken is not null. Even thought GeminiChatContainer should disable the prompt submission, but we check here to avoid any problems

Link copied to clipboard
fun setPicture(picture: Picture)
Link copied to clipboard
fun takePicture(imageCaptureUseCase: ImageCapture)
Link copied to clipboard
fun toggleGemini(onFail: () -> Unit)