Adding users to permission set through Apex

Here is how we can add/remove a user to/from a permission set through Apex. My requirement was to add or remove a user from a permission set when the value of a checkbox changes in user object.So I have this code in User Update After trigger.

Here is the code to add to a PermissionSet:

Here is the code to remove from a PermissionSet: