Skip to content

Instantly share code, notes, and snippets.

View VitalyPeryatin's full-sized avatar
🎯
Focusing

Vitaly Peryatin VitalyPeryatin

🎯
Focusing
View GitHub Profile
abstract class Destination(
...
) {
private var parcelableArguments: HashMap<String, Parcelable> = hashMapOf()
protected fun saveParcelableArgument(key: String, argument: Parcelable) {
parcelableArguments[key] = argument
}
@Suppress("UNCHECKED_CAST")
class AppViewModel(
...
): BaseViewModel() {
private val _pendingNavigationRoutes = Channel<List<String>>(
capacity = 1,
onBufferOverflow = BufferOverflow.DROP_LATEST
)
fun tryOpenScreenChain(routes: List<String>) {
class AppActivity : BaseActivity() {
private val viewModel: AppViewModel by viewModel()
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
tryOpenScreenChain(intent)
}
private fun tryOpenScreenChain(intent: Intent?) {
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)
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)
...
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)
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)
// Inner navigation graph
val sharedViewModelHolder = SharedViewModelHolder<PostCreateViewModel>()
navGraphBuilder.navigation(
route = PostCreateDestination.RootCreatePostRoute,
startDestination = PostCreateDestination.SelectCreatablePost.createRoute()
) {
composable(PostCreateDestination.SelectCreatablePost) {
SelectCreatablePostScreen(
modifier = modifier,
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
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"