Exemplo n.º 1
0
        public async static Task <Enums.BackupRestoreStatus> RestoreData()
        {
            var fileName       = Path.Combine(DependencyService.Get <IFileAccessService>().GetFileLocation(), Constants.BACKUP_FILE_NAME);
            var restoreSetting = await SettingService.GetSettingValue <bool>(Constants.RESTORE_SETTING_KEY);

            if (!restoreSetting && File.Exists(fileName))
            {
                var data = File.ReadAllText(fileName);
                if (data.Length > 0)
                {
                    var tableBackups = JsonConvert.DeserializeObject <List <BackupModel> >(data);
                    if (tableBackups.Any())
                    {
                        foreach (var backupData in tableBackups)
                        {
                            var isInserted = await App.DbService.BulkInsertAsync(backupData.TableType, backupData.BackupData);

                            if (isInserted > 0)
                            {
                                var setting = await SettingService.GetSetting(Constants.RESTORE_SETTING_KEY);

                                if (setting != null)
                                {
                                    setting.SettingValue = "true";
                                    await App.DbService.UpdateAsync(setting);

                                    return(Enums.BackupRestoreStatus.Sucess);
                                }
                            }
                        }
                    }
                }
            }

            return(Enums.BackupRestoreStatus.Failed);
        }
Exemplo n.º 2
0
        private async Task InitializeAsync()
        {
            try
            {
                if (!initialized)
                {
                    if (!Db.TableMappings.Any(m => m.MappedType.Name == nameof(Setting)))
                    {
                        await Db.CreateTablesAsync(CreateFlags.None, TablesList).ConfigureAwait(true);

                        if (Db.TableMappings.Any(x => x.MappedType.Equals(typeof(Setting))) && await SettingService.GetSetting(Constants.RESTORE_SETTING_KEY) == null)
                        {
                            await InsertAsync(new Setting { SettingName = Constants.RESTORE_SETTING_KEY, SettingValue = "false" });
                        }
                    }
                    initialized = true;
                }
            }
            catch (Exception ex)
            {
                LoggerService.LogError(ex);
            }
        }