Skip to content

Commit 17fe362

Browse files
author
Fatme
authored
Merge pull request #4087 from NativeScript/fatme/preview-qr-code-api
feat(preview-api): expose public method for getting the qr code of playground app
2 parents 9daeb0c + 2c14c96 commit 17fe362

File tree

8 files changed

+106
-51
lines changed

8 files changed

+106
-51
lines changed

lib/bootstrap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ $injector.require("previewAppLiveSyncService", "./services/livesync/playground/p
135135
$injector.require("previewAppPluginsService", "./services/livesync/playground/preview-app-plugins-service");
136136
$injector.require("previewSdkService", "./services/livesync/playground/preview-sdk-service");
137137
$injector.requirePublicClass("previewDevicesService", "./services/livesync/playground/devices/preview-devices-service");
138-
$injector.require("playgroundQrCodeGenerator", "./services/livesync/playground/qr-code-generator");
138+
$injector.requirePublic("previewQrCodeService", "./services/livesync/playground/preview-qr-code-service");
139139
$injector.requirePublic("sysInfo", "./sys-info");
140140

141141
$injector.require("iOSNotificationService", "./services/ios-notification-service");

lib/commands/preview.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class PreviewCommand implements ICommand {
88
private $networkConnectivityValidator: INetworkConnectivityValidator,
99
private $projectData: IProjectData,
1010
private $options: IOptions,
11-
private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator) { }
11+
private $previewQrCodeService: IPreviewQrCodeService) { }
1212

1313
public async execute(): Promise<void> {
1414
await this.$liveSyncService.liveSync([], {
@@ -24,7 +24,7 @@ export class PreviewCommand implements ICommand {
2424
useHotModuleReload: this.$options.hmr
2525
});
2626

27-
await this.$playgroundQrCodeGenerator.generateQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link });
27+
await this.$previewQrCodeService.printLiveSyncQrCode({ useHotModuleReload: this.$options.hmr, link: this.$options.link });
2828
}
2929

3030
public async canExecute(args: string[]): Promise<boolean> {

lib/common/declarations.d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,21 @@ interface IQrCodeGenerator {
969969
generateDataUri(data: string): Promise<string>;
970970
}
971971

972+
interface IQrCodeImageData {
973+
/**
974+
* The original URL used for generating QR code image.
975+
*/
976+
originalUrl: string;
977+
/**
978+
* The shorten URL used for generating QR code image.
979+
*/
980+
shortenUrl: string;
981+
/**
982+
* Base64 encoded data used for generating QR code image.
983+
*/
984+
imageData: string;
985+
}
986+
972987
interface IDynamicHelpProvider {
973988
/**
974989
* Checks if current project's framework is one of the specified as arguments.

lib/definitions/preview-app-livesync.d.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ declare global {
2222
getExternalPlugins(device: Device): string[];
2323
}
2424

25-
interface IPlaygroundQrCodeGenerator {
26-
generateQrCode(options: IGenerateQrCodeOptions): Promise<void>;
25+
interface IPreviewQrCodeService {
26+
getPlaygroundAppQrCode(options?: IPlaygroundAppQrCodeOptions): Promise<IDictionary<IQrCodeImageData>>;
27+
printLiveSyncQrCode(options: IPrintLiveSyncOptions): Promise<void>;
2728
}
2829

29-
interface IGenerateQrCodeOptions extends IHasUseHotModuleReloadOption {
30+
interface IPlaygroundAppQrCodeOptions {
31+
platform?: string;
32+
}
33+
34+
interface IPrintLiveSyncOptions extends IHasUseHotModuleReloadOption {
3035
/**
3136
* If set to true, a link will be shown on console instead of QR code
3237
* Default value is false.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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);

lib/services/livesync/playground/qr-code-generator.ts

-42
This file was deleted.

lib/services/platform-environment-requirements.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
1313
private $staticConfig: IStaticConfig,
1414
private $analyticsService: IAnalyticsService,
1515
private $injector: IInjector,
16-
private $playgroundQrCodeGenerator: IPlaygroundQrCodeGenerator) { }
16+
private $previewQrCodeService: IPreviewQrCodeService) { }
1717

1818
@cache()
1919
private get $liveSyncService(): ILiveSyncService {
@@ -194,7 +194,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
194194
useHotModuleReload: options.hmr
195195
});
196196

197-
await this.$playgroundQrCodeGenerator.generateQrCode({ useHotModuleReload: options.hmr, link: options.link });
197+
await this.$previewQrCodeService.printLiveSyncQrCode({ useHotModuleReload: options.hmr, link: options.link });
198198
}
199199
}
200200

test/services/platform-environment-requirements.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function createTestInjector() {
2828
testInjector.register("platformEnvironmentRequirements", PlatformEnvironmentRequirements);
2929
testInjector.register("staticConfig", { SYS_REQUIREMENTS_LINK: "" });
3030
testInjector.register("nativeScriptCloudExtensionService", {});
31-
testInjector.register("playgroundQrCodeGenerator", {});
31+
testInjector.register("previewQrCodeService", {});
3232

3333
return testInjector;
3434
}

0 commit comments

Comments
 (0)