123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- using KdanCommon.CMSCollection.Data;
- using KdanCommon.Helpers;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Windows.Data.Json;
- using Windows.Globalization;
- using Windows.Networking.Connectivity;
- using Windows.UI.Popups;
- using Windows.Web.Http;
- using Windows.Web.Http.Filters;
- namespace KdanCommon.CMSCollection
- {
- public class CMSCollection
- {
- public static string CMSDomain = "https://cms.kdanmobile.com";
- private List<ISetting> _settingList = null;
- private HttpClient _httpClient = null;
- private Uri _windowsCardsUri = null;
- private Uri _windowsEventbarUri = null;
- private Uri _pdfViewerEventBarSettingUri = null;
- private Uri _pdfOtherSettingUri = null;
- private string _appType = null;
- private bool _loadCardsOnly = false;
- private SemaphoreSlim _loadSettingSS = new SemaphoreSlim(1);
- private bool _isLoaded = false;
- private object settingLock = new object();
- public CMSCollection(string appType, bool loadCardOnly = true)
- {
- var filter = new HttpBaseProtocolFilter();
- filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent;
- _httpClient = new HttpClient(filter);
- _windowsCardsUri = new Uri($"{CMSDomain}/items/windows_cards");
- _pdfViewerEventBarSettingUri = new Uri($"{CMSDomain}/items/pdf_remote_config");
- _pdfOtherSettingUri = new Uri($"{CMSDomain}/items/PDF_UWP_RemoteConfig");
- _windowsEventbarUri = new Uri($"{CMSDomain}/items/windows_eventbar");
- _appType = appType;
- _loadCardsOnly = loadCardOnly;
- _settingList = new List<ISetting>();
- Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
- }
- ~CMSCollection()
- {
- _httpClient.Dispose();
- Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged -= NetworkInformation_NetworkStatusChanged;
- }
- private async void NetworkInformation_NetworkStatusChanged(object sender)
- {
- if (IsInternetAvailable())
- await LoadSettings();
- }
- #region Public Method
- public async Task<ISetting> GetSetting(string settingId)
- {
- await LoadSettings();
- lock (settingLock)
- return _settingList.FirstOrDefault(s => s.SettingName == settingId);
- }
- public async Task<List<WindowsCardSetting>> GetCardSettings()
- {
- await LoadSettings();
- lock (settingLock)
- return _settingList.Where(s => s is WindowsCardSetting).Select(s => s as WindowsCardSetting).OrderByDescending(w => w.Weight).ToList();
- }
- #endregion
- #region Private Method
- private async Task LoadSettings()
- {
- try
- {
- await _loadSettingSS.WaitAsync();
- if (!_isLoaded)
- {
- lock (settingLock)
- _settingList.Clear();
- if (!IsInternetAvailable())
- return;
- var allTask = new List<Task>();
- allTask.Add(GetWindowsCards());
- if (!_loadCardsOnly)
- {
- allTask.Add(GetViewerEventBarSetting());
- allTask.Add(GetOtherSetting());
- }
- await Task.WhenAll(allTask);
- _isLoaded = true;
- }
- }
- catch
- {
- }
- finally
- {
- _loadSettingSS.Release();
- }
- }
- private async Task GetViewerEventBarSetting()
- {
- var region = GetSystemRegion();
- var appLang = GetAppLanguage();
- var querys = new List<KeyValuePair<string, string>>();
- querys.Add(new KeyValuePair<string, string>("deep[evnet_bar_text_translations][_filter][languages_code][_eq]", appLang));
- querys.Add(new KeyValuePair<string, string>("deep[dialog_text_translations][_filter][languages_code][_eq]", appLang));
- querys.Add(new KeyValuePair<string, string>("deep[dialog_button][dialog_button_translations][_filter][languages_code][_eq]", appLang));
- querys.Add(new KeyValuePair<string, string>("deep[do_not_show_translations][_filter][languages_code][_eq]", appLang));
- querys.Add(new KeyValuePair<string, string>("filter[platform][_eq]", "windows"));
- querys.Add(new KeyValuePair<string, string>("filter[event_visible][_eq]", "true"));
- querys.Add(new KeyValuePair<string, string>("filter[regions][_contains]", region));
- querys.Add(new KeyValuePair<string, string>("fields", "*,do_not_show_translations.*,evnet_bar_text_translations.*,dialog_text_translations.*,dialog_button.*,dialog_button.dialog_button_translations.*"));
- var result = await _httpClient.GetRequest(_pdfViewerEventBarSettingUri, querys);
- if (result.StatusCode == HttpStatusCode.Ok)
- {
- var jsonString = await result.Content.ReadAsStringAsync();
- var response = JsonTool.DeserializeJSON<ViewerEventBarSettingResponse>(jsonString);
- if (response != null && response.Data != null && response.Data.Length > 0)
- {
- lock (settingLock)
- _settingList.Add(response.Data[0]);
- }
- }
- }
- private async Task GetOtherSetting()
- {
- var querys = new List<KeyValuePair<string, string>>();
- querys.Add(new KeyValuePair<string, string>("filter[status][_eq]", "published"));
- querys.Add(new KeyValuePair<string, string>("filter[appTypes][_contains]", _appType));
- querys.Add(new KeyValuePair<string, string>("fields", "*"));
- var result = await _httpClient.GetRequest(_pdfOtherSettingUri, querys);
- if (result.StatusCode == HttpStatusCode.Ok)
- {
- var jsonString = await result.Content.ReadAsStringAsync();
- dynamic dyna = DynamicJsonHelper.ToDynamicJson(jsonString);
- if (dyna == null)
- return;
- List<dynamic> settings = DynamicJsonHelper.GetArraySafety(dyna.data);
- if (settings == null)
- return;
- foreach (var setting in settings)
- {
- string configName = DynamicJsonHelper.GetValueSafety<string>(setting.config_name, null);
- if (!string.IsNullOrEmpty(configName) && setting.json_data != null)
- {
- lock (settingLock)
- _settingList.Add(new JsonSetting(configName, setting.json_data));
- }
- }
- }
- }
- private async Task GetWindowsCards()
- {
- string region = GetSystemRegion();
- string lang = GetAppLanguage();
- var querys = new List<KeyValuePair<string, string>>();
- querys.Add(new KeyValuePair<string, string>("deep[Information][_filter][languages_code][_eq]", lang));
- querys.Add(new KeyValuePair<string, string>("filter[_or][0][include_regions][_eq]", "true"));
- querys.Add(new KeyValuePair<string, string>("filter[_or][0][regions][_contains]", region));
- querys.Add(new KeyValuePair<string, string>("filter[_or][1][include_regions][_eq]", "false"));
- querys.Add(new KeyValuePair<string, string>("filter[_or][1][regions][_ncontains]", region));
- querys.Add(new KeyValuePair<string, string>("filter[appTypes][_contains]", _appType));
- querys.Add(new KeyValuePair<string, string>("filter[status][_eq]", "published"));
- querys.Add(new KeyValuePair<string, string>("fields", "*,Information.*"));
- var result = await _httpClient.GetRequest(_windowsCardsUri, querys);
- if (result.StatusCode == HttpStatusCode.Ok)
- {
- var jsonString = await result.Content.ReadAsStringAsync();
- var response = JsonTool.DeserializeJSON<WindowsCardsResponse>(jsonString);
- if (response != null && response.Data != null && response.Data.Length > 0)
- {
- foreach (var card in response.Data)
- {
- lock (settingLock)
- _settingList.Add(card);
- }
- }
- }
- }
- public async Task<List<WindowsEventbarSetting>> GetWindowsEventbarAsync()
- {
- var current = DateTime.UtcNow.ToString("s", CultureInfo.GetCultureInfo("en-us"));
- string region = GetSystemRegion();
- string lang = GetAppLanguage();
- var querys = new List<KeyValuePair<string, string>>();
- querys.Add(new KeyValuePair<string, string>("deep[event_information][_filter][languages_code][_eq]", lang));
- querys.Add(new KeyValuePair<string, string>("filter[_or][0][include_regions][_eq]", "true"));
- querys.Add(new KeyValuePair<string, string>("filter[_or][0][regions][_contains]", region));
- querys.Add(new KeyValuePair<string, string>("filter[_or][1][include_regions][_eq]", "false"));
- querys.Add(new KeyValuePair<string, string>("filter[_or][1][regions][_ncontains]", region));
- querys.Add(new KeyValuePair<string, string>("filter[_or][2][include_regions][_eq]", "false"));
- querys.Add(new KeyValuePair<string, string>("filter[_or][2][regions][_null]", "true"));
- querys.Add(new KeyValuePair<string, string>("filter[start][_lte]", current));
- querys.Add(new KeyValuePair<string, string>("filter[end][_gte]", current));
- querys.Add(new KeyValuePair<string, string>("filter[app_types][_contains]", _appType));
- querys.Add(new KeyValuePair<string, string>("filter[status][_eq]", "published"));
- querys.Add(new KeyValuePair<string, string>("fields", "*,event_information.*"));
- var result = await _httpClient.GetRequest(_windowsEventbarUri, querys);
- if (result.StatusCode == HttpStatusCode.Ok)
- {
- var jsonString = await result.Content.ReadAsStringAsync();
- var response = JsonTool.DeserializeJSON<WindowsEventbarResponse>(jsonString);
- if (response != null && response.Data != null && response.Data.Length > 0)
- {
- return response.Data.ToList();
- }
- else
- return new List<WindowsEventbarSetting>();
- }
- return null;
- }
- private string GetSystemRegion()
- {
- var geographyCountryCode = new GeographicRegion().CodeTwoLetter;
- switch (geographyCountryCode)
- {
- case "GB":
- case "US":
- case "TW":
- case "CN":
- case "JP":
- case "ES":
- case "IT":
- case "FR":
- case "DE":
- case "RU":
- case "PT":
- case "KR":
- return geographyCountryCode;
- default:
- return "OTHER";
- }
- }
- private string GetAppLanguage()
- {
- try
- {
- var currentLang = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Languages[0];
- switch (currentLang)
- {
- case "de-DE":
- case "de":
- return "de";
- case "es-ES":
- case "es":
- return "es";
- case "fr-FR":
- case "fr":
- return "fr";
- case "it-IT":
- case "it":
- return "it";
- case "ja-JP":
- case "ja":
- return "ja";
- case "zh-Hans-CN":
- case "zh-Hans":
- case "zh-CN":
- return "zh-cn";
- case "zh-Hant-TW":
- case "zh-Hant":
- case "zh-TW":
- return "zh-tw";
- case "ru-RU":
- case "ru":
- return "ru";
- case "ko-KR":
- case "ko":
- return "ko";
- case "pt-PT":
- case "pt":
- return "pt";
- default:
- return "en";
- }
- }
- catch
- {
- return "en";
- }
- }
- private bool IsInternetAvailable()
- {
- ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
- bool isInternetAvailable = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
- return isInternetAvailable;
- }
- #endregion
- }
- }
|