-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactors TwaManifest.shortcuts #99
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ import * as fs from 'fs'; | |
import fetch from 'node-fetch'; | ||
import * as util from './util'; | ||
import Color = require('color'); | ||
import {WebManifestIcon, WebManifestJson, WebManifestShortcutJson} from './types/WebManifest'; | ||
import {WebManifestIcon, WebManifestJson} from './types/WebManifest'; | ||
|
||
// Regex for disallowed characters on Android Packages, as per | ||
// https://developer.android.com/guide/topics/manifest/manifest-element.html#package | ||
|
@@ -61,34 +61,19 @@ function generatePackageId(host: string): string { | |
/** | ||
* A wrapper around the WebManifest's ShortcutInfo. | ||
*/ | ||
class ShortcutInfo { | ||
name: string; | ||
shortName: string; | ||
url: string | undefined; | ||
chosenIconUrl: string | undefined; | ||
|
||
export class ShortcutInfo { | ||
/** | ||
* @param {Object} the WebManifest's ShortcutInfo. | ||
* @param {URL} webManifestUrl the URL where the webmanifest is available. | ||
* @param {string} name | ||
* @param {string} shortName | ||
* @param {string} url target Url for when the shortcut is clicked | ||
* @param {string} chosenIconUrl Url for the icon | ||
*/ | ||
constructor(shortcutInfo: WebManifestShortcutJson, webManifestUrl: URL) { | ||
this.name = shortcutInfo['name'] || ''; | ||
this.shortName = shortcutInfo['short_name'] || this.name; | ||
this.url = shortcutInfo['url'] ? | ||
new URL(shortcutInfo['url'], webManifestUrl).toString() : undefined; | ||
|
||
if (shortcutInfo.icons === undefined) { | ||
return; | ||
} | ||
|
||
const suitableIcon = util.findSuitableIcon(shortcutInfo.icons, 'any'); | ||
if (suitableIcon !== null) { | ||
this.chosenIconUrl = new URL(suitableIcon.src, webManifestUrl).toString(); | ||
} | ||
} | ||
|
||
isValid(): boolean { | ||
return this.name != undefined && this.url != undefined && this.chosenIconUrl != undefined; | ||
constructor(readonly name: string, readonly shortName: string, readonly url: string, | ||
readonly chosenIconUrl: string) { | ||
this.name = name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're using Parameter Properties here right? I don't think you need lines 76 to 79. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great! Updated! |
||
this.shortName = shortName; | ||
this.url = url; | ||
this.chosenIconUrl = chosenIconUrl; | ||
} | ||
} | ||
|
||
|
@@ -132,7 +117,7 @@ export class TwaManifest { | |
signingKey: SigningKeyInfo; | ||
appVersionCode: number; | ||
appVersionName: string; | ||
shortcuts: string; | ||
shortcuts: ShortcutInfo[]; | ||
generatorApp: string; | ||
webManifestUrl?: URL; | ||
|
||
|
@@ -199,7 +184,7 @@ export class TwaManifest { | |
} | ||
|
||
generateShortcuts(): string { | ||
return '[' + JSON.parse(this.shortcuts).map((s: ShortcutInfo, i: number) => | ||
return '[' + this.shortcuts.map((s: ShortcutInfo, i: number) => | ||
`[name:'${s.name}', short_name:'${s.shortName}', url:'${s.url}', icon:'shortcut_${i}']`) | ||
.join(',') + | ||
']'; | ||
|
@@ -222,16 +207,39 @@ export class TwaManifest { | |
|
||
const fullStartUrl: URL = new URL(webManifest['start_url'] || '/', webManifestUrl); | ||
|
||
const shortcuts: ShortcutInfo[] = (webManifest.shortcuts || []) | ||
.map((s: WebManifestShortcutJson) => new ShortcutInfo(s, webManifestUrl)) | ||
.filter((s: ShortcutInfo) => s.isValid()) | ||
.filter((_: ShortcutInfo, i: number) => i < 4); | ||
const shortcuts: ShortcutInfo[] = []; | ||
|
||
if (webManifest.shortcuts) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'm not too familiar with TS but it seems to me that webManifest.shortcuts is guaranteed to be a list instead of a null or undefined though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a WebManifest shortcut, defined here: https://github.com/GoogleChromeLabs/llama-pack/blob/master/src/lib/types/WebManifest.ts#L40 It can be undefined. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed the for loop. |
||
for (const s of webManifest.shortcuts) { | ||
if (!s.icons || !s.url || (!s.name && !s.short_name)) { | ||
continue; | ||
} | ||
|
||
const suitableIcon = util.findSuitableIcon(s.icons, 'any'); | ||
if (!suitableIcon) { | ||
continue; | ||
} | ||
|
||
const name = s.name || s.short_name; | ||
const shortName = s.short_name || s.name!.substring(0, 12); | ||
const url = new URL(s.url, webManifestUrl).toString(); | ||
const iconUrl = new URL(suitableIcon.src, webManifestUrl).toString(); | ||
const shortcutInfo = new ShortcutInfo(name!, shortName!, url, iconUrl); | ||
|
||
shortcuts.push(shortcutInfo); | ||
|
||
if (shortcuts.length === 3) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
break; | ||
} | ||
} | ||
} | ||
|
||
const twaManifest = new TwaManifest({ | ||
packageId: generatePackageId(webManifestUrl.host), | ||
host: webManifestUrl.host, | ||
name: webManifest['name'] || webManifest['short_name'] || DEFAULT_APP_NAME, | ||
launcherName: webManifest['short_name'] || webManifest['name'] || DEFAULT_APP_NAME, | ||
launcherName: | ||
webManifest['short_name'] || webManifest['name']?.substring(0, 12) || DEFAULT_APP_NAME, | ||
themeColor: webManifest['theme_color'] || DEFAULT_THEME_COLOR, | ||
navigationColor: DEFAULT_NAVIGATION_COLOR, | ||
backgroundColor: webManifest['background_color'] || DEFAULT_BACKGROUND_COLOR, | ||
|
@@ -246,7 +254,7 @@ export class TwaManifest { | |
}, | ||
splashScreenFadeOutDuration: DEFAULT_SPLASHSCREEN_FADEOUT_DURATION, | ||
enableNotifications: DEFAULT_ENABLE_NOTIFICATIONS, | ||
shortcuts: JSON.stringify(shortcuts, undefined, 2), | ||
shortcuts: shortcuts, | ||
webManifestUrl: webManifestUrl.toString(), | ||
}); | ||
return twaManifest; | ||
|
@@ -295,7 +303,7 @@ export interface TwaManifestJson { | |
signingKey: SigningKeyInfo; | ||
appVersionCode?: number; // Older Manifests may not have this field. | ||
appVersion: string; // appVersionName - Old Manifests use `appVersion`. Keeping compatibility. | ||
shortcuts: string; | ||
shortcuts: ShortcutInfo[]; | ||
generatorApp?: string; | ||
webManifestUrl?: string; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,7 +148,7 @@ task generateShorcutsFile { | |
'intent'( | ||
'android:action': 'android.intent.action.MAIN', | ||
'android:targetPackage': twaManifest.applicationId, | ||
'android:targetClass': 'android.support.customtabs.trusted.LauncherActivity', | ||
'android:targetClass': 'com.google.androidbrowserhelper.trusted.LauncherActivity', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure about this? I'm guessing you tested it and the previous targetClass did not work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a left-over when we migrated to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've tested this, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, tested against There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the previous value not work though? It's still working for me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you using the |
||
'android:data': s.url) | ||
'categories'('android:name': 'android.intent.category.LAUNCHER') | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PEConn I think this is the last
any
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎊