private async void OpenProgressIndicator(IProgressIndicator indicator) { await indicator.AwaitTransitionAsync(); if (indicator.State == ProgressIndicatorState.Closed) { await indicator.OpenAsync(); } }
private async void HandleButtonClick(IProgressIndicator indicator) { // If the indicator is opening or closing, wait for that to finish before trying to open / close it // Otherwise the indicator will display an error and take no action await indicator.AwaitTransitionAsync(); switch (indicator.State) { case ProgressIndicatorState.Closed: OpenProgressIndicator(indicator); break; case ProgressIndicatorState.Open: await indicator.CloseAsync(); break; } }
/// <summary> /// Queries the Litecoin Pool REST API asynchronously via https request and updates the slate with the current stats. /// </summary> /// <returns>The asynchronous task which should be awaited the result of which is the http response message.</returns> public async void QueryRESTAPI() { // If we're already in the middle of querying the REST API, don't query it again. if (updating) { // Set the refresh timer back to 0. refreshTimer = 0f; return; } updating = true; if (Status.gameObject.activeSelf) { // Allow a brief moment so the status text is viewable on the first run or after an error. await Task.Delay(1000); } // Don't show the progress indicator on the first run (the mining stats will not be active on the first run or after an error). if (MiningStats.gameObject.activeSelf) { QueueOnUpdate(() => { ProgressIndicator.SetActive(true); }); await indicator.AwaitTransitionAsync(); UpdateProgressIndicator(); } client.CancelPendingRequests(); // Get the currently selected currency from the currency hand menu. currency = currencyHandMenu.GetComponent <CurrencySelection>().Currency; HttpResponseMessage response = await client.GetAsync(client.BaseAddress).ConfigureAwait(false); if (response.IsSuccessStatusCode) { JObject jobject = (JObject)JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); QueueOnUpdate(() => { // The hash rate is returned as kilohash. Convert it to a size appropriate unit of measure formatted for easy reading. HashRate.text = $"{FormatHash((decimal)jobject["user"]["hash_rate"] * 1000, 2)}/s"; }); QueueOnUpdate(() => { // Format the paid rewards to two decimal points and include a currency conversion for the currently selected currency. PaidRewards.text = $"{Math.Round((decimal)jobject["user"]["paid_rewards"], 2)} LTC ({ConvertCurrency((decimal)jobject["user"]["paid_rewards"], (decimal)jobject["market"][currency])})"; }); QueueOnUpdate(() => { // Format the unpaid rewards to six decimal points and include a currency conversion for the currently selected currency. UnpaidRewards.text = $"{Math.Round((decimal)jobject["user"]["unpaid_rewards"], 6)} LTC ({ConvertCurrency((decimal)jobject["user"]["unpaid_rewards"], (decimal)jobject["market"][currency])})"; }); QueueOnUpdate(() => { // The total work is returned as hash (lowest unit of measure). Convert it to a size appropriate unit of measure formatted for easy reading. TotalWork.text = FormatHash((decimal)jobject["user"]["total_work"] / 10, 0); }); QueueOnUpdate(() => { Blocks.text = (string)jobject["user"]["blocks_found"]; }); QueueOnUpdate(() => { // Format the price per share ratio as a percentage with no decimal points. Ratio.text = $"{Math.Round((decimal)jobject["pool"]["pps_ratio"] * 100, 0)}%"; }); QueueOnUpdate(() => { // Format the network difficulty to five decimal points and add standard numerical comma notation. Difficulty.text = Math.Round((decimal)jobject["network"]["difficulty"], 5).ToString("#,##0.00000"); }); QueueOnUpdate(() => { Status.gameObject.SetActive(false); MiningStats.SetActive(true); }); } else { QueueOnUpdate(() => { MiningStats.SetActive(false); Status.gameObject.SetActive(true); ProgressIndicator.SetActive(false); Status.text = "Error communicating with litecoinpool.org"; }); // Allow a brief moment so the status text to be viewable. await Task.Delay(2000); } updating = false; await indicator.AwaitTransitionAsync(); await indicator.CloseAsync(); }