Home View Model
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:
cameraPreviewRatio, pictureTaken, isGeminiEnabled, geminiStatus, geminiMessage etc
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)
Properties
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.
Functions
Creates a camera UseCase with MyImageAnalyser and with objectDetectorSettings The cameraImageAnalyser runs in a single thread pool executorForImgAnalysis
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