Added daily summary in archive

This commit is contained in:
Krokogator 2019-01-21 20:27:38 +01:00
parent 71baf82164
commit ef91d04b78
7 changed files with 91 additions and 7 deletions

View File

@ -9,6 +9,9 @@ import android.widget.Toast;
import com.krokogator.photoeat.database.Result;
import com.krokogator.photoeat.database.ResultDBHelper;
import java.text.DateFormat;
import java.util.Date;
public class ArchiveActivity extends AppCompatActivity {
@Override

View File

@ -29,7 +29,9 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;
public class CaloriesActivity extends AppCompatActivity {
@ -72,6 +74,7 @@ public class CaloriesActivity extends AppCompatActivity {
values.put(Result.COLUMN_NAME_FOOD, name);
values.put(Result.COLUMN_NAME_CALORIES, calories);
values.put(Result.COLUMN_NAME_DATE, new Date().toString());
long newRowId = db.insert(Result.TABLE_NAME,
null, values);
@ -134,7 +137,7 @@ public class CaloriesActivity extends AppCompatActivity {
String calories = (result.getJSONArray("foods").getJSONObject(i).getString("nf_calories"));
out.add(new CaloriesItem(name, Float.parseFloat(calories)));
out.add(new CaloriesItem(name, Float.parseFloat(calories), null, false));
}
} catch (Exception e){
Log.d("CALORIES_ACTIVITY", e.getMessage());

View File

@ -13,7 +13,11 @@ import com.krokogator.photoeat.database.Result;
import com.krokogator.photoeat.database.ResultDBHelper;
import com.krokogator.photoeat.model.CaloriesItem;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class NewArchiveActivity extends AppCompatActivity {
@ -38,7 +42,8 @@ public class NewArchiveActivity extends AppCompatActivity {
String[] projection = {
Result._ID,
Result.COLUMN_NAME_FOOD,
Result.COLUMN_NAME_CALORIES
Result.COLUMN_NAME_CALORIES,
Result.COLUMN_NAME_DATE
};
String sortOrder = Result.COLUMN_NAME_FOOD + " DESC";
Cursor cursor = db.query(
@ -52,6 +57,12 @@ public class NewArchiveActivity extends AppCompatActivity {
);
List<CaloriesItem> items = new ArrayList<>();
CaloriesItem prev = null;
Calendar dailyDate = Calendar.getInstance();
Float dailyCalories = 0f;
CaloriesItem current = null;
while (cursor.moveToNext()) {
long itemID = cursor.getLong(cursor.getColumnIndexOrThrow(
@ -60,11 +71,53 @@ public class NewArchiveActivity extends AppCompatActivity {
Result.COLUMN_NAME_FOOD));
Float calories = cursor.getFloat(cursor.getColumnIndexOrThrow(
Result.COLUMN_NAME_CALORIES));
Date date = new Date(cursor.getString(cursor.getColumnIndexOrThrow(
Result.COLUMN_NAME_DATE))) {
};
Toast.makeText(this, food +" "+ calories+ "kcal", Toast.LENGTH_SHORT).show();
items.add(new CaloriesItem(food, calories));
//Toast.makeText(this, food +" "+ calories+ "kcal" + date + "date", Toast.LENGTH_SHORT).show();
current = new CaloriesItem(food, calories, date, false);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
if (prev != null){
if (checkIfSameDay(prev.getDate(), current.getDate())){
dailyCalories += current.getCalories();
} else {
items.add(new CaloriesItem(sdf.format(dailyDate.getTime())+" Daily calories", dailyCalories, dailyDate.getTime(), true));
dailyCalories = current.getCalories();
dailyDate.setTime(current.getDate());
}
} else {
dailyCalories = current.getCalories();
dailyDate.setTime(current.getDate());
}
prev = current;
items.add(current);
if(cursor.isLast() && dailyCalories != 0f){
items.add(new CaloriesItem(sdf.format(dailyDate.getTime())+" Daily calories", dailyCalories, dailyDate.getTime(), true));
}
}
return items;
}
private Boolean checkIfSameDay(Date prev, Date current) {
Calendar calendarPrev = Calendar.getInstance();
calendarPrev.setTime(prev);
Calendar calendarCurrent = Calendar.getInstance();
calendarCurrent.setTime(current);
if(calendarCurrent.get(Calendar.YEAR) == calendarPrev.get(Calendar.YEAR)){
if(calendarCurrent.get(Calendar.DAY_OF_YEAR) == calendarPrev.get(Calendar.DAY_OF_YEAR)){
return true;
}
}
return false;
}
}

View File

@ -1,6 +1,8 @@
package com.krokogator.photoeat.adapter;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
@ -19,6 +21,7 @@ import com.krokogator.photoeat.model.FoodQuantityTypeEnum;
import org.w3c.dom.Text;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -45,9 +48,15 @@ public class CaloriesAdapter extends RecyclerView.Adapter<CaloriesAdapter.ViewHo
//PERFORM ACTION ON BINDING x ELEMENT OF RECYCLER VIEW
CaloriesItem item = items.get(position);
holder.foodName.setText(item.getName());
String calories = item.getCalories() + " kcal";
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
String calories = df.format(item.getCalories()) + " kcal";
holder.calories.setText(calories);
if(item.isDailySummary()){
holder.foodName.setTypeface(null, Typeface.BOLD);
holder.calories.setTypeface(null, Typeface.BOLD);
}
}

View File

@ -8,4 +8,6 @@ public class Result implements BaseColumns {
"food";
public static final String COLUMN_NAME_CALORIES =
"calories";
public static final String COLUMN_NAME_DATE =
"date";
}

View File

@ -9,7 +9,8 @@ public class ResultDBHelper extends SQLiteOpenHelper {
"CREATE TABLE " + Result.TABLE_NAME + " (" +
Result._ID + " INTEGER PRIMARY KEY," +
Result.COLUMN_NAME_FOOD + " TEXT," +
Result.COLUMN_NAME_CALORIES + " TEXT)";
Result.COLUMN_NAME_CALORIES + " TEXT," +
Result.COLUMN_NAME_DATE + " TEXT)";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + Result.TABLE_NAME;
public static final int DATABASE_VERSION=1;

View File

@ -1,13 +1,19 @@
package com.krokogator.photoeat.model;
import java.util.Date;
public class CaloriesItem {
private String name;
private Float calories;
private Date date;
private Boolean dailySummary;
public CaloriesItem(String name, Float calories){
public CaloriesItem(String name, Float calories, Date date, Boolean dailySummary){
this.name = name;
this.calories = calories;
this.date = date;
this.dailySummary = dailySummary;
}
public String getName() {
@ -25,4 +31,11 @@ public class CaloriesItem {
public void setCalories(Float calories) {
this.calories = calories;
}
public Date getDate() {
return date;
}
public Boolean isDailySummary() {
return dailySummary;
}
}