Skip to content

Commit

Permalink
UserBdAdapter now have the name of the pantry
Browse files Browse the repository at this point in the history
More bugfix
  • Loading branch information
FabioPinheiro committed May 4, 2014
1 parent f9860db commit 17950c6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_my_pantries);
TextView myMail = (TextView) findViewById(R.id.myMail);
myMail.setText(EndPointCall.getEmailAccount());

//FIXME final String[] pantreisName = EndPointCall.getUserDbAdapter().avalablePantrysNamesFromUser(EndPointCall.getEmailAccount());

newPantry = (Button) findViewById(R.id.createnewpantry);
newPantry.setOnClickListener(new OnClickListener() {
@Override
Expand All @@ -55,9 +58,6 @@ public void onClick(View arg0) {
}
public void xpto() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
List<String> aux = EndPointCall.getUserDbAdapter().avalablePantrysFromUser(EndPointCall.getEmailAccount());
final String[] pantry = new String[aux.size()];
aux.toArray(pantry);
alert.setTitle("New Pantry in " + EndPointCall.getEmailAccount()); // TEXT
// FIXME
alert.setMessage("Chose the Pantry Name:"); // TEXT FIXME
Expand Down Expand Up @@ -86,7 +86,7 @@ public class NewPantryTask extends AsyncTask<Context, Integer, UserPantry> {
protected void onPostExecute(UserPantry result) {
super.onPostExecute(result);
if(result != null){
EndPointCall.getUserDbAdapter().createPantry(EndPointCall.getEmailAccount(), pantryName + result.getPantry().toString());
EndPointCall.getUserDbAdapter().createPantry(EndPointCall.getEmailAccount(), result.getPantry(), pantryName);
EndPointCall.msg(EndPointCall.DEBUG, "! "+ EndPointCall.getUserDbAdapter().getAllPantry().size());
}
EndPointCall.msg(LOG_TAG, EndPointCall.DONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import epic.easystock.assist.MetaProductAdapter;
import epic.easystock.data.LocalMetaProduct;
import epic.easystock.io.EndPointCall;
import epic.easystock.io.UserBdAdapter;

public class PantyActivity extends ListActivity {
String mail;
Expand All @@ -34,14 +35,12 @@ protected void onCreate(Bundle savedInstanceState) {
setListAdapter(adapter);

AlertDialog.Builder alert = new AlertDialog.Builder(this);
List<String> aux = EndPointCall.getUserDbAdapter().avalablePantrysFromUser(EndPointCall.getEmailAccount());
final String[] pantry = new String[aux.size()];
aux.toArray(pantry);
final String[] pantreisName = EndPointCall.getUserDbAdapter().avalablePantrysNamesFromUser(EndPointCall.getEmailAccount());
alert.setTitle("Select Pantry");
alert.setItems(pantry, new DialogInterface.OnClickListener() {
alert.setItems(pantreisName, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EndPointCall.setSelectedPantry(pantry[which]);
EndPointCall.setSelectedPantry(pantreisName[which]);
EndPointCall.listPantryProductTask(adapter, EndPointCall.getSelectedPantry()); //FIXME
}
});
Expand Down
2 changes: 0 additions & 2 deletions EasyStockAndroid/src/epic/easystock/io/EndPointCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ static public void onApplicationCreate(Context context) {
globalSettings.edit().putString(PREFS_LAST_USED_EMAIL, aux.name).commit();// FIXME
mEmailAccount = aux.name;
msg("Hello " + mEmailAccount);
EndPointCall.getUserDbAdapter().createPantry(mEmailAccount, "pantry1");// FIXME
EndPointCall.getUserDbAdapter().createPantry(mEmailAccount, "pantry2");// FIXME
} else {
mEmailAccount = globalSettings.getString(PREFS_LAST_USED_EMAIL, "EMAIL_ERROR");
msg("Welcome Back " + mEmailAccount);// FIXME TEXT
Expand Down
65 changes: 46 additions & 19 deletions EasyStockAndroid/src/epic/easystock/io/UserBdAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ public class UserBdAdapter {
public static final String TABLE_NAME = "users";
public static final String USER = "user_id";
public static final String PANTRY_ID = "pantry_id";
public static final String PANTRY_NAME = "pantry_name";
private static final String TAG = "NotesDbAdapter";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = TABLE_NAME;
private static final int DATABASE_VERSION = 1;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
/**
* Database creation sql statement
*/
private static final String DATABASE_CREATE = "create table " + TABLE_NAME
+ " (" + PANTRY_ID + " text not null, " + USER + " text not null ,"
+ " (" + PANTRY_NAME + " text not null, " + USER + " text not null ," + PANTRY_ID + " bigint not null, "
+ "primary key(" + PANTRY_ID + ", " + USER + "));";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = TABLE_NAME;
private static final int DATABASE_VERSION = 1;

private final Context mCtx;

private static class DatabaseHelper extends SQLiteOpenHelper {
Expand Down Expand Up @@ -105,10 +107,11 @@ public void close() {
* successfully created return the new rowId for that note, otherwise return
* a -1 to indicate failure.
*/
public long createPantry(String user, String pantry) {
public long createPantry(String user, long pantryID ,String pantryName) {
ContentValues initialValues = new ContentValues();
initialValues.put(USER, user);
initialValues.put(PANTRY_ID, pantry);
initialValues.put(PANTRY_ID, pantryID);
initialValues.put(PANTRY_NAME, pantryName);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
/**
Expand All @@ -135,32 +138,56 @@ public Cursor fetchAllPantry() {
*
* @return Cursor over all notes
*/
public List<Pair<String, String>> getAllPantry() {
List<Pair<String, String>> users_pantrys = new ArrayList<Pair<String, String>>();
public List<UserPantryAux> getAllPantry() {
List<UserPantryAux> users_pantrys = new ArrayList<UserPantryAux>();
Cursor cursor = fetchAllPantry();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Pair<String, String> prod = cursorToUserPantry(cursor);
UserPantryAux prod = cursorToUserPantry(cursor);
users_pantrys.add(prod);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return users_pantrys;
}
public List<String> avalablePantrysFromUser(String user) {
List<Pair<String, String>> pairList = getAllPantry();
List<String> pantrysID = new ArrayList<String>();
for (Pair<String, String> pair : pairList) {
if (pair.first.equalsIgnoreCase(user)) {
pantrysID.add(pair.second);//*"" + (pair.first.equalsIgnoreCase(user)) + "#" + pair.first + "#"+ user+ "#" +pair.second);
private/*FIXME public*/ List<UserPantryAux> avalablePantrysFromUser(String user) {
List<UserPantryAux> list = getAllPantry();
List<UserPantryAux> ret = new ArrayList<UserPantryAux>();
for (UserPantryAux el : list) {
if (el.user.equalsIgnoreCase(user)) {
ret.add(el);
}
}
return pantrysID;
return ret;
}
public String[] avalablePantrysNamesFromUser(String user) {
List<UserBdAdapter.UserPantryAux> aux = avalablePantrysFromUser(user);
String[] pantreisName = new String[aux.size()];
int i = 0;
for (UserBdAdapter.UserPantryAux el : aux) {
pantreisName[i]= el.pantryName;
i++;
}
return pantreisName;
}

static public class UserPantryAux{
public String user;
public long pantryID;
public String pantryName;
public UserPantryAux(String user, long pantryID, String pantryName) {
super();
this.user = user;
this.pantryID = pantryID;
this.pantryName = pantryName;
}

}
private Pair<String, String> cursorToUserPantry(Cursor cursor) {
private UserPantryAux cursorToUserPantry(Cursor cursor) {
String user = cursor.getString(cursor.getColumnIndex(USER));
String pantry = cursor.getString(cursor.getColumnIndex(PANTRY_ID));
return new Pair<String, String>(user, pantry);
long pantryID = cursor.getLong(cursor.getColumnIndex(PANTRY_ID));
String pantryName = cursor.getString(cursor.getColumnIndex(PANTRY_NAME));
return new UserPantryAux(user, pantryID, pantryName);
}
}

0 comments on commit 17950c6

Please sign in to comment.