Exemple #1
0
        private void UpdateLabels()
        {
            if (transactions == null)
            {
                return;
            }

            HodlStatus status = HodlStatus.GetCurrent(transactions);

            portfolioGrid.Children.Clear();
            portfolioGrid.ColumnDefinitions.Clear();

            for (int i = 0; i < AppUtils.CryptoCurrencies.Length; i++)
            {
                string key = AppUtils.CryptoCurrencies[i];
                portfolioGrid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                });
                portfolioGrid.Children.Add(new Label
                {
                    HorizontalTextAlignment = TextAlignment.Center,
                    HorizontalOptions       = LayoutOptions.CenterAndExpand,
                    FontSize = 12,
                    Text     = string.Format(
                        "{0:0.000000} {1}",
                        status.TotalCryptos[key],
                        key)
                }, i, 0);
                portfolioGrid.Children.Add(
                    new Label
                {
                    HorizontalTextAlignment = TextAlignment.Center,
                    HorizontalOptions       = LayoutOptions.CenterAndExpand,
                    FontSize = 12,
                    Text     = AppUtils.GetMoneyString(AppUtils.GetFiatValOfCrypto(
                                                           AppUtils.FiatPref,
                                                           key,
                                                           status.TotalCryptos[key]),
                                                       AppUtils.FiatPref)
                }, i, 1);
                portfolioGrid.Children.Add(
                    new Label
                {
                    HorizontalTextAlignment = TextAlignment.Center,
                    HorizontalOptions       = LayoutOptions.CenterAndExpand,
                    FontSize = 11,
                    Text     = "at " + AppUtils.GetMoneyString(
                        AppUtils.ConvertFiat(
                            "USD",
                            AppUtils.FiatPref,
                            AppUtils.FiatConvert.UsdToCrypto[key]),
                        AppUtils.FiatPref)
                }, i, 2);
            }

            cashValueLabel.Text = "Cashed out: " + AppUtils.GetMoneyString(status.FloatingFiat, AppUtils.FiatPref);
            userValueLabel.Text = AppUtils.GetMoneyString(status.CryptoFiatVal, AppUtils.FiatPref);

            string profLoss  = (status.Profit >= 0) ? "Profit" : "Loss";
            string plusMinus = (status.Profit >= 0) ? "+" : "-";

            profitLabel.TextColor = (status.Profit >= 0) ? Color.ForestGreen : Color.IndianRed;
            profitLabel.Text      = string.Format("{0}: {1} ({2}{3:0.0}%)",
                                                  profLoss,
                                                  AppUtils.GetMoneyString(Math.Abs(status.Profit), AppUtils.FiatPref),
                                                  plusMinus,
                                                  Math.Abs(status.PercentChange));

            // Update the user's widgets if they have any
            DependencyService.Get <IWidgetManager>().UpdateWidget(status.CryptoFiatVal, status.Profit, AppUtils.FiatPref);
        }
Exemple #2
0
        public override async void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
        {
            UpdateWidgets(context, appWidgetManager, appWidgetIds, "Loading", null, null, Color.Blue, true);

            try
            {
                // Load database manually - can't use interface without initializing Xamarin Forms
                SQLite_Android   dbManager = new SQLite_Android();
                SQLiteConnection db        = dbManager.GetConnection();
                db.CreateTable <Transaction>();
                db.CreateTable <AppCache>();

                List <Transaction> transactions = (from t in db.Table <Transaction>()
                                                   select t).ToList();
                AppCache    cache   = db.Table <AppCache>().FirstOrDefault();
                FiatConvert convert = JsonConvert.DeserializeObject <FiatConvert>(cache.ConvertDataJson);;

                if (convert == null || (DateTime.Now - cache.LastConvertRefresh) > TimeSpan.FromDays(1))
                {
                    var fiatResp = await Comms.Get <FiatConvert>(Comms.ConverterApi, "latest?base=USD");

                    if (fiatResp.Success && fiatResp.Data != null)
                    {
                        convert = fiatResp.Data;
                        cache.LastConvertRefresh = DateTime.Now;
                    }

                    if (!fiatResp.Success)
                    {
                        throw new Exception("Failed to refresh");
                    }
                }

                convert = await AppUtils.RefreshCryptos(convert, AppUtils.PriceSources[cache.SourcePref]);

                if (convert == null)
                {
                    throw new Exception("Failed to refresh");
                }

                HodlStatus status = HodlStatus.GetCurrent(transactions, convert, cache.FiatPref);

                if (symbolManager == null)
                {
                    symbolManager = new CurrencySymbolManager_Android();
                }
                RegionInfo  region  = symbolManager.GetRegion(cache.FiatPref);
                CultureInfo culture = symbolManager.GetCulture(region.Name);

                string totalVal  = string.Format(culture, "{0:C}", status.CryptoFiatVal);
                string profitVal = string.Format(culture, "{0:C}", status.Profit);
                string timeVal   = string.Format("Updated: {0:t}", DateTime.Now);
                Color  profCol   = (status.Profit >= 0) ? Color.ForestGreen : Color.IndianRed;

                UpdateWidgets(context, appWidgetManager, appWidgetIds, timeVal, totalVal, profitVal, profCol, false, true);

                cache.ConvertDataJson = JsonConvert.SerializeObject(convert);
                db.DeleteAll <AppCache>();
                db.Insert(cache);
                db.Close();
            }
            catch (Exception e)
            {
                UpdateWidgets(context, appWidgetManager, appWidgetIds,
                              string.Format("Update failed at: {0:t}", DateTime.Now), null, null, Color.Blue, false, true);
                Console.WriteLine("HODLR error: " + e.Message);
                Android.Util.Log.Error("Hodlr", "HODLR error: " + e.Message);
            }
        }