ViewModel Scoping APIs Android Jetpack- এর অংশ।

ViewModels কার্যকরভাবে ব্যবহার করার জন্য স্কোপ হল চাবিকাঠি। প্রতিটি ViewModel একটি বস্তুর জন্য স্কোপ করা হয় যা ViewModelStoreOwner ইন্টারফেস প্রয়োগ করে। অনেকগুলি API রয়েছে যা আপনাকে আপনার ভিউমডেলগুলির সুযোগ আরও সহজে পরিচালনা করতে দেয়৷ এই নথিতে কিছু মূল কৌশলের রূপরেখা দেওয়া হয়েছে যা আপনার জানা উচিত।

ViewModelProvider.get() পদ্ধতি আপনাকে যেকোন ViewModelStoreOwner এ স্কোপ করা একটি ViewModel-এর একটি উদাহরণ পেতে দেয়। Kotlin ব্যবহারকারীদের জন্য, সবচেয়ে সাধারণ ব্যবহারের ক্ষেত্রে বিভিন্ন এক্সটেনশন ফাংশন উপলব্ধ। সমস্ত Kotlin এক্সটেনশন ফাংশন বাস্তবায়ন হুডের অধীনে ViewModelProvider API ব্যবহার করে।

নিকটতম ViewModelStoreOwner পর্যন্ত ViewModels স্কোপ করা হয়েছে

আপনি একটি ক্রিয়াকলাপ, খণ্ড বা একটি নেভিগেশন গ্রাফের গন্তব্যে একটি ViewModel স্কোপ করতে পারেন। Activity, Fragment এবং নেভিগেশন লাইব্রেরি দ্বারা প্রদত্ত viewModels() এক্সটেনশন ফাংশন এবং Compose এ viewModel() ফাংশন আপনাকে নিকটতম ViewModelStoreOwner এর স্কোপ করা ViewModel-এর একটি উদাহরণ পেতে দেয়।

ভিউ

import androidx.activity.viewModels

class MyActivity : AppCompatActivity() {
    // ViewModel API available in activity.activity-ktx
    // The ViewModel is scoped to `this` Activity
    val viewModel: MyViewModel by viewModels()
}

import androidx.fragment.app.viewModels

class MyFragment : Fragment() {
    // ViewModel API available in fragment.fragment-ktx
    // The ViewModel is scoped to `this` Fragment
    val viewModel: MyViewModel by viewModels()
}

ভিউ

import androidx.lifecycle.ViewModelProvider;

public class MyActivity extends AppCompatActivity {
    // The ViewModel is scoped to `this` Activity
    MyViewModel viewModel = new ViewModelProvider(this).get(MyViewModel.class);
}

public class MyFragment extends Fragment {
    // The ViewModel is scoped to `this` Fragment
    MyViewModel viewModel = new ViewModelProvider(this).get(MyViewModel.class);
}

রচনা করা

import androidx.lifecycle.viewmodel.compose.viewModel

@Composable
fun MyScreen(
    modifier: Modifier = Modifier,
    // ViewModel API available in lifecycle.lifecycle-viewmodel-compose
    // The ViewModel is scoped to the closest ViewModelStoreOwner provided
    // via the LocalViewModelStoreOwner CompositionLocal. In order of proximity,
    // this could be the destination of a Navigation graph, the host Fragment,
    // or the host Activity.
    viewModel: MyViewModel = viewModel()
) { /* ... */ }

ViewModels যেকোন ViewModelStoreOwner এর জন্য স্কোপ করা হয়েছে

View সিস্টেমে ComponentActivity.viewModels() এবং Fragment.viewModels() ফাংশন এবং Compose এ viewModel() ফাংশন একটি ঐচ্ছিক ownerProducer প্যারামিটার নেয় যা আপনি কোন ViewModelStoreOwner এর জন্য ViewModel-এর উদাহরণ স্কোপ করা হয়েছে তা নির্দিষ্ট করতে ব্যবহার করতে পারেন। নিম্নলিখিত নমুনাটি দেখায় কিভাবে একটি ভিউমডেল প্যারেন্ট ফ্র্যাগমেন্টে স্কোপ করা যায়:

ভিউ

import androidx.fragment.app.viewModels

class MyFragment : Fragment() {

    // ViewModel API available in fragment.fragment-ktx
    // The ViewModel is scoped to the parent of `this` Fragment
    val viewModel: SharedViewModel by viewModels(
        ownerProducer = { requireParentFragment() }
    )
}

ভিউ

import androidx.lifecycle.ViewModelProvider;

public class MyFragment extends Fragment {

    SharedViewModel viewModel;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // The ViewModel is scoped to the parent of `this` Fragment
        viewModel = new ViewModelProvider(requireParentFragment())
            .get(SharedViewModel.class);
    }
}

রচনা করা

import androidx.lifecycle.viewmodel.compose.viewModel

@Composable
fun MyScreen(
    context: Context = LocalContext.current,
    // ViewModel API available in lifecycle.lifecycle-viewmodel-compose
    // The ViewModel is scoped to the parent of the host Fragment
    // where this composable function is called
    viewModel: SharedViewModel = viewModel(
        viewModelStoreOwner = (context as Fragment).requireParentFragment()
    )
) { /* ... */ }

একটি ফ্র্যাগমেন্ট থেকে একটি অ্যাক্টিভিটি-স্কোপড ভিউমডেল পাওয়া একটি সাধারণ ব্যবহারের ক্ষেত্রে। এটি করার সময়, activityViewModels() ভিউ এক্সটেনশন ফাংশনটি ব্যবহার করুন। আপনি যদি ভিউ এবং কোটলিন ব্যবহার না করে থাকেন, তাহলে আপনি উপরের মত একই API ব্যবহার করতে পারেন এবং সঠিক মালিককে পাস করে।

ভিউ

import androidx.fragment.app.activityViewModels

class MyFragment : Fragment() {

    // ViewModel API available in fragment.fragment-ktx
    // The ViewModel is scoped to the host Activity
    val viewModel: SharedViewModel by activityViewModels()
}

ভিউ

import androidx.lifecycle.ViewModelProvider;

public class MyFragment extends Fragment {

    SharedViewModel viewModel;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // The ViewModel is scoped to the host Activity
        viewModel = new ViewModelProvider(requireActivity())
            .get(SharedViewModel.class);
    }
}

রচনা করা

import androidx.lifecycle.viewmodel.compose.viewModel

@Composable
fun MyScreen(
    context: Context = LocalContext.current,
    // ViewModel API available in lifecycle.lifecycle-viewmodel-compose
    // The ViewModel is scoped to the Activity of the host Fragment
    // where this composable function is called
    viewModel: SharedViewModel = viewModel(
        viewModelStoreOwner = (context as Fragment).requireActivity()
    )
) { /* ... */ }

ন্যাভিগেশন গ্রাফ পর্যন্ত ViewModels স্কোপ

নেভিগেশন গ্রাফগুলিও ViewModel স্টোরের মালিক৷ আপনি যদি নেভিগেশন ফ্র্যাগমেন্ট বা নেভিগেশন কম্পোজ ব্যবহার করেন, তাহলে আপনি navGraphViewModels(graphId) ভিউ এক্সটেনশন ফাংশন সহ একটি নেভিগেশন গ্রাফে স্কোপ করা একটি ভিউমডেলের একটি উদাহরণ পেতে পারেন৷

ভিউ

import androidx.navigation.navGraphViewModels

class MyFragment : Fragment() {

    // ViewModel API available in navigation.navigation-fragment
    // The ViewModel is scoped to the `nav_graph` Navigation graph
    val viewModel: SharedViewModel by navGraphViewModels(R.id.nav_graph)

    // Equivalent navGraphViewModels code using the viewModels API
    val viewModel: SharedViewModel by viewModels(
        { findNavController().getBackStackEntry(R.id.nav_graph) }
    )
}

ভিউ

import androidx.lifecycle.ViewModelProvider;

public class MyFragment extends Fragment {

    SharedViewModel viewModel;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        NavController navController = NavHostFragment.findNavController(this);
        NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.my_graph);

        // The ViewModel is scoped to the `nav_graph` Navigation graph
        viewModel = new ViewModelProvider(backStackEntry).get(SharedViewModel.class);
    }
}

রচনা করা

import androidx.lifecycle.viewmodel.compose.viewModel

@Composable
fun MyAppNavHost() {
    // ...
    composable("myScreen") { backStackEntry ->
        // Retrieve the NavBackStackEntry of "parentNavigationRoute"
        val parentEntry = remember(backStackEntry) {
            navController.getBackStackEntry("parentNavigationRoute")
        }
        // Get the ViewModel scoped to the `parentNavigationRoute` Nav graph
        val parentViewModel: SharedViewModel = viewModel(parentEntry)
        // ...
    }
}

আপনি যদি জেটপ্যাক নেভিগেশন ছাড়াও হিল্ট ব্যবহার করেন, আপনি নিম্নরূপ hiltNavGraphViewModels(graphId) API ব্যবহার করতে পারেন।

ভিউ

import androidx.hilt.navigation.fragment.hiltNavGraphViewModels

class MyFragment : Fragment() {

    // ViewModel API available in hilt.hilt-navigation-fragment
    // The ViewModel is scoped to the `nav_graph` Navigation graph
    // and is provided using the Hilt-generated ViewModel factory
    val viewModel: SharedViewModel by hiltNavGraphViewModels(R.id.nav_graph)
}

ভিউ

import androidx.hilt.navigation.HiltViewModelFactory;
import androidx.lifecycle.ViewModelProvider;

public class MyFragment extends Fragment {

    SharedViewModel viewModel;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        NavController navController = NavHostFragment.findNavController(this);
        NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.my_graph);

        // The ViewModel is scoped to the `nav_graph` Navigation graph
        // and is provided using the Hilt-generated ViewModel factory
        viewModel = new ViewModelProvider(
            backStackEntry,
            HiltViewModelFactory.create(getContext(), backStackEntry)
        ).get(SharedViewModel.class);
    }
}

রচনা করা

import androidx.hilt.navigation.compose.hiltViewModel

@Composable
fun MyAppNavHost() {
    // ...
    composable("myScreen") { backStackEntry ->
        val parentEntry = remember(backStackEntry) {
            navController.getBackStackEntry("parentNavigationRoute")
        }

        // ViewModel API available in hilt.hilt-navigation-compose
        // The ViewModel is scoped to the `parentNavigationRoute` Navigation graph
        // and is provided using the Hilt-generated ViewModel factory
        val parentViewModel: SharedViewModel = hiltViewModel(parentEntry)
        // ...
    }
}
{% শব্দার্থে %} {% endverbatim %} {% শব্দার্থে %} {% endverbatim %}