public ThemeSelectorResult GetTheme(RequestContext context) {
     if (AdminFilter.IsApplied(context)) return null;
     if (_result != null) return _result;
     // If the user reverted to the default, short-circuit this
     var workContext = _workContextAccessor.GetContext();
     var session = workContext.HttpContext.Session;
     if (session != null &&
         (bool)(session[workContext.CurrentSite.SiteName + "Vandelay.ThemePicker.UseDefault"] ?? false)) {
         
         return null;
     }
     var settings = _cacheManager.Get(
         "Vandelay.ThemePicker",
         ctx => {
             ctx.Monitor(_signals.When("Vandelay.ThemePicker.SettingsChanged"));
             return _settingsService.Get();
         });
     var selectedThemeRule =
         (from settingsRecord in settings
          let rule = _rules.Where(r =>
                                  r.GetType().Name.Equals(settingsRecord.RuleType, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()
          where rule != null && rule.Matches(settingsRecord.Name, settingsRecord.Criterion)
          select settingsRecord).FirstOrDefault();
     if (selectedThemeRule == default(ThemePickerSettingsRecord)) return null;
     if (!String.IsNullOrWhiteSpace(selectedThemeRule.Zone)) {
         dynamic linkShape = _shapeFactory.Create("Vandelay_ThemePicker_LinkToDefault");
         var zone = workContext.Layout.Zones[selectedThemeRule.Zone];
         zone.Add(linkShape, selectedThemeRule.Position);
     }
     _result = new ThemeSelectorResult {
         Priority = selectedThemeRule.Priority,
         ThemeName = selectedThemeRule.Theme
     };
     return _result;
 }
        public ThemeSelectorResult GetTheme(RequestContext context)
        {
            if (AdminFilter.IsApplied(context)) return null;

            if (_result != null) return _result;

            try
            {
                var workContext = _workContextAccessor.GetContext();
                var session = workContext.HttpContext.Session;
                if (session != null)
                {
                    string groupName = session[workContext.CurrentSite.SiteName + "MobileContrib.ThemeSwitcher.DeviceGroup"] as string;
                    if(groupName != null)
                    {
                        var devicegroup = _deviceGroupService.GetGroup(groupName);

                        _result = new ThemeSelectorResult
                        {
                            Priority = 50,
                            ThemeName = devicegroup.Theme
                        };
                        return _result;
                    }
                }

                var part = _deviceGroupService.GetCurrentGroup();
                if (part != null)
                {
                    _result = new ThemeSelectorResult
                    {
                        Priority = 50,
                        ThemeName = part.Theme
                    };
                    return _result;
                }
            }
            catch (Exception)
            {
                // HACK (jamesr): The theme selector seems to be getting called before the DeviceGroupRecord database table is created. Need to find a proper fix.
            }

            return null;
        }