public async void InitAsync(List <FoodInformation> foodInformations) { FoodWeightChangesList = await _userFavorService.ReadJsonAsync(); if (FoodWeightChangesList.Count == 0) { FoodWeightChangesList = new List <FoodWeightChange>(); for (int i = 0; i < foodInformations.Count; i++) { FoodWeightChange foodchange = new FoodWeightChange(); foodchange.FoodName = foodInformations[i].Name; foodchange.weightChangeList = new List <int>() { 0, 0, 0, 0, 0, 0 }; FoodWeightChangesList.Add(foodchange); } } }
public async System.Threading.Tasks.Task MaintenanceAsync() { List <Log> localLog = _logService.GetLogs(); if (localLog == null) { localLog = new List <Log>(); } List <Log> cloudLog = await _oneDriveService.LoadLogAsync(); DateTime lastCommitTime = await _lastTimeCommitService.ReadJsonAsync(); if (lastCommitTime == null) { lastCommitTime = DateTime.Now; } List <FoodWeightChange> totalWeight = await _oneDriveService.LoadFoodWeightAsync(); if (totalWeight.Count == 0) { List <FoodInformation> foodInformations = _recommendationService.GetFoodInfs(); for (int i = 0; i < foodInformations.Count; i++) { FoodWeightChange foodWeightChange = new FoodWeightChange(); foodWeightChange.FoodName = foodInformations[i].Name; int[] arr = { 0, 0, 0, 0, 0, 0 }; List <int> reasonList = new List <int>(arr); foodWeightChange.weightChangeList = reasonList; totalWeight.Add(foodWeightChange); } } // bug var foodInformationList = _recommendationService.GetFoodInfs(); for (int i = 0; i < foodInformationList.Count; i++) { var FoodName = foodInformationList[i].Name; ChangeInformation cloudChangeInfList = GetChangeInf(cloudLog, lastCommitTime, FoodName); ChangeInformation localChangeInfList = GetChangeInf(localLog, lastCommitTime, FoodName); int[] arr = { 0, 0, 0, 0, 0, 0 }; List <int> tmpWeightChangeList = totalWeight[i].weightChangeList; for (int j = 0; j < 9; j++) { int flag = 0; for (int k = 0; k < 6; k++) { if (cloudChangeInfList.Weight[j][k] >= 0 && localChangeInfList.Weight[j][k] >= 0) { if (cloudChangeInfList.Weight[j][k] >= localChangeInfList.Weight[j][k]) { //tmpWeightChangeList[k] = tmpWeightChangeList[k] + cloudChangeInfList.Weight[j][k]; tmpWeightChangeList[k] = tmpWeightChangeList[k]; continue; } else if (cloudChangeInfList.Weight[j][k] < localChangeInfList.Weight[j][k]) { if (flag == 0) { cloudLog = DeleteRecord(cloudLog, FoodName, lastCommitTime, j); cloudLog = InsertRecord(cloudLog, localLog, FoodName, lastCommitTime, j); flag = 1; for (int m = 0; m < 6; m++) { tmpWeightChangeList[m] = tmpWeightChangeList[m] - cloudChangeInfList.Weight[j][m] + localChangeInfList.Weight[j][m]; } } } } else if (cloudChangeInfList.Weight[j][k] <= 0 && localChangeInfList.Weight[j][k] <= 0) { if (cloudChangeInfList.Weight[j][k] <= localChangeInfList.Weight[j][k]) { tmpWeightChangeList[k] = tmpWeightChangeList[k]; continue; } else if (cloudChangeInfList.Weight[j][k] > localChangeInfList.Weight[j][k]) { if (flag == 0) { cloudLog = DeleteRecord(cloudLog, FoodName, lastCommitTime, j); cloudLog = InsertRecord(cloudLog, localLog, FoodName, lastCommitTime, j); flag = 1; for (int m = 0; m < 6; m++) { tmpWeightChangeList[m] = tmpWeightChangeList[m] - cloudChangeInfList.Weight[j][m] + localChangeInfList.Weight[j][m]; } } } } else { if (flag == 0) { cloudLog = InsertRecord(cloudLog, localLog, FoodName, lastCommitTime, j); flag = 1; for (int m = 0; m < 6; m++) { tmpWeightChangeList[m] = tmpWeightChangeList[m] + localChangeInfList.Weight[j][m]; } } } } } _foodFavorService.SetWeight(i, tmpWeightChangeList); } localLog = cloudLog; _logService.SetLogs(cloudLog); _logService.SaveLogAsync(); _foodFavorService.SaveChangeWeightAsync(); lastCommitTime = DateTime.Now; _lastTimeCommitService.SaveJsonAsync(lastCommitTime); }