4

I want to add a Cordova plugin which is available on GitHub to my IONIC 5+ Capacitor (Angular) project.

Also, I don't know how to install and integrate this plugin, because the official manual says that after the step npm install https://github.com/DigitalsunrayMedia/cordova-plugin-stepcounter also npm install ionic-native/???????

My problem is right here! What should I do with npm install ionic-native/???????? enter? The desired plugin does not exist as an Ionic Native plugin.

Is it sufficient, if I simply execute the following:

npm install https://github.com/DigitalsunrayMedia/cordova-plugin-stepcounter.git npx cap sync

without the step of npm install ionic-native/????

I would also like to know if I can easily add and use it in Ionic Capacitor or if I have to make changes in a file.

How do I address this plugin in Typescript? Do I have to add anything to module.app?

Is it sufficient if I do it the way Capacitor prescribes: import { Plugins } from '@capacitor/core'; const { Stepcounter } = Plugins;

I am very grateful for any advice! Thank youu :) Best regards, programmerg

G-EA
  • 337
  • 7
  • 17

1 Answers1

1

Yes you can install plugin and use it without ionic-native, basically ionic-native is just typed wrapper of library.

The easiest way would be to implement service

import { Injectable } from '@angular/core';

declare var stepcounter: any;

@Injectable({
    providedIn: 'root'
})
export class StepCounterService {
    constructor() {}

    start(startingOffset) {
        return new Promise((resolve, reject) => {
            stepcounter.start(
                startingOffset,
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    stop() {
        return new Promise((resolve, reject) => {
            stepcounter.stop(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    getTodayStepCount() {
        return new Promise((resolve, reject) => {
            stepcounter.getTodayStepCount(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    getStepCount() {
        return new Promise((resolve, reject) => {
            stepcounter.getStepCount(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    deviceCanCountSteps() {
        return new Promise((resolve, reject) => {
            stepcounter.deviceCanCountSteps(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }

    getHistory() {
        return new Promise((resolve, reject) => {
            stepcounter.getHistory(
                message => {
                    resolve(message);
                },
                () => {
                    reject();
                }
            );
        });
    }
}

Now you inject it where you need so you can use it

PS. I assume you are using angular and typescript if you are using vanilla ionic and javascript you can install plugin and use it

Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
  • @programmerg hello :) I've updated answer more copy paste solution – Vova Bilyachat Nov 13 '19 at 00:44
  • Hello Volodymyr, thank you for your support! I'll try it out now and let you know. :) By the way, yes I use Angular. ; – G-EA Nov 13 '19 at 01:05
  • Hello Volodymyr, I would like to be able to disable the step counter in the app. The function "stop()" stops the stepcounter, but as soon as the smartphone is restarted, the stepcounter is reactivated. Could you help me with this please? – G-EA Apr 27 '20 at 21:19
  • @programmerg Hey, I would add logic to save state into local storage, then on platofrm ready read state and call stop if you need – Vova Bilyachat Apr 28 '20 at 06:06
  • Hey @Volodymyr, thank you for your help again! I implemented your proposal. Unfortunately, there is the problem that after restarting the device the stepcounter is activated automatically without starting the app. This is implemented in the plugin. I think that it will only be possible not to activate the pedometer when restarting the device if I change the native Java code in the plugin. What do you think? My main intention is to hide the fixed stepcounter-value from the status bar. – G-EA Apr 29 '20 at 00:22
  • @programmerg unfortunately I dont know how that plugin works :( – Vova Bilyachat Apr 29 '20 at 02:27