Fragment.Class

mail@pastecode.io avatar
unknown
java
a year ago
3.7 kB
4
Indexable
Never

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        internalStorageBinding = FragmentInternalBinding.inflate(inflater, container, false);
        View view = internalStorageBinding.getRoot();



        String internalStorage = System.getenv("EXTERNAL_STORAGE");
        storage = new File(internalStorage);

        txtPath = view.findViewById(R.id.txt_Path);
        txtPath.setText(storage.getAbsolutePath());

        // Retrieve the path argument from the fragment's arguments
        Bundle bundle = getArguments();
        if (bundle != null) {
            String path = bundle.getString("path");
            if (path != null) {
                txtPath.setText(path);
            }
        }

        try {
            data = getArguments().getString("path");
            File file = new File(data);
            storage = file;
        } catch (Exception e) {
            e.printStackTrace();
        }



        setListeners();
        setLayout();
        displayFiles();
        reStoreSortBy();

        return view;
    }



    private void displayFiles() {
        internalStorageBinding.recyclerInternal.setVisibility(View.GONE);
        internalStorageBinding.alternateView.setVisibility(View.GONE);

        fileList = new ArrayList<>();
        fileAdapter = new FileAdapter(getContext(), fileList, this, getActivity().getPackageManager());
        internalStorageBinding.recyclerInternal.setAdapter(fileAdapter);


        boolean isEmpty = fileAdapter.getItemCount() == 0;
        if (isEmpty) {
            internalStorageBinding.recyclerInternal.setVisibility(View.GONE);
            internalStorageBinding.alternateView.setVisibility(View.VISIBLE);
            internalStorageBinding.searchToolbar.setVisibility(View.GONE);
        } else {
            internalStorageBinding.recyclerInternal.setVisibility(View.VISIBLE);
            internalStorageBinding.alternateView.setVisibility(View.GONE);
        }

        fetchData();
        setupPagination();

    }

    private void setupPagination() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        internalStorageBinding.recyclerInternal.setLayoutManager(layoutManager);

        filePagination = new FilePagination(layoutManager) {
            @Override
            protected void loadMoreItems() {
                // Calling AsyncTaskto fetch more items
                fetchData();
            }

            @Override
            public boolean isLastPage() {

                return false;
            }

            @Override
            public boolean isLoading() {

                return isLoading;
            }
        };

        internalStorageBinding.recyclerInternal.addOnScrollListener(filePagination);
    }

    private void fetchData() {
        internalStorageBinding.bottomProgressBarInternal.setVisibility(View.VISIBLE);
        isLoading = true;
        fileList.add(null);
        fileAdapter.notifyItemInserted(fileList.size() - 1);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                fileList.remove(fileList.size() - 1);
                int scrollPosition = fileList.size();
                fileAdapter.notifyItemRemoved(scrollPosition);

                // Calling AsyncTaskto fetch more items
                new FileAsyncTask(storage, currentPage, fileList, fileAdapter, internalStorageBinding).execute();

                isLoading = false;
            }
        }, 2000);
    }