Extending native class


#1

Hi!

I am triying to extend a native android class but I am facing some difficulties. I try to extend the nativescript-drawingpad plugin based on android-signaturepad native plugin to avoid some memory leaks.

My goal is to overwrite some methods in the native class “com.github.gcacace.signaturepad.views.SignaturePad”.

my first step is to extend the nativescript plugin like this to use a CustomSignaturePad object intead of the com.github.gcacace.signaturepad.views.SignaturePad:

import { DrawingPad as DrawingPadVendor } from "nativescript-drawingpad";

export class DrawingPad extends DrawingPadVendor {
  public createNativeView() {
    const signaturePad = new CustomSignaturePad(this._context, null);
    if (this.penColor) {
      signaturePad.setPenColor(this.penColor.android);
    }
    if (this.penWidth) {
      signaturePad.setMinWidth(this.penWidth);
    }
    return signaturePad;
  }
}

next step is to create the new CustomSignaturePad class:

declare var com: any;

class CustomSignaturePad extends com.github.gcacace.signaturepad.views.SignaturePad {
    constructor(context, attrs) {
        super(context, attrs);
        return global.__native(this);
    }

    init() {
        return global.__native(this);
    }

    clear() {
        this.super.clear();
    }

    clearView() {
        this.super.clearView();
    }
}

here it the first typescript issue: super(context, attrs); cause a “call target does not contain any signatures” error. This error can be fixed with a little workaround described here https://github.com/Microsoft/TypeScript/issues/17032 . declartion is now:
class CustomSignaturePad extends (com.github.gcacace.signaturepad.views.SignaturePad as { new(context, attrs):any; }) { }

This code works (no error), but there is still 2 issues:

  • clear() function, called from nativescript plugin is executed but clearView() function, called in the native clear() method is not used (if I log something it doesn’t display anything). -> Is it possible to overwrite a method that is called in native androd class?
  • I can’t have access to android class private properties, I tried this.property and this.super.property but it doesn’t works -> is it possible to access this properties?

Thank you for your help!
serge


Specify repositories for android dependencies
#2

Is there a reason you are trying to extend the plugin’s code, instead of forking the plugin, making the changes local to that source and referencing your version of the plugin in your source code?

If you maintained a fork, you could even PR it back to the original plugin’s repository to help the community when they run into this issue as well.

Lastly, if there is a measurable and known memory leak in an upstream library, I would open an issue on that Github project so that the author can fix the problem up-stream for everyone in the future.


#3

Hi and thanks for your answer.

You are right and I will maybe do a fork on the native project (didn’t do that for the moment because the issue is allready open ant the PR is allready proposed but not merged).

But I also wanted to discover Nativescript further and understand how I can extend a native class…


#4

I responded to your Github issue, though I’d like to ask you to use GitHub for framework-related issues, and the forums for questions. This way you help us keep everything cleaner, and we can be more efficient when talking to you.

Thank you!


#5

Hi and thank you very much for your answer (and I will try to improve my github/forum usage).

I have duplicated the native repository to: https://github.com/Serge-SDL/android-signaturepad
and I try to include this library in my Nativesript project. I add in App_Resources/Android/app.gradle:

repositories {
  maven { url 'https://jitpack.io' }
}

dependencies {
  compile "com.github.Serge-SDL:android-signaturepad:1.2.1"
}

but I have this error: Could not find com.github.Serge-SDL:android-signaturepad:1.2.1.

I don’t relly know where I need to add the repositories part to use jitpack. Is it in this file? Elsewhere?

Thanks!
serge