1
1
import * as util from "util" ;
2
2
import { EOL } from "os" ;
3
3
import { PlaygroundStoreUrls } from "./preview-app-constants" ;
4
+ import { exported } from "../../../common/decorators" ;
4
5
5
6
export class PreviewQrCodeService implements IPreviewQrCodeService {
6
- constructor ( private $previewSdkService : IPreviewSdkService ,
7
+ constructor (
8
+ private $config : IConfiguration ,
7
9
private $httpClient : Server . IHttpClient ,
10
+ private $logger : ILogger ,
11
+ private $mobileHelper : Mobile . IMobileHelper ,
12
+ private $previewSdkService : IPreviewSdkService ,
8
13
private $qrCodeTerminalService : IQrCodeTerminalService ,
9
- private $config : IConfiguration ,
10
- private $logger : ILogger ) {
14
+ private $qr : IQrCodeGenerator
15
+ ) { }
16
+
17
+ @exported ( "previewQrCodeService" )
18
+ public async getPlaygroundAppQrCode ( options ?: IPlaygroundAppQrCodeOptions ) : Promise < IDictionary < IQrCodeImageData > > {
19
+ const result = Object . create ( null ) ;
20
+
21
+ if ( ! options || ! options . platform || this . $mobileHelper . isAndroidPlatform ( options . platform ) ) {
22
+ result . android = await this . getQrCodeImageData ( PlaygroundStoreUrls . GOOGLE_PLAY_URL ) ;
23
+ }
24
+
25
+ if ( ! options || ! options . platform || this . $mobileHelper . isiOSPlatform ( options . platform ) ) {
26
+ result . ios = await this . getQrCodeImageData ( PlaygroundStoreUrls . APP_STORE_URL ) ;
27
+ }
28
+
29
+ return result ;
11
30
}
12
31
13
32
public async printLiveSyncQrCode ( options : IGenerateQrCodeOptions ) : Promise < void > {
14
- let url = this . $previewSdkService . getQrCodeUrl ( options ) ;
15
- const shortenUrlEndpoint = util . format ( this . $config . SHORTEN_URL_ENDPOINT , encodeURIComponent ( url ) ) ;
16
- try {
17
- const response = await this . $httpClient . httpRequest ( shortenUrlEndpoint ) ;
18
- const responseBody = JSON . parse ( response . body ) ;
19
- url = responseBody . shortURL || url ;
20
- } catch ( e ) {
21
- // use the longUrl
22
- }
33
+ const qrCodeUrl = this . $previewSdkService . getQrCodeUrl ( options ) ;
34
+ const url = await this . getShortenUrl ( qrCodeUrl ) ;
23
35
24
36
this . $logger . info ( ) ;
25
37
const message = `${ EOL } Generating qrcode for url ${ url } .` ;
@@ -38,5 +50,28 @@ To scan the QR code and deploy your app on a device, you need to have the \`Nati
38
50
Google Play (Android): ${ PlaygroundStoreUrls . GOOGLE_PLAY_URL } ` ) ;
39
51
}
40
52
}
53
+
54
+ private async getShortenUrl ( url : string ) : Promise < string > {
55
+ const shortenUrlEndpoint = util . format ( this . $config . SHORTEN_URL_ENDPOINT , encodeURIComponent ( url ) ) ;
56
+ try {
57
+ const response = await this . $httpClient . httpRequest ( shortenUrlEndpoint ) ;
58
+ const responseBody = JSON . parse ( response . body ) ;
59
+ url = responseBody . shortURL || url ;
60
+ } catch ( e ) {
61
+ // use the longUrl
62
+ }
63
+
64
+ return url ;
65
+ }
66
+
67
+ private async getQrCodeImageData ( url : string ) : Promise < IQrCodeImageData > {
68
+ const shortenUrl = await this . getShortenUrl ( url ) ;
69
+ const imageData = await this . $qr . generateDataUri ( shortenUrl ) ;
70
+ return {
71
+ originalUrl : url ,
72
+ shortenUrl,
73
+ imageData
74
+ } ;
75
+ }
41
76
}
42
77
$injector . register ( "previewQrCodeService" , PreviewQrCodeService ) ;
0 commit comments