Пример #1
0
        public bool DeleteAAEmail(Models.AlertManager aO)
        {
            try
            {
                using (var context = MangContext)
                {
                    var arecs = (from x in context.ActiveAlerts
                                 where x.Code == aO.Code && x.Value == aO.Email
                                 select x);

                    if (arecs == null)
                    {
                        throw new Exception("record does not exist");
                    }

                    context.ActiveAlerts.RemoveRange(arecs);

                    if (context.SaveChanges() > 0)
                    {
                        return(true);
                    }
                }
            } catch (Exception ex)
            {
                ErrorFlag    = true;
                ErrorMessage = ex.Message;
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            }
            return(false);
        }
Пример #2
0
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if ((null != navigationContext) &&
                !navigationContext.Parameters["view"].Equals(_AlertSource))
            {
                try
                {
                    _AlertSource = (string)navigationContext.Parameters["view"];

                    Models.AlertManager Alert = _Container.Resolve <Models.AlertManager>();
                    // Replace Live list
                    if (null != Lives)
                    {
                        _Disposables.Remove(Lives);
                    }
                    Lives?.Dispose();
                    Lives = Alert.GetLiveList(_AlertSource).ToReadOnlyReactiveCollection().AddTo(_Disposables);
                    // View filter
                    CollectionViewSource.GetDefaultView(Lives).Filter = x =>
                    {
                        if (string.IsNullOrWhiteSpace(FavoriteQuery.Value))
                        {
                            return(true);
                        }
                        // sanitize
                        //string escaped = System.Text.RegularExpressions.Regex.Escape(FavoriteQuery.Value);
                        //return System.Text.RegularExpressions.Regex.IsMatch(x.Title, $"({escaped})+");
                        return(0 <= (x as Models.LiveItem)?.Descriptors[SelectedFavoriteQueryTarget.Value.Value].IndexOf(FavoriteQuery.Value));
                    };
                    // Generate columns
                    List <Models.LiveItemColumn> columns = new List <Models.LiveItemColumn>();
                    Models.LiveDescriptor[]      descs   = Alert.GetQueryTargets(_AlertSource).Keys.ToArray();
                    for (int i = 0; i < descs.Length; i++)
                    {
                        columns.Add(new Models.LiveItemColumn(descs[i].Label, $"Descriptors[{i}]"));
                    }
                    columns.Add(new Models.LiveItemColumn("Start Date", nameof(Models.LiveItem.StartDate)));
                    columns.Add(new Models.LiveItemColumn("URL", nameof(Models.LiveItem.Url)));
                    LiveColumns = columns.ToArray();
                    // Notify LiveList PropertyChanged to View
                    RaisePropertyChanged(nameof(Lives));
                    RaisePropertyChanged(nameof(LiveColumns));
                    // Replace query type list
                    FavoriteQueryTargets = Alert.GetQueryTargets(_AlertSource);
                    RaisePropertyChanged(nameof(FavoriteQueryTargets));
                    SelectedFavoriteQueryTarget.Value = FavoriteQueryTargets.First();
                    // Refresh favorite
                    CollectionViewSource.GetDefaultView(Favorites).Refresh();
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }
            }
        }
