লোডিং স্টেট (ভিউ) ম্যানেজ ও উপস্থাপন করা

ধারণা ও Jetpack Compose-এর প্রয়োগ

পেজিং লাইব্রেরি পেজ করা ডেটার জন্য লোড অনুরোধের স্ট্যাটাস ট্র্যাক করে এবং LoadState ক্লাসের মাধ্যমে এটি প্রকাশ করে। বর্তমান স্ট্যাটাস সম্পর্কে তথ্য পেতে এবং সেই অনুযায়ী UI আপডেট করতে, আপনার অ্যাপ PagingDataAdapter-এর সাথে লিসেনার রেজিস্টার করতে পারে। এই স্টেটগুলি অ্যাডাপ্টার থেকে প্রদান করা হয় কারণ সেগুলি UI-এর সাথে সিঙ্ক করা থাকে। এর অর্থ হল, UI-তে পৃষ্ঠা লোড প্রয়োগ করা হলে আপনার লিসেনার আপডেট পায়।

প্রতিটি LoadType এবং ডেটা সোর্সের ধরনের (PagingSource অথবা RemoteMediator) জন্য আলাদা LoadState সিগন্যাল প্রদান করা হয়। লিসনারের মাধ্যমে প্রদান করা CombinedLoadStates অবজেক্ট, এইসব সিগন্যাল থেকে লোডিং স্টেট সম্পর্কে তথ্য প্রদান করে। আপনি এই বিস্তারিত তথ্য ব্যবহার করে আপনার ব্যবহারকারীদের জন্য উপযুক্ত লোডিং ইন্ডিকেটর দেখাতে পারেন।

রাজ্য লোড করা হচ্ছে

পেজিং লাইব্রেরি LoadState অবজেক্টের মাধ্যমে UI-তে ব্যবহারের জন্য লোডিং স্টেট প্রকাশ করে। LoadState অবজেক্ট বর্তমান লোডিং অবস্থার উপর নির্ভর করে তিনটি ফর্মের মধ্যে একটি নেয়:

  • কোনও অ্যাক্টিভ লোড অপারেশন না থাকলে এবং কোনও সমস্যা না হলে, LoadState হল একটি LoadState.NotLoading অবজেক্ট। এই সাবক্লাসে endOfPaginationReached প্রপার্টিও অন্তর্ভুক্ত থাকে, যা পেজিনেশনের শেষ প্রান্তে পৌঁছেছে কিনা তা নির্দেশ করে।
  • কোনও অ্যাক্টিভ লোড অপারেশন থাকলে, LoadState হল একটি LoadState.Loading অবজেক্ট।
  • কোনও সমস্যা হলে, LoadState হল একটি LoadState.Error অবজেক্ট।

আপনার UI-তে LoadState ব্যবহার করার দুটি উপায় আছে: লিসেনার ব্যবহার করে অথবা RecyclerView তালিকায় সরাসরি লোডিং স্টেট দেখানোর জন্য বিশেষ লিস্ট অ্যাডাপ্টার ব্যবহার করে।

লিসনারের মাধ্যমে লোডিং স্ট্যাটাস অ্যাক্সেস করুন

আপনার UI-তে সাধারণ ব্যবহারের জন্য লোডিং স্টেট পেতে, আপনার PagingDataAdapter-এর মাধ্যমে প্রদান করা loadStateFlow স্ট্রিম বা addLoadStateListener() পদ্ধতি ব্যবহার করুন। এইসব মেকানিজম CombinedLoadStates অবজেক্টে অ্যাক্সেস প্রদান করে, যার মধ্যে প্রতিটি লোডের ধরনের জন্য LoadState আচরণ সম্পর্কে তথ্য অন্তর্ভুক্ত থাকে।

নিম্নলিখিত উদাহরণে, রিফ্রেশ লোডের বর্তমান অবস্থার উপর নির্ভর করে PagingDataAdapter বিভিন্ন UI কম্পোনেন্ট দেখায়:

Kotlin

// Activities can use lifecycleScope directly, but Fragments should instead use
// viewLifecycleOwner.lifecycleScope.
lifecycleScope.launch {
  pagingAdapter.loadStateFlow.collectLatest { loadStates ->
    progressBar.isVisible = loadStates.refresh is LoadState.Loading
    retry.isVisible = loadState.refresh !is LoadState.Loading
    errorMsg.isVisible = loadState.refresh is LoadState.Error
  }
}

জাভা

pagingAdapter.addLoadStateListener(loadStates -> {
  progressBar.setVisibility(loadStates.refresh instanceof LoadState.Loading
    ? View.VISIBLE : View.GONE);
  retry.setVisibility(loadStates.refresh instanceof LoadState.Loading
    ? View.GONE : View.VISIBLE);
  errorMsg.setVisibility(loadStates.refresh instanceof LoadState.Error
    ? View.VISIBLE : View.GONE);
});

জাভা

pagingAdapter.addLoadStateListener(loadStates -> {
  progressBar.setVisibility(loadStates.refresh instanceof LoadState.Loading
    ? View.VISIBLE : View.GONE);
  retry.setVisibility(loadStates.refresh instanceof LoadState.Loading
    ? View.GONE : View.VISIBLE);
  errorMsg.setVisibility(loadStates.refresh instanceof LoadState.Error
    ? View.VISIBLE : View.GONE);
});

CombinedLoadStates সম্পর্কে আরও তথ্যের জন্য, অতিরিক্ত লোডিং স্টেট সংক্রান্ত তথ্য অ্যাক্সেস করুন দেখুন।

অ্যাডাপ্টারের সাহায্যে লোডিং স্টেট দেখানো

পেজিং লাইব্রেরি আরেকটি লিস্ট অ্যাডাপ্টার প্রদান করে, যাকে LoadStateAdapter বলা হয়। এর উদ্দেশ্য হল, পেজ করা ডেটার দেখানো তালিকায় সরাসরি লোডিং স্ট্যাটাস দেখানো। এই অ্যাডাপ্টার তালিকার বর্তমান লোড স্টেট অ্যাক্সেস করার সুবিধা প্রদান করে, যা আপনি তথ্য দেখানো কাস্টম ভিউ হোল্ডারে পাস করতে পারবেন।

প্রথমে, একটি ভিউ হোল্ডার ক্লাস তৈরি করুন যা আপনার স্ক্রিনে লোডিং এবং ত্রুটি ভিউয়ের রেফারেন্স রাখে। একটি bind() ফাংশন তৈরি করো যা প্যারামিটার হিসেবে LoadState গ্রহণ করে। এই ফাংশনটি লোড স্টেট প্যারামিটারের উপর ভিত্তি করে ভিউয়ের দৃশ্যমানতা টগল করবে:

Kotlin

class LoadStateViewHolder(
  parent: ViewGroup,
  retry: () -> Unit
) : RecyclerView.ViewHolder(
  LayoutInflater.from(parent.context)
    .inflate(R.layout.load_state_item, parent, false)
) {
  private val binding = LoadStateItemBinding.bind(itemView)
  private val progressBar: ProgressBar = binding.progressBar
  private val errorMsg: TextView = binding.errorMsg
  private val retry: Button = binding.retryButton
    .also {
      it.setOnClickListener { retry() }
    }

  fun bind(loadState: LoadState) {
    if (loadState is LoadState.Error) {
      errorMsg.text = loadState.error.localizedMessage
    }

    progressBar.isVisible = loadState is LoadState.Loading
    retry.isVisible = loadState is LoadState.Error
    errorMsg.isVisible = loadState is LoadState.Error
  }
}

জাভা

class LoadStateViewHolder extends RecyclerView.ViewHolder {
  private ProgressBar mProgressBar;
  private TextView mErrorMsg;
  private Button mRetry;

  LoadStateViewHolder(
    @NonNull ViewGroup parent,
    @NonNull View.OnClickListener retryCallback) {
    super(LayoutInflater.from(parent.getContext())
      .inflate(R.layout.load_state_item, parent, false));

    LoadStateItemBinding binding = LoadStateItemBinding.bind(itemView);
    mProgressBar = binding.progressBar;
    mErrorMsg = binding.errorMsg;
    mRetry = binding.retryButton;
  }

  public void bind(LoadState loadState) {
    if (loadState instanceof LoadState.Error) {
      LoadState.Error loadStateError = (LoadState.Error) loadState;
      mErrorMsg.setText(loadStateError.getError().getLocalizedMessage());
    }
    mProgressBar.setVisibility(loadState instanceof LoadState.Loading
      ? View.VISIBLE : View.GONE);
    mRetry.setVisibility(loadState instanceof LoadState.Error
      ? View.VISIBLE : View.GONE);
    mErrorMsg.setVisibility(loadState instanceof LoadState.Error
      ? View.VISIBLE : View.GONE);
  }
}

জাভা

class LoadStateViewHolder extends RecyclerView.ViewHolder {
  private ProgressBar mProgressBar;
  private TextView mErrorMsg;
  private Button mRetry;

  LoadStateViewHolder(
    @NonNull ViewGroup parent,
    @NonNull View.OnClickListener retryCallback) {
    super(LayoutInflater.from(parent.getContext())
      .inflate(R.layout.load_state_item, parent, false));

    LoadStateItemBinding binding = LoadStateItemBinding.bind(itemView);
    mProgressBar = binding.progressBar;
    mErrorMsg = binding.errorMsg;
    mRetry = binding.retryButton;
  }

  public void bind(LoadState loadState) {
    if (loadState instanceof LoadState.Error) {
      LoadState.Error loadStateError = (LoadState.Error) loadState;
      mErrorMsg.setText(loadStateError.getError().getLocalizedMessage());
    }
    mProgressBar.setVisibility(loadState instanceof LoadState.Loading
      ? View.VISIBLE : View.GONE);
    mRetry.setVisibility(loadState instanceof LoadState.Error
      ? View.VISIBLE : View.GONE);
    mErrorMsg.setVisibility(loadState instanceof LoadState.Error
      ? View.VISIBLE : View.GONE);
  }
}

এরপর, LoadStateAdapter প্রয়োগ করে এমন একটি ক্লাস তৈরি করুন এবং onCreateViewHolder() ও onBindViewHolder() পদ্ধতিগুলি সংজ্ঞায়িত করুন। এইসব পদ্ধতি আপনার কাস্টম ভিউ হোল্ডারের একটি ইনস্ট্যান্স তৈরি করে এবং সংশ্লিষ্ট লোড স্টেটকে বাইন্ড করে।

Kotlin

// Adapter that displays a loading spinner when
// state is LoadState.Loading, and an error message and retry
// button when state is LoadState.Error.
class ExampleLoadStateAdapter(
  private val retry: () -> Unit
) : LoadStateAdapter<LoadStateViewHolder>() {

  override fun onCreateViewHolder(
    parent: ViewGroup,
    loadState: LoadState
  ) = LoadStateViewHolder(parent, retry)

  override fun onBindViewHolder(
    holder: LoadStateViewHolder,
    loadState: LoadState
  ) = holder.bind(loadState)
}

জাভা

// Adapter that displays a loading spinner when
// state is LoadState.Loading, and an error message and retry
// button when state is LoadState.Error.
class ExampleLoadStateAdapter extends LoadStateAdapter<LoadStateViewHolder> {
  private View.OnClickListener mRetryCallback;

  ExampleLoadStateAdapter(View.OnClickListener retryCallback) {
    mRetryCallback = retryCallback;
  }

  @NotNull
  @Override
  public LoadStateViewHolder onCreateViewHolder(@NotNull ViewGroup parent,
    @NotNull LoadState loadState) {
    return new LoadStateViewHolder(parent, mRetryCallback);
  }

  @Override
  public void onBindViewHolder(@NotNull LoadStateViewHolder holder,
    @NotNull LoadState loadState) {
    holder.bind(loadState);
  }
}

জাভা

// Adapter that displays a loading spinner when
// state is LoadState.Loading, and an error message and retry
// button when state is LoadState.Error.
class ExampleLoadStateAdapter extends LoadStateAdapter<LoadStateViewHolder> {
  private View.OnClickListener mRetryCallback;

  ExampleLoadStateAdapter(View.OnClickListener retryCallback) {
    mRetryCallback = retryCallback;
  }

  @NotNull
  @Override
  public LoadStateViewHolder onCreateViewHolder(@NotNull ViewGroup parent,
    @NotNull LoadState loadState) {
    return new LoadStateViewHolder(parent, mRetryCallback);
  }

  @Override
  public void onBindViewHolder(@NotNull LoadStateViewHolder holder,
    @NotNull LoadState loadState) {
    holder.bind(loadState);
  }
}

হেডার ও ফুটারে লোডিং প্রগ্রেস দেখাতে, আপনার PagingDataAdapter অবজেক্ট থেকে withLoadStateHeaderAndFooter() মেথড কল করুন:

Kotlin

pagingAdapter
  .withLoadStateHeaderAndFooter(
    header = ExampleLoadStateAdapter(adapter::retry),
    footer = ExampleLoadStateAdapter(adapter::retry)
  )

জাভা

pagingAdapter
  .withLoadStateHeaderAndFooter(
    new ExampleLoadStateAdapter(pagingAdapter::retry),
    new ExampleLoadStateAdapter(pagingAdapter::retry));

জাভা

pagingAdapter
  .withLoadStateHeaderAndFooter(
    new ExampleLoadStateAdapter(pagingAdapter::retry),
    new ExampleLoadStateAdapter(pagingAdapter::retry));

আপনি এর পরিবর্তে withLoadStateHeader() বা withLoadStateFooter() কল করতে পারেন, যদি আপনি চান যে RecyclerView তালিকাটি শুধুমাত্র হেডার বা শুধুমাত্র ফুটারে লোডিং স্ট্যাটাস দেখাক।

অতিরিক্ত লোডিং স্ট্যাটাস সংক্রান্ত তথ্য অ্যাক্সেস করুন

PagingDataAdapter থেকে CombinedLoadStates অবজেক্ট আপনার PagingSource ইমপ্লিমেন্টেশন এবং RemoteMediator ইমপ্লিমেন্টেশনের (যদি থাকে) লোড স্টেট সম্পর্কে তথ্য প্রদান করে।

সুবিধার জন্য, আপনি উপযুক্ত লোড টাইপের জন্য LoadState অবজেক্ট অ্যাক্সেস করতে CombinedLoadStates থেকে refresh, append এবং prepend প্রপার্টি ব্যবহার করতে পারেন। এইসব প্রপার্টি সাধারণত RemoteMediator ইমপ্লিমেন্টেশন থেকে লোড স্টেটকে ডিফার করে, যদি কোনও একটি থাকে; অন্যথায়, এগুলিতে PagingSource ইমপ্লিমেন্টেশন থেকে উপযুক্ত লোড স্টেট থাকে। অন্তর্নিহিত লজিক সম্পর্কে আরও বিস্তারিত তথ্যের জন্য, CombinedLoadStates-এর রেফারেন্স ডকুমেন্টেশন দেখুন।

Kotlin

lifecycleScope.launch {
  pagingAdapter.loadStateFlow.collectLatest { loadStates ->
    // Observe refresh load state from RemoteMediator if present, or
    // from PagingSource otherwise.
    refreshLoadState: LoadState = loadStates.refresh
    // Observe prepend load state from RemoteMediator if present, or
    // from PagingSource otherwise.
    prependLoadState: LoadState = loadStates.prepend
    // Observe append load state from RemoteMediator if present, or
    // from PagingSource otherwise.
    appendLoadState: LoadState = loadStates.append
  }
}

জাভা

pagingAdapter.addLoadStateListener(loadStates -> {
  // Observe refresh load state from RemoteMediator if present, or
  // from PagingSource otherwise.
  LoadState refreshLoadState = loadStates.refresh;
  // Observe prepend load state from RemoteMediator if present, or
  // from PagingSource otherwise.
  LoadState prependLoadState = loadStates.prepend;
  // Observe append load state from RemoteMediator if present, or
  // from PagingSource otherwise.
  LoadState appendLoadState = loadStates.append;
});

জাভা

pagingAdapter.addLoadStateListener(loadStates -> {
  // Observe refresh load state from RemoteMediator if present, or
  // from PagingSource otherwise.
  LoadState refreshLoadState = loadStates.refresh;
  // Observe prepend load state from RemoteMediator if present, or
  // from PagingSource otherwise.
  LoadState prependLoadState = loadStates.prepend;
  // Observe append load state from RemoteMediator if present, or
  // from PagingSource otherwise.
  LoadState appendLoadState = loadStates.append;
});

তবে, এটি মনে রাখা গুরুত্বপূর্ণ যে শুধুমাত্র PagingSource লোড স্টেট UI আপডেটের সাথে সিঙ্ক হওয়ার গ্যারান্টি দেয়। যেহেতু refresh, append এবং prepend প্রপার্টি PagingSource বা RemoteMediator থেকে লোড স্টেট নিতে পারে, তাই UI আপডেটের সাথে এগুলি সিঙ্ক হবেই এমন কোনও গ্যারেন্টি নেই। এর ফলে UI-তে সমস্যা হতে পারে, যেখানে UI-তে কোনও নতুন ডেটা যোগ করার আগেই লোডিং শেষ হয়ে গেছে বলে মনে হয়।

এই কারণে, হেডার বা ফুটারে লোড স্টেট দেখানোর জন্য কনভেনিয়েন্স অ্যাক্সেসর ভালভাবে কাজ করে, তবে অন্যান্য ব্যবহারের ক্ষেত্রে আপনাকে PagingSource বা RemoteMediator থেকে লোড স্টেট নির্দিষ্টভাবে অ্যাক্সেস করতে হতে পারে। এই উদ্দেশ্যে CombinedLoadStates, source এবং mediator প্রপার্টি প্রদান করে। এইসব প্রপার্টির প্রত্যেকটি একটি LoadStates অবজেক্ট প্রকাশ করে যাতে যথাক্রমে PagingSource বা RemoteMediator-এর জন্য LoadState অবজেক্ট থাকে:

Kotlin

lifecycleScope.launch {
  pagingAdapter.loadStateFlow.collectLatest { loadStates ->
    // Directly access the RemoteMediator refresh load state.
    mediatorRefreshLoadState: LoadState? = loadStates.mediator.refresh
    // Directly access the RemoteMediator append load state.
    mediatorAppendLoadState: LoadState? = loadStates.mediator.append
    // Directly access the RemoteMediator prepend load state.
    mediatorPrependLoadState: LoadState? = loadStates.mediator.prepend
    // Directly access the PagingSource refresh load state.
    sourceRefreshLoadState: LoadState = loadStates.source.refresh
    // Directly access the PagingSource append load state.
    sourceAppendLoadState: LoadState = loadStates.source.append
    // Directly access the PagingSource prepend load state.
    sourcePrependLoadState: LoadState = loadStates.source.prepend
  }
}

জাভা

pagingAdapter.addLoadStateListener(loadStates -> {
  // Directly access the RemoteMediator refresh load state.
  LoadState mediatorRefreshLoadState = loadStates.mediator.refresh;
  // Directly access the RemoteMediator append load state.
  LoadState mediatorAppendLoadState = loadStates.mediator.append;
  // Directly access the RemoteMediator prepend load state.
  LoadState mediatorPrependLoadState = loadStates.mediator.prepend;
  // Directly access the PagingSource refresh load state.
  LoadState sourceRefreshLoadState = loadStates.source.refresh;
  // Directly access the PagingSource append load state.
  LoadState sourceAppendLoadState = loadStates.source.append;
  // Directly access the PagingSource prepend load state.
  LoadState sourcePrependLoadState = loadStates.source.prepend;
});

জাভা

pagingAdapter.addLoadStateListener(loadStates -> {
  // Directly access the RemoteMediator refresh load state.
  LoadState mediatorRefreshLoadState = loadStates.mediator.refresh;
  // Directly access the RemoteMediator append load state.
  LoadState mediatorAppendLoadState = loadStates.mediator.append;
  // Directly access the RemoteMediator prepend load state.
  LoadState mediatorPrependLoadState = loadStates.mediator.prepend;
  // Directly access the PagingSource refresh load state.
  LoadState sourceRefreshLoadState = loadStates.source.refresh;
  // Directly access the PagingSource append load state.
  LoadState sourceAppendLoadState = loadStates.source.append;
  // Directly access the PagingSource prepend load state.
  LoadState sourcePrependLoadState = loadStates.source.prepend;
});

LoadState-এ চেন অপারেটর

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

যেমন, ধরুন আপনি একটি খালি ভিউ দেখাতে চান, কিন্তু শুধুমাত্র প্রাথমিক ডেটা লোড সম্পূর্ণ হওয়ার পরে। এই ব্যবহারের ক্ষেত্রে আপনাকে যাচাই করতে হবে যে ডেটা রিফ্রেশ লোড শুরু হয়েছে, তারপর NotLoading স্ট্যাটাস কনফার্ম করার জন্য অপেক্ষা করুন যে রিফ্রেশ সম্পূর্ণ হয়েছে। আপনার প্রয়োজন আছে এমন সিগন্যাল ছাড়া বাকি সব সিগন্যাল আপনাকে ফিল্টার করে বাদ দিতে হবে:

Kotlin

lifecycleScope.launchWhenCreated {
  adapter.loadStateFlow
    // Only emit when REFRESH LoadState for RemoteMediator changes.
    .distinctUntilChangedBy { it.refresh }
    // Only react to cases where REFRESH completes, such as NotLoading.
    .filter { it.refresh is LoadState.NotLoading }
    // Scroll to top is synchronous with UI updates, even if remote load was
    // triggered.
    .collect { binding.list.scrollToPosition(0) }
}

জাভা

PublishSubject<CombinedLoadStates> subject = PublishSubject.create();
Disposable disposable =
  subject.distinctUntilChanged(CombinedLoadStates::getRefresh)
  .filter(
    combinedLoadStates -> combinedLoadStates.getRefresh() instanceof LoadState.NotLoading)
  .subscribe(combinedLoadStates -> binding.list.scrollToPosition(0));

pagingAdapter.addLoadStateListener(loadStates -> {
  subject.onNext(loadStates);
});

জাভা

LiveData<CombinedLoadStates> liveData = new MutableLiveData<>();
LiveData<LoadState> refreshLiveData =
  Transformations.map(liveData, CombinedLoadStates::getRefresh);
LiveData<LoadState> distinctLiveData =
  Transformations.distinctUntilChanged(refreshLiveData);

distinctLiveData.observeForever(loadState -> {
  if (loadState instanceof LoadState.NotLoading) {
    binding.list.scrollToPosition(0);
  }
});

এই উদাহরণটি রিফ্রেশ লোড স্টেট আপডেট না হওয়া পর্যন্ত অপেক্ষা করে, কিন্তু শুধুমাত্র তখনই ট্রিগার করে যখন স্টেট NotLoading হয়। এটি নিশ্চিত করে যে কোনও UI আপডেট হওয়ার আগে রিমোট রিফ্রেশ সম্পূর্ণরূপে শেষ হয়েছে।

Stream API এই ধরনের অপারেশন সম্ভব করে তোলে। আপনার অ্যাপ, প্রয়োজনীয় লোড ইভেন্ট নির্দিষ্ট করতে পারে এবং উপযুক্ত মাপকাঠি পূরণ হলে নতুন ডেটা ম্যানেজ করতে পারে।