Categories
discuss

How to show empty view with Paging 3 library in Android

I want to show empty view when paging3 is loaded with an empty list.

It seems to work with following code. Is this the proper way to do with the paging 3 library?:

        adapter?.addLoadStateListener { loadState ->
            adapter?.apply {
                if (itemCount <= 0 && !loadState.source.refresh.endOfPaginationReached) {
                    Timber.d("==> to show empty view")
                    tvEmptyView.isGone = false
                } else {
                    Timber.d("==> to hide empty view")
                    tvEmptyView.isGone = true
                }
            }
        } 

Answer

You can directly plug into the adapter loadStateFlow, e.g

    lifecycleScope.launchWhenCreated {
        @OptIn(ExperimentalCoroutinesApi::class)
        adapter.loadStateFlow.collectLatest { loadStates ->
            val refresher = loadStates.refresh
            val displayEmptyMessage =  (refresher is LoadState.NotLoading && refresher.endOfPaginationReached && adapter.itemCount == 0)
            layoutBinding.emptyStateMessage.isVisible = displayEmptyMessage
            layoutBinding.emptyStateImage.isVisible = displayEmptyMessage
            layoutBinding.swipeToRefresh.isRefreshing = refresher is LoadState.Loading
        }
    }
Categories
discuss

When retrieving passed arguments from a bundle, why does Android Studio warn and suggest using requireArguments() instead of arguments!!?

So, like the title says I’m trying to retrieve passed arguments from a bundle according to the documentation I’m following by using var args = GameWonFragmentArgs.fromBundle(arguments!!).
But Android Studio suggests using the following code:
var args = GameWonFragmentArgs.fromBundle(requireArguments())

I looked up the Android Documentation for Fragment and found that requireArguments() returns a @NonNull Bundle or an IllegalStateException. Why does Android Studio suggest using it and what’s the difference between the two?

Also, I couldn’t find relevant documentation about fromBundle().

Answer

There is some api changes in androidx.fragment:fragment

requireArguments() — method which returns a @NonNull Bundle or throws an IllegalStateException.

fromBundle(arguments!!) — these case App may be crashed because of Null pointer exception. if arguments is null app will be crashed.

Categories
discuss

npm install changes my package-lock.json, will that interfere with the remote code?

I know there are numerous issues about this, and I discovered the command npm ci that is supposed to not change package-lock.json, but when I run npm ci it fails:

ERR! cipm can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.

Also tried another solution involving deleting my node_modules directory and running npm i again, but that’s not changing the outcome.

I’m a junior dev working with a team remotely.. I was given a task, so I created a new branch on Gitlab, pulled it down to my local machine and ran npm i to get up-to-speed…

But it keeps changing my package-lock.json DRAMATICALLY(it adds like 20,000 lines of code)

Committing that to the team’s project seems insane to me. Anyone have advice?

Answer

Since this post got so many views I thought I’d come back and post what I found.

Yarn and NPM both update and install packages and dependencies, but the difference is:

yarn creates a file called yarn.lock

npm install creates a file called package-lock.json.

I didn’t know this at the time, so when I cloned the project repo to my local machine, I ran npm i which created the package-lock.json. My teammates were already using yarn, however.

So make sure you use the one already being used.

Categories
discuss

EntityManager.createNativeQuery returning list of objects instead of list of BigDecimal when using Pagination

I am trying to use Pagination with EntityManager.createNativeQuery(). Below is the skeleton code that I am using:

var query = em.createNativeQuery("select distinct id from ... group by ... having ...");
List<BigDecimal> results = query
        .setMaxResults(pageSize)
        .setFirstResult(pageNumber * pageSize)
        .getResultList();

When pageNumber is 0 (first page), I get the expected List of BigDecimals:

But as soon as pageNumber > 0 (example, second page), I get a List of Objects, and each object in this list seems to contain two BigDecimals, the first of which contains the value from the db, and the second BigDecimal seems to be the position of this row.

and obviously I get this exception

java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class java.math.BigDecimal

Can someone please explain this discrepancy, and how this can be fixed to always return a List of BigDecimals? Thank you.

Update-1 : I have created a sample project to reproduce this issue. I was able to reproduce this issue only with an Oracle database. With H2 database, it worked fine, and I consistently got a list of BigDecimals irrelevant of the page number.

Update-2 : I have also created a sample project with H2 where it works without this issue.

Answer

After a lot of trails with different versions of different spring libraries, I was finally able to figure out the issue. In one of my attempts, the issue seems to have disappeared, as soon as I updated the spring-data-commons library from v2.1.5.RELEASE to v2.1.6.RELEASE. I looked up the changelog of this release, and this bug, which is related to this bug in spring-data-commons, is the root cause of this issue. I was able to fix the issue after upgrading the spring-data-commons library.

Categories
discuss

Why operator inside parenthesis changes `this` in function call

Talk is cheap; show me the code.

// equals to this.test = "inside window"
var test = "inside window";

function f () {
  console.log(this.test)
};

var obj = {
  test: "inside object",
  fn: f
};

obj.fn();      // "inside object"   --> fine
(obj).fn();    // "inside object"   --> fine
(1, obj).fn(); // "inside object"   --> fine
(obj.fn)();    // "inside object"   --> fine
(0 || obj.fn)(); // "inside window"   --> why?

// reference equality check
console.log(
  f === obj.fn && 
  (obj.fn) === f && 
  f === (1, obj.fn)
); // all equal :/

I had read ydkjs book and Im familiar with call-site and dynamic this binding, but I don’t understand why the last function call has window as its this context; in this controlled experiment that only thing that is changed () and comma operator and as you can see in the last statement comma operator is doing something weird. I suspect it does an assignment when it returns the value (since if we do an assignment the same result happens) but I’m not sure.

UPDATE:

The effect is also seen on &&, || operators: (0 || obj.fn)()

Answer

Given:

foo.bar()

Inside bar, this will be foo.

(There are exceptions, such as when bar is defined with an arrow function, but they don’t apply in this case).

Given:

const bar = foo.bar;
bar();

Now the function has been called without the context of foo so this is now the default object (which is window in a browser).

The expression: (1, foo.bar) evaluates as the right-hand side. This is the function.

Just as if you had copied it to a variable, this disconnects the function from the object before you call it, so you get the same effect.

There’s no assignment because you haven’t involved a variable, but you are calling the result of an expression and not the object method directly.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..