সাধারণ UI রেসিপি

এই রেসিপিটি দেখায় কিভাবে একটি সাধারণ নেভিগেশন UI প্যাটার্ন বাস্তবায়ন করতে হয় যার মধ্যে একটি নীচের নেভিগেশন বার এবং একাধিক ব্যাক স্ট্যাক থাকে, যেখানে নেভিগেশন বারের প্রতিটি ট্যাবের নিজস্ব নেভিগেশন ইতিহাস থাকে।

কিভাবে এটা কাজ করে

এই উদাহরণে তিনটি শীর্ষ-স্তরের গন্তব্য রয়েছে: Home , ChatList , এবং CameraChatList গন্তব্যের একটি সাব-রুটও রয়েছে, ChatDetail

TopLevelBackStack

এই রেসিপির মূল বিষয় হল TopLevelBackStack ক্লাস, যা নেভিগেশন অবস্থা পরিচালনার জন্য দায়ী। এটি নিম্নরূপ কাজ করে:

  • এটি প্রতিটি শীর্ষ-স্তরের গন্তব্যের (ট্যাব) জন্য একটি পৃথক ব্যাক স্ট্যাক বজায় রাখে।
  • এটি বর্তমানে নির্বাচিত শীর্ষ-স্তরের গন্তব্যের ট্র্যাক রাখে।
  • এটি একটি একক, সমতল ব্যাক স্ট্যাক প্রদান করে যা NavDisplay কম্পোজেবল দ্বারা ব্যবহার করা যেতে পারে। এই সমতল ব্যাক স্ট্যাকটি সমস্ত ট্যাবের পৃথক ব্যাক স্ট্যাকের সংমিশ্রণ।

UI স্ট্রাকচার

UI টি একটি Scaffold কম্পোজেবল ব্যবহার করে তৈরি করা হয়েছে, যেখানে একটি NavigationBar bottomBar হিসেবে ব্যবহার করা হয়েছে।

  • NavigationBar প্রতিটি শীর্ষ-স্তরের গন্তব্যের জন্য একটি আইটেম প্রদর্শন করে। যখন কোনও আইটেমে ক্লিক করা হয়, তখন এটি topLevelBackStack.addTopLevel সংশ্লিষ্ট ট্যাবে স্যুইচ করার জন্য কল করে, প্রতিটি ট্যাবের নেভিগেশন ইতিহাস সংরক্ষণ করে।
  • NavDisplay কম্পোজেবলটি Scaffold এর কন্টেন্ট এরিয়ায় স্থাপন করা হয়। এটি TopLevelBackStack দ্বারা প্রদত্ত ফ্ল্যাটেড ব্যাক স্ট্যাকের উপর ভিত্তি করে বর্তমান স্ক্রিন প্রদর্শনের জন্য দায়ী।

এই পদ্ধতিটি একটি সাধারণ নেভিগেশন প্যাটার্নের অনুমতি দেয় যেখানে ব্যবহারকারীরা অ্যাপের বিভিন্ন বিভাগের মধ্যে স্যুইচ করতে পারে এবং প্রতিটি বিভাগ তার নিজস্ব নেভিগেশন ইতিহাস বজায় রাখে।

রাষ্ট্রীয় সংরক্ষণ

এই রেসিপিতে নেভিগেশন অবস্থা কীভাবে পরিচালিত হয় তা লক্ষ্য করা গুরুত্বপূর্ণ। যখন একজন ব্যবহারকারী একটি শীর্ষ-স্তরের গন্তব্য থেকে দূরে নেভিগেট করেন (যেমন, পূর্ববর্তী ট্যাবে ফিরে না যাওয়া পর্যন্ত পিছনের বোতাম টিপে), তখন সেই গন্তব্যের সম্পূর্ণ নেভিগেশন ইতিহাস সাফ হয়ে যায়। অবস্থাটি সংরক্ষিত হয় না। যখন ব্যবহারকারী পরে সেই ট্যাবে ফিরে আসেন, তখন তারা এর প্রাথমিক স্ক্রিন থেকে শুরু করবেন।

দ্রষ্টব্য : এই উদাহরণে, Home রুটটি ChatList এবং Camera রুটের উপরে যেতে পারে, অর্থাৎ Home থেকে ফিরে আসার জন্য অ্যাপটি ছেড়ে যাওয়া আবশ্যক নয়। ব্যবহারকারী যখন ব্যাক স্ট্যাকের অবশিষ্ট একটি শীর্ষ স্তরের রুট থেকে ফিরে যাবেন তখন অ্যাপটি প্রস্থান করবে।

/*
 * 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.commonui

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Face
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.lifecycle.compose.dropUnlessResumed
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.ui.NavDisplay
import com.example.nav3recipes.content.ContentBlue
import com.example.nav3recipes.content.ContentGreen
import com.example.nav3recipes.content.ContentPurple
import com.example.nav3recipes.content.ContentRed
import com.example.nav3recipes.ui.setEdgeToEdgeConfig

private sealed interface TopLevelRoute {
    val icon: ImageVector
}
private data object Home : TopLevelRoute { override val icon = Icons.Default.Home }
private data object ChatList : TopLevelRoute { override val icon = Icons.Default.Face }
private data object ChatDetail
private data object Camera : TopLevelRoute { override val icon = Icons.Default.PlayArrow }

private val TOP_LEVEL_ROUTES : List<TopLevelRoute> = listOf(Home, ChatList, Camera)

class CommonUiActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setEdgeToEdgeConfig()
        super.onCreate(savedInstanceState)
        setContent {
            val topLevelBackStack = remember { TopLevelBackStack<Any>(Home) }

            Scaffold(
                bottomBar = {
                    NavigationBar {
                        TOP_LEVEL_ROUTES.forEach { topLevelRoute ->

                            val isSelected = topLevelRoute == topLevelBackStack.topLevelKey
                            NavigationBarItem(
                                selected = isSelected,
                                onClick = {
                                    topLevelBackStack.addTopLevel(topLevelRoute)
                                },
                                icon = {
                                    Icon(
                                        imageVector = topLevelRoute.icon,
                                        contentDescription = null
                                    )
                                }
                            )
                        }
                    }
                }
            ) { _ ->
                NavDisplay(
                    backStack = topLevelBackStack.backStack,
                    onBack = { topLevelBackStack.removeLast() },
                    entryProvider = entryProvider {
                        entry<Home>{
                            ContentRed("Home screen")
                        }
                        entry<ChatList>{
                            ContentGreen("Chat list screen"){
                                Button(onClick = dropUnlessResumed {
                                    topLevelBackStack.add(ChatDetail)
                                }) {
                                    Text("Go to conversation")
                                }
                            }
                        }
                        entry<ChatDetail>{
                            ContentBlue("Chat detail screen")
                        }
                        entry<Camera>{
                            ContentPurple("Camera screen")
                        }
                    },
                )
            }
        }
    }
}

class TopLevelBackStack<T: Any>(startKey: T) {

    // Maintain a stack for each top level route
    private var topLevelStacks : LinkedHashMap<T, SnapshotStateList<T>> = linkedMapOf(
        startKey to mutableStateListOf(startKey)
    )

    // Expose the current top level route for consumers
    var topLevelKey by mutableStateOf(startKey)
        private set

    // Expose the back stack so it can be rendered by the NavDisplay
    val backStack = mutableStateListOf(startKey)

    private fun updateBackStack() =
        backStack.apply {
            clear()
            addAll(topLevelStacks.flatMap { it.value })
        }

    fun addTopLevel(key: T){

        // If the top level doesn't exist, add it
        if (topLevelStacks[key] == null){
            topLevelStacks.put(key, mutableStateListOf(key))
        } else {
            // Otherwise just move it to the end of the stacks
            topLevelStacks.apply {
                remove(key)?.let {
                    put(key, it)
                }
            }
        }
        topLevelKey = key
        updateBackStack()
    }

    fun add(key: T){
        topLevelStacks[topLevelKey]?.add(key)
        updateBackStack()
    }

    fun removeLast(){
        val removedKey = topLevelStacks[topLevelKey]?.removeLastOrNull()
        // If the removed key was a top level key, remove the associated top level stack
        topLevelStacks.remove(removedKey)
        topLevelKey = topLevelStacks.keys.last()
        updateBackStack()
    }
}