Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending native class #874

Closed
NickIliev opened this issue Nov 2, 2017 · 1 comment
Closed

Extending native class #874

NickIliev opened this issue Nov 2, 2017 · 1 comment
Labels

Comments

@NickIliev
Copy link

From @Serge-SDL on November 1, 2017 18:21

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 microsoft/TypeScript#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

Copied from original issue: NativeScript/NativeScript#5022

@petekanev
Copy link
Contributor

Hey @Serge-SDL, NativeScript does not officially support extending of extended native classes in TypeScript - which appears to be the case with DrawingPad extending DrawingPadVendor.

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

Anyway - I tend to agree with sean-perkins's point on the forums and think you should work directly with the plugin author.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants