Guest User

Untitled

a guest
Dec 17th, 2022
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.74 KB | None | 0 0
  1. package com.example.nutritiontracker3;
  2.  
  3. import androidx.annotation.NonNull;
  4. import androidx.annotation.Nullable;
  5. import androidx.appcompat.app.AlertDialog;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. import androidx.recyclerview.widget.LinearLayoutManager;
  8. import androidx.recyclerview.widget.RecyclerView;
  9.  
  10. import android.content.Context;
  11. import android.content.DialogInterface;
  12. import android.content.Intent;
  13. import android.content.SharedPreferences;
  14. import android.database.Cursor;
  15. import android.graphics.Color;
  16. import android.os.Bundle;
  17. import android.view.Menu;
  18. import android.view.MenuInflater;
  19. import android.view.MenuItem;
  20. import android.view.View;
  21. import android.widget.ImageView;
  22. import android.widget.TextView;
  23. import android.widget.Toast;
  24.  
  25. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  26.  
  27. import java.util.ArrayList;
  28. import java.util.Collections;
  29. import java.util.Comparator;
  30.  
  31. public class MainActivity extends AppCompatActivity {
  32.  
  33. TextView calories_count, protein_count, carbs_count, fat_count;
  34. TextView calories_settings, protein_settings, carbs_settings, fat_settings;
  35. TextView tv_emptyFoodData;
  36. RecyclerView recyclerView;
  37. FloatingActionButton fetchData;
  38. ImageView iv_emptyFoodData;
  39.  
  40. MyDatabaseHelper myDB;
  41. ArrayList<String> food_ID, food_title, food_amount, food_calories, food_protein, food_carbs, food_fat, food_timestamp;
  42. CustomAdapter customAdapter;
  43.  
  44.  
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.activity_main);
  49. getSupportActionBar().setTitle("Nutrition Information");
  50.  
  51. tv_emptyFoodData = findViewById(R.id.tv_addSomeFood);
  52. iv_emptyFoodData = findViewById(R.id.iv_emptyFoodData);
  53.  
  54. recyclerView = findViewById(R.id.foodLog);
  55.  
  56. calories_settings = findViewById(R.id.tv_calories_settings);
  57. protein_settings = findViewById(R.id.tv_protein_settings);
  58. carbs_settings = findViewById(R.id.tv_carbs_settings);
  59. fat_settings = findViewById(R.id.tv_fat_settings);
  60.  
  61. calories_count = findViewById(R.id.tv_calories_counter);
  62. protein_count = findViewById(R.id.tv_protein_counter);
  63. carbs_count = findViewById(R.id.tv_carbs_counter);
  64. fat_count = findViewById(R.id.tv_fat_counter);
  65.  
  66. myDB = new MyDatabaseHelper(MainActivity.this);
  67.  
  68. food_ID = new ArrayList<>();
  69. food_title = new ArrayList<>();
  70. food_amount = new ArrayList<>();
  71. food_calories = new ArrayList<>();
  72. food_protein = new ArrayList<>();
  73. food_carbs = new ArrayList<>();
  74. food_fat = new ArrayList<>();
  75. food_timestamp = new ArrayList<>();
  76.  
  77. storeFoodDataInArrayList();
  78.  
  79. customAdapter = new CustomAdapter(MainActivity.this, this, food_ID, food_title, food_amount, food_calories,
  80. food_protein, food_carbs, food_fat, food_timestamp);
  81.  
  82. recyclerView.setAdapter(customAdapter);
  83. recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
  84.  
  85. SharedPreferences sp = getApplicationContext().getSharedPreferences("NUTRITIONINFORMATION", Context.MODE_PRIVATE);
  86. String st_calories_settings = sp.getString("caloriesNeeded", "");
  87. String st_protein_settings = sp.getString("proteinNeeded", "");
  88. String st_carbs_settings = sp.getString("carbsNeeded", "");
  89. String st_fat_settings = sp.getString("fatNeeded", "");
  90. calories_settings.setText(st_calories_settings);
  91. protein_settings.setText(st_protein_settings);
  92. carbs_settings.setText(st_carbs_settings);
  93. fat_settings.setText(st_fat_settings);
  94.  
  95. fetchData = findViewById(R.id.fetchDataButton);
  96.  
  97. calories_count.setText(readAllCalories());
  98. protein_count.setText(readAllProtein());
  99. carbs_count.setText(readAllCarbs());
  100. fat_count.setText(readAllFat());
  101.  
  102. if (Integer.valueOf(calories_count.getText().toString()) >= Integer.valueOf(calories_settings.getText().toString())) {
  103. calories_count.setTextColor(Color.parseColor("#FF0000"));
  104. }
  105.  
  106. if (Integer.valueOf(protein_count.getText().toString()) >= Integer.valueOf(protein_settings.getText().toString())) {
  107. protein_count.setTextColor(Color.parseColor("#FF0000"));
  108. }
  109.  
  110. if (Integer.valueOf(carbs_count.getText().toString()) >= Integer.valueOf(carbs_settings.getText().toString())) {
  111. carbs_count.setTextColor(Color.parseColor("#FF0000"));
  112. }
  113.  
  114. if (Integer.valueOf(fat_count.getText().toString()) >= Integer.valueOf(fat_settings.getText().toString())) {
  115. fat_count.setTextColor(Color.parseColor("#FF0000"));
  116. }
  117.  
  118. fetchData.setOnClickListener(new View.OnClickListener() {
  119. @Override
  120. public void onClick(View view) {
  121.  
  122. Intent in = new Intent(MainActivity.this, FetchData.class);
  123. String st_caloriesCount = calories_count.getText().toString();
  124. String st_proteinCount = protein_count.getText().toString();
  125. String st_carbsCount = carbs_count.getText().toString();
  126. String st_fatCount = fat_count.getText().toString();
  127.  
  128. in.putExtra("caloriesCount", st_caloriesCount);
  129. in.putExtra("proteinCount", st_proteinCount);
  130. in.putExtra("carbsCount", st_carbsCount);
  131. in.putExtra("fatCount", st_fatCount);
  132.  
  133. startActivity(in);
  134.  
  135. }
  136. });
  137. }
  138.  
  139. @Override
  140. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  141. super.onActivityResult(requestCode, resultCode, data);
  142. if (requestCode == 1) {
  143. recreate();
  144. }
  145. }
  146.  
  147. public String readAllCalories() {
  148. int newCaloriesCountValue = 0;
  149. for (String i : food_calories) {
  150. int parseString = Integer.valueOf(i);
  151. newCaloriesCountValue += parseString;
  152. }
  153. return String.valueOf(newCaloriesCountValue);
  154. }
  155.  
  156. public String readAllProtein() {
  157. int newProteinCountValue = 0;
  158. for (String i : food_protein) {
  159. int parseString = Integer.valueOf(i);
  160. newProteinCountValue += parseString;
  161. }
  162. return String.valueOf(newProteinCountValue);
  163. }
  164.  
  165. public String readAllCarbs() {
  166. int newCarbsCountValue = 0;
  167. for (String i : food_carbs) {
  168. int parseString = Integer.valueOf(i);
  169. newCarbsCountValue += parseString;
  170. }
  171. return String.valueOf(newCarbsCountValue);
  172. }
  173.  
  174. public String readAllFat() {
  175. int newFatCountValue = 0;
  176. for (String i : food_fat) {
  177. int parseString = Integer.valueOf(i);
  178. newFatCountValue += parseString;
  179. }
  180. return String.valueOf(newFatCountValue);
  181. }
  182.  
  183.  
  184. void storeFoodDataInArrayList() {
  185.  
  186. Cursor cursor = myDB.readFoodData();
  187. if (cursor.getCount() == 0) {
  188. iv_emptyFoodData.setVisibility(View.VISIBLE);
  189. tv_emptyFoodData.setVisibility(View.VISIBLE);
  190. } else {
  191. while (cursor.moveToNext()) {
  192. food_ID.add(cursor.getString(0));
  193. food_title.add(cursor.getString(1));
  194. food_amount.add(cursor.getString(2));
  195. food_calories.add(cursor.getString(3));
  196. food_protein.add(cursor.getString(4));
  197. food_carbs.add(cursor.getString(5));
  198. food_fat.add(cursor.getString(6));
  199. food_timestamp.add(cursor.getString(7));
  200. }
  201. iv_emptyFoodData.setVisibility(View.GONE);
  202. tv_emptyFoodData.setVisibility(View.GONE);
  203. }
  204. }
  205.  
  206. void storeFoodDataInArrayListAtoZ() {
  207.  
  208. Cursor cursor = myDB.readFoodDataAtoZ();
  209. if (cursor.getCount() == 0) {
  210. iv_emptyFoodData.setVisibility(View.VISIBLE);
  211. tv_emptyFoodData.setVisibility(View.VISIBLE);
  212. } else {
  213. while (cursor.moveToNext()) {
  214. food_ID.add(cursor.getString(0));
  215. food_title.add(cursor.getString(1));
  216. food_amount.add(cursor.getString(2));
  217. food_calories.add(cursor.getString(3));
  218. food_protein.add(cursor.getString(4));
  219. food_carbs.add(cursor.getString(5));
  220. food_fat.add(cursor.getString(6));
  221. food_timestamp.add(cursor.getString(7));
  222. }
  223. iv_emptyFoodData.setVisibility(View.GONE);
  224. tv_emptyFoodData.setVisibility(View.GONE);
  225. }
  226. }
  227.  
  228. @Override
  229. public boolean onCreateOptionsMenu(Menu menu) {
  230. MenuInflater inflater = getMenuInflater();
  231. inflater.inflate(R.menu.menu_button, menu);
  232. return true;
  233. }
  234.  
  235. @Override
  236. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  237. switch (item.getItemId()) {
  238. case R.id.settings_menu_button:
  239. openSettings();
  240. break;
  241. case R.id.profile_menu_button:
  242. openProfile();
  243. break;
  244. case R.id.delete_all_button:
  245. confirmDialog();
  246. MyDatabaseHelper myDB = new MyDatabaseHelper(this);
  247. myDB.deleteAllFoodData();
  248. break;
  249. case R.id.sort_AZ:
  250. storeFoodDataInArrayListAtoZ();
  251. customAdapter.notifyDataSetChanged();
  252. recyclerView.setAdapter(customAdapter);
  253. }
  254. return super.onOptionsItemSelected(item);
  255. }
  256.  
  257. private void openProfile() {
  258. Intent intent = new Intent(this, Profile.class);
  259. startActivity(intent);
  260. }
  261.  
  262. private void openSettings() {
  263. Intent intent = new Intent(this, Settings.class);
  264. startActivity(intent);
  265. }
  266.  
  267. void confirmDialog() {
  268. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  269. builder.setTitle("Delete All?");
  270. builder.setMessage("Are you sure you want to delete all food logs?");
  271. builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  272. @Override
  273. public void onClick(DialogInterface dialog, int which) {
  274. MyDatabaseHelper myDB = new MyDatabaseHelper((MainActivity.this));
  275. myDB.deleteAllFoodData();
  276. recreate();
  277. }
  278. });
  279. builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
  280. @Override
  281. public void onClick(DialogInterface dialog, int which) {
  282.  
  283. }
  284. });
  285. builder.create().show();
  286. }
  287.  
  288.  
  289. }
Add Comment
Please, Sign In to add comment