Skip to content

Commit

Permalink
Merge pull request #638 from SUI-Components/feat/sui-app-icons
Browse files Browse the repository at this point in the history
feat(components/tool/app): Improve API organization
  • Loading branch information
oegea committed Nov 16, 2023
2 parents c5d2373 + dc3b16e commit 3585f74
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 41 deletions.
71 changes: 62 additions & 9 deletions components/tool/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ Note that the string will be displayed on the user interface sometimes.
### Check if biometric login is available

```
import {isBiometricLoginAvailable} from '@s-ui/sui-tool-app'
import {biometric} from '@s-ui/sui-tool-app'
const isAvailable = await isBiometricLoginAvailable()
const isAvailable = await biometric.isAvailable()
```

### Set biometric login
Expand All @@ -87,9 +87,9 @@ After user logs in, credentials can be stored for later usage through biometric
Normally the OS will prompt users to confirm they really want to use biometric auth for login.

```
import {setBiometricLoginCredentials} from '@s-ui/sui-tool-app'
import {biometric} from '@s-ui/sui-tool-app'
const isAvailable = await setBiometricLoginCredentials({
await biometric.setCredentials({
username: '123',
password: '456',
domain: 'pro.coches.net'
Expand All @@ -98,18 +98,20 @@ const isAvailable = await setBiometricLoginCredentials({

### Get credentials

If the user has previously authorized and registered biometric login, `getBiometricLoginCredentials` will prompt users to confirm their identity, and if authentication is successful, the credentials object will be returned.
If the user has previously authorized and registered biometric login, `getCredentials` will prompt users to confirm their identity, and if authentication is successful, the credentials object will be returned.

```
import {getBiometricLoginCredentials} from '@s-ui/sui-tool-app'
import {biometric} from '@s-ui/sui-tool-app'
const isAvailable = await getBiometricLoginCredentials({
const credentials = await biometric.getCredentials({
domain: 'pro.coches.net',
reason: 'Log in into the app',
title: 'Identify with your fingerprint or face',
subtitle: 'Confirm your identity without having to remember your password',
description: 'Please use a biometric device to identify yourself',
})
console.log(credentials.username, credentials.password)
```

## Local notifications
Expand All @@ -123,6 +125,57 @@ const isAvailable = await getBiometricLoginCredentials({
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
```

### Other features
### Getting permissions

Before sending notifications, in some operating systems the user has to accept them. This can be easily done by calling to the `prepare` method.

If the `prepare` method is not executed, permissions will be requested when the first notification is scheduled.

```
import {localNotifications} from '@s-ui/sui-tool-app'
await localNotifications.prepare() // Returns true or false
```

### Scheduling a notification

A basic local notification can be scheduled by running the `schedule` command:

```
import {localNotifications} from '@s-ui/sui-tool-app'
const ONE_MINUTE = 1000 * 60
localNotifications.schedule({
notifications: [
{
title: 'Fancy title here',
body: 'I am a fancy notification. Just click me!',
id: 1,
schedule: {at: new Date(Date.now() + ONE_MINUTE)},
sound: null,
attachments: null,
actionTypeId: '',
extra: null
}
]
})
```

### Other available methods

There are other methods available to interact with local notifications, that are offered by the original Capacitor plugin and can be accessed directly, without adding custom logic nor altering their behaviour.

```
import {localNotifications} from '@s-ui/sui-tool-app'
// localNotifications.plugin.getPending()
// .registerActionTypes
// .cancel
// .areEnabled
// .createChannel
// .deleteChannel
// .listChannels
```

Please refer to the following documentation: `https://capacitorjs.com/docs/apis/local-notifications`
The exposed API through the `plugin` prop can be reviewed here `https://capacitorjs.com/docs/apis/local-notifications#api`
23 changes: 4 additions & 19 deletions components/tool/app/src/biometric.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import {NativeBiometric} from '@capgo/capacitor-native-biometric'

// Delete user's credentials
/* NativeBiometric.deleteCredentials({
server: "www.example.com",
}).then(); */

export const isBiometricLoginAvailable = () => NativeBiometric.isAvailable()

export const getBiometricLoginCredentials = async ({
domain,
reason,
title,
subtitle,
description
}) => {
export const isAvailable = () => NativeBiometric.isAvailable()

export const getCredentials = async ({domain, reason, title, subtitle, description}) => {
const biometrics = await NativeBiometric.isAvailable()

if (!biometrics.isAvailable) return null
Expand All @@ -36,11 +25,7 @@ export const getBiometricLoginCredentials = async ({
return credentials
}

export const setBiometricLoginCredentials = async ({
username,
password,
domain
}) => {
export const setCredentials = async ({username, password, domain}) => {
const biometrics = await NativeBiometric.isAvailable()

if (!biometrics.isAvailable) return
Expand Down
17 changes: 4 additions & 13 deletions components/tool/app/src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import {
getBiometricLoginCredentials,
isBiometricLoginAvailable,
setBiometricLoginCredentials
} from './biometric.js'
import * as biometric from './biometric.js'
import * as localNotifications from './localNotifications.js'

export default function SuiApp() {
throw new Error(
'sui-app is a set of tools and is not intended to be renderized as a React component'
)
throw new Error('sui-app is a set of tools and is not intended to be renderized as a React component')
}

export {
isBiometricLoginAvailable,
getBiometricLoginCredentials,
setBiometricLoginCredentials
}
export {biometric, localNotifications}
16 changes: 16 additions & 0 deletions components/tool/app/src/localNotifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Plugins} from '@capacitor/core'

const {LocalNotifications} = Plugins

export const prepare = async () =>
LocalNotifications &&
((await LocalNotifications.checkPermissions()).display === 'granted' ||
(await LocalNotifications.requestPermissions()).display === 'granted')

export const schedule = props => {
if (prepare()) return LocalNotifications.schedule(props)

return null
}

export {LocalNotifications as plugin}

0 comments on commit 3585f74

Please sign in to comment.