ShareX/ShareX/NewsManager.cs

96 lines
3 KiB
C#
Raw Normal View History

2017-06-10 08:04:23 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2023-01-10 09:31:02 +13:00
Copyright (c) 2007-2023 ShareX Team
2017-06-10 08:04:23 +12:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.IO;
2018-03-01 11:52:11 +13:00
using System.Linq;
2017-06-10 08:04:23 +12:00
namespace ShareX
{
public class NewsManager
{
public List<NewsItem> NewsItems { get; private set; } = new List<NewsItem>();
2017-06-12 10:21:12 +12:00
public DateTime LastReadDate { get; set; }
2017-07-30 21:49:08 +12:00
public bool IsUnread => UnreadCount > 0;
public int UnreadCount => NewsItems != null ? NewsItems.Count(x => x.IsUnread) : 0;
2017-06-10 08:04:23 +12:00
public void UpdateNews()
{
try
{
2017-06-11 07:45:46 +12:00
NewsItems = GetNews();
2017-06-10 08:04:23 +12:00
}
catch (Exception e)
2017-06-10 08:04:23 +12:00
{
DebugHelper.WriteException(e);
}
}
2017-06-12 11:49:17 +12:00
public void UpdateUnread()
{
if (NewsItems != null)
{
foreach (NewsItem newsItem in NewsItems)
{
newsItem.IsUnread = newsItem.DateTime > LastReadDate;
}
}
}
2017-06-10 08:04:23 +12:00
private List<NewsItem> GetNews()
{
2022-08-25 17:35:33 +12:00
string url = URLHelpers.CombineURL(Links.Website, "news.json");
string response = URLHelpers.DownloadString(url);
2017-06-10 08:04:23 +12:00
2022-08-25 17:35:33 +12:00
if (!string.IsNullOrEmpty(response))
{
JsonSerializerSettings settings = new JsonSerializerSettings
2017-06-10 08:04:23 +12:00
{
2022-08-25 17:35:33 +12:00
DateTimeZoneHandling = DateTimeZoneHandling.Local
};
2017-06-12 08:01:11 +12:00
2022-08-25 17:35:33 +12:00
return JsonConvert.DeserializeObject<List<NewsItem>>(response, settings);
2017-06-10 08:04:23 +12:00
}
return null;
}
private void ExportNews(List<NewsItem> newsItems)
{
JsonSerializerSettings settings = new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore
};
string json = JsonConvert.SerializeObject(newsItems, settings);
File.WriteAllText("news.json", json);
}
2017-06-10 08:04:23 +12:00
}
}