শর্তসাপেক্ষ নেভিগেশন রেসিপি
এই রেসিপিটি দেখায় যে কীভাবে কন্ডিশনাল নেভিগেশন প্রয়োগ করতে হয়, যেখানে কোনও শর্ত পূরণ হলে (এই ক্ষেত্রে, ব্যবহারকারী লগ-ইন করলে) তবেই নির্দিষ্ট কিছু ডেস্টিনেশন অ্যাক্সেস করা যায়।
এটি কীভাবে কাজ করে
এই উদাহরণে Profile এমন ডেস্টিনেশন আছে যেখানে ব্যবহারকারীকে লগ-ইন করতে হবে। ব্যবহারকারী লগ-ইন না করে Profile-এ নেভিগেট করার চেষ্টা করলে, তাকে Login স্ক্রিনে রিডাইরেক্ট করা হয়। সফলভাবে লগ-ইন করার পরে, তারা অটোমেটিক Profile স্ক্রিনে নেভিগেট করে।
AppBackStack
এই রেসিপির মূল হল কাস্টম AppBackStack ক্লাস, যা শর্তসাপেক্ষে নেভিগেশনের লজিককে এনক্যাপসুলেট করে।
-
RequiresLoginইন্টারফেস: একটি মার্কার ইন্টারফেস,RequiresLogin, সেইসব গন্তব্য শনাক্ত করতে ব্যবহার করা হয় যেখানে ব্যবহারকারীকে লগ-ইন করতে হয়।Profileগন্তব্য এই ইন্টারফেস প্রয়োগ করে। -
লগ-ইন পৃষ্ঠায় রিডাইরেক্ট করা:
RequiresLoginপ্রয়োগ করা হয়েছে এমন কোনও গন্তব্যেaddফাংশন কল করা হলে এবং ব্যবহারকারী লগ-ইন না করে থাকলে,AppBackStackঅভিপ্রেত গন্তব্য স্টোর করে এবং পরিবর্তে ব্যাক স্ট্যাকেLoginরুট যোগ করে। -
লগ-ইন ম্যানেজ করা:
loginফাংশন কল করা হলে, এটি ব্যবহারকারীর স্ট্যাটাস লগ-ইন করা হিসেবে সেট করে। ব্যবহারকারী যদি আগে সেভ করা কোনও গন্তব্য অ্যাক্সেস করার চেষ্টা করে থাকেন, তাহলে এটি ব্যাক স্ট্যাকে সেই গন্তব্য যোগ করে এবংLoginস্ক্রিনটি সরিয়ে দেয়। -
লগ-আউট ম্যানেজ করা:
logoutফাংশন কল করা হলে, এটি ব্যবহারকারীর স্ট্যাটাসকে লগ-আউট করা হিসেবে সেট করে এবং ব্যাক স্ট্যাক থেকে এমন যেকোনও গন্তব্য সরিয়ে দেয় যেখানে ব্যবহারকারীকে লগ-ইন করতে হয়।
এই পদ্ধতি কাস্টম ব্যাক স্ট্যাক প্রয়োগের মাধ্যমে লজিককে সেন্ট্রালাইজ করে কন্ডিশনাল নেভিগেশন হ্যান্ডেল করার একটি পরিষ্কার উপায় প্রদান করে।
/* * Copyright 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.nav3recipes.conditional import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSerializable import androidx.compose.runtime.setValue import androidx.lifecycle.compose.dropUnlessResumed import androidx.navigation3.runtime.NavBackStack import androidx.navigation3.runtime.NavKey import androidx.navigation3.runtime.entryProvider import androidx.navigation3.runtime.serialization.NavBackStackSerializer import androidx.navigation3.runtime.serialization.NavKeySerializer import androidx.navigation3.ui.NavDisplay import com.example.nav3recipes.content.ContentBlue import com.example.nav3recipes.content.ContentGreen import com.example.nav3recipes.content.ContentYellow import com.example.nav3recipes.ui.setEdgeToEdgeConfig import kotlinx.serialization.Serializable /** * Class for representing navigation keys in the app. * * Note: We use a sealed class because KotlinX Serialization handles * polymorphic serialization of sealed classes automatically. * * @param requiresLogin - true if the navigation key requires that the user is logged in * to navigate to it */ @Serializable sealed class ConditionalNavKey(val requiresLogin: Boolean = false) : NavKey /** * Key representing home screen */ @Serializable private data object Home : ConditionalNavKey() /** * Key representing profile screen that is only accessible once the user has logged in */ @Serializable private data object Profile : ConditionalNavKey(requiresLogin = true) /** * Key representing login screen * * @param redirectToKey - navigation key to redirect to after successful login */ @Serializable private data class Login( val redirectToKey: ConditionalNavKey? = null ) : ConditionalNavKey() class ConditionalActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { setEdgeToEdgeConfig() super.onCreate(savedInstanceState) setContent { val backStack = rememberNavBackStack<ConditionalNavKey>(Home) var isLoggedIn by rememberSaveable { mutableStateOf(false) } val navigator = remember { Navigator( backStack = backStack, onNavigateToRestrictedKey = { redirectToKey -> Login(redirectToKey) }, isLoggedIn = { isLoggedIn } ) } NavDisplay( backStack = backStack, onBack = { navigator.goBack() }, entryProvider = entryProvider { entry<Home> { ContentGreen("Welcome to Nav3. Logged in? ${isLoggedIn}") { Column { Button(onClick = dropUnlessResumed { navigator.navigate(Profile) }) { Text("Profile") } Button(onClick = dropUnlessResumed { navigator.navigate(Login()) }) { Text("Login") } } } } entry<Profile> { ContentBlue("Profile screen (only accessible once logged in)") { Button(onClick = dropUnlessResumed { isLoggedIn = false navigator.navigate(Home) }) { Text("Logout") } } } entry<Login> { key -> ContentYellow("Login screen. Logged in? $isLoggedIn") { Button(onClick = dropUnlessResumed { isLoggedIn = true key.redirectToKey?.let { targetKey -> backStack.remove(key) navigator.navigate(targetKey) } }) { Text("Login") } } } } ) } } } // An overload of `rememberNavBackStack` that returns a subtype of `NavKey`. // See https://issuetracker.google.com/issues/463382671 for a discussion of this function @Composable fun <T : NavKey> rememberNavBackStack(vararg elements: T): NavBackStack<T> { return rememberSerializable( serializer = NavBackStackSerializer(elementSerializer = NavKeySerializer()) ) { NavBackStack(*elements) } }
/* * Copyright 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.nav3recipes.conditional import androidx.navigation3.runtime.NavBackStack /** * Provides navigation events with built-in support for conditional access. If the user attempts to * navigate to a [ConditionalNavKey] that requires login ([ConditionalNavKey.requiresLogin] is true) * but is not currently logged in, the Navigator will redirect the user to a login key. * * @property backStack The back stack that is modified by this class * @property onNavigateToRestrictedKey A lambda that is called when the user attempts to navigate * to a key that requires login. This should return the key that represents the login screen. The * user's target key is supplied as a parameter so that after successful login the user can be * redirected to their target destination. * @property isLoggedIn A lambda that returns whether the user is logged in. */ class Navigator( private val backStack: NavBackStack<ConditionalNavKey>, private val onNavigateToRestrictedKey: (targetKey: ConditionalNavKey?) -> ConditionalNavKey, private val isLoggedIn: () -> Boolean, ) { fun navigate(key: ConditionalNavKey) { if (key.requiresLogin && !isLoggedIn()) { val loginKey = onNavigateToRestrictedKey(key) backStack.add(loginKey) } else { backStack.add(key) } } fun goBack() = backStack.removeLastOrNull() }