From e83452785cb14a2e6e452bed5210d0fb2a6d7e28 Mon Sep 17 00:00:00 2001 From: Mo Date: Fri, 24 Jun 2022 07:37:49 -0500 Subject: [PATCH] fix(mobile): force redownload of themes if unable to parse first time (#1150) --- packages/mobile/ios/Podfile.lock | 4 +- packages/mobile/src/Lib/ComponentManager.ts | 2 + packages/mobile/src/Style/ThemeService.ts | 49 +++++++++++++++------ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/packages/mobile/ios/Podfile.lock b/packages/mobile/ios/Podfile.lock index 010d8b262..1075c51f1 100644 --- a/packages/mobile/ios/Podfile.lock +++ b/packages/mobile/ios/Podfile.lock @@ -419,7 +419,7 @@ PODS: - React-RCTImage - RNSearchBar (3.5.1): - React-Core - - RNShare (7.6.2): + - RNShare (7.6.4): - React-Core - RNStoreReview (0.2.1): - React-Core @@ -744,7 +744,7 @@ SPEC CHECKSUMS: RNReanimated: 46cdb89ca59ab7181334f4ed05a70e82ddb36751 RNScreens: 40a2cb40a02a609938137a1e0acfbf8fc9eebf19 RNSearchBar: 5ed8e13ba8a6c701fbd2afdfe4164493d24b2aee - RNShare: da0fd52f08c7b9f1ca31aef98a4890e98fc2c4c5 + RNShare: 4406f61af043027b695c3a0b8f39e2c2bdacde12 RNStoreReview: e05edbbf426563070524cec384ac0b8df69be801 RNSVG: 302bfc9905bd8122f08966dc2ce2d07b7b52b9f8 RNVectorIcons: 7923e585eaeb139b9f4531d25a125a1500162a0b diff --git a/packages/mobile/src/Lib/ComponentManager.ts b/packages/mobile/src/Lib/ComponentManager.ts index e3538dd3b..245c544ad 100644 --- a/packages/mobile/src/Lib/ComponentManager.ts +++ b/packages/mobile/src/Lib/ComponentManager.ts @@ -260,10 +260,12 @@ export class ComponentManager extends SNComponentManager { if (!(await RNFS.exists(componentPath))) { return undefined } + const filePath = `${componentPath}/${relativePath}` if (!(await RNFS.exists(filePath))) { return undefined } + const fileContents = await RNFS.readFile(filePath) return fileContents } diff --git a/packages/mobile/src/Style/ThemeService.ts b/packages/mobile/src/Style/ThemeService.ts index 685a543e5..54774cc0a 100644 --- a/packages/mobile/src/Style/ThemeService.ts +++ b/packages/mobile/src/Style/ThemeService.ts @@ -372,22 +372,38 @@ export class ThemeService { private async downloadTheme(theme: SNTheme): Promise { const componentManager = this.application?.mobileComponentManager if (componentManager?.isComponentDownloadable(theme)) { - if (await componentManager.doesComponentNeedDownload(theme)) { - await componentManager.downloadComponentOffline(theme) - } + return this.downloadThemeVariablesFromPackage(theme) + } else { + return this.downloadThemeVariablesFromHostedUrl(theme) + } + } - const file = await componentManager.getIndexFile(theme.identifier) - if (!file) { - console.error(`Did not find local index file for ${theme.identifier}`) - return undefined - } - const variables: ThemeVariables = CSSParser.cssToObject(file) - if (!variables || Object.keys(variables).length === 0) { - return undefined - } - return variables + private async downloadThemeVariablesFromPackage( + theme: SNTheme, + forceDownload = false, + ): Promise { + const componentManager = this.application!.mobileComponentManager + if (forceDownload || (await componentManager.doesComponentNeedDownload(theme))) { + await componentManager.downloadComponentOffline(theme) } + const file = await componentManager.getIndexFile(theme.identifier) + if (!file) { + console.error(`Did not find local index file for ${theme.identifier}`) + return undefined + } + + const variables: ThemeVariables = CSSParser.cssToObject(file) + if (!variables || Object.keys(variables).length === 0) { + if (!forceDownload) { + return this.downloadThemeVariablesFromPackage(theme, true) + } + } + + return variables + } + + private async downloadThemeVariablesFromHostedUrl(theme: SNTheme): Promise { let url = theme.hosted_url if (!url) { console.error('Theme download error') @@ -423,16 +439,19 @@ export class ThemeService { this.activateTheme(theme.uuid) return } + const variables = await this.downloadTheme(theme) if (!variables) { Alert.alert('Not Available', 'This theme is not available on mobile.') return } + const appliedVariables = Object.assign(this.templateVariables(), variables) const finalVariables = { ...appliedVariables, ...ThemeService.constants, } + const mobileTheme = new MobileTheme( theme.payload.copy({ content: { @@ -490,17 +509,21 @@ export class ThemeService { if (!variables) { return false } + /** Merge default variables to ensure this theme has all the variables. */ const appliedVariables = Object.assign(this.templateVariables(), variables) const mobileTheme = this.findOrCreateTheme(theme.uuid, { ...appliedVariables, ...ThemeService.constants, }) + this.addTheme(mobileTheme) void this.cacheThemes() + if (theme.uuid === this.activeThemeId) { this.setActiveTheme(theme.uuid) } + return true }