Пример #3
0
        public bool EditActiveAlert(Models.AlertManager aO)
        {
            try
            {
                using (var context = MangContext)
                {
                    var arecs = (from x in context.ActiveAlerts
                                 where x.Code == aO.Code && x.Value == aO.Email
                                 select x);

                    IEnumerable <ActiveAlert> icEnts = arecs.Where(o => o.Name == "INSCOMP").Select(o => o);

                    int icCnt, acCnt, icxCnt = 0;

                    icCnt = icEnts.ToList().Count();

                    if (aO.INSCOMP == false && icCnt > 0)
                    {
                        context.ActiveAlerts.RemoveRange(icEnts);
                    }
                    else if (aO.INSCOMP == true && icCnt == 0)
                    {
                        context.ActiveAlerts.Add(new ActiveAlert()
                        {
                            Code = aO.Code, CID = aO.CID, Name = "INSCOMP", Value = aO.Email
                        });
                    }

                    IEnumerable <ActiveAlert> acEnts = arecs.Where(o => o.Name == "AUTOCOMP").Select(o => o);

                    acCnt = acEnts.ToList().Count();

                    if (aO.AUTOCOMP == false && acCnt > 0)
                    {
                        context.ActiveAlerts.RemoveRange(acEnts);
                    }
                    else if (aO.AUTOCOMP == true && acCnt == 0)
                    {
                        context.ActiveAlerts.Add(new ActiveAlert()
                        {
                            Code = aO.Code, CID = aO.CID, Name = "AUTOCOMP", Value = aO.Email
                        });
                    }

                    IEnumerable <ActiveAlert> icxEnts = arecs.Where(o => o.Name == "INSCOMPEX").Select(o => o);

                    icxCnt = icxEnts.ToList().Count();

                    if (aO.INSCOMPEX == false && icxCnt > 0)
                    {
                        context.ActiveAlerts.RemoveRange(icxEnts);
                    }
                    else if (aO.INSCOMPEX == true && icxCnt == 0)
                    {
                        context.ActiveAlerts.Add(new ActiveAlert()
                        {
                            Code = aO.Code, CID = aO.CID, Name = "INSCOMPEX", Value = aO.Email
                        });
                    }

                    var rCID = (from x in arecs select x.CID).FirstOrDefault();

                    if (rCID != aO.CID)
                    {
                        foreach (var item in arecs)
                        {
                            item.CID = aO.CID;
                        }
                    }

                    if (context.SaveChanges() > 0)
                    {
                        return(true);
                    }
                }
            } catch (Exception ex)
            {
                ErrorFlag    = false;
                ErrorMessage = ex.Message;
            }

            return(false);
        }
Пример #4
0
        public bool AddEmail(Models.AlertManager aO)
        {
            try
            {
                using (var context = MangContext)
                {
                    var crs = (from x in context.ActiveAlerts
                               where x.Name == "ACTIVE" && x.Value == aO.Email
                               select x).Count();

                    if (crs > 0)
                    {
                        throw new Exception("Email already exists");
                    }

                    if (aO.Email.Split('@').Length != 2)
                    {
                        throw new Exception("Incorrect format");
                    }

                    context.ActiveAlerts.Add(new ActiveAlert()
                    {
                        Code = "INS", CID = aO.CID, Name = "ACTIVE", Value = aO.Email
                    });

                    if (aO.INSCOMP == true)
                    {
                        context.ActiveAlerts.Add(new ActiveAlert()
                        {
                            Code = "INS", CID = aO.CID, Name = "INSCOMP", Value = aO.Email
                        });
                    }

                    if (aO.AUTOCOMP == true)
                    {
                        context.ActiveAlerts.Add(new ActiveAlert()
                        {
                            Code = "INS", CID = aO.CID, Name = "AUTOCOMP", Value = aO.Email
                        });
                    }

                    if (aO.INSCOMPEX == true)
                    {
                        context.ActiveAlerts.Add(new ActiveAlert()
                        {
                            Code = "INS", CID = aO.CID, Name = "INSCOMPEX", Value = aO.Email
                        });
                    }

                    if (context.SaveChanges() > 0)
                    {
                        return(true);
                    }
                }
            } catch (Exception ex)
            {
                ErrorFlag    = false;
                ErrorMessage = ex.Message;
            }

            return(false);
        }
