IanTDuncan / MealTime

Project for CSC 480
0 stars 0 forks source link

API - Edamam Shopping List - Class ApiAndDatabaseHandler #116

Closed BeepDroid closed 4 months ago

BeepDroid commented 4 months ago

API - Edamam Shopping List - Class ApiAndDatabaseHandler


Description:


"ApiAndDatabaseHandler" is a java class that handles both the API call and the database operations. The "ApiTask AsyncTask" performs the API call in the background and then processes the result in the "onPostExecute" method. The parsed search results are then inserted into the existing database using the "insertDataIntoDatabase" method. The "DatabaseHelper" class is responsible for handling most of our Database logic, and it DOES at the moment use SQLLite just so I could get said logic written, and then update it with the appropriate Firebase logic.

Steps to Reproduce:


  1. Firstly, create the "ShoppingSearchResult" java class. This is where our getter and setter methods are being stored and from which the lists are being made. This will be referenced heavily throughout the code!
  2. After that, create the "ApiAndDatabaseHandler" class. A lot is about to go into this.
  3. Make an internal class called "DatabaseHelper". This can also be an external, secondary java class that's referenced, but I wrote it as an internal class for now. During our clean up phase, if it helps building, I may move it.
  4. Ensure that this class has the" ApiTask AsyncTask". What do these do? They handle our API call logic! The "ApiTask AsyncTask" performs the API call in the background and then processes the result in the "onPostExecute" method.

Expected vs. Actual Behavior:


Expected behavior: Successful connection to the database and uploading of data points to the appropriate columns.

Actual behavior: Encountering errors on database connection.

Code Snippets:


` private static class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Insert a search result into the existing database
    public long insertSearchResult(ShoppingSearchResult searchResult) {
        SQLiteDatabase db = getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("columnName1", searchResult.getItemName()); // Replace with your actual column names
        values.put("columnName2", searchResult.getItemPrice());
        values.put("ColumnName2", searchResult.getServings());

        // Insert the new row, returning the primary key value of the new row
        return db.insert("existingTableName", null, values);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //We already have our tables made, so I didn't fill anything on this override.
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        //No use to us right now, this is for upgrading.
    }
}`

This is the previously mentioned DatabaseHelper class that is going to be handling our database connection logic.

private class ApiTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { // Perform the API call in the background try { return makeApiCall(urls[0]); } catch (IOException e) { Log.e(TAG, "Error making API call", e); return null; } } } Our ApiTask AsyncTask. What do these do? They handle our API call logic! The "ApiTask AsyncTask" performs the API call in the background and then processes the result in the "onPostExecute" method, shown below.

protected void onPostExecute(String apiResult) { // Handle the API result and insert into the database if (apiResult != null) { List<ShoppingSearchResult> searchResults = parseApiResult(apiResult); insertDataIntoDatabase(searchResults); } else { Log.e(TAG, "API result is null"); } }

Environment Details:


Built in Android Studio, Windows OS.

Current Status:


In progress / Completed

aaleksandraristic commented 4 months ago

Hey Kerry, same here. Could you please just update the due date in the roadmap for it? Put the date that you expect to finish it.

BeepDroid commented 4 months ago

Description:


I've updated the code to be prepared for Firebase implementation; however, I've started encountering errors due to missing dependencies. Resolving these both using manual input and Android Studios automatic resolution results in the same errors, current fix suggestions have to do with re-formatting my gradle.

Steps to Reproduce:


  1. Add the approrpiate dependencies to your gradle build kt, the app module specifically.
  2. Import Database Reference and Firebase Database.
  3. Use Databse Reference to connect to a specific node in the database.
  4. Push object builder with needed column values to said node using the node reference.

Expected vs. Actual Behavior:


Expected behavior is successful database connection; however, what's happening is a number of dependecy errors I still haven't found how to fix.

Code Snippets:


`dependencies {

implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
implementation("androidx.activity:activity-compose:1.8.0")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation ("com.google.firebase:firebase-database:24.0.0")
implementation("com.google.firebase:firebase-common:20.4.2")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")

}` Depencies in the Gradle build.

Code written for the connection that is providing errors: ` //Reference to the root of the Firebase RealTime Database DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

// This is for calling nodes in the database.
DatabaseReference shoppinglistRef = databaseReference.child("shoppinglist");

//Adding to the database

ShoppingSearchResult shoppinglist = new ShoppingSearchResult("Value1", 8.2); shoppinglistRef.push().setValue(shopplingList); `

Environment Details:


Android Studio version 3.5, Windows OS.

For Issue Resolution:


I've looked up some resources that have mentioned my problem, a lot of them suggest reformatting the gradle file and restarting the IDE, or verifying my files. I unfortunately didn't have much time tonight to try these fixes after wrestling with the coding structure for a while, but I intend to try them out as soon as possible.

Current Status:


In progress