public static async Task <Dictionary <string, int> > FillGroupsDictionary(bool rewrite, CancellationToken cts) { var networkChecker = new NetworkChecker(Instance); string resultJson; var resourceName = "groups.json"; var b = File.Exists(GetFileFullPath(resourceName)); if (rewrite && networkChecker.Check()) { var client = new HttpClient(); resultJson = await HttpClientSL.GetResponseAsync(client, Instance.groupLink, cts); File.WriteAllText(GetFileFullPath(resourceName), resultJson); } else { using (var stream = GetEmbeddedResourceStream(resourceName)) using (var reader = b ? new StreamReader(GetFileFullPath(resourceName)) : new StreamReader(stream)) { resultJson = await reader.ReadToEndAsync(); } } var groups = JsonConvert.DeserializeObject <GroupRoot>(resultJson); var dictionary = groups.Groups.ToDictionary(x => x.Name, x => x.Id); return(dictionary); }
private void TextViewUpdate_Click(object sender, EventArgs e) { if (networkChecker.Check()) { progressBar.Visibility = ViewStates.Visible; Task.Run(async() => { MainApp.Instance.GroupsDictionary = new Nito.AsyncEx.AsyncLazy <Dictionary <string, int> >(async() => { return(await MainApp.FillGroupsDictionary(true, new System.Threading.CancellationToken())); }); var newGroupsDictionary = await MainApp.Instance.GroupsDictionary; array = newGroupsDictionary.Select(x => x.Key).ToArray(); groupsDictionary = newGroupsDictionary; RunOnUiThread(() => { suggestAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleDropDownItem1Line, array); autoCompleteTextViewAuth.Adapter = null; autoCompleteTextViewAuth.Adapter = suggestAdapter; progressBar.Visibility = ViewStates.Invisible; }); }); } else { Toast.MakeText(this, GetString(Resource.String.no_connection_title), ToastLength.Short).Show(); } }