Skip to content

Instantly share code, notes, and snippets.

@jacksonfdam
Forked from Mohammed-Alhams/Compose Screen.kt
Created October 6, 2023 06:07
Show Gist options
  • Save jacksonfdam/1f25f6cb845e56a6355a0c03ae549cf1 to your computer and use it in GitHub Desktop.
Save jacksonfdam/1f25f6cb845e56a6355a0c03ae549cf1 to your computer and use it in GitHub Desktop.
// Screen Template
package ${PACKAGE_NAME}
import androidx.compose.runtime.Composable
import androidx.navigation.NavController
@Composable
fun ${NAME}Screen(
${NAME}State: ${NAME}UIState,
listener: ${NAME}ScreenInteractionListener,
navController: NavController
){
}
//My_Class_name //this will be filled while using the template
//Class_name //using this variable,
//you can use the filled variable inside the template
#set( $Class_name = $My_Class_name )
class $Class_name
// End Screen Template
// ViewModel Template
package ${PACKAGE_NAME}
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
class ${NAME}ViewModel(
savedStateHandle: SavedStateHandle
): ViewModel(), ${NAME}ScreenInteractionListener{
private val _uiState = MutableStateFlow(${NAME}UIState())
val uiState = _uiState.asStateFlow()
private val _uiEffect = MutableSharedFlow<${NAME}UIEffect>()
val uiEffect = _uiEffect.asSharedFlow()
private val args: ${NAME}Args = ${NAME}Args(savedStateHandle)
}
// End ViewModel Template
// Route Template
package ${PACKAGE_NAME}
import android.annotation.SuppressLint
import androidx.compose.runtime.Composable
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.hilt.navigation.compose.hiltViewModel
private const val ROUTE = "${NAME}Screen"
fun NavController.navigateTo${NAME}(){
navigate(ROUTE)
}
@SuppressLint("ComposableDestinationInComposeScope")
@Composable
fun NavGraphBuilder.${NAME}ScreenRoute(
navController: NavController,
viewModel: ${NAME}ViewModel = hiltViewModel()
){
composable(
route = ROUTE,
// arguments = listOf(navArgument("exampleArg") { NavType.IntType }),
) {
val state = viewModel.uiState.collectAsStateWithLifecycle()
${NAME}Screen(state.value, viewModel, navController)
}
}
internal class ${NAME}Args() {
constructor(savedStateHandle: SavedStateHandle) :
this(/*checkNotNull(savedStateHandle[conversationIdArg]) as String*/)
companion object{
//const val argName = "exampleArg"
}
}
// End Route Template
// Screen UI effect
package ${PACKAGE_NAME}
sealed interface ${NAME}UIEffect{
}
// End Screen UI effect
// Screen UI Interaction listener
package ${PACKAGE_NAME}
interface ${NAME}ScreenInteractionListener
// End Screen UI Interaction listener
// Screen UI State
package ${PACKAGE_NAME}
data class ${NAME}UIState(
val state: String = ""
)
// Screen UI State
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment