Search in sources :

Example 1 with PagingConfig

use of androidx.paging.PagingConfig in project FirebaseUI-Android by firebase.

the class FirebaseDbPagingActivity method setUpAdapter.

private void setUpAdapter() {
    // Initialize Paging Configurations
    PagingConfig config = new PagingConfig(30, 5, false);
    // Initialize Firebase Paging Options
    DatabasePagingOptions<Item> options = new DatabasePagingOptions.Builder<Item>().setLifecycleOwner(this).setQuery(mQuery, config, Item.class).build();
    // Initializing Adapter
    final FirebaseRecyclerPagingAdapter<Item, ItemViewHolder> mAdapter = new FirebaseRecyclerPagingAdapter<Item, ItemViewHolder>(options) {

        @NonNull
        @Override
        public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_item, parent, false);
            return new ItemViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull ItemViewHolder holder, int position, @NonNull Item model) {
            holder.bind(model);
        }
    };
    mAdapter.addLoadStateListener(states -> {
        LoadState refresh = states.getRefresh();
        LoadState append = states.getAppend();
        if (refresh instanceof LoadState.Error) {
            LoadState.Error loadStateError = (LoadState.Error) refresh;
            mBinding.swipeRefreshLayout.setRefreshing(false);
            Log.e(TAG, loadStateError.getError().getLocalizedMessage());
        }
        if (append instanceof LoadState.Error) {
            LoadState.Error loadStateError = (LoadState.Error) append;
            mBinding.swipeRefreshLayout.setRefreshing(false);
            Log.e(TAG, loadStateError.getError().getLocalizedMessage());
        }
        if (append instanceof LoadState.Loading) {
            mBinding.swipeRefreshLayout.setRefreshing(true);
        }
        if (append instanceof LoadState.NotLoading) {
            LoadState.NotLoading notLoading = (LoadState.NotLoading) append;
            if (notLoading.getEndOfPaginationReached()) {
                // This indicates that the user has scrolled
                // until the end of the data set.
                mBinding.swipeRefreshLayout.setRefreshing(false);
                showToast("Reached end of data set.");
                return null;
            }
            if (refresh instanceof LoadState.NotLoading) {
                // This indicates the most recent load
                // has finished.
                mBinding.swipeRefreshLayout.setRefreshing(false);
                return null;
            }
        }
        return null;
    });
    mBinding.pagingRecycler.setLayoutManager(new LinearLayoutManager(this));
    mBinding.pagingRecycler.setAdapter(mAdapter);
    // Reload data on swipe
    mBinding.swipeRefreshLayout.setOnRefreshListener(() -> {
        // Reload Data
        mAdapter.refresh();
    });
}
Also used : ViewGroup(android.view.ViewGroup) DatabasePagingOptions(com.firebase.ui.database.paging.DatabasePagingOptions) FirebaseRecyclerPagingAdapter(com.firebase.ui.database.paging.FirebaseRecyclerPagingAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) TextView(android.widget.TextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) MenuItem(android.view.MenuItem) NonNull(androidx.annotation.NonNull) LoadState(androidx.paging.LoadState) PagingConfig(androidx.paging.PagingConfig)

Example 2 with PagingConfig

use of androidx.paging.PagingConfig in project FirebaseUI-Android by firebase.

the class FirestorePagingActivity method setUpAdapter.

private void setUpAdapter() {
    Query baseQuery = mItemsCollection.orderBy("value", Query.Direction.ASCENDING);
    PagingConfig config = new PagingConfig(20, 10, false);
    FirestorePagingOptions<Item> options = new FirestorePagingOptions.Builder<Item>().setLifecycleOwner(this).setQuery(baseQuery, config, Item.class).build();
    final FirestorePagingAdapter<Item, ItemViewHolder> adapter = new FirestorePagingAdapter<Item, ItemViewHolder>(options) {

        @NonNull
        @Override
        public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_item, parent, false);
            return new ItemViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull ItemViewHolder holder, int position, @NonNull Item model) {
            holder.bind(model);
        }
    };
    adapter.addLoadStateListener(states -> {
        LoadState refresh = states.getRefresh();
        LoadState append = states.getAppend();
        if (refresh instanceof LoadState.Error || append instanceof LoadState.Error) {
            showToast("An error occurred.");
            adapter.retry();
        }
        if (append instanceof LoadState.Loading) {
            mBinding.swipeRefreshLayout.setRefreshing(true);
        }
        if (append instanceof LoadState.NotLoading) {
            LoadState.NotLoading notLoading = (LoadState.NotLoading) append;
            if (notLoading.getEndOfPaginationReached()) {
                // This indicates that the user has scrolled
                // until the end of the data set.
                mBinding.swipeRefreshLayout.setRefreshing(false);
                showToast("Reached end of data set.");
                return null;
            }
            if (refresh instanceof LoadState.NotLoading) {
                // This indicates the most recent load
                // has finished.
                mBinding.swipeRefreshLayout.setRefreshing(false);
                return null;
            }
        }
        return null;
    });
    mBinding.pagingRecycler.setLayoutManager(new LinearLayoutManager(this));
    mBinding.pagingRecycler.setAdapter(adapter);
    mBinding.swipeRefreshLayout.setOnRefreshListener(() -> adapter.refresh());
}
Also used : FirestorePagingAdapter(com.firebase.ui.firestore.paging.FirestorePagingAdapter) Query(com.google.firebase.firestore.Query) ViewGroup(android.view.ViewGroup) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) MenuItem(android.view.MenuItem) FirestorePagingOptions(com.firebase.ui.firestore.paging.FirestorePagingOptions) NonNull(androidx.annotation.NonNull) LoadState(androidx.paging.LoadState) PagingConfig(androidx.paging.PagingConfig)

Aggregations

MenuItem (android.view.MenuItem)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 TextView (android.widget.TextView)2 NonNull (androidx.annotation.NonNull)2 LoadState (androidx.paging.LoadState)2 PagingConfig (androidx.paging.PagingConfig)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 DatabasePagingOptions (com.firebase.ui.database.paging.DatabasePagingOptions)1 FirebaseRecyclerPagingAdapter (com.firebase.ui.database.paging.FirebaseRecyclerPagingAdapter)1 FirestorePagingAdapter (com.firebase.ui.firestore.paging.FirestorePagingAdapter)1 FirestorePagingOptions (com.firebase.ui.firestore.paging.FirestorePagingOptions)1 Query (com.google.firebase.firestore.Query)1