Ejemplo n.º 1
0
        public static async Task <List <Rule> > GetRules(RulesSource source)
        {
            string text = await LoadRules(source);

            if (text == null)
            {
                return(null);
            }
            return(TextToRules(text));
        }
Ejemplo n.º 2
0
        private static async Task <string> loadRules(RulesSource source)
        {
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;
            StorageFile   file        = null;

            if (await localFolder.TryGetItemAsync(source.FileName) != null)
            {
                try {
                    file = await localFolder.GetFileAsync(source.FileName);

                    return(await FileIO.ReadTextAsync(file));
                } catch {
                    // ignored
                }
            }

            bool saveToFile = true;

            try
            { // Create file
                file = await localFolder.CreateFileAsync(source.FileName);
            }
            catch
            {
                saveToFile = false;
            }

            try
            {
                HttpClient client   = new HttpClient();
                var        response = await client.SendRequestAsync(new HttpRequestMessage(HttpMethod.Get, source.Uri));

                byte[]   bodyBytes = (await response.Content.ReadAsBufferAsync()).ToArray();
                Encoding encoding  = source.Encoding;

                string rules = encoding.GetString(bodyBytes, 0, bodyBytes.Length);

                if (saveToFile)
                {
                    await FileIO.WriteTextAsync(file, rules);
                }
                return(rules);
            } catch {
                // ignored
            }

            return(null);
        }
Ejemplo n.º 3
0
        private async Task <bool> LoadDataAsync(int rulesIndex)
        {
            RulesSource source    = rulesSources[rulesIndex];
            List <Rule> tempRules = await Rule.getRules(source);

            if (tempRules == null)
            {
                return(false);
            }
            clearCacheButton.IsEnabled = true;
            rules = tempRules;
            actualRulesTextBlock.Text = source.Date.ToString("dd/MM/yyyy") +
                                        (rulesIndex == rulesSources.Count - 1 ? " (" + ResourceLoader.GetForCurrentView().GetString("newest") + ")" : "");
            actualRules = rulesIndex;
            return(true);
        }
Ejemplo n.º 4
0
        private static async Task <string> LoadRules(RulesSource source)
        {
            string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), source.FileName);

            if (File.Exists(filePath))
            {
                try
                {
                    return(File.ReadAllText(filePath));
                }
                catch
                {
                    // ignored
                }
            }

            try
            {
                HttpClient client   = new HttpClient();
                var        response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, source.Uri));

                byte[] bodyBytes = await response.Content.ReadAsByteArrayAsync();

                Encoding encoding = source.Encoding;

                string rules = encoding.GetString(bodyBytes, 0, bodyBytes.Length);

                try
                {
                    File.WriteAllText(filePath, rules);
                }
                catch
                {
                    // ignored
                }

                return(rules);
            }
            catch
            {
                // ignored
            }

            return(null);
        }