Exemplo n.º 1
0
        private static async Task LoadContext()
        {
            await ServiceLocator.TraceService.LoadLocal();

            _context = new CatrobatContext();

            var localSettings = await ServiceLocator.ContextService.RestoreLocalSettings();

            _context.LocalSettings = localSettings;

            if (localSettings == null)
            {
                _context.LocalSettings = new LocalSettings();

                var defaultProject = await ServiceLocator.ContextService.
                                     RestoreDefaultProgram(AppResources.Main_DefaultProgramName);

                ProgramChecker.CheckProgram(defaultProject);

                _context.LocalSettings.CurrentProgramName = defaultProject.Name;
                await defaultProject.Save();
            }

            if (_context.LocalSettings.CurrentLanguageString == null)
            {
                _context.LocalSettings.CurrentLanguageString =
                    ServiceLocator.CultureService.GetCulture().TwoLetterISOLanguageName;
            }

            var themeChooser = ServiceLocator.ThemeChooser;

            if (_context.LocalSettings.CurrentThemeIndex != -1)
            {
                themeChooser.SelectedThemeIndex = _context.LocalSettings.CurrentThemeIndex;
            }

            if (_context.LocalSettings.CurrentLanguageString != null)
            {
                ServiceLocator.GetInstance <SettingsViewModel>().CurrentCulture =
                    new CultureInfo(_context.LocalSettings.CurrentLanguageString);
            }

            var themeChooserChangedMessage = new GenericMessage <ThemeChooser>(themeChooser);

            Messenger.Default.Send(themeChooserChangedMessage, ViewModelMessagingToken.ThemeChooserListener);

            var contextChangedMessage = new GenericMessage <CatrobatContextBase>(_context);

            Messenger.Default.Send(contextChangedMessage, ViewModelMessagingToken.ContextListener);

            var localProjectsChangedMessage = new MessageBase();

            Messenger.Default.Send(localProjectsChangedMessage, ViewModelMessagingToken.LocalProgramsChangedListener);

            //var message = new GenericMessage<Project>(currentProject);
            //Messenger.Default.Send(message, ViewModelMessagingToken.CurrentProgramChangedListener);

            // allow viewmodels to load from settings
            Messenger.Default.Send(new GenericMessage <LocalSettings>(_context.LocalSettings), ViewModelMessagingToken.LoadSettings);
        }