মেটেরিয়াল সাপোর্টিং প্যানেল রেসিপি

এই রেসিপি দেখায় যে Material 3 অ্যাডাপ্টিভ লাইব্রেরির SupportingPaneSceneStrategy ব্যবহার করে কীভাবে একটি প্রধান প্যানেল ও একটি সহায়ক প্যানেল সহ অ্যাডাপ্টিভ লেআউট তৈরি করতে হয়। এই লেআউটটি বড় স্ক্রিনে মূল কন্টেন্টের পাশাপাশি সাপ্লিমেন্টারি কন্টেন্ট দেখানোর জন্য উপযোগী।

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

এই উদাহরণে তিনটি গন্তব্য আছে: MainVideo, RelatedVideos ও Profile।

SupportingPaneSceneStrategy

rememberSupportingPaneSceneStrategy এই অ্যাডাপ্টিভ লেআউটের জন্য লজিক প্রদান করে।

  • প্যানেলের ভূমিকা: প্রতিটি গন্তব্যকে মেটাডেটা ব্যবহার করে একটি ভূমিকা অ্যাসাইন করা হয়:

    • SupportingPaneSceneStrategy.mainPane(): মূল কন্টেন্টের জন্য। এই প্যানেল সবসময় দেখা যায়।
    • SupportingPaneSceneStrategy.supportingPane(): অতিরিক্ত কন্টেন্টের জন্য। বড় স্ক্রিনে মূল প্যানেলের পাশে এই প্যানেল দেখানো হয়।
    • SupportingPaneSceneStrategy.extraPane(): আরও বড় স্ক্রিনে সহায়ক প্যানেলের পাশাপাশি দেখানো যায় এমন টারশিয়ারি কন্টেন্টের জন্য।
  • অ্যাডাপ্টিভ লেআউট: SupportingPaneSceneStrategy অটোমেটিক লেআউট ম্যানেজ করে। ছোট স্ক্রিনে, শুধুমাত্র প্রধান প্যানেল দেখানো হয়। আরও বড় স্ক্রিনে, মূল প্যানেলের পাশে সহায়ক প্যানেল দেখানো হয়।

  • ব্যাক নেভিগেশন: এই উদাহরণে BackNavigationBehavior কাস্টমাইজ করে PopUntilCurrentDestinationChange করা হয়েছে। এর অর্থ হল, ব্যবহারকারী 'ব্যাক' বোতাম প্রেস করলে, সহায়ক প্যানটি সরিয়ে দেওয়া হবে এবং তার নিচে থাকা মূল প্যানটি দেখা যাবে।

  • নেভিগেশন: ব্যাক স্ট্যাক থেকে গন্তব্য যোগ করা ও সরিয়ে দেওয়ার মাধ্যমে নেভিগেশন ম্যানেজ করা হয়। SupportingPaneSceneStrategy এইসব পরিবর্তন লক্ষ্য করে এবং সেই অনুযায়ী লেআউট অ্যাডজাস্ট করে।

package com.example.nav3recipes.material.supportingpane

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

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.material3.adaptive.ExperimentalMaterial3AdaptiveApi
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfoV2
import androidx.compose.material3.adaptive.layout.calculatePaneScaffoldDirective
import androidx.compose.material3.adaptive.navigation.BackNavigationBehavior
import androidx.compose.material3.adaptive.navigation3.SupportingPaneSceneStrategy
import androidx.compose.material3.adaptive.navigation3.rememberSupportingPaneSceneStrategy
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.dropUnlessResumed
import androidx.navigation3.runtime.NavKey
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.runtime.rememberNavBackStack
import androidx.navigation3.ui.NavDisplay
import com.example.nav3recipes.content.ContentBlue
import com.example.nav3recipes.content.ContentGreen
import com.example.nav3recipes.content.ContentRed
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
import kotlinx.serialization.Serializable

@Serializable
private object MainVideo : NavKey

@Serializable
private data object RelatedVideos : NavKey

@Serializable
private data object Profile : NavKey

class MaterialSupportingPaneActivity : ComponentActivity() {

    @OptIn(ExperimentalMaterial3AdaptiveApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        setEdgeToEdgeConfig()
        super.onCreate(savedInstanceState)

        setContent {

            val backStack = rememberNavBackStack(MainVideo)

            // Override the defaults so that there isn't a horizontal or vertical space between the panes.
            // See b/444438086
            val windowAdaptiveInfo = currentWindowAdaptiveInfoV2()
            val directive = remember(windowAdaptiveInfo) {
                calculatePaneScaffoldDirective(windowAdaptiveInfo)
                    .copy(horizontalPartitionSpacerSize = 0.dp, verticalPartitionSpacerSize = 0.dp)
            }

            // Override the defaults so that the supporting pane can be dismissed by pressing back.
            // See b/445826749
            val supportingPaneStrategy = rememberSupportingPaneSceneStrategy<NavKey>(
                backNavigationBehavior = BackNavigationBehavior.PopUntilCurrentDestinationChange,
                directive = directive
            )

            NavDisplay(
                backStack = backStack,
                onBack = { backStack.removeLastOrNull() },
                sceneStrategies = listOf(supportingPaneStrategy),
                entryProvider = entryProvider {
                    entry<MainVideo>(
                        metadata = SupportingPaneSceneStrategy.mainPane()
                    ) {
                        ContentRed("Video content") {
                            Button(onClick = dropUnlessResumed {
                                backStack.add(RelatedVideos)
                            }) {
                                Text("View related videos")
                            }
                        }
                    }
                    entry<RelatedVideos>(
                        metadata = SupportingPaneSceneStrategy.supportingPane()
                    ) {
                        ContentBlue("Related videos") {
                            Column(horizontalAlignment = Alignment.CenterHorizontally) {
                                Button(onClick = dropUnlessResumed {
                                    backStack.add(Profile)
                                }) {
                                    Text("View profile")
                                }
                            }
                        }
                    }
                    entry<Profile>(
                        metadata = SupportingPaneSceneStrategy.extraPane()
                    ) {
                        ContentGreen("Profile")
                    }
                }
            )
        }
    }
}