Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.nutritiontracker3;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.database.Cursor;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.material.floatingactionbutton.FloatingActionButton;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- public class MainActivity extends AppCompatActivity {
- TextView calories_count, protein_count, carbs_count, fat_count;
- TextView calories_settings, protein_settings, carbs_settings, fat_settings;
- TextView tv_emptyFoodData;
- RecyclerView recyclerView;
- FloatingActionButton fetchData;
- ImageView iv_emptyFoodData;
- MyDatabaseHelper myDB;
- ArrayList<String> food_ID, food_title, food_amount, food_calories, food_protein, food_carbs, food_fat, food_timestamp;
- CustomAdapter customAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- getSupportActionBar().setTitle("Nutrition Information");
- tv_emptyFoodData = findViewById(R.id.tv_addSomeFood);
- iv_emptyFoodData = findViewById(R.id.iv_emptyFoodData);
- recyclerView = findViewById(R.id.foodLog);
- calories_settings = findViewById(R.id.tv_calories_settings);
- protein_settings = findViewById(R.id.tv_protein_settings);
- carbs_settings = findViewById(R.id.tv_carbs_settings);
- fat_settings = findViewById(R.id.tv_fat_settings);
- calories_count = findViewById(R.id.tv_calories_counter);
- protein_count = findViewById(R.id.tv_protein_counter);
- carbs_count = findViewById(R.id.tv_carbs_counter);
- fat_count = findViewById(R.id.tv_fat_counter);
- myDB = new MyDatabaseHelper(MainActivity.this);
- food_ID = new ArrayList<>();
- food_title = new ArrayList<>();
- food_amount = new ArrayList<>();
- food_calories = new ArrayList<>();
- food_protein = new ArrayList<>();
- food_carbs = new ArrayList<>();
- food_fat = new ArrayList<>();
- food_timestamp = new ArrayList<>();
- storeFoodDataInArrayList();
- customAdapter = new CustomAdapter(MainActivity.this, this, food_ID, food_title, food_amount, food_calories,
- food_protein, food_carbs, food_fat, food_timestamp);
- recyclerView.setAdapter(customAdapter);
- recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
- SharedPreferences sp = getApplicationContext().getSharedPreferences("NUTRITIONINFORMATION", Context.MODE_PRIVATE);
- String st_calories_settings = sp.getString("caloriesNeeded", "");
- String st_protein_settings = sp.getString("proteinNeeded", "");
- String st_carbs_settings = sp.getString("carbsNeeded", "");
- String st_fat_settings = sp.getString("fatNeeded", "");
- calories_settings.setText(st_calories_settings);
- protein_settings.setText(st_protein_settings);
- carbs_settings.setText(st_carbs_settings);
- fat_settings.setText(st_fat_settings);
- fetchData = findViewById(R.id.fetchDataButton);
- calories_count.setText(readAllCalories());
- protein_count.setText(readAllProtein());
- carbs_count.setText(readAllCarbs());
- fat_count.setText(readAllFat());
- if (Integer.valueOf(calories_count.getText().toString()) >= Integer.valueOf(calories_settings.getText().toString())) {
- calories_count.setTextColor(Color.parseColor("#FF0000"));
- }
- if (Integer.valueOf(protein_count.getText().toString()) >= Integer.valueOf(protein_settings.getText().toString())) {
- protein_count.setTextColor(Color.parseColor("#FF0000"));
- }
- if (Integer.valueOf(carbs_count.getText().toString()) >= Integer.valueOf(carbs_settings.getText().toString())) {
- carbs_count.setTextColor(Color.parseColor("#FF0000"));
- }
- if (Integer.valueOf(fat_count.getText().toString()) >= Integer.valueOf(fat_settings.getText().toString())) {
- fat_count.setTextColor(Color.parseColor("#FF0000"));
- }
- fetchData.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent in = new Intent(MainActivity.this, FetchData.class);
- String st_caloriesCount = calories_count.getText().toString();
- String st_proteinCount = protein_count.getText().toString();
- String st_carbsCount = carbs_count.getText().toString();
- String st_fatCount = fat_count.getText().toString();
- in.putExtra("caloriesCount", st_caloriesCount);
- in.putExtra("proteinCount", st_proteinCount);
- in.putExtra("carbsCount", st_carbsCount);
- in.putExtra("fatCount", st_fatCount);
- startActivity(in);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == 1) {
- recreate();
- }
- }
- public String readAllCalories() {
- int newCaloriesCountValue = 0;
- for (String i : food_calories) {
- int parseString = Integer.valueOf(i);
- newCaloriesCountValue += parseString;
- }
- return String.valueOf(newCaloriesCountValue);
- }
- public String readAllProtein() {
- int newProteinCountValue = 0;
- for (String i : food_protein) {
- int parseString = Integer.valueOf(i);
- newProteinCountValue += parseString;
- }
- return String.valueOf(newProteinCountValue);
- }
- public String readAllCarbs() {
- int newCarbsCountValue = 0;
- for (String i : food_carbs) {
- int parseString = Integer.valueOf(i);
- newCarbsCountValue += parseString;
- }
- return String.valueOf(newCarbsCountValue);
- }
- public String readAllFat() {
- int newFatCountValue = 0;
- for (String i : food_fat) {
- int parseString = Integer.valueOf(i);
- newFatCountValue += parseString;
- }
- return String.valueOf(newFatCountValue);
- }
- void storeFoodDataInArrayList() {
- Cursor cursor = myDB.readFoodData();
- if (cursor.getCount() == 0) {
- iv_emptyFoodData.setVisibility(View.VISIBLE);
- tv_emptyFoodData.setVisibility(View.VISIBLE);
- } else {
- while (cursor.moveToNext()) {
- food_ID.add(cursor.getString(0));
- food_title.add(cursor.getString(1));
- food_amount.add(cursor.getString(2));
- food_calories.add(cursor.getString(3));
- food_protein.add(cursor.getString(4));
- food_carbs.add(cursor.getString(5));
- food_fat.add(cursor.getString(6));
- food_timestamp.add(cursor.getString(7));
- }
- iv_emptyFoodData.setVisibility(View.GONE);
- tv_emptyFoodData.setVisibility(View.GONE);
- }
- }
- void storeFoodDataInArrayListAtoZ() {
- Cursor cursor = myDB.readFoodDataAtoZ();
- if (cursor.getCount() == 0) {
- iv_emptyFoodData.setVisibility(View.VISIBLE);
- tv_emptyFoodData.setVisibility(View.VISIBLE);
- } else {
- while (cursor.moveToNext()) {
- food_ID.add(cursor.getString(0));
- food_title.add(cursor.getString(1));
- food_amount.add(cursor.getString(2));
- food_calories.add(cursor.getString(3));
- food_protein.add(cursor.getString(4));
- food_carbs.add(cursor.getString(5));
- food_fat.add(cursor.getString(6));
- food_timestamp.add(cursor.getString(7));
- }
- iv_emptyFoodData.setVisibility(View.GONE);
- tv_emptyFoodData.setVisibility(View.GONE);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu_button, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(@NonNull MenuItem item) {
- switch (item.getItemId()) {
- case R.id.settings_menu_button:
- openSettings();
- break;
- case R.id.profile_menu_button:
- openProfile();
- break;
- case R.id.delete_all_button:
- confirmDialog();
- MyDatabaseHelper myDB = new MyDatabaseHelper(this);
- myDB.deleteAllFoodData();
- break;
- case R.id.sort_AZ:
- storeFoodDataInArrayListAtoZ();
- customAdapter.notifyDataSetChanged();
- recyclerView.setAdapter(customAdapter);
- }
- return super.onOptionsItemSelected(item);
- }
- private void openProfile() {
- Intent intent = new Intent(this, Profile.class);
- startActivity(intent);
- }
- private void openSettings() {
- Intent intent = new Intent(this, Settings.class);
- startActivity(intent);
- }
- void confirmDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Delete All?");
- builder.setMessage("Are you sure you want to delete all food logs?");
- builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- MyDatabaseHelper myDB = new MyDatabaseHelper((MainActivity.this));
- myDB.deleteAllFoodData();
- recreate();
- }
- });
- builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- builder.create().show();
- }
- }
Add Comment
Please, Sign In to add comment