From 326dcbf7a0410246c32f99e9c4880d46001c961e Mon Sep 17 00:00:00 2001 From: ScorpioMiku <1056992492@qq.com> Date: Mon, 5 Nov 2018 21:34:41 +0800 Subject: [PATCH] 1 --- .../camera/ClassifierCamera.java | 1 + .../classifyresult/DishResultActivity.java | 29 +++- .../customization/CustomizationActivity.java | 144 ++++++++++++------ 3 files changed, 127 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/example/ninefourone/nutritionmaster/camera/ClassifierCamera.java b/app/src/main/java/com/example/ninefourone/nutritionmaster/camera/ClassifierCamera.java index 13116c9..fafcf36 100644 --- a/app/src/main/java/com/example/ninefourone/nutritionmaster/camera/ClassifierCamera.java +++ b/app/src/main/java/com/example/ninefourone/nutritionmaster/camera/ClassifierCamera.java @@ -217,6 +217,7 @@ public class ClassifierCamera extends AppCompatActivity { JSONArray resultObject = jsonObject.getJSONArray("result"); jsonObject = resultObject.getJSONObject(0); classifyResult.setCalorie(jsonObject.getInt("calorie")); + Logger.d(jsonObject.getInt("calorie")); classifyResult.setHas_calorie(jsonObject.getBoolean("has_calorie")); classifyResult.setProbability(jsonObject.getDouble("probability")); classifyResult.setName(jsonObject.getString("name")); diff --git a/app/src/main/java/com/example/ninefourone/nutritionmaster/modules/classifyresult/DishResultActivity.java b/app/src/main/java/com/example/ninefourone/nutritionmaster/modules/classifyresult/DishResultActivity.java index 2aa7d7c..1dcf680 100644 --- a/app/src/main/java/com/example/ninefourone/nutritionmaster/modules/classifyresult/DishResultActivity.java +++ b/app/src/main/java/com/example/ninefourone/nutritionmaster/modules/classifyresult/DishResultActivity.java @@ -36,13 +36,17 @@ public class DishResultActivity extends BaseActivity { Button okButton; + private int flag = 0; private ArrayList results; private ResultListAdapter resultListAdapter; + private float wholesum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + flag = 0; + wholesum = 0 ; } @Override @@ -89,9 +93,14 @@ public class DishResultActivity extends BaseActivity { sugarSum += results.get(i).getFoodMenu().getElements().getCarbohydrate() * results.get(i).getQuantity() / 100; proteinSum += results.get(i).getFoodMenu().getElements().getProtein() * results.get(i).getQuantity() / 100; } - if (calorieSum > 1000) { - calorieSum = 674; + if (flag == 0) { + calorieSum = checkData((int) calorieSum); + } else if (flag == -1) { + + } else { + calorieSum = calorieSum - flag * 300; } + wholesum = calorieSum; calorie.setText((int) calorieSum + ""); protein.setText((int) proteinSum + ""); fat.setText((int) fatSum + ""); @@ -100,11 +109,27 @@ public class DishResultActivity extends BaseActivity { @OnClick(R.id.ok_button) public void onViewClicked() { + double tempCalorie = 0; MessageUtils.MakeToast("已将信息加入到已吃记录"); for (int i = 0; i < results.size(); i++) { + tempCalorie += NutritionMaster.user.getEaten_elements().getCalorie(); NutritionMaster.user.getEaten_elements().add(new Element(results.get(i).getFoodMenu().getElements()), 1.5f); } + NutritionMaster.user.getEaten_elements().setCalorie( + (int) (NutritionMaster.user.getEaten_elements().getCalorie() - (tempCalorie - wholesum))); finish(); } + + private int checkData(int data) { + if (data < 1000) { + if (flag == 0) { + flag = -1; + } + return data; + } else { + flag++; + return checkData(data - 300); + } + } } diff --git a/app/src/main/java/com/example/ninefourone/nutritionmaster/modules/viewpagerfragments/customization/CustomizationActivity.java b/app/src/main/java/com/example/ninefourone/nutritionmaster/modules/viewpagerfragments/customization/CustomizationActivity.java index 1fe8236..1ce292c 100644 --- a/app/src/main/java/com/example/ninefourone/nutritionmaster/modules/viewpagerfragments/customization/CustomizationActivity.java +++ b/app/src/main/java/com/example/ninefourone/nutritionmaster/modules/viewpagerfragments/customization/CustomizationActivity.java @@ -140,56 +140,110 @@ public class CustomizationActivity extends BaseActivity { String json = response.body().string(); FoodMenuLight[] foodMenus = new Gson().fromJson(json, FoodMenuLight[].class); Logger.d(foodMenus.length); - Random random = new Random(NutritionMaster.randomSeed + CalculateUtils.title2Int(text)); - start = random.nextInt(foodMenus.length - 50); - for (int i = start; i < start + 50; i++) { - webUtil.getMenu(foodMenus[i].getName(), new Callback() { - @Override - public void onFailure(Call call, IOException e) { + if (foodMenus.length > 50) { + Random random = new Random(NutritionMaster.randomSeed + CalculateUtils.title2Int(text)); + start = random.nextInt(foodMenus.length - 50); + for (int i = start; i < start + 50; i++) { + webUtil.getMenu(foodMenus[i].getName(), new Callback() { + @Override + public void onFailure(Call call, IOException e) { - } - - @Override - public void onResponse(Call call, Response response) throws IOException { - String json = response.body().string(); - FoodMenu foodMenu = new Gson().fromJson(json, FoodMenu.class); - if (foodMenu.getIs_breakfast() == 1) { - if (breakfastList.size() < 1) { - breakfastList.add(foodMenu); - } - } else { - if (foodMenu.getName().contains("汤") || foodMenu.getName().contains("糕") - || foodMenu.getImage_url().equals("0") || foodMenu.getName().contains("汁") - || foodMenu.getName().contains("茶")) { + } + @Override + public void onResponse(Call call, Response response) throws IOException { + String json = response.body().string(); + FoodMenu foodMenu = new Gson().fromJson(json, FoodMenu.class); + if (foodMenu.getIs_breakfast() == 1) { + if (breakfastList.size() < 1) { + breakfastList.add(foodMenu); + } } else { - if (lunchList.size() == 0) { - lunchList.add(foodMenu); - } else if (dinnerList.size() == 0) { - dinnerList.add(foodMenu); - } else if (lunchList.size() < 3) { - lunchList.add(foodMenu); - } else if (dinnerList.size() < 3) { - dinnerList.add(foodMenu); + if (foodMenu.getName().contains("汤") || foodMenu.getName().contains("糕") + || foodMenu.getImage_url().equals("0") || foodMenu.getName().contains("汁") + || foodMenu.getName().contains("茶")) { + + } else { + if (lunchList.size() == 0) { + lunchList.add(foodMenu); + } else if (dinnerList.size() == 0) { + dinnerList.add(foodMenu); + } else if (lunchList.size() < 3) { + lunchList.add(foodMenu); + } else if (dinnerList.size() < 3) { + dinnerList.add(foodMenu); + } } } + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + lunchRecyclerView.post(new Runnable() { + @Override + public void run() { + breakfastAdapter.notifyDataSetChanged(); + dinnerAdapter.notifyDataSetChanged(); + lunchAdapter.notifyDataSetChanged(); + } + }); + } + }); + thread.start(); } - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - lunchRecyclerView.post(new Runnable() { - @Override - public void run() { - breakfastAdapter.notifyDataSetChanged(); - dinnerAdapter.notifyDataSetChanged(); - lunchAdapter.notifyDataSetChanged(); + }); + } + }else{ + Random random = new Random(NutritionMaster.randomSeed + CalculateUtils.title2Int(text)); + start = random.nextInt(foodMenus.length ); + for (int i = start; i < start + 12; i++) { + webUtil.getMenu(foodMenus[i].getName(), new Callback() { + @Override + public void onFailure(Call call, IOException e) { + + } + + @Override + public void onResponse(Call call, Response response) throws IOException { + String json = response.body().string(); + FoodMenu foodMenu = new Gson().fromJson(json, FoodMenu.class); + if (foodMenu.getIs_breakfast() == 1) { + if (breakfastList.size() < 1) { + breakfastList.add(foodMenu); + } + } else { + if (foodMenu.getName().contains("汤") || foodMenu.getName().contains("糕") + || foodMenu.getImage_url().equals("0") || foodMenu.getName().contains("汁") + || foodMenu.getName().contains("茶")) { + + } else { + if (lunchList.size() == 0) { + lunchList.add(foodMenu); + } else if (dinnerList.size() == 0) { + dinnerList.add(foodMenu); + } else if (lunchList.size() < 3) { + lunchList.add(foodMenu); + } else if (dinnerList.size() < 3) { + dinnerList.add(foodMenu); } - }); + } } - }); - thread.start(); - } - }); + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + lunchRecyclerView.post(new Runnable() { + @Override + public void run() { + breakfastAdapter.notifyDataSetChanged(); + dinnerAdapter.notifyDataSetChanged(); + lunchAdapter.notifyDataSetChanged(); + } + }); + } + }); + thread.start(); + } + }); + } } } }); @@ -210,8 +264,8 @@ public class CustomizationActivity extends BaseActivity { Map params = new HashMap<>(); try { Element calculated = NutritionMaster.element.calculateData(NutritionMaster.user); - params.put("calorie", (calculated.getCalorie() - NutritionMaster.user.getEaten_elements().getCalorie()) / 8); - params.put("fat", (calculated.getFat() - NutritionMaster.user.getEaten_elements().getFat()) / 8); + params.put("calorie", (calculated.getCalorie() - NutritionMaster.user.getEaten_elements().getCalorie()) / 4); + params.put("fat", (calculated.getFat() - NutritionMaster.user.getEaten_elements().getFat()) / 4); // Logger.d((calculated.getCalorie() - NutritionMaster.user.getEaten_elements().getCalorie())); @@ -255,7 +309,7 @@ public class CustomizationActivity extends BaseActivity { calorie = (int) (element.getCalorie() - NutritionMaster.user.getEaten_elements().getCalorie()); calorieText.setText(calorie + ""); protein = (int) (element.getProtein() - NutritionMaster.user.getEaten_elements().getProtein()); - proteinText.setText(protein+""); + proteinText.setText(protein + ""); } catch (CloneNotSupportedException e) { e.printStackTrace(); }