parent
ba1a463498
commit
4470957988
@ -0,0 +1,90 @@
|
|||||||
|
# Now In Android Style Guide
|
||||||
|
|
||||||
|
# Introduction
|
||||||
|
This style guide outlines the coding conventions for the Now In Android application.
|
||||||
|
Generally speaking, this application follows the official Kotlin coding styleguide and best practises used when writing modern Android mobile applications.
|
||||||
|
|
||||||
|
# Key Principles
|
||||||
|
* **Security:** Code should always follow security best practises and avoid introducing vulnerbilities.
|
||||||
|
* **Readability:** Code should be easy to understand for all team members.
|
||||||
|
* **Maintainability:** Code should be easy to modify and extend.
|
||||||
|
* **Consistency:** Adhering to a consistent style across all projects improves
|
||||||
|
collaboration and reduces errors.
|
||||||
|
* **Performance:** While readability is paramount, code should be efficient especially when working with custom UI or business logic.
|
||||||
|
|
||||||
|
# Tooling
|
||||||
|
* **Code formatter:** [Spotless] - Enforces consistent formatting automatically.
|
||||||
|
* **Linter:** [ktlint] - Identifies potential issues and style violations.
|
||||||
|
|
||||||
|
# Example
|
||||||
|
```kotlin
|
||||||
|
package com.google.samples.apps.nowinandroid
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading
|
||||||
|
import com.google.samples.apps.nowinandroid.MainActivityUiState.Success
|
||||||
|
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
|
||||||
|
import com.google.samples.apps.nowinandroid.core.model.data.UserData
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class MainActivityViewModel @Inject constructor(
|
||||||
|
userDataRepository: UserDataRepository,
|
||||||
|
) : ViewModel() {
|
||||||
|
val uiState: StateFlow<MainActivityUiState> = userDataRepository.userData.map {
|
||||||
|
Success(it)
|
||||||
|
}.stateIn(
|
||||||
|
scope = viewModelScope,
|
||||||
|
initialValue = Loading,
|
||||||
|
started = SharingStarted.WhileSubscribed(5_000),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed interface MainActivityUiState {
|
||||||
|
data object Loading : MainActivityUiState
|
||||||
|
|
||||||
|
data class Success(val userData: UserData) : MainActivityUiState {
|
||||||
|
override val shouldDisableDynamicTheming = !userData.useDynamicColor
|
||||||
|
|
||||||
|
override val shouldUseAndroidTheme: Boolean = when (userData.themeBrand) {
|
||||||
|
ThemeBrand.DEFAULT -> false
|
||||||
|
ThemeBrand.ANDROID -> true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun shouldUseDarkTheme(isSystemDarkTheme: Boolean) =
|
||||||
|
when (userData.darkThemeConfig) {
|
||||||
|
DarkThemeConfig.FOLLOW_SYSTEM -> isSystemDarkTheme
|
||||||
|
DarkThemeConfig.LIGHT -> false
|
||||||
|
DarkThemeConfig.DARK -> true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if the state wasn't loaded yet and it should keep showing the splash screen.
|
||||||
|
*/
|
||||||
|
fun shouldKeepSplashScreen() = this is Loading
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if the dynamic color is disabled.
|
||||||
|
*/
|
||||||
|
val shouldDisableDynamicTheming: Boolean get() = true
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if the Android theme should be used.
|
||||||
|
*/
|
||||||
|
val shouldUseAndroidTheme: Boolean get() = false
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if dark theme should be used.
|
||||||
|
*/
|
||||||
|
fun shouldUseDarkTheme(isSystemDarkTheme: Boolean) = isSystemDarkTheme
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in new issue