private void SearchButton_onClick(object sender, RoutedEventArgs e)
        {
            if (App.Timer != null)
            {
                App.Timer.Stop();
            }

            List <string> syms = App.StrToSymbols(KeywordStr.Text);

            if (syms.Count == 0)
            {
                string           message = "Please provide symbol(s) to search, separated by a comma.\nE.g., QQQ,SPY";
                string           caption = "Empty symbol to search";
                MessageBoxButton buttons = MessageBoxButton.OK;
                MessageBoxResult result  = MessageBox.Show(message, caption, buttons);
                return;
            }

            progressBar.IsVisible = true;
            WebClient client = new WebClient();

            client.DownloadStringCompleted += (obj, args) =>
            {
                try
                {
                    YahooAPI.UpdateQuotes(args.Result);
                    PortfolioViewModel currentView      = (PortfolioViewModel)PortfolioPivot.SelectedItem;
                    Portfolio          currentPortfolio = App.GetPortfolio(currentView.Title);
                    foreach (string s in syms)
                    {
                        Quote quote = App.GetQuote(s);
                        if (quote == null)
                        {
                            System.Diagnostics.Debug.WriteLine("Symbol " + s + " was not found in db.");
                            continue;
                        }
                        if (currentPortfolio.AddQuote(quote))
                        {
                            currentView.AddStockToView(quote);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // can be caused by network issue
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    System.Diagnostics.Debug.WriteLine(ex.Source);
                    System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                }
                if (App.Timer != null)
                {
                    App.Timer.Start();
                }
                progressBar.IsVisible = false;
            };
            client.DownloadStringAsync(YahooAPI.GetQuotesXmlUrl(syms));
        }
        private void ContextMenu_removeItem(object sender, RoutedEventArgs e)
        {
            // sender is a MenuItem.
            MenuItem           obj              = sender as MenuItem;
            Quote              item             = obj.DataContext as Quote;
            PortfolioViewModel currentView      = (PortfolioViewModel)PortfolioPivot.SelectedItem;
            Portfolio          currentPortfolio = App.GetPortfolio(currentView.Title);

            currentView.RemoveStockFromView(item);
            currentPortfolio.RemoveQuote(item);
            //currentView.LoadData();
        }
        private void CurrentList_Edit(object sender, EventArgs e)
        {
            if (App.Timer != null)
            {
                App.Timer.Stop();
            }
            var tb  = new TextBox();
            var box = new CustomMessageBox()
            {
                Caption            = "Rename list",
                Message            = "Please enter the new name",
                LeftButtonContent  = "Rename",
                RightButtonContent = "Cancel",
                Content            = tb,
                IsFullScreen       = false
            };

            box.Dismissed += (s, ev) =>
            {
                if (ev.Result == CustomMessageBoxResult.LeftButton)
                {
                    if (!App.PortfolioExists(tb.Text))
                    {
                        PortfolioViewModel currentView = (PortfolioViewModel)PortfolioPivot.SelectedItem;
                        int       currentViewIndex     = PortfolioPivot.SelectedIndex;
                        Portfolio currentPortfolio     = App.GetPortfolio(currentView.Title);
                        currentPortfolio.Name = tb.Text;
                        ViewModel.LoadData();
                        PortfolioPivot.SelectedIndex = currentViewIndex;
                    }
                    else
                    {
                        // prompt for redundant list name
                        string           message = "Oops, a list named " + tb.Text + " already exists.\nPlease try another one.";
                        string           caption = "List name used";
                        MessageBoxButton buttons = MessageBoxButton.OK;
                        MessageBoxResult result  = MessageBox.Show(message, caption, buttons);
                    }
                }
                if (App.Timer != null)
                {
                    App.Timer.Start();
                }
            };

            // focus on the text box by default
            box.Loaded += (s, ev) =>
            {
                tb.Focus();
            };

            box.Show();
        }
Пример #4
0
        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData()
        {
            this.PageCollection.Clear();

            List <Portfolio> portfolios = App.PortfolioList;

            foreach (Portfolio entry in portfolios)
            {
                PortfolioViewModel pvm = new PortfolioViewModel()
                {
                    Title = entry.Name
                };
                pvm.LoadData();
                this.PageCollection.Add(pvm);
                System.Diagnostics.Debug.WriteLine("Added portfolio view " + entry.Name + " to main page.");
            }

            // this.IsDataLoaded = true;
        }
        private void CurrentList_RefreshView()
        {
            if (App.Timer != null)
            {
                App.Timer.Stop();
            }

            PortfolioViewModel currentView      = (PortfolioViewModel)PortfolioPivot.SelectedItem;
            Portfolio          currentPortfolio = App.GetPortfolio(currentView.Title);
            List <string>      currentStockList = currentPortfolio.StockList;

            // do not refresh if the portfolio has nothing
            if (currentStockList.Count == 0)
            {
                return;
            }

            WebClient client = new WebClient();

            client.DownloadStringCompleted += (obj, args) =>
            {
                try
                {
                    YahooAPI.UpdateQuotes(args.Result.ToString());
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    System.Diagnostics.Debug.WriteLine(ex.Source);
                    System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                }
                if (App.Timer != null)
                {
                    App.Timer.Start();
                }
            };
            client.DownloadStringAsync(YahooAPI.GetQuotesXmlUrl(currentStockList));
        }
        private void CurrentList_Delete(object sender, EventArgs e)
        {
            if (App.Timer != null)
            {
                App.Timer.Stop();
            }
            PortfolioViewModel currentView = (PortfolioViewModel)PortfolioPivot.SelectedItem;
            int currentIndex = PortfolioPivot.SelectedIndex - 1;

            if (currentIndex < 0)
            {
                currentIndex = 0;
            }

            App.DeletePortfolio(App.GetPortfolio(currentView.Title));
            ViewModel.PageCollection.Remove(currentView);
            PortfolioPivot.SelectedIndex = currentIndex;

            if (App.Timer != null)
            {
                App.Timer.Start();
            }
        }