예제 #1
0
        public async Task <List <Account> > WaitNewAccountAsync()
        {
            try
            {
                using (var context = new ViberConfigDbContext(_dataSource))
                {
                    var count = context.Accounts.Count();
                    while (Viber.IsOpen())
                    {
                        await Task.Delay(100);

                        if (count < context.Accounts.Count())
                        {
                            break;
                        }
                    }

                    foreach (var account in context.Accounts)
                    {
                        account.IsValid = true;
                        context.Entry(account).State = EntityState.Modified;
                    }
                    await context.SaveChangesAsync();

                    return(await context.Accounts.ToListAsync());
                }
            }
            catch
            {
                throw;
            }
        }
예제 #2
0
 public static Viber Instance(string path)
 {
     if (_instance == null)
     {
         lock (SyncRoot)
         {
             if (_instance == null)
             {
                 _instance = new Viber(path);
             }
         }
     }
     else
     {
         lock (SyncRoot)
         {
             if (string.IsNullOrEmpty(path) || path == _instance.ExePath)
             {
                 return(_instance);
             }
             _instance.ExePath = path;
             if (IsRunning())
             {
                 _instance.Restart();
             }
         }
     }
     return(_instance);
 }
예제 #3
0
        public async void ChangeViberProfile_Click(object sender, RoutedEventArgs e)
        {
            var viewMode = (MainViewModel)DataContext;

            var client = Viber.Instance(viewMode.Config.ViberClientPath);

            client.Close();
            SetDisabled(Start, ChangeViberProfile);
            await ReplaceDefaultViberProfileInRoamingAppData(viewMode.Config.ProfilePath);
        }
예제 #4
0
        private async void RegistrationNewAccount_Click(object sender, RoutedEventArgs e)
        {
            var viewMode = (MainViewModel)DataContext;

            var client = Viber.Instance(viewMode.Config.ViberClientPath);

            client.Close();

            SetDisabled(RegistrationNewAccount);
            var db = new ViberDb(DefaultViberConfigDbInRoamingAppData());
            await db.OffAccountsAsync();

            client.Run();
            ViberAccounts.DataContext = await db.WaitNewAccountAsync();

            SetEnabled(RegistrationNewAccount);
        }
예제 #5
0
        private async void ChangeAccount_Click(object sender, EventArgs e)
        {
            try
            {
                var viewMode = (MainViewModel)DataContext;

                var client = Viber.Instance(viewMode.Config.ViberClientPath);
                client.Close();
                SetDisabled(ChangeAccount);

                await SelectNextViberProfileAsync();

                client.Run();
            }
            finally
            {
                SetEnabled(ChangeAccount);
            }
        }
예제 #6
0
        private async void Start_Click(object sender, RoutedEventArgs e)
        {
            var viewMode = (MainViewModel)DataContext;

            var errors = CheckConfig();

            if (errors.Any())
            {
                System.Windows.MessageBox.Show(string.Join("\n", errors.Select(err => err.Value)));
                return;
            }

            var client = Viber.Instance(viewMode.Config.ViberClientPath);

            client.StopIfRunning();

            SetDisabled(Start);
            SetEnabled(Stop);

            _cts = new CancellationTokenSource();
            var token = _cts.Token;

            await RunWork(client, viewMode.Config, token);
        }
예제 #7
0
        private Task RunWork(Viber client, Config config, CancellationToken token)
        {
            API.BaseAddress = config.ApiUrl;
            client.Run();

            var t = new Task(async() =>
            {
                try
                {
                    var count = 0;
                    while (true)
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        var tasks        = await API.GetTasksAsync(config.MaxCountMessage);
                        var responseTask = new ResponseTask
                        {
                            Tasks = new List <Result>()
                        };

                        foreach (var domain in tasks.Domains)
                        {
                            try
                            {
                                foreach (var task in domain.Tasks)
                                {
                                    if (CountViberProfile() > 1 && count >= config.AccountChangeAfter)
                                    {
                                        client.Close();
                                        await SelectNextViberProfileAsync();
                                        client.Run();
                                        count = 1;
                                    }

                                    Guid statusId;
                                    if (client.Send(task.Phone, domain.Message))
                                    {
                                        count++;
                                    }

                                    switch (client.State)
                                    {
                                    case ViberState.ClickMessageMenu:
                                        statusId = NotRegisteredId;
                                        break;

                                    case ViberState.SendMessage:
                                        statusId = Sended;
                                        break;

                                    case ViberState.Run:
                                        return;

                                    default:
                                        statusId = Failure;
                                        break;
                                    }

                                    responseTask.Tasks.Add(new Result
                                    {
                                        Id       = task.Id,
                                        StatusId = statusId
                                    });
                                }
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex);
                            }
                            finally
                            {
                                if (responseTask.Tasks.Any())
                                {
                                    await API.UpdateTasksAsync(responseTask);
                                }
                            }
                        }

                        Thread.Sleep(config.PauseBetweenTasks);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
                finally
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(
                        (Action)(() =>
                    {
                        SetDisabled(Stop);
                        SetEnabled(Start);
                    }
                                 ));
                }
            });

            t.Start();

            return(t);
        }