Example #1
0
        public bool TryInitializeApp(out Exception exception)
        {
            exception = null;

            try
            {
                Directory.CreateDirectory(_options.DataBaseFolderPath);

                this.NewsPaper = NewsPaper.Load(Path.Combine(_options.DataBaseFolderPath, "news.json"));

                var filterSettingsFilePath = Path.Combine(_options.DataBaseFolderPath, "filters.json");
                this.FilterSettings = new FilterSettingsViewModel(filterSettingsFilePath);
                this.InitializeFilterSettings(this.FilterSettings.Model, filterSettingsFilePath);

                _channelCache            = new Dictionary <ProjectContainer, List <ChannelInfoViewModel> >();
                _updateDatabaseSemaphore = new SemaphoreSlim(initialCount: 1, maxCount: 1);

                _userManager.Initialize();
                _options.Save(Program.OptionsFilePath);

                _ = this.UpdateDatabaseAsync();
            }
            catch (Exception ex)
            {
                exception = ex;
                this.Logger.LogError(ex.GetFullMessage());
                return(false);
            }

            this.IsAppInitialized = true;
            return(true);
        }
Example #2
0
        public static NewsPaper Load(string filePath)
        {
            if (File.Exists(filePath))
            {
                var jsonString = File.ReadAllText(filePath);
                var newsPaper  = JsonSerializer.Deserialize <NewsPaper>(jsonString);

                return(newsPaper);
            }
            else
            {
                var newsPaper = new NewsPaper(new List <NewsEntry>()
                {
                    new NewsEntry(DateTime.UtcNow, "First News", "News description.")
                });
                var jsonString = JsonSerializer.Serialize(newsPaper, new JsonSerializerOptions()
                {
                    WriteIndented = true
                });
                File.WriteAllText(filePath, jsonString);

                return(newsPaper);
            }
        }