Skip to content
This repository was archived by the owner on Jun 15, 2024. It is now read-only.

Commit 515b2ac

Browse files
committed
Merge branch 'events'
2 parents 225ad91 + 85d8ba6 commit 515b2ac

7 files changed

+84
-12
lines changed

AndroidPdfViewer.d.ts

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ declare module com.github.barteksc.pdfviewer {
1010
export interface OnErrorListener {
1111
onError(throwable: java.lang.Throwable): void;
1212
}
13+
14+
interface IOnLoadCompleteListener {
15+
/**
16+
* Called when the PDF is loaded
17+
* @param numPages the number of pages in this PDF file
18+
*/
19+
loadComplete(numPages: number): void;
20+
}
21+
22+
export class OnLoadCompleteListener implements IOnLoadCompleteListener {
23+
constructor(implementation: IOnLoadCompleteListener);
24+
25+
/**
26+
* Called when the PDF is loaded
27+
* @param numPages the number of pages in this PDF file
28+
*/
29+
loadComplete(numPages: number): void;
30+
}
1331
}
1432
}
1533

@@ -21,5 +39,6 @@ declare class Configurator {
2139
pages(...pageNumbers: number[]): this;
2240
enableDoubletap(enable: boolean): this;
2341
enableSwipe(enable: boolean): this;
42+
onLoad(onLoadCompleteListener: pdfviewer.listener.OnLoadCompleteListener): this;
2443
swipeHorizontal(horizontal: boolean): this;
2544
}

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a very basic PDF view implementation that does only one thing, and
44
that is to display PDF files. It conveniently uses the iOS `UIWebView`, but
5-
for Android it uses AndroidPdfView.
5+
for Android it uses [AndroidPdfViewer](https://github.com/barteksc/AndroidPdfViewer).
66

77
This plugin does the bare minimum required to render the PDF, no configuration
88
options, and no error handling have been built yet. I welcome all Pull Requests!
@@ -11,7 +11,7 @@ options, and no error handling have been built yet. I welcome all Pull Requests!
1111

1212
##
1313

14-
Check out the demo folder for a sample usage.
14+
Check out the [demo](./demo) folder for a sample usage.
1515

1616
## Angular 2
1717

@@ -27,7 +27,7 @@ If you're using the plugin with Angular 2, the plugin automatically registers
2727
2. Include the tag in your template:
2828

2929
```html
30-
<PDFView [src]="'http://www.pdf995.com/samples/pdf.pdf'"></PDFView>
30+
<PDFView [src]="src" (load)="onLoad()"></PDFView>
3131
```
3232

3333
# Try the Demo

demo/app/main-page.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
xmlns:pdf="nativescript-pdf-view">
44
<StackLayout>
55
<Button text="Show Another!" tap="{{ changePDF }}" />
6-
<pdf:PDFView src="{{ pdfUrl }}"/>
6+
<pdf:PDFView src="{{ pdfUrl }}" load="{{ onLoad }}"/>
77
</StackLayout>
88
</Page>

demo/app/main-view-model.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ export class HelloWorldModel extends Observable {
1919
this.set('pdfUrl', this.pdfUrls[(this.current + 1) % this.pdfUrls.length]);
2020
}
2121

22+
public onLoad() {
23+
alert('Loaded PDF!');
24+
}
25+
2226
private current = 0;
2327
}

src/plugin.android.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ export class PDFView extends common.PDFView {
99
private promise: Promise<any>;
1010
private tempFolder = fs.knownFolders.temp().getFolder('PDFViewer.temp/');
1111

12+
private onLoadHandler = (() => {
13+
const pdfViewRef = new WeakRef(this);
14+
15+
return new pdfviewer.listener.OnLoadCompleteListener({
16+
loadComplete: (numPages) => {
17+
common.PDFView.notifyOfEvent(common.PDFView.loadEvent, pdfViewRef);
18+
}
19+
});
20+
})();
21+
1222
public get android() {
1323
return this._android;
1424
}
@@ -17,12 +27,12 @@ export class PDFView extends common.PDFView {
1727
this._android = value;
1828
}
1929

20-
public load(src: string) {
30+
public loadPDF(src: string) {
2131
if (!src || !this.android) {
2232
return;
2333
}
2434

25-
// reset any previous promise since we've called load again
35+
// reset any previous promise since we've called loadPDF again
2636
this.promise = void 0;
2737

2838
if (src.indexOf('://') === -1) {
@@ -36,6 +46,7 @@ export class PDFView extends common.PDFView {
3646

3747
this.android
3848
.fromUri(uri)
49+
.onLoad(this.onLoadHandler)
3950
.load();
4051
}
4152

@@ -48,7 +59,7 @@ export class PDFView extends common.PDFView {
4859
.getFile(url, `${this.tempFolder.path}/${java.util.UUID.randomUUID()}`)
4960
.then(file => {
5061
if (this.promise === promise) { // make sure we haven't switched
51-
this.load(file.path);
62+
this.loadPDF(file.path);
5263
}
5364
}).catch(error => {
5465
console.error(error);
@@ -58,6 +69,6 @@ export class PDFView extends common.PDFView {
5869

5970
private _createUI() {
6071
this._android = new pdfviewer.PDFView(this._context, void 0);
61-
this.load(this.src);
72+
this.loadPDF(this.src);
6273
}
6374
}

src/plugin.common.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,26 @@ export abstract class PDFView extends view.View {
88
private static srcProperty = new Property(
99
'src', 'PdfView', new proxy.PropertyMetadata(''));
1010

11+
public static loadEvent = 'load';
12+
13+
public static notifyOfEvent(eventName: string, pdfViewRef: WeakRef<PDFView>) {
14+
const viewer = pdfViewRef.get();
15+
16+
if (viewer) {
17+
viewer.notify({ eventName, object: viewer });
18+
}
19+
}
20+
1121
public get src(): string {
1222
return this._getValue(PDFView.srcProperty);
1323
}
1424

1525
public set src(src: string) {
1626
this._setValue(PDFView.srcProperty, src);
17-
this.load(src);
27+
this.loadPDF(src);
1828
}
1929

20-
public abstract load(src: string);
30+
public abstract loadPDF(src: string);
2131
}
2232

2333
try {

src/plugin.ios.ts

+30-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import * as common from './plugin.common';
44

55
export class PDFView extends common.PDFView {
66
private _ios: UIWebView;
7+
private delegate: PDFViewDelegate;
78

89
constructor() {
910
super();
1011
this.init();
11-
this.load(this.src);
12+
this.loadPDF(this.src);
1213
}
1314

1415
public get ios() {
@@ -19,7 +20,7 @@ export class PDFView extends common.PDFView {
1920
this._ios = value;
2021
}
2122

22-
public load(src: string) {
23+
public loadPDF(src: string) {
2324
if (!src) {
2425
return;
2526
}
@@ -36,13 +37,40 @@ export class PDFView extends common.PDFView {
3637
this.ios.loadRequest(urlRequest);
3738
}
3839

40+
public onLoaded() {
41+
super.onLoaded();
42+
this._ios.delegate = this.delegate;
43+
}
44+
45+
public onUnloaded() {
46+
this._ios.delegate = null;
47+
super.onUnloaded();
48+
}
49+
3950
private init() {
4051
this.ios = new UIWebView(UIScreen.mainScreen().bounds);
52+
this.delegate = PDFViewDelegate.initWithOwner(new WeakRef(this));
4153

4254
this.ios.autoresizingMask =
4355
UIViewAutoresizing.UIViewAutoresizingFlexibleWidth |
4456
UIViewAutoresizing.UIViewAutoresizingFlexibleHeight;
4557

4658
this.ios.scalesPageToFit = true;
4759
}
60+
}
61+
62+
class PDFViewDelegate extends NSObject implements UIWebViewDelegate {
63+
public static ObjCProtocols = [UIWebViewDelegate];
64+
65+
public static initWithOwner(owner: WeakRef<PDFView>): PDFViewDelegate {
66+
let delegate = <PDFViewDelegate>PDFViewDelegate.new();
67+
delegate.owner = owner;
68+
return delegate;
69+
}
70+
71+
public webViewDidFinishLoad(webView: UIWebView) {
72+
return common.PDFView.notifyOfEvent(common.PDFView.loadEvent, this.owner);
73+
}
74+
75+
private owner: WeakRef<PDFView>;
4876
}

0 commit comments

Comments
 (0)