মডুলার নেভিগেশন রেসিপি (হিল্ট)

এই রেসিপিটি দেখায় কিভাবে নেভিগেশন 3 এবং ড্যাগার/হিল্ট ব্যবহার করে নির্ভরতা ইনজেকশনের জন্য একটি মাল্টি-মডিউল অ্যাপ্লিকেশন গঠন করা যায়। লক্ষ্য হল একটি ডিকপলড আর্কিটেকচার তৈরি করা যেখানে নেভিগেশনকে পৃথক বৈশিষ্ট্য মডিউলে সংজ্ঞায়িত এবং প্রয়োগ করা হয়।

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

অ্যাপ্লিকেশনটি কয়েকটি মডিউলে বিভক্ত:

  • app মডিউল : এটি হল প্রধান অ্যাপ্লিকেশন মডিউল। এটি একটি সাধারণ Navigator চালু করে এবং বৈশিষ্ট্য মডিউলগুলি থেকে EntryProviderInstaller এর একটি সেট ইনজেক্ট করে। এরপর এটি NavDisplay এর জন্য চূড়ান্ত entryProvider তৈরি করতে এই ইনস্টলারগুলি ব্যবহার করে।

  • common মডিউল : এই মডিউলটিতে মূল নেভিগেশন লজিক রয়েছে, যার মধ্যে রয়েছে:

    • একটি Navigator ক্লাস যা ব্যাক স্ট্যাক পরিচালনা করে।
    • একটি EntryProviderInstaller টাইপ, যা এমন একটি ফাংশন যা বৈশিষ্ট্য মডিউলগুলি অ্যাপ্লিকেশনের entryProvider এ তাদের নেভিগেশন এন্ট্রি অবদান রাখতে ব্যবহার করে।
  • বৈশিষ্ট্য মডিউল (যেমন, conversation , profile ) : প্রতিটি বৈশিষ্ট্য দুটি উপ-মডিউলে বিভক্ত:

    • api মডিউল : বৈশিষ্ট্যটির জন্য পাবলিক API সংজ্ঞায়িত করে, যার মধ্যে এর নেভিগেশন রুটও অন্তর্ভুক্ত। এটি অন্যান্য মডিউলগুলিকে এর বাস্তবায়নের বিশদ সম্পর্কে না জেনেই এই বৈশিষ্ট্যটিতে নেভিগেট করার অনুমতি দেয়।
    • impl মডিউল : বৈশিষ্ট্যটির বাস্তবায়ন প্রদান করে, যার মধ্যে রয়েছে এর কম্পোজেবল এবং একটি EntryProviderInstaller যা বৈশিষ্ট্যটির রুটগুলিকে এর কম্পোজেবলগুলিতে ম্যাপ করে। এই ইনস্টলারটি তারপর Dagger/Hilt ব্যবহার করে app মডিউলে সরবরাহ করা হয়।

এই মডুলার পদ্ধতির মাধ্যমে সমস্যাগুলির একটি পরিষ্কার বিচ্ছেদ সম্ভব, যা কোডবেসকে আরও স্কেলেবল এবং রক্ষণাবেক্ষণযোগ্য করে তোলে। প্রতিটি বৈশিষ্ট্য তার নিজস্ব নেভিগেশন লজিকের জন্য দায়ী, এবং app মডিউলটি কেবল এই অংশগুলিকে একত্রিত করে।

package com.example.nav3recipes.modular.hilt

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import dagger.multibindings.IntoSet

// API
object Profile

// IMPLEMENTATION
@Module
@InstallIn(ActivityRetainedComponent::class)
object ProfileModule {

    @IntoSet
    @Provides
    fun provideEntryProviderInstaller() : EntryProviderInstaller = {
        entry<Profile>{
            ProfileScreen()
        }
    }
}

@Composable
private fun ProfileScreen() {
    val profileColor = MaterialTheme.colorScheme.surfaceVariant
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(profileColor)
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text(
            text = "Profile Screen",
            style = MaterialTheme.typography.headlineMedium,
            color = MaterialTheme.colorScheme.onSurface
        )
    }
}
package com.example.nav3recipes.modular.hilt

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.ui.Modifier
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.ui.NavDisplay
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class HiltModularActivity : ComponentActivity() {

    @Inject
    lateinit var navigator: Navigator

    @Inject
    lateinit var entryProviderScopes: Set<@JvmSuppressWildcards EntryProviderInstaller>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setEdgeToEdgeConfig()
        setContent {
            Scaffold { paddingValues ->
                NavDisplay(
                    backStack = navigator.backStack,
                    modifier = Modifier.padding(paddingValues),
                    onBack = { navigator.goBack() },
                    entryProvider = entryProvider {
                        entryProviderScopes.forEach { builder -> this.builder() }
                    }
                )
            }
        }
    }
}
package com.example.nav3recipes.modular.hilt

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.dropUnlessResumed
import com.example.nav3recipes.ui.theme.colors
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import dagger.multibindings.IntoSet

// API
object ConversationList
data class ConversationDetail(val id: Int) {
    val color: Color
        get() = colors[id % colors.size]
}

// IMPL
@Module
@InstallIn(ActivityRetainedComponent::class)
object ConversationModule {

    @IntoSet
    @Provides
    fun provideEntryProviderInstaller(navigator: Navigator): EntryProviderInstaller =
        {
            entry<ConversationList> {
                ConversationListScreen(
                    onConversationClicked = { conversationDetail ->
                        navigator.goTo(conversationDetail)
                    }
                )
            }
            entry<ConversationDetail> { key ->
                ConversationDetailScreen(key) { navigator.goTo(Profile) }
            }
        }
}

@Composable
private fun ConversationListScreen(
    onConversationClicked: (ConversationDetail) -> Unit
) {
    LazyColumn(
        modifier = Modifier.fillMaxSize(),
    ) {
        items(10) { index ->
            val conversationId = index + 1
            val conversationDetail = ConversationDetail(conversationId)
            val backgroundColor = conversationDetail.color
            ListItem(
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable(onClick = dropUnlessResumed {
                        onConversationClicked(conversationDetail)
                    }),
                headlineContent = {
                    Text(
                        text = "Conversation $conversationId",
                        style = MaterialTheme.typography.headlineSmall,
                        color = MaterialTheme.colorScheme.onSurface
                    )
                },
                colors = ListItemDefaults.colors(
                    containerColor = backgroundColor // Set container color directly
                )
            )
        }
    }
}

@Composable
private fun ConversationDetailScreen(
    conversationDetail: ConversationDetail,
    onProfileClicked: () -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(conversationDetail.color)
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text(
            text = "Conversation Detail Screen: ${conversationDetail.id}",
            style = MaterialTheme.typography.headlineMedium,
            color = MaterialTheme.colorScheme.onSurface
        )
        Spacer(modifier = Modifier.height(16.dp))
        Button(onClick = dropUnlessResumed(block = onProfileClicked)) {
            Text("View Profile")
        }
    }
}
package com.example.nav3recipes.modular.hilt

import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.navigation3.runtime.EntryProviderScope
import dagger.hilt.android.scopes.ActivityRetainedScoped


typealias EntryProviderInstaller = EntryProviderScope<Any>.() -> Unit

@ActivityRetainedScoped
class Navigator(startDestination: Any) {
    val backStack : SnapshotStateList<Any> = mutableStateListOf(startDestination)

    fun goTo(destination: Any){
        backStack.add(destination)
    }

    fun goBack(){
        backStack.removeLastOrNull()
    }
}
package com.example.nav3recipes.modular.hilt

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import dagger.hilt.android.scopes.ActivityRetainedScoped

@Module
@InstallIn(ActivityRetainedComponent::class)
object AppModule {

    @Provides
    @ActivityRetainedScoped
    fun provideNavigator() : Navigator = Navigator(startDestination = ConversationList)
}