Skip to content

Instantly share code, notes, and snippets.

View blueberry404's full-sized avatar

Anum Amin blueberry404

View GitHub Profile
@blueberry404
blueberry404 / DefaultLandingComponent.kt
Last active January 10, 2024 20:36
Landing component implementation for Decompose navigation
class DefaultLandingComponent(
componentContext: ComponentContext
) : LandingComponent, ComponentContext by componentContext {
private val landingNavigation = StackNavigation<LandingConfig>()
override val routerState: Value<ChildStack<*, LandingComponent.LandingChild>> =
childStack(
source = landingNavigation,
serializer = LandingConfig.serializer(),
@blueberry404
blueberry404 / LandingComponent.kt
Created January 10, 2024 18:53
Landing component example for Decompose navigation
interface LandingComponent {
val routerState: Value<ChildStack<*, LandingChild>>
sealed class LandingChild {
data class SplashChild(val component: SplashComponent) : LandingChild()
data class OnboardingChild(val component: OnboardingComponent) : LandingChild()
}
}
@blueberry404
blueberry404 / MainActivity.kt
Created January 10, 2024 18:10
Set root content from activity using Decompose
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val rootComponent = DefaultRootComponent(defaultComponentContext())
setContent {
AppTheme {
Surface(
modifier = Modifier.fillMaxSize(),
@blueberry404
blueberry404 / RootContent.kt
Last active January 10, 2024 18:05
Root content example for Decompose navigation
@Composable
fun RootContent(rootComponent: RootComponent) {
val routerState by rootComponent.routerState.subscribeAsState()
when (val child = routerState.active.instance) {
is LandingRoot -> LandingContent(landingComponent = child.landingComponent)
is AuthRoot -> AuthContent(authComponent = child.authComponent)
is MainRoot -> MainContent(mainComponent = child.mainComponent)
}
}
@blueberry404
blueberry404 / DefaultRootComponent.kt
Last active January 10, 2024 20:37
Root component implementation for Decompose navigation
class DefaultRootComponent(
componentContext: ComponentContext
) : RootComponent, ComponentContext by componentContext {
private val rootNavigation = StackNavigation<RootConfig>()
override val routerState: Value<ChildStack<*, RootChild>> =
childStack(
source = rootNavigation,
serializer = RootConfig.serializer(),
@blueberry404
blueberry404 / RootComponent.kt
Last active January 10, 2024 18:06
Root component example for Decompose navigation
interface RootComponent {
val routerState: Value<ChildStack<*, RootChild>>
fun navigateToAuth()
fun navigateToHome()
//This will be used to decide between navigations
sealed class RootChild {
data class LandingRoot(val landingComponent: LandingComponent): RootChild()
@blueberry404
blueberry404 / ProductVariant.kt
Created December 1, 2022 08:30
ProductVariant Data
data class ProductVariant(val id: String, val productName: String, val type: String)
sealed class ProductVariantUIState {
data class ProductVariantStateSuccess(val items: List<ProductVariant>): ProductVariantUIState()
data class ProductVariantStateFailure(val error: String): ProductVariantUIState()
object Loading: ProductVariantUIState()
}
@blueberry404
blueberry404 / ProductVariantViewModel.kt
Created December 1, 2022 08:29
BottomSheet sharing viewmodel
class ProductVariantViewModel: ViewModel() {
private var _variantsUiState = MutableStateFlow<ProductVariantUIState>(ProductVariantUIState.Loading)
val variantsUiState = _variantsUiState.asStateFlow()
private var _selectedVariant = MutableSharedFlow<ProductVariant>()
val selectedVariant = _selectedVariant.asSharedFlow()
fun loadVariantsForProduct(id: String) {
viewModelScope.launch {
@blueberry404
blueberry404 / ProductVariantBottomSheet.kt
Created December 1, 2022 08:28
BottomSheet with shared viewmodel
class ProductVariantBottomSheet: BottomSheetDialogFragment() {
private val viewModel: ProductVariantViewModel by sharedViewModel()
private val args: ProductVariantBottomSheetArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupUI()
observeViewModel()
viewModel.loadVariantsForProduct(args.productId)
@blueberry404
blueberry404 / HomeFragment.kt
Created November 28, 2022 19:50
Shared ViewModel example
class HomeFragment : Fragment() {
private val userViewModel: UserViewModel by sharedViewModel() //using koin
// OR
// private val userViewModel: UserViewModel by activityViewModels() //using androidx
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
userViewModel.fetchUserProfile()
observeViewModel()