Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
Migrate old theme data to new format
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Jan 30, 2018
1 parent ce6e8e2 commit f064e8b
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/Styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,22 @@ export default class GlobalStyles {
// JSON stringified content is generic and includes all items property at time of stringification
// So we parse it, then set content to itself, so that the mapping can be handled correctly.
try {
var theme = JSON.parse(themeResult);
let content = Object.assign({}, theme);
theme.content = content;
theme = new Theme(theme);
var parsedTheme = JSON.parse(themeResult);
var needsMigration = false;
if(!parsedTheme.appData) {
// Newer versions of the app persist a Theme object where mobileRules are nested in AppData.
// We want to check if the currently saved data is of the old format, which uses theme.mobileRules
// instead of theme.getMobileRules(). If so, we want to prepare it for the new format.
needsMigration = true;
}
let content = Object.assign({}, parsedTheme);
parsedTheme.content = content;

var theme = new Theme(parsedTheme);
if(needsMigration) {
theme.setMobileRules(parsedTheme.mobileRules);
}

theme.isSwapIn = true;
var constants = _.merge(this.defaultConstants(), theme.getMobileRules().constants);
var rules = _.merge(this.defaultRules(constants), theme.getMobileRules().rules);
Expand Down Expand Up @@ -93,22 +105,22 @@ export default class GlobalStyles {
}

static constantForKey(key) {
var value = this.get().constants[key];

// For the platform value, if the active theme does not have a specific value, but the defaults do, we don't
// want to use the defaults, but instead just look at the activeTheme. Because default platform values only apply
// to the default theme
var platform = Platform.OS == "android" ? "Android" : "IOS";
if(!this.get().activeTheme.hasMobileRules()) {
return value;
}
var defaultValue = this.get().constants[key];

var platformValue = this.get().activeTheme.getMobileRules().constants[key+platform];
try {
// For the platform value, if the active theme does not have a specific value, but the defaults do, we don't
// want to use the defaults, but instead just look at the activeTheme. Because default platform values only apply
// to the default theme
var platform = Platform.OS == "android" ? "Android" : "IOS";
var platformValue = this.get().activeTheme.getMobileRules().constants[key+platform];

if(platformValue) {
return platformValue;
} else {
return value;
if(platformValue) {
return platformValue;
} else {
return defaultValue;
}
} catch (e) {
return defaultValue;
}
}

Expand Down

0 comments on commit f064e8b

Please sign in to comment.