Skip to content

Commit 9fe376b

Browse files
authored
Update styleguide.md : added example on how to use AndroidViewModel. (#76)
1 parent 3b59124 commit 9fe376b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

.gemini/styleguide.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ sealed class ScreenState {
7373
* **Recommended:** Do not use `AndroidViewModel`. Use the `ViewModel` class. Avoid using the `Application` class in ViewModels; move the dependency to the UI or data layer.
7474
* **Recommended:** Don't use `LiveData`, use state flow instead.
7575
* **Recommended:** Expose a UI state. Use a single `uiState` property (a `StateFlow`) for data exposure. Multiple properties can be used for unrelated data. Use `stateIn` with `WhileSubscribed(5000)` for data streams. For simpler cases, use a `MutableStateFlow` exposed as an immutable `StateFlow`. Consider using a data class or sealed class for the `UiState`.
76-
* **Recommeded:** Don’t pass `Context` to your `ViewModel`. To avoid memory leaks only UI (Composables) should have a reference to `context`.
76+
* **Recommeded:** Don’t pass `Context` to your `ViewModel`. To avoid memory leaks only UI (Composables) should have a reference to `context`. If you really need to use `Context` in `ViewModel`, you can use AndroidViewModel instead which provides access to an Application context, e.g:
77+
```kotlin
78+
class MyViewModel @Inject constructor(val context: Application): AndroidViewModel(context)
79+
```
80+
7781
* **Recommeded:** Don’t use `fetchData()` in a ViewModel `init {}` block. If you do end up in a case where you need to do something on initial load, you use `stateIn()` instead.
7882

7983

@@ -112,4 +116,4 @@ Optional naming conventions:
112116
* Methods: Verb phrases (e.g., `makePayment()`).
113117
* Properties: Noun phrases (e.g., `inProgressTopicSelection`).
114118
* Streams: `get{Model}Stream()` (e.g., `getAuthorStream(): Flow<Author>`, `getAuthorsStream(): Flow<List<Author>>`).
115-
* Interface Implementations: Meaningful names. Prefix with `Default` if no better name is found (e.g., `OfflineFirstNewsRepository`, `InMemoryNewsRepository`, `DefaultNewsRepository`). Prefix fake implementations with `Fake` (e.g., `FakeAuthorsRepository`).
119+
* Interface Implementations: Meaningful names. Prefix with `Default` if no better name is found (e.g., `OfflineFirstNewsRepository`, `InMemoryNewsRepository`, `DefaultNewsRepository`). Prefix fake implementations with `Fake` (e.g., `FakeAuthorsRepository`).

0 commit comments

Comments
 (0)