Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Vitaly Peryatin VitalyPeryatin

🎯
Focusing
View GitHub Profile
View Destination.kt
abstract class Destination(
...
) {
private var parcelableArguments: HashMap<String, Parcelable> = hashMapOf()
protected fun saveParcelableArgument(key: String, argument: Parcelable) {
parcelableArguments[key] = argument
}
@Suppress("UNCHECKED_CAST")
View AppViewModel.kt
class AppViewModel(
...
): BaseViewModel() {
private val _pendingNavigationRoutes = Channel<List<String>>(
capacity = 1,
onBufferOverflow = BufferOverflow.DROP_LATEST
)
fun tryOpenScreenChain(routes: List<String>) {
View AppActivity.kt
class AppActivity : BaseActivity() {
private val viewModel: AppViewModel by viewModel()
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
tryOpenScreenChain(intent)
}
private fun tryOpenScreenChain(intent: Intent?) {
View AppInnerNavGraph.kt
val sharedViewModelHolder = SharedViewModelHolder<PostCreateViewModel>()
navGraphBuilder.navigation(
route = PostCreateDestination.RootCreatePostRoute,
startDestination = PostCreateDestination.SelectCreatablePost.createRoute()
) {
composable(PostCreateDestination.SelectCreatablePost) {
SelectCreatablePostScreen(
modifier = modifier,
navController = navController,
sharedViewModel = sharedViewModelHolder.createNewSharedViewModel()
@VitalyPeryatin
VitalyPeryatin / SharedViewModel.kt
Created July 6, 2022 19:54
SharedViewModel (Compose Navigation)
View SharedViewModel.kt
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.ViewModel
import org.koin.androidx.compose.getViewModel
import java.lang.ref.WeakReference
class SharedViewModelHolder<T: ViewModel> {
var sharedViewModelReference = WeakReference<T>(null)
}
@VitalyPeryatin
VitalyPeryatin / AppNavGraph.kt
Created July 6, 2022 19:26
Pass parcelable arguments (Compose)
View AppNavGraph.kt
...
override fun registerGraph(
navGraphBuilder: NavGraphBuilder,
navController: NavHostController,
modifier: Modifier,
parameters: NavGraphParameters
) {
...
navGraphBuilder.composable(VideoAnswerDestination) { navBackStackEntry ->
val arguments: VideoAnswerDestination.Arguments =
@VitalyPeryatin
VitalyPeryatin / AppActivity.kt
Created July 6, 2022 19:17
Open screen chain (Compose)
View AppActivity.kt
class AppActivity : BaseActivity() {
private val viewModel: AppViewModel by viewModel()
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
tryOpenScreenChain(intent)
}
private fun tryOpenScreenChain(intent: Intent?) {
@VitalyPeryatin
VitalyPeryatin / AppNavGraph.kt
Created July 6, 2022 19:14
SharedViewModelHolder (Compose)
View AppNavGraph.kt
// Inner navigation graph
val sharedViewModelHolder = SharedViewModelHolder<PostCreateViewModel>()
navGraphBuilder.navigation(
route = PostCreateDestination.RootCreatePostRoute,
startDestination = PostCreateDestination.SelectCreatablePost.createRoute()
) {
composable(PostCreateDestination.SelectCreatablePost) {
SelectCreatablePostScreen(
modifier = modifier,
View BaseBottomSheetDialog.kt
import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
View MainActivity.kt
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.TextView
import java.util.*
const val ARG_ACTIVITY_NO = "ARG_ACTIVITY_NO"