private void UpdateNotifySettings(TLInputPeerBase inputPeer, TLInt muteUntil)
        {
            var deviceInfoService   = new DeviceInfoService(GetInitConnection(), true, "InteractiveNotificationsBackgroundTask", _id);
            var eventAggregator     = new TelegramEventAggregator();
            var cacheService        = new InMemoryCacheService(eventAggregator);
            var updatesService      = new UpdatesService(cacheService, eventAggregator);
            var transportService    = new TransportService();
            var connectionService   = new ConnectionService(deviceInfoService);
            var publicConfigService = new MockupPublicConfigService();

            var manualResetEvent = new ManualResetEvent(false);

            Log("before init");
            var mtProtoService = new MTProtoService(deviceInfoService, updatesService, cacheService, transportService, connectionService, publicConfigService);

            mtProtoService.Initialized += (o, e) =>
            {
                Log("init completed");

                mtProtoService.GetNotifySettingsAsync(new TLInputNotifyPeer {
                    Peer = inputPeer
                },
                                                      result =>
                {
                    Log("getNotifySettings completed", () =>
                    {
                        var peerNotifySettings = result as TLPeerNotifySettings;
                        if (peerNotifySettings != null)
                        {
                            if (muteUntil.Value < int.MaxValue)
                            {
                                muteUntil = TLUtils.DateToUniversalTimeTLInt(mtProtoService.ClientTicksDelta, DateTime.Now.AddSeconds(muteUntil.Value));
                            }

                            var inputPeerNotifySettings = new TLInputPeerNotifySettings78
                            {
                                Flags     = new TLInt(0),
                                MuteUntil = muteUntil,
                                Sound     = peerNotifySettings.Sound,
                            };

                            mtProtoService.UpdateNotifySettingsAsync(new TLInputNotifyPeer {
                                Peer = inputPeer
                            },
                                                                     inputPeerNotifySettings,
                                                                     result2 =>
                            {
                                Log("setNotifySettings completed", () =>
                                {
                                    manualResetEvent.Set();
                                });
                            },
                                                                     error2 =>
                            {
                                Log(string.Format("setNotifySettings error={0}\n{1}", error2, error2.Exception),
                                    async() =>
                                {
                                    await Task.Delay(TimeSpan.FromSeconds(1.0));
                                    manualResetEvent.Set();
                                });
                            });
                        }
                        else
                        {
                            manualResetEvent.Set();
                        }
                    });
                },
                                                      error =>
                {
                    Log(string.Format("getNotifySettings error={0}\n{1}", error, error.Exception),
                        async() =>
                    {
                        await Task.Delay(TimeSpan.FromSeconds(1.0));
                        manualResetEvent.Set();
                    });
                });
            };
            mtProtoService.InitializationFailed += (o, e) =>
            {
                Log("init failed");

                manualResetEvent.Set();
            };
            mtProtoService.Initialize();
#if DEBUG
            manualResetEvent.WaitOne();
#else
            manualResetEvent.WaitOne(15000);
#endif
        }
Example #2
0
        public NotificationsViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            var settings = StateService.Settings;

            StateService.Settings = null;

            var sound = StateService.Sounds.FirstOrDefault(x => string.Equals(x, settings.ContactSound, StringComparison.OrdinalIgnoreCase));

            settings.ContactSound = sound ?? StateService.Sounds[0];

            sound = StateService.Sounds.FirstOrDefault(x => string.Equals(x, settings.GroupSound, StringComparison.OrdinalIgnoreCase));
            settings.GroupSound = sound ?? StateService.Sounds[0];

            _settings = settings;

            settings.PropertyChanged += OnSettingsChanged;

            BeginOnThreadPool(() =>
            {
                MTProtoService.GetNotifySettingsAsync(new TLInputNotifyUsers(),
                                                      result => BeginOnUIThread(() =>
                {
                    _suppressUpdating = true;

                    var notifySettings = result as TLPeerNotifySettings;
                    if (notifySettings != null)
                    {
                        Settings.ContactAlert          = notifySettings.MuteUntil == null || notifySettings.MuteUntil.Value == 0;
                        Settings.ContactMessagePreview = notifySettings.ShowPreviews != null && notifySettings.ShowPreviews.Value;

                        sound = notifySettings.Sound == null ? null : StateService.Sounds.FirstOrDefault(x => string.Equals(x, notifySettings.Sound.Value, StringComparison.OrdinalIgnoreCase));
                        Settings.ContactSound = sound ?? StateService.Sounds[0];
                    }

                    _suppressUpdating = false;

                    SaveSettings();
                }));

                MTProtoService.GetNotifySettingsAsync(new TLInputNotifyChats(),
                                                      result => BeginOnUIThread(() =>
                {
                    _suppressUpdating = true;

                    var notifySettings = result as TLPeerNotifySettings;
                    if (notifySettings != null)
                    {
                        Settings.GroupAlert          = notifySettings.MuteUntil == null || notifySettings.MuteUntil.Value == 0;
                        Settings.GroupMessagePreview = notifySettings.ShowPreviews != null && notifySettings.ShowPreviews.Value;

                        sound = notifySettings.Sound == null ? null : StateService.Sounds.FirstOrDefault(x => string.Equals(x, notifySettings.Sound.Value, StringComparison.OrdinalIgnoreCase));
                        Settings.GroupSound = sound ?? StateService.Sounds[0];
                    }

                    _suppressUpdating = false;

                    SaveSettings();
                }));

                SaveSettings();
            });
        }