Пример #5
0
        public SystemTrayViewModel(IContainer container)
        {
            TaskTrayIcon = new ReactiveProperty <string>("/LiveNotify;component/Resources/App.ico");

            try
            {
                // Create Window
                if (null == App.Current.MainWindow)
                {
                    _logger.Info("Open " + nameof(Views.MainWindow));
                    App.Current.MainWindow = container.Resolve <Views.MainWindow>();
                }
                _Messanger = container.Resolve <Models.NotifyBalloonMessage>();

                // Create Alert Manager
                _Alert = container.Resolve <Models.AlertManager>();
                _Disposables.Add(_Alert);
                _Alert.FavMatchedLiveArrived += Alert_FavMatchedLiveArrived;
                _Alert.FavMatchedLiveEnd     += Alert_FavMatchedLiveEnd;
                // Watch favorite list changed
                _Alert.Favorites.ToCollectionChanged().Subscribe(x =>
                {
                    if (_Alert.Favorites.All(y => 0 >= y.MatchedLives.Count))
                    {
                        TaskTrayIcon.Value = TaskTrayIconPaths[0];
                    }
                });

                // Delayed alart start
                Task.Delay(500).ContinueWith(x =>
                {
                    if (!_Alert.Start())
                    {
                        throw new Exception("Can not start Alert");
                    }
                });
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }

            ShowDialogCommand = new ReactiveCommand();
            ShowDialogCommand.Subscribe(() =>
            {
                if (false == App.Current.MainWindow?.IsVisible)
                {
                    _logger.Info("Show " + nameof(Views.MainWindow));
                    App.Current.MainWindow.Show();
                    App.Current.MainWindow.Activate();
                }
                else if (true == App.Current.MainWindow?.IsVisible)
                {
                    App.Current.MainWindow.Activate();
                }
            });

            HideDialogCommand = new ReactiveCommand();
            HideDialogCommand.Subscribe(() =>
            {
                if (true == App.Current.MainWindow?.IsVisible)
                {
                    App.Current.MainWindow.Hide();
                }
            });

            ExitCommand = new ReactiveCommand();
            ExitCommand.Subscribe(() =>
            {
                _logger.Info("Exit Application");
                this.Dispose();
                App.Current.MainWindow?.Close();
                App.Current.Shutdown();
            });
        }
Пример #6
0
        public LiveListViewModel(IContainer container)
        {
            _Container = container;

            FavoriteLabel = new ReactiveProperty <string>();
            FavoriteQuery = new ReactiveProperty <string>()
                            .SetValidateNotifyError(x => string.IsNullOrWhiteSpace(x) ? "Please input query" : null);

            FavoriteQueryTargets        = new Dictionary <Models.LiveDescriptor, int>();
            SelectedFavoriteQueryTarget = new ReactiveProperty <KeyValuePair <Models.LiveDescriptor, int> >();

            Favorites = _Container.Resolve <Models.AlertManager>().Favorites.ToReadOnlyReactiveCollection();
            CollectionViewSource.GetDefaultView(Favorites).Filter = x =>
            {
                return((x as Models.Favorite)?.AlertSource == _AlertSource);
            };

            // Open in System default WebBrowser
            DoubleClickLiveItemCommand = new ReactiveCommand();
            DoubleClickLiveItemCommand.Subscribe(x =>
            {
                if (x?.GetType() == typeof(Models.LiveItem))
                {
                    Process.Start(new ProcessStartInfo((x as Models.LiveItem).Url.ToString())
                    {
                        UseShellExecute = true
                    });
                }
            });

            AddFavoriteCommand = FavoriteQuery.ObserveHasErrors.Inverse().ToReactiveCommand();
            AddFavoriteCommand.Subscribe(async() =>
            {
                try
                {
                    Models.AlertManager manager = _Container.Resolve <Models.AlertManager>();
                    Models.Favorite fav         = new Models.Favorite(
                        _AlertSource
                        , FavoriteLabel.Value
                        , FavoriteQuery.Value
                        , SelectedFavoriteQueryTarget.Value.Key.DataName);
                    fav.IsEnabled = true;
                    if (await manager.AddFavorite(fav))
                    {
                        manager.Save();
                        // Clear edits.
                        FavoriteLabel.Value = string.Empty;
                        FavoriteQuery.Value = string.Empty;
                        SelectedFavoriteQueryTarget.Value = FavoriteQueryTargets.First();
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }
            });

            RemoveFavoriteCommand = new ReactiveCommand();
            RemoveFavoriteCommand.Subscribe(x =>
            {
                try
                {
                    Models.AlertManager manager = _Container.Resolve <Models.AlertManager>();
                    manager.RemoveFavorite((x as Models.Favorite));
                    manager.Save();
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }
            });

            SearchCommand = new ReactiveCommand();
            SearchCommand.Subscribe(() =>
            {
                // Refresh view
                CollectionViewSource.GetDefaultView(Lives).Refresh();
            });

            ClearSearchBoxCommand = new ReactiveCommand();
            ClearSearchBoxCommand.Subscribe(() =>
            {
                FavoriteLabel.Value = string.Empty;
                FavoriteQuery.Value = string.Empty;
                SelectedFavoriteQueryTarget.Value = FavoriteQueryTargets.First();
                CollectionViewSource.GetDefaultView(Lives).Refresh();
            });
        }