mirror of
https://github.com/derekbrameyer/MacroDroid.git
synced 2026-09-03 07:15:14 +08:00
Third commit, AFood done with adding and deleting, db4o begun and kinda done.
This commit is contained in:
@@ -46,6 +46,13 @@
|
||||
android:name=".MyFoodsActivity"
|
||||
android:screenOrientation="portrait" >
|
||||
</activity>
|
||||
<activity
|
||||
android:configChanges="keyboardHidden|orientation"
|
||||
android:theme="@style/FullHeightDialog"
|
||||
android:label="@string/app_name"
|
||||
android:name=".MyFoodsAddActivity"
|
||||
android:screenOrientation="portrait" >
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
BIN
libs/db4o-8.0.184.15484-all-java5.jar
Normal file
BIN
libs/db4o-8.0.184.15484-all-java5.jar
Normal file
Binary file not shown.
131
res/layout/my_foods_add_activity.xml
Normal file
131
res/layout/my_foods_add_activity.xml
Normal file
@@ -0,0 +1,131 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center|center_vertical" >
|
||||
|
||||
<ScrollView
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentTop="true"
|
||||
android:fillViewport="true" >
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:gravity="center|center_vertical"
|
||||
android:orientation="vertical" >
|
||||
|
||||
<TableLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content" >
|
||||
|
||||
<TextView
|
||||
style="@style/CenteredHeaderTV"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:text="@string/Add_Food" />
|
||||
|
||||
<TableRow >
|
||||
|
||||
<TextView
|
||||
style="@style/MyMacrosLeftColumnTL"
|
||||
android:text="@string/Name" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/ET_add_food_name"
|
||||
style="@style/MyMacrosIKnowRightColumnET" />
|
||||
</TableRow>
|
||||
|
||||
<TableRow >
|
||||
|
||||
<TextView
|
||||
style="@style/MyMacrosLeftColumnTL"
|
||||
android:text="@string/Serving_Size" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/ET_add_food_serving_size"
|
||||
style="@style/MyMacrosIKnowRightColumnET" />
|
||||
</TableRow>
|
||||
|
||||
<TableRow >
|
||||
|
||||
<TextView
|
||||
style="@style/MyMacrosLeftColumnTL"
|
||||
android:text="@string/kCal" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/ET_add_food_kCal"
|
||||
style="@style/MyMacrosIKnowRightColumnET" />
|
||||
</TableRow>
|
||||
</TableLayout>
|
||||
|
||||
<TableLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content" >
|
||||
|
||||
<TextView
|
||||
style="@style/CenteredHeaderTV"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:text="@string/Macros" />
|
||||
|
||||
<TableRow >
|
||||
|
||||
<TextView
|
||||
style="@style/ThreeColumnCenteredTV"
|
||||
android:text="@string/protein" />
|
||||
|
||||
<TextView
|
||||
style="@style/ThreeColumnCenteredTV"
|
||||
android:text="@string/carbs" />
|
||||
|
||||
<TextView
|
||||
style="@style/ThreeColumnCenteredTV"
|
||||
android:text="@string/fat" />
|
||||
</TableRow>
|
||||
|
||||
<TableRow >
|
||||
|
||||
<EditText
|
||||
android:id="@+id/ET_add_food_protein"
|
||||
style="@style/ThreeColumnCenteredET" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/ET_add_food_carbs"
|
||||
style="@style/ThreeColumnCenteredET" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/ET_add_food_fat"
|
||||
style="@style/ThreeColumnCenteredET" />
|
||||
</TableRow>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:gravity="center"
|
||||
android:text="@string/grams"
|
||||
android:textSize="14sp" />
|
||||
</TableLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/LL_button_container"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal" >
|
||||
|
||||
<Button
|
||||
android:id="@+id/BTN_save"
|
||||
style="@style/TwoColumnButtons"
|
||||
android:text="@string/Save" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/BTN_cancel"
|
||||
style="@style/TwoColumnButtons"
|
||||
android:text="@string/Cancel" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -22,6 +22,7 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_marginLeft="4dp"
|
||||
android:layout_below="@id/TV_food_list_item_name"
|
||||
android:text="@string/my_foods_list_item_kCal_label"
|
||||
android:textSize="12sp"
|
||||
@@ -31,6 +32,7 @@
|
||||
android:id="@+id/TV_food_list_item_kCal_value"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBaseline="@id/TV_food_list_item_kCal_label"
|
||||
android:layout_toLeftOf="@id/TV_food_list_item_kCal_label"
|
||||
android:layout_below="@id/TV_food_list_item_name"
|
||||
android:lines="1"
|
||||
@@ -43,6 +45,7 @@
|
||||
android:id="@+id/TV_food_list_item_serving_size"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBaseline="@id/TV_food_list_item_kCal_label"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_toLeftOf="@id/TV_food_list_item_kCal_value"
|
||||
android:layout_below="@id/TV_food_list_item_name"
|
||||
@@ -65,6 +68,9 @@
|
||||
android:id="@+id/TV_food_list_item_protein_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBaseline="@id/TV_food_list_item_protein_value"
|
||||
android:layout_marginLeft="4dp"
|
||||
android:layout_marginRight="4dp"
|
||||
android:layout_toRightOf="@id/TV_food_list_item_protein_value"
|
||||
android:layout_below="@id/TV_food_list_item_serving_size"
|
||||
android:text="@string/my_foods_list_item_protein_label"
|
||||
@@ -74,6 +80,7 @@
|
||||
android:id="@+id/TV_food_list_item_carbs_value"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBaseline="@id/TV_food_list_item_protein_value"
|
||||
android:layout_toRightOf="@id/TV_food_list_item_protein_label"
|
||||
android:layout_below="@id/TV_food_list_item_serving_size"
|
||||
android:text="1000"
|
||||
@@ -84,6 +91,9 @@
|
||||
android:id="@+id/TV_food_list_item_carbs_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBaseline="@id/TV_food_list_item_protein_value"
|
||||
android:layout_marginLeft="4dp"
|
||||
android:layout_marginRight="4dp"
|
||||
android:layout_toRightOf="@id/TV_food_list_item_carbs_value"
|
||||
android:layout_below="@id/TV_food_list_item_serving_size"
|
||||
android:text="@string/my_foods_list_item_carbs_label"
|
||||
@@ -93,6 +103,7 @@
|
||||
android:id="@+id/TV_food_list_item_fat_value"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBaseline="@id/TV_food_list_item_protein_value"
|
||||
android:layout_toRightOf="@id/TV_food_list_item_carbs_label"
|
||||
android:layout_below="@id/TV_food_list_item_serving_size"
|
||||
android:text="9001"
|
||||
@@ -103,6 +114,8 @@
|
||||
android:id="@+id/TV_food_list_item_fat_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignBaseline="@id/TV_food_list_item_protein_value"
|
||||
android:layout_marginLeft="4dp"
|
||||
android:layout_toRightOf="@id/TV_food_list_item_fat_value"
|
||||
android:layout_below="@id/TV_food_list_item_serving_size"
|
||||
android:text="@string/my_foods_list_item_fat_label"
|
||||
|
||||
@@ -46,3 +46,4 @@ Mixed vegetables
|
||||
3
|
||||
13
|
||||
0
|
||||
|
||||
|
||||
@@ -6,17 +6,22 @@
|
||||
<string name="carbs">carbs</string>
|
||||
<string name="deficit">deficit</string>
|
||||
<string name="fat">fat</string>
|
||||
<string name="grams">grams</string>
|
||||
<string name="kCal">kCal</string>
|
||||
<string name="kCal_day">kCal/day</string>
|
||||
<string name="kCal_g">kCal/g</string>
|
||||
<string name="protein">protein</string>
|
||||
<string name="Add">Add</string>
|
||||
<string name="Add_Food">Add Food</string>
|
||||
<string name="Cancel">Cancel</string>
|
||||
<string name="Import">Import</string>
|
||||
<string name="Macros">Macros</string>
|
||||
<string name="Macro_Conversions">Macro Conversions</string>
|
||||
<string name="Measurements">Measurements</string>
|
||||
<string name="My_Foods">My Foods</string>
|
||||
<string name="Name">Name</string>
|
||||
<string name="Save">Save</string>
|
||||
<string name="Serving_Size">Serving Size</string>
|
||||
<string name="Set">Set</string>
|
||||
<string name="Your_macros_have_been_saved">Your macros have been saved!</string>
|
||||
<string name="main_activity_today">Today</string>
|
||||
@@ -24,10 +29,10 @@
|
||||
<string name="main_activity_my_foods">My Foods</string>
|
||||
<string name="main_activity_my_macros">My Macros</string>
|
||||
<string name="main_activity_settings">Settings/Info/FAQ</string>
|
||||
<string name="my_foods_list_item_kCal_label"> kCal</string>
|
||||
<string name="my_foods_list_item_protein_label"> g protein, </string>
|
||||
<string name="my_foods_list_item_carbs_label"> g carbs, </string>
|
||||
<string name="my_foods_list_item_fat_label"> g fat</string>
|
||||
<string name="my_foods_list_item_kCal_label">kCal</string>
|
||||
<string name="my_foods_list_item_protein_label">g protein,</string>
|
||||
<string name="my_foods_list_item_carbs_label">g carbs,</string>
|
||||
<string name="my_foods_list_item_fat_label">g fat</string>
|
||||
<string name="my_macros_calculate">Calculate my macros!</string>
|
||||
<string name="my_macros_i_know">I know my macros!</string>
|
||||
<string name="my_macros_no_input">You have not inputted or calculated your macros. Please select one of the buttons below to do so.</string>
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
|
||||
<item name="android:windowNoTitle">true</item>
|
||||
</style>
|
||||
|
||||
<style name="ThreeColumnCenteredTV">
|
||||
<item name="android:layout_width">0dp</item>
|
||||
<item name="android:layout_height">wrap_content</item>
|
||||
|
||||
36
src/com/doomonafireball/macrodroid/ADay.java
Normal file
36
src/com/doomonafireball/macrodroid/ADay.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.doomonafireball.macrodroid;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class ADay implements Comparable<ADay> {
|
||||
public long id;
|
||||
private ArrayList<AFood> foods;
|
||||
private Calendar date;
|
||||
// TODO Array of pictures
|
||||
|
||||
public ArrayList<AFood> getFoods() {
|
||||
return foods;
|
||||
}
|
||||
|
||||
public void setFoods(ArrayList<AFood> foods) {
|
||||
this.foods = foods;
|
||||
}
|
||||
|
||||
public Calendar getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Calendar date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ADay arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
if ((this.getDate() != null) && (arg0.getDate() != null)) {
|
||||
return arg0.getDate().compareTo(this.getDate());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
47
src/com/doomonafireball/macrodroid/AFood.java
Normal file
47
src/com/doomonafireball/macrodroid/AFood.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.doomonafireball.macrodroid;
|
||||
|
||||
public class AFood {
|
||||
private String foodName;
|
||||
private String foodServingSize;
|
||||
private float foodGramsProtein;
|
||||
private float foodGramsCarbs;
|
||||
private float foodGramsFat;
|
||||
private float foodKCal;
|
||||
|
||||
public String getFoodName() {
|
||||
return foodName;
|
||||
}
|
||||
public void setFoodName(String foodName) {
|
||||
this.foodName = foodName;
|
||||
}
|
||||
public String getFoodServingSize() {
|
||||
return foodServingSize;
|
||||
}
|
||||
public void setFoodServingSize(String foodServingSize) {
|
||||
this.foodServingSize = foodServingSize;
|
||||
}
|
||||
public float getFoodGramsProtein() {
|
||||
return foodGramsProtein;
|
||||
}
|
||||
public void setFoodGramsProtein(float foodGramsProtein) {
|
||||
this.foodGramsProtein = foodGramsProtein;
|
||||
}
|
||||
public float getFoodGramsCarbs() {
|
||||
return foodGramsCarbs;
|
||||
}
|
||||
public void setFoodGramsCarbs(float foodGramsCarbs) {
|
||||
this.foodGramsCarbs = foodGramsCarbs;
|
||||
}
|
||||
public float getFoodGramsFat() {
|
||||
return foodGramsFat;
|
||||
}
|
||||
public void setFoodGramsFat(float foodGramsFat) {
|
||||
this.foodGramsFat = foodGramsFat;
|
||||
}
|
||||
public float getFoodKCal() {
|
||||
return foodKCal;
|
||||
}
|
||||
public void setFoodKCal(float foodKCal) {
|
||||
this.foodKCal = foodKCal;
|
||||
}
|
||||
}
|
||||
142
src/com/doomonafireball/macrodroid/Db4oHelper.java
Normal file
142
src/com/doomonafireball/macrodroid/Db4oHelper.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package com.doomonafireball.macrodroid;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.db4o.Db4oEmbedded;
|
||||
import com.db4o.ObjectContainer;
|
||||
import com.db4o.ObjectSet;
|
||||
import com.db4o.config.EmbeddedConfiguration;
|
||||
import com.db4o.query.Predicate;
|
||||
|
||||
public class Db4oHelper {
|
||||
private static ObjectContainer oc = null;
|
||||
private Context mContext;
|
||||
|
||||
public Db4oHelper(Context ctx) {
|
||||
this.mContext = ctx;
|
||||
}
|
||||
|
||||
public ObjectContainer db() {
|
||||
// Create, open, and close the database
|
||||
try {
|
||||
if (oc == null || oc.ext().isClosed()) {
|
||||
oc = Db4oEmbedded
|
||||
.openFile(dbConfig(), db4oDBFullPath(mContext));
|
||||
}
|
||||
return oc;
|
||||
} catch (Exception e) {
|
||||
Log.e(Tags.LOG_TAG, e.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private EmbeddedConfiguration dbConfig() throws IOException {
|
||||
// Configure the behavior of the database
|
||||
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
|
||||
configuration.common().objectClass(ADay.class).objectField("id")
|
||||
.indexed(true);
|
||||
return configuration;
|
||||
}
|
||||
|
||||
private String db4oDBFullPath(Context ctx) {
|
||||
// Returns the path for the database location
|
||||
return ctx.getDir("data", 0) + "/" + "macrodroid.db4o";
|
||||
}
|
||||
|
||||
public void close() {
|
||||
// Closes the database
|
||||
if (oc != null) {
|
||||
oc.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteDatabase() {
|
||||
close();
|
||||
new File(db4oDBFullPath(mContext)).delete();
|
||||
}
|
||||
|
||||
public void saveDay(ADay day) {
|
||||
if (day.id == 0) {
|
||||
day.id = getNextId();
|
||||
}
|
||||
db().store(day);
|
||||
db().commit();
|
||||
}
|
||||
|
||||
public void deleteDay(long id) {
|
||||
ObjectSet<ADay> result = fetchDaysById(id);
|
||||
while (result.hasNext()) {
|
||||
db().delete((ADay) result.next());
|
||||
}
|
||||
db().commit();
|
||||
}
|
||||
|
||||
public ADay fetchDayById(long id) {
|
||||
ObjectSet<ADay> result = fetchDaysById(id);
|
||||
if (result.hasNext()) {
|
||||
return (ADay) result.next();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public int countDays(long id) {
|
||||
ObjectSet<ADay> days = fetchDaysById(id);
|
||||
return days == null ? 0 : days.size();
|
||||
}
|
||||
|
||||
public List<ADay> fetchAllDayRows() {
|
||||
return db().query(ADay.class);
|
||||
}
|
||||
|
||||
public List<ADay> fetchExercisesForDate(final Calendar calendar) {
|
||||
final int checkCalMonth = calendar.get(Calendar.MONTH);
|
||||
final int checkCalDate = calendar.get(Calendar.DATE);
|
||||
final int checkCalYear = calendar.get(Calendar.YEAR);
|
||||
List<ADay> result = db().query(new Predicate<ADay>() {
|
||||
public boolean match(ADay day) {
|
||||
Log.d(Tags.LOG_TAG, "Passed calendar: " + calendar.get(Calendar.MONTH) + "/"
|
||||
+ calendar.get(Calendar.DATE) + "/"
|
||||
+ calendar.get(Calendar.YEAR));
|
||||
Calendar dayCal = day.getDate();
|
||||
int dayCalMonth = dayCal.get(Calendar.MONTH);
|
||||
int dayCalDate = dayCal.get(Calendar.DATE);
|
||||
int dayCalYear = dayCal.get(Calendar.YEAR);
|
||||
if ((dayCalMonth == checkCalMonth) && (dayCalDate == checkCalDate)
|
||||
&& (dayCalYear == checkCalYear)) {
|
||||
Log.d(Tags.LOG_TAG, "Found match, adding item!");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
private ObjectSet<ADay> fetchDaysById(long id) {
|
||||
ADay day = new ADay();
|
||||
day.id = id;
|
||||
return db().queryByExample(day);
|
||||
}
|
||||
|
||||
public long getNextId() {
|
||||
ObjectSet<Db4oIncrementedId> result = db().queryByExample(
|
||||
Db4oIncrementedId.class);
|
||||
Db4oIncrementedId ii = null;
|
||||
long nextId;
|
||||
if (result.hasNext()) {
|
||||
ii = (Db4oIncrementedId) result.next();
|
||||
} else {
|
||||
ii = new Db4oIncrementedId();
|
||||
}
|
||||
nextId = ii.getNextID();
|
||||
db().store(ii);
|
||||
return nextId;
|
||||
}
|
||||
}
|
||||
17
src/com/doomonafireball/macrodroid/Db4oIncrementedId.java
Normal file
17
src/com/doomonafireball/macrodroid/Db4oIncrementedId.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.doomonafireball.macrodroid;
|
||||
|
||||
public class Db4oIncrementedId {
|
||||
public long id;
|
||||
|
||||
public Db4oIncrementedId() {
|
||||
this.id = 0;
|
||||
}
|
||||
|
||||
public long getNextID() {
|
||||
return ++id;
|
||||
}
|
||||
|
||||
public void resetID() {
|
||||
id = 0;
|
||||
}
|
||||
}
|
||||
@@ -6,15 +6,20 @@ import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
public class MacroDroidApplication extends Application {
|
||||
private static final String MACROS_PREFS = "MacroDroidMacrosPrefs";
|
||||
public SharedPreferences macrosPrefs;
|
||||
public SharedPreferences.Editor macrosPrefsEditor;
|
||||
private Db4oHelper dbHelper;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
@@ -36,6 +41,31 @@ public class MacroDroidApplication extends Application {
|
||||
macrosPrefsEditor.commit();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasData(Context ctx) {
|
||||
dbHelper = new Db4oHelper(ctx);
|
||||
if (dbHelper.fetchAllDayRows().isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteDatabase() {
|
||||
dbHelper.deleteDatabase();
|
||||
}
|
||||
|
||||
public void saveDay(ADay day) {
|
||||
dbHelper.saveDay(day);
|
||||
}
|
||||
|
||||
public List<ADay> getDaysFromDb() {
|
||||
return dbHelper.fetchAllDayRows();
|
||||
}
|
||||
|
||||
public List<ADay> getADayForCalendar(Calendar cal) {
|
||||
return dbHelper.fetchExercisesForDate(cal);
|
||||
}
|
||||
|
||||
public byte[] readInternalStoragePrivate(String filename) {
|
||||
int len = 1024;
|
||||
@@ -75,6 +105,66 @@ public class MacroDroidApplication extends Application {
|
||||
}
|
||||
}
|
||||
|
||||
public void writeFoodsToInternalStorage(ArrayList<AFood> foods) {
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
for (int i = 0; i < foods.size(); i++) {
|
||||
AFood f = foods.get(i);
|
||||
Log.d(Tags.LOG_TAG, "Writing Name: " + f.getFoodName());
|
||||
Log.d(Tags.LOG_TAG, "Writing Serving size: " + f.getFoodServingSize());
|
||||
Log.d(Tags.LOG_TAG, "Writing kCal: " + f.getFoodKCal());
|
||||
Log.d(Tags.LOG_TAG, "Writing Protein: " + f.getFoodGramsProtein());
|
||||
Log.d(Tags.LOG_TAG, "Writing Carbs: " + f.getFoodGramsCarbs());
|
||||
Log.d(Tags.LOG_TAG, "Writing Fat: " + f.getFoodGramsFat());
|
||||
baos.write(foodToByteArray(f));
|
||||
}
|
||||
byte[] newFoodList = baos.toByteArray();
|
||||
writeInternalStoragePrivate(Tags.MY_FOODS_FILENAME, newFoodList);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void addFoodToInternalStorage(AFood food) {
|
||||
try {
|
||||
byte[] retChar = "\n".getBytes();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
baos.write(readInternalStoragePrivate(Tags.MY_FOODS_FILENAME));
|
||||
baos.write(foodToByteArray(food));
|
||||
byte[] newFoodList = baos.toByteArray();
|
||||
writeInternalStoragePrivate(Tags.MY_FOODS_FILENAME, newFoodList);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] foodToByteArray(AFood food) {
|
||||
try {
|
||||
byte[] retChar = "\n".getBytes();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
baos.write(food.getFoodName().getBytes());
|
||||
baos.write(retChar);
|
||||
baos.write(food.getFoodServingSize().getBytes());
|
||||
baos.write(retChar);
|
||||
baos.write(Float.toString(food.getFoodKCal()).getBytes());
|
||||
baos.write(retChar);
|
||||
baos.write(Float.toString(food.getFoodGramsProtein()).getBytes());
|
||||
baos.write(retChar);
|
||||
baos.write(Float.toString(food.getFoodGramsCarbs()).getBytes());
|
||||
baos.write(retChar);
|
||||
baos.write(Float.toString(food.getFoodGramsFat()).getBytes());
|
||||
baos.write(retChar);
|
||||
baos.write(retChar);
|
||||
return baos.toByteArray();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public byte[] defaultFoodsToByteArray() {
|
||||
int len = 1024;
|
||||
byte[] buffer = new byte[len];
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package com.doomonafireball.macrodroid;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
@@ -13,6 +18,8 @@ public class MainActivity extends Activity {
|
||||
private Button myFoodsBTN;
|
||||
private Button myMacrosBTN;
|
||||
private Button settingsBTN;
|
||||
private Context mContext;
|
||||
private MacroDroidApplication application;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@@ -20,6 +27,9 @@ public class MainActivity extends Activity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.main_activity);
|
||||
|
||||
mContext = this;
|
||||
application = ((MacroDroidApplication) getApplication());
|
||||
|
||||
todayBTN = (Button) findViewById(R.id.BTN_today);
|
||||
previousBTN = (Button) findViewById(R.id.BTN_previous);
|
||||
myFoodsBTN = (Button) findViewById(R.id.BTN_my_foods);
|
||||
@@ -30,7 +40,37 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
if (application.hasData(mContext)) {
|
||||
// Try to find the ADay object
|
||||
List<ADay> possibleCurrDays =
|
||||
application.getADayForCalendar(Calendar.getInstance());
|
||||
if (!possibleCurrDays.isEmpty()) {
|
||||
// We have a current object
|
||||
Log.d(Tags.LOG_TAG, "We have a current day for: "
|
||||
+ Calendar.getInstance().get(Calendar.MONTH) + "/"
|
||||
+ Calendar.getInstance().get(Calendar.DATE) + "/"
|
||||
+ Calendar.getInstance().get(Calendar.YEAR));
|
||||
} else {
|
||||
// Instantiate a new object
|
||||
Log.d(Tags.LOG_TAG, "We don't have a current day for: "
|
||||
+ Calendar.getInstance().get(Calendar.MONTH) + "/"
|
||||
+ Calendar.getInstance().get(Calendar.DATE) + "/"
|
||||
+ Calendar.getInstance().get(Calendar.YEAR));
|
||||
ADay currDay = new ADay();
|
||||
currDay.setDate(Calendar.getInstance());
|
||||
application.saveDay(currDay);
|
||||
}
|
||||
} else {
|
||||
// Instantiate a new object
|
||||
Log.d(Tags.LOG_TAG,
|
||||
"We don't even have data! AND We don't have a current day for: "
|
||||
+ Calendar.getInstance().get(Calendar.MONTH) + "/"
|
||||
+ Calendar.getInstance().get(Calendar.DATE) + "/"
|
||||
+ Calendar.getInstance().get(Calendar.YEAR));
|
||||
ADay currDay = new ADay();
|
||||
currDay.setDate(Calendar.getInstance());
|
||||
application.saveDay(currDay);
|
||||
}
|
||||
}
|
||||
});
|
||||
previousBTN.setOnClickListener(new OnClickListener() {
|
||||
|
||||
@@ -1,22 +1,33 @@
|
||||
package com.doomonafireball.macrodroid;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.AdapterView.OnItemLongClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class MyFoodsActivity extends Activity {
|
||||
private Button addBTN;
|
||||
private Button importBTN;
|
||||
private ListView myFoodsLV;
|
||||
private MyFoodsAdapter myFoodsAdapter;
|
||||
private MacroDroidApplication application;
|
||||
private Context mContext;
|
||||
private byte[] myFoodsByteArray;
|
||||
private String myFoods;
|
||||
private String myFoodsRaw;
|
||||
private String[] myFoods;
|
||||
private ArrayList<AFood> myFoodsArrayList;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
@@ -35,19 +46,89 @@ public class MyFoodsActivity extends Activity {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
startActivity(new Intent(MyFoodsActivity.this, MyFoodsAddActivity.class));
|
||||
}
|
||||
});
|
||||
importBTN.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
application.writeInternalStoragePrivate(Tags.MY_FOODS_FILENAME,
|
||||
application.defaultFoodsToByteArray());
|
||||
refreshList();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
refreshList();
|
||||
|
||||
myFoodsLV.setOnItemLongClickListener(myFoodsItemLongClickListener);
|
||||
}
|
||||
|
||||
private OnItemLongClickListener myFoodsItemLongClickListener = new OnItemLongClickListener() {
|
||||
@Override
|
||||
public boolean onItemLongClick(AdapterView<?> a, View v, int position, long id) {
|
||||
// TODO Auto-generated method stub
|
||||
final AFood selectedFood = (AFood) myFoodsAdapter.getItem(position);
|
||||
|
||||
final CharSequence[] items = { "Delete" };
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
|
||||
builder.setTitle("Options");
|
||||
builder.setItems(items, new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int item) {
|
||||
if (item == 0) {
|
||||
// Delete this food
|
||||
myFoodsArrayList.remove(selectedFood);
|
||||
application.writeFoodsToInternalStorage(myFoodsArrayList);
|
||||
refreshList();
|
||||
dialog.dismiss();
|
||||
}
|
||||
}
|
||||
});
|
||||
AlertDialog alert = builder.create();
|
||||
alert.show();
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
private void refreshList() {
|
||||
myFoodsByteArray = application.readInternalStoragePrivate(Tags.MY_FOODS_FILENAME);
|
||||
myFoods = new String(myFoodsByteArray);
|
||||
Log.d(Tags.LOG_TAG, myFoods);
|
||||
myFoodsRaw = new String(myFoodsByteArray);
|
||||
myFoods = myFoodsRaw.split("\\r?\\n");
|
||||
|
||||
// Parse the String array into an ArrayList<AFood>
|
||||
myFoodsArrayList = new ArrayList<AFood>();
|
||||
if (myFoods != null) {
|
||||
if (myFoods.length > 5) {
|
||||
for (int i = 0; i < myFoods.length; i += 7) {
|
||||
AFood food = new AFood();
|
||||
|
||||
Log.d(Tags.LOG_TAG, "Name: " + myFoods[i]);
|
||||
Log.d(Tags.LOG_TAG, "Serving size: " + myFoods[i + 1]);
|
||||
Log.d(Tags.LOG_TAG, "kCal: " + myFoods[i + 2]);
|
||||
Log.d(Tags.LOG_TAG, "Protein: " + myFoods[i + 3]);
|
||||
Log.d(Tags.LOG_TAG, "Carbs: " + myFoods[i + 4]);
|
||||
Log.d(Tags.LOG_TAG, "Fat: " + myFoods[i + 5]);
|
||||
|
||||
food.setFoodName(myFoods[i]);
|
||||
food.setFoodServingSize(myFoods[i + 1]);
|
||||
food.setFoodKCal(Float.parseFloat(myFoods[i + 2]));
|
||||
food.setFoodGramsProtein(Float.parseFloat(myFoods[i + 3]));
|
||||
food.setFoodGramsCarbs(Float.parseFloat(myFoods[i + 4]));
|
||||
food.setFoodGramsFat(Float.parseFloat(myFoods[i + 5]));
|
||||
|
||||
myFoodsArrayList.add(food);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
myFoodsAdapter = new MyFoodsAdapter(mContext, myFoodsArrayList);
|
||||
myFoodsLV.setAdapter(myFoodsAdapter);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
refreshList();
|
||||
}
|
||||
}
|
||||
|
||||
63
src/com/doomonafireball/macrodroid/MyFoodsAdapter.java
Normal file
63
src/com/doomonafireball/macrodroid/MyFoodsAdapter.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package com.doomonafireball.macrodroid;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class MyFoodsAdapter extends BaseAdapter {
|
||||
private ArrayList<AFood> items;
|
||||
private Context mContext;
|
||||
|
||||
public MyFoodsAdapter(Context context, ArrayList<AFood> items) {
|
||||
super();
|
||||
this.mContext = context;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return items.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int i) {
|
||||
return items.get(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
// TODO Auto-generated method stub
|
||||
if (convertView == null) {
|
||||
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
|
||||
convertView = inflater.inflate(R.layout.my_foods_list_item, parent, false);
|
||||
}
|
||||
TextView nameTV = (TextView) convertView.findViewById(R.id.TV_food_list_item_name);
|
||||
TextView kCalTV = (TextView) convertView.findViewById(R.id.TV_food_list_item_kCal_value);
|
||||
TextView servingSizeTV = (TextView) convertView.findViewById(R.id.TV_food_list_item_serving_size);
|
||||
TextView proteinTV = (TextView) convertView.findViewById(R.id.TV_food_list_item_protein_value);
|
||||
TextView carbsTV = (TextView) convertView.findViewById(R.id.TV_food_list_item_carbs_value);
|
||||
TextView fatTV = (TextView) convertView.findViewById(R.id.TV_food_list_item_fat_value);
|
||||
|
||||
AFood food = items.get(position);
|
||||
|
||||
nameTV.setText(food.getFoodName());
|
||||
kCalTV.setText(Float.toString(food.getFoodKCal()));
|
||||
servingSizeTV.setText(food.getFoodServingSize());
|
||||
proteinTV.setText(Float.toString(food.getFoodGramsProtein()));
|
||||
carbsTV.setText(Float.toString(food.getFoodGramsCarbs()));
|
||||
fatTV.setText(Float.toString(food.getFoodGramsFat()));
|
||||
|
||||
return convertView;
|
||||
}
|
||||
}
|
||||
64
src/com/doomonafireball/macrodroid/MyFoodsAddActivity.java
Normal file
64
src/com/doomonafireball/macrodroid/MyFoodsAddActivity.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.doomonafireball.macrodroid;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
import android.view.Window;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class MyFoodsAddActivity extends Activity {
|
||||
private MacroDroidApplication application;
|
||||
private Button saveBTN;
|
||||
private Button cancelBTN;
|
||||
private EditText nameET;
|
||||
private EditText servingSizeET;
|
||||
private EditText kCalET;
|
||||
private EditText proteinET;
|
||||
private EditText carbsET;
|
||||
private EditText fatET;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.my_foods_add_activity);
|
||||
|
||||
application = ((MacroDroidApplication) getApplication());
|
||||
|
||||
saveBTN = (Button) findViewById(R.id.BTN_save);
|
||||
cancelBTN = (Button) findViewById(R.id.BTN_cancel);
|
||||
nameET = (EditText) findViewById(R.id.ET_add_food_name);
|
||||
servingSizeET = (EditText) findViewById(R.id.ET_add_food_serving_size);
|
||||
kCalET = (EditText) findViewById(R.id.ET_add_food_kCal);
|
||||
proteinET = (EditText) findViewById(R.id.ET_add_food_protein);
|
||||
carbsET = (EditText) findViewById(R.id.ET_add_food_carbs);
|
||||
fatET = (EditText) findViewById(R.id.ET_add_food_fat);
|
||||
|
||||
saveBTN.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// Persist values to storage
|
||||
AFood food = new AFood();
|
||||
food.setFoodName(nameET.getText().toString());
|
||||
food.setFoodServingSize(servingSizeET.getText().toString());
|
||||
food.setFoodKCal(Float.parseFloat(kCalET.getText().toString()));
|
||||
food.setFoodGramsProtein(Float.parseFloat(proteinET.getText().toString()));
|
||||
food.setFoodGramsCarbs(Float.parseFloat(carbsET.getText().toString()));
|
||||
food.setFoodGramsFat(Float.parseFloat(fatET.getText().toString()));
|
||||
|
||||
application.addFoodToInternalStorage(food);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
cancelBTN.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user