public void UpdateWidget(double totalMoney, double profit, string fiatPref)
        {
            HodlrWidgetProvider widgetProvider = new HodlrWidgetProvider();

            Context context = MainActivity.Context;

            int[] ids = AppWidgetManager.GetInstance(context.ApplicationContext)
                        .GetAppWidgetIds(new ComponentName(
                                             context.ApplicationContext,
                                             Java.Lang.Class.FromType(typeof(HodlrWidgetProvider))));

            CurrencySymbolManager_Android symbolManager = new CurrencySymbolManager_Android();

            RegionInfo  region  = symbolManager.GetRegion(fiatPref);
            CultureInfo culture = symbolManager.GetCulture(region.Name);

            string totalVal  = string.Format(culture, "{0:C}", totalMoney);
            string profitVal = string.Format(culture, "{0:C}", profit);
            string timeVal   = string.Format("Updated: {0:t}", DateTime.Now);

            Android.Graphics.Color profCol = (profit >= 0) ?
                                             Android.Graphics.Color.ForestGreen : Android.Graphics.Color.IndianRed;

            widgetProvider.UpdateWidgets(
                context,
                AppWidgetManager.GetInstance(context),
                ids,
                timeVal,
                totalVal,
                profitVal,
                profCol,
                false,
                true
                );
        }
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);
            }
        }