Skip to content

Commit ced06d1

Browse files
committed
feat(preview-api): expose public method for getting qr code of playground app
1 parent 636d619 commit ced06d1

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
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("previewQrCodeService", "./services/livesync/playground/preview-qr-code-service");
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/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

+5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ declare global {
2323
}
2424

2525
interface IPreviewQrCodeService {
26+
getPlaygroundAppQrCode(options?: IPlaygroundAppQrCodeOptions): Promise<IDictionary<IQrCodeImageData>>;
2627
printLiveSyncQrCode(options: IGenerateQrCodeOptions): Promise<void>;
2728
}
2829

30+
interface IPlaygroundAppQrCodeOptions {
31+
platform?: string;
32+
}
33+
2934
interface IGenerateQrCodeOptions extends IHasUseHotModuleReloadOption {
3035
/**
3136
* If set to true, a link will be shown on console instead of QR code
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
import * as util from "util";
22
import { EOL } from "os";
33
import { PlaygroundStoreUrls } from "./preview-app-constants";
4+
import { exported } from "../../../common/decorators";
45

56
export class PreviewQrCodeService implements IPreviewQrCodeService {
6-
constructor(private $previewSdkService: IPreviewSdkService,
7+
constructor(
8+
private $config: IConfiguration,
79
private $httpClient: Server.IHttpClient,
10+
private $logger: ILogger,
11+
private $mobileHelper: Mobile.IMobileHelper,
12+
private $previewSdkService: IPreviewSdkService,
813
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;
1130
}
1231

1332
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);
2335

2436
this.$logger.info();
2537
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
3850
Google Play (Android): ${PlaygroundStoreUrls.GOOGLE_PLAY_URL}`);
3951
}
4052
}
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+
}
4176
}
4277
$injector.register("previewQrCodeService", PreviewQrCodeService);

0 commit comments

Comments
 (0)