Пример #1
0
        public MainWindow()
        {
            LoadFromXmlFile("Resources/dictionary.txd");

            try
            {
                IEHelper.EnsureBrowserEmulationEnabled(AppDomain.CurrentDomain.FriendlyName);
            }
            catch (IeVersionTooOldException)
            {
                MessageBox.Show(
                    T("dialog.no_ie"),
                    T("dialog.header.warning"),
                    MessageBoxButton.OK,
                    MessageBoxImage.Warning);
                System.Diagnostics.Process.Start("https://support.microsoft.com/en-us/help/18520/download-internet-explorer-11-offline-installer");
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    T("dialog.ie_fail",
                      "exception", ex.ToString()),
                    T("dialog.header.warning"),
                    MessageBoxButton.OK,
                    MessageBoxImage.Warning);
            }

            InitializeComponent();
            _viewModel  = new MainViewModel(_database, DialogCoordinator.Instance);
            DataContext = _viewModel;
            _database.Database.EnsureCreatedAsync();
            _database.Items.LoadAsync();
            _localListPersistence = new LocalListPersistence(_database);

            QaListSelector.SelectedIndex = 0;
            //QaList.ItemsSource = _viewModel.ActiveQaList;
            _peingService = new PeingService();
            _peingService.OnLoginStatusChanged += (sender, status) =>
                                                  Dispatcher?.Invoke(() =>
            {
                if (sender != _peingService)
                {
                    return;
                }
                _viewModel.UpdatePeingStatus(status);
                if (status == ServiceStatus.Available)
                {
                    _viewModel.StatusText = T("status.logged_in.peing");
                }
                else if (status == ServiceStatus.NotLoggedIn)
                {
                    _viewModel.StatusText = T("status.dropped.peing");
                }
            });
            _marshmallowService = new MarshmallowService();
            _marshmallowService.OnLoginStatusChanged += (sender, status) =>
                                                        Dispatcher?.Invoke(() =>
            {
                if (sender != _marshmallowService)
                {
                    return;
                }
                _viewModel.UpdateMarshmallowStatus(status);
                if (status == ServiceStatus.Available)
                {
                    _viewModel.StatusText = T("status.logged_in.marshmallow");
                }
                else if (status == ServiceStatus.NotLoggedIn)
                {
                    _viewModel.StatusText = T("status.dropped.marshmallow");
                }
            });

            _viewModel.PropertyChanged += (sender, args) =>
            {
                if (args.PropertyName != nameof(_viewModel.ActiveQaList))
                {
                    return;
                }
                var list = _viewModel.ActiveQaList;
                switch (list)
                {
                case QaListStubsViewModel stubs:
                    _viewModel.ActiveQaItems = stubs.PopulatedItems;
                    break;

                case QaList qaList:
                    var coll2 = new ObservableCollection <QaItem>(qaList.Items);
                    _viewModel.ActiveQaItems = coll2;
                    break;

                case QaListObservable qaListObservable:
                    _viewModel.ActiveQaItems = qaListObservable.Items;
                    break;
                }
            };

            foreach (var stubs in _localListPersistence.GetAllStubs())
            {
                var vm = new QaListStubsViewModel(stubs, _database);
                vm.CollectionChanged += OnQaListModified;
                vm.PropertyChanged   += OnQaListLockStatusChanged;
                _viewModel.QaListStubs.Add(vm);
            }
            _localListPersistence.OnListModified += (sender, args) =>
            {
                Dispatcher?.InvokeAsync(() =>
                {
                    switch (args.Action)
                    {
                    case NotifyCollectionChangedAction.Add:
                        var vm                = new QaListStubsViewModel((QaListStubs)args.NewItems[0], _database);
                        vm.PropertyChanged   += OnQaListLockStatusChanged;
                        vm.CollectionChanged += OnQaListModified;
                        _viewModel.QaListStubs.Add(vm);

                        QaListSelector.SelectedItem = vm;
                        OpenCollectorFor(vm);
                        break;

                    case NotifyCollectionChangedAction.Remove:
                        _viewModel.QaListStubs.Remove(_viewModel.QaListStubs.FirstOrDefault(it => (QaListStubs)args.OldItems[0] == it.Underlying));
                        break;

                    case NotifyCollectionChangedAction.Replace:
                        _viewModel.QaListStubs.FirstOrDefault(it => (QaListStubs)args.NewItems[0] == it.Underlying)?.OnPropertyChanged(nameof(QaListStubsViewModel.Name));
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                });
            };

            _displayCommunication = new DisplayCommunication();
            try
            {
                _displayCommunication.Start();
                _viewModel.ServerStatusText = T("status.display.running", "port", DisplayCommunication.DisplayWSPort.ToString());
            }
            catch (Exception)
            {
                _viewModel.ServerStatusText = T("status.display.failed");
            }

            if (File.Exists("resources/index_preview.html"))
            {
                PreviewBrowser.NavigateToStream(File.Open("resources/index_preview.html", FileMode.Open, FileAccess.Read));
            }
        }
Пример #2
0
 internal MarsherWS(DisplayCommunication comm)
 {
     _comm = comm;
 }