}//CheckServerChanges /// <summary> /// SyncRequestResponseStorage /// </summary> /// <param name="logTimerType"></param> /// <param name="syncUi"></param> private void SyncRequestResponseStorage(string logTimerType, Action SyncUi, bool SaveWithSameHash) { if (string.IsNullOrEmpty(ShAppContext.UserId)) { return; } ReqSyncDTO SyncDTO = ListsManager.Instance.GenerateRequestDTOForSync(ShAppContext.UserId, ShAppContext.UserLogin); Task <string> resSync = HelperFactory.GetHttpHelper().HttpPut <ReqSyncDTO>(SyncDTO, Constants.URL_SYNC, ShAppContext.UserToken); string sResJson = resSync.Result; Log.Debug(logTimerType, "Send request server DTO"); ResSyncDTO ResSync = null; try { ResSync = JsonConvert.DeserializeObject <ResSyncDTO>(sResJson); Log.Debug(logTimerType, "OK response from server"); } catch (Exception) { ResSync = new ResSyncDTO(); Log.Debug(logTimerType, "Exception deserialize response from server"); } string newhash = Guid.NewGuid().ToString(); if (ResSync.ErrorCode == 0) { newhash = Tools.GetMd5Hash(sResJson); if (newhash != ListsManager.Instance.CurrentJsonHash && SyncUi != null) { ListsManager.Instance.ImportSyncData(newhash, ResSync); SyncUi(); }//endif Log.Debug(logTimerType, "Sync OK"); } else { Log.Debug(logTimerType, "Error response from server - NOT sync"); } if ((SaveWithSameHash && newhash == ListsManager.Instance.CurrentJsonHash) || (newhash != ListsManager.Instance.CurrentJsonHash)) { string sJson = ListsManager.Instance.GetSerializedDataForLocalStorage(); FilesManager.WriteShListsState(sJson); Log.Debug(logTimerType, "Save in file the changes"); } //endif } //SyncRequestResponseStorage
public void ImportSyncData(string jsonHash, ResSyncDTO dto) { UpdateStorageHash(jsonHash); //delete those that are not returned List <string> idsLists = dto.listsMeta.items.Where(z => !string.IsNullOrEmpty(z.clientTag)).Select(x => x.clientTag).ToList(); mStorage.ShLists.RemoveAll(x => !idsLists.Contains(x.InternalId) && !string.IsNullOrEmpty(x.Id)); //delete those that exists but not the new items that were just created dto.listsMeta.items.ForEach(L => { ShoppingListDTO wantedlist = mStorage.ShLists.Where(x => x.InternalId == L.clientTag).FirstOrDefault(); if (wantedlist == null) { ShoppingListDTO newList = new ShoppingListDTO() { ListDate = Tools.UnixTimeStampToDateTime(L.created), IsDirty = false, InternalId = Guid.NewGuid().ToString(), Id = L.id, ListDescription = L.description, ListName = L.name }; L.items.ForEach(I => { ItemListDTO lstItem = new ItemListDTO(I); newList.Items.Add(lstItem); }); mStorage.ShLists.Add(newList); }//new one else { //update list wantedlist.Id = L.id; wantedlist.ListDescription = L.description; wantedlist.ListName = L.name; //delete listItems those that are not returned List <string> lstItemsRet = L.items.Where(z => !string.IsNullOrEmpty(z.clientTag)).Select(x => x.clientTag).ToList(); wantedlist.Items.RemoveAll(x => !lstItemsRet.Contains(x.InternalId) && !string.IsNullOrEmpty(x.ProductId)); //delete those that exists but not the new items that were just created L.items.ForEach(I => { ItemListDTO wantedItem = wantedlist.Items.FirstOrDefault(x => x.InternalId == I.clientTag); if (wantedItem == null) { wantedlist.Items.Add(new ItemListDTO(I)); } else { wantedItem.Description = I.description; wantedItem.ProductId = I.productId; wantedItem.Quantity = I.quantity; wantedItem.Bought = (I.bought == 1); }//else });//items wantedlist.IsDirty = false;//we did the sync }//else }); }//ImportSyncData