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 _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(); 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 GetSetting(string settingId) { await LoadSettings(); lock (settingLock) return _settingList.FirstOrDefault(s => s.SettingName == settingId); } public async Task> 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(); 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>(); querys.Add(new KeyValuePair("deep[evnet_bar_text_translations][_filter][languages_code][_eq]", appLang)); querys.Add(new KeyValuePair("deep[dialog_text_translations][_filter][languages_code][_eq]", appLang)); querys.Add(new KeyValuePair("deep[dialog_button][dialog_button_translations][_filter][languages_code][_eq]", appLang)); querys.Add(new KeyValuePair("deep[do_not_show_translations][_filter][languages_code][_eq]", appLang)); querys.Add(new KeyValuePair("filter[platform][_eq]", "windows")); querys.Add(new KeyValuePair("filter[event_visible][_eq]", "true")); querys.Add(new KeyValuePair("filter[regions][_contains]", region)); querys.Add(new KeyValuePair("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(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>(); querys.Add(new KeyValuePair("filter[status][_eq]", "published")); querys.Add(new KeyValuePair("filter[appTypes][_contains]", _appType)); querys.Add(new KeyValuePair("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 settings = DynamicJsonHelper.GetArraySafety(dyna.data); if (settings == null) return; foreach (var setting in settings) { string configName = DynamicJsonHelper.GetValueSafety(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>(); querys.Add(new KeyValuePair("deep[Information][_filter][languages_code][_eq]", lang)); querys.Add(new KeyValuePair("filter[_or][0][include_regions][_eq]", "true")); querys.Add(new KeyValuePair("filter[_or][0][regions][_contains]", region)); querys.Add(new KeyValuePair("filter[_or][1][include_regions][_eq]", "false")); querys.Add(new KeyValuePair("filter[_or][1][regions][_ncontains]", region)); querys.Add(new KeyValuePair("filter[appTypes][_contains]", _appType)); querys.Add(new KeyValuePair("filter[status][_eq]", "published")); querys.Add(new KeyValuePair("fields", "*,Information.*")); var result = await _httpClient.GetRequest(_windowsCardsUri, querys); if (result.StatusCode == HttpStatusCode.Ok) { var jsonString = await result.Content.ReadAsStringAsync(); var response = JsonTool.DeserializeJSON(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> GetWindowsEventbarAsync() { var current = DateTime.UtcNow.ToString("s", CultureInfo.GetCultureInfo("en-us")); string region = GetSystemRegion(); string lang = GetAppLanguage(); var querys = new List>(); querys.Add(new KeyValuePair("deep[event_information][_filter][languages_code][_eq]", lang)); querys.Add(new KeyValuePair("filter[_or][0][include_regions][_eq]", "true")); querys.Add(new KeyValuePair("filter[_or][0][regions][_contains]", region)); querys.Add(new KeyValuePair("filter[_or][1][include_regions][_eq]", "false")); querys.Add(new KeyValuePair("filter[_or][1][regions][_ncontains]", region)); querys.Add(new KeyValuePair("filter[_or][2][include_regions][_eq]", "false")); querys.Add(new KeyValuePair("filter[_or][2][regions][_null]", "true")); querys.Add(new KeyValuePair("filter[start][_lte]", current)); querys.Add(new KeyValuePair("filter[end][_gte]", current)); querys.Add(new KeyValuePair("filter[app_types][_contains]", _appType)); querys.Add(new KeyValuePair("filter[status][_eq]", "published")); querys.Add(new KeyValuePair("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(jsonString); if (response != null && response.Data != null && response.Data.Length > 0) { return response.Data.ToList(); } else return new List(); } 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 } }