Last active
March 22, 2025 12:02
-
-
Save delacrixmorgan/8a637e23cd0181614dbcdd1dd0ca9206 to your computer and use it in GitHub Desktop.
Android Studio File and Code Template - Compose MVI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "") package ${PACKAGE_NAME} #end | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.lifecycle.compose.collectAsStateWithLifecycle | |
import org.jetbrains.compose.ui.tooling.preview.Preview | |
@Composable | |
fun ${NAME}Root(viewModel: ${NAME}ViewModel) { | |
val state by viewModel.state.collectAsStateWithLifecycle() | |
${NAME}Screen(state = state, onAction = viewModel::onAction) | |
} | |
@Composable | |
fun ${NAME}Screen( | |
state: ${NAME}UiState, | |
onAction: (${NAME}Action) -> Unit, | |
) { | |
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { | |
Text(text = "${NAME}") | |
} | |
} | |
@Preview | |
@Composable | |
private fun Preview() { | |
AppTheme { | |
${NAME}Screen( | |
state = ${NAME}UiState(), | |
onAction = {} | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME} #end | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.viewModelScope | |
import kotlinx.coroutines.flow.MutableStateFlow | |
import kotlinx.coroutines.flow.SharingStarted | |
import kotlinx.coroutines.flow.onStart | |
import kotlinx.coroutines.flow.stateIn | |
class ${NAME}ViewModel : ViewModel() { | |
private var hasLoadedInitialData = false | |
private val _state = MutableStateFlow(${NAME}UiState()) | |
val state = _state | |
.onStart { | |
if(!hasLoadedInitialData) { | |
hasLoadedInitialData = true | |
loadData() | |
} | |
} | |
.stateIn( | |
scope = viewModelScope, | |
started = SharingStarted.WhileSubscribed(5_000L), | |
initialValue = ${NAME}UiState() | |
) | |
private fun loadData() { | |
} | |
fun onAction(action: ${NAME}Action) { | |
} | |
} | |
data class ${NAME}UiState( | |
val closeScreen: Boolean = false, | |
) | |
sealed interface ${NAME}Action { | |
data object OnCloseScreen : ${NAME}Action | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment