|
| 1 | +import * as util from "util"; |
| 2 | +import { EOL } from "os"; |
| 3 | +import { PlaygroundStoreUrls } from "./preview-app-constants"; |
| 4 | +import { exported } from "../../../common/decorators"; |
| 5 | + |
| 6 | +export class PreviewQrCodeService implements IPreviewQrCodeService { |
| 7 | + constructor( |
| 8 | + private $config: IConfiguration, |
| 9 | + private $httpClient: Server.IHttpClient, |
| 10 | + private $logger: ILogger, |
| 11 | + private $mobileHelper: Mobile.IMobileHelper, |
| 12 | + private $previewSdkService: IPreviewSdkService, |
| 13 | + private $qrCodeTerminalService: IQrCodeTerminalService, |
| 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; |
| 30 | + } |
| 31 | + |
| 32 | + public async printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise<void> { |
| 33 | + const qrCodeUrl = this.$previewSdkService.getQrCodeUrl(options); |
| 34 | + const url = await this.getShortenUrl(qrCodeUrl); |
| 35 | + |
| 36 | + this.$logger.info(); |
| 37 | + const message = `${EOL} Generating qrcode for url ${url}.`; |
| 38 | + this.$logger.trace(message); |
| 39 | + |
| 40 | + if (options.link) { |
| 41 | + this.$logger.printMarkdown(message); |
| 42 | + } else { |
| 43 | + this.$qrCodeTerminalService.generate(url); |
| 44 | + |
| 45 | + this.$logger.info(); |
| 46 | + this.$logger.printMarkdown(`# Use \`NativeScript Playground app\` and scan the \`QR code\` above to preview the application on your device.`); |
| 47 | + this.$logger.printMarkdown(` |
| 48 | +To scan the QR code and deploy your app on a device, you need to have the \`NativeScript Playground app\`: |
| 49 | + App Store (iOS): ${PlaygroundStoreUrls.APP_STORE_URL} |
| 50 | + Google Play (Android): ${PlaygroundStoreUrls.GOOGLE_PLAY_URL}`); |
| 51 | + } |
| 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 | + } |
| 76 | +} |
| 77 | +$injector.register("previewQrCodeService", PreviewQrCodeService); |
0 commit comments