From 9c001d19432460abdd0f2e250cc110285391980b Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Mon, 6 Nov 2023 21:29:45 -0500 Subject: [PATCH] Final --- .../Controllers/AccessDBHelper.java | 5 +- .../Controllers/StudentProfileDBHelper.java | 22 +- .../Views/InsertProfileDialogFragment.java | 6 +- .../Views/MainActivity.java | 19 +- .../Views/ProfileActivity.java | 37 ++- app/src/main/res/layout/activity_profile.xml | 305 ++++++++++-------- .../layout/fragment_insert_profile_dialog.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 222 insertions(+), 174 deletions(-) diff --git a/app/src/main/java/com/example/coen390_assignment2/Controllers/AccessDBHelper.java b/app/src/main/java/com/example/coen390_assignment2/Controllers/AccessDBHelper.java index 378c566..5f96158 100644 --- a/app/src/main/java/com/example/coen390_assignment2/Controllers/AccessDBHelper.java +++ b/app/src/main/java/com/example/coen390_assignment2/Controllers/AccessDBHelper.java @@ -1,6 +1,5 @@ package com.example.coen390_assignment2.Controllers; -import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; @@ -21,7 +20,6 @@ import java.util.List; public class AccessDBHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; - private Context context = null; public AccessDBHelper(@Nullable Context context) { super(context, AccessContract.AccessEntry.DATABASE_NAME, null, DATABASE_VERSION); @@ -46,7 +44,7 @@ public class AccessDBHelper extends SQLiteOpenHelper { onCreate(db); } - public long insertAccess(Access access, Context context) { + public void insertAccess(Access access, Context context) { long id = -1; SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); @@ -62,7 +60,6 @@ public class AccessDBHelper extends SQLiteOpenHelper { } finally { db.close(); } - return id; } public List getAccessFromProfileID(long profileID, Context context) { diff --git a/app/src/main/java/com/example/coen390_assignment2/Controllers/StudentProfileDBHelper.java b/app/src/main/java/com/example/coen390_assignment2/Controllers/StudentProfileDBHelper.java index c5e96fa..10107a8 100644 --- a/app/src/main/java/com/example/coen390_assignment2/Controllers/StudentProfileDBHelper.java +++ b/app/src/main/java/com/example/coen390_assignment2/Controllers/StudentProfileDBHelper.java @@ -9,7 +9,6 @@ import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.example.coen390_assignment2.Models.StudentProfile; @@ -22,7 +21,6 @@ import java.util.List; public class StudentProfileDBHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; - private Context context = null; public StudentProfileDBHelper(@Nullable Context context) { super(context, StudentProfileContract.StudentProfileEntry.DATABASE_NAME, null, DATABASE_VERSION); @@ -44,7 +42,7 @@ public class StudentProfileDBHelper extends SQLiteOpenHelper { onCreate(db); } - public long insertStudentProfile(StudentProfile studentProfile, Context context) { + public void insertStudentProfile(StudentProfile studentProfile, Context context) { long id = -1; SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); @@ -65,7 +63,6 @@ public class StudentProfileDBHelper extends SQLiteOpenHelper { } finally { db.close(); } - return id; } public List getAllStudentProfile(Context context) { @@ -98,4 +95,21 @@ public class StudentProfileDBHelper extends SQLiteOpenHelper { } return studentProfiles; } + + public void deleteProfile(long profileID, Context context) { + SQLiteDatabase db = this.getWritableDatabase(); + + try{ + // Define the WHERE clause with the ProfileID + String whereClause = "profile_id = ?"; + String[] whereArgs = { String.valueOf(profileID) }; + + // Execute the DELETE statement + db.delete("Profile", whereClause, whereArgs); + } catch (Exception e) { + Toast.makeText(context, "Error deleting Student Profile", Toast.LENGTH_SHORT).show(); + } finally { + db.close(); + } + } } diff --git a/app/src/main/java/com/example/coen390_assignment2/Views/InsertProfileDialogFragment.java b/app/src/main/java/com/example/coen390_assignment2/Views/InsertProfileDialogFragment.java index 173abf1..410bb45 100644 --- a/app/src/main/java/com/example/coen390_assignment2/Views/InsertProfileDialogFragment.java +++ b/app/src/main/java/com/example/coen390_assignment2/Views/InsertProfileDialogFragment.java @@ -24,8 +24,6 @@ public class InsertProfileDialogFragment extends DialogFragment { private EditText profile_surname_edit_text, profile_name_edit_text, profile_ID_edit_text, profile_GPA_edit_text; - private Button cancelButton, saveButton; - public InsertProfileDialogFragment() { // Required empty public constructor } @@ -41,8 +39,8 @@ public class InsertProfileDialogFragment extends DialogFragment { profile_ID_edit_text = view.findViewById(R.id.profile_ID_edit_text); profile_GPA_edit_text = view.findViewById(R.id.profile_GPA_edit_text); - cancelButton = view.findViewById(R.id.cancelButton); - saveButton = view.findViewById(R.id.saveButton); + Button cancelButton = view.findViewById(R.id.cancelButton); + Button saveButton = view.findViewById(R.id.saveButton); cancelButton.setOnClickListener(new View.OnClickListener() { @Override diff --git a/app/src/main/java/com/example/coen390_assignment2/Views/MainActivity.java b/app/src/main/java/com/example/coen390_assignment2/Views/MainActivity.java index f216adc..58a4ad0 100644 --- a/app/src/main/java/com/example/coen390_assignment2/Views/MainActivity.java +++ b/app/src/main/java/com/example/coen390_assignment2/Views/MainActivity.java @@ -1,6 +1,5 @@ package com.example.coen390_assignment2.Views; -import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.view.Menu; @@ -10,7 +9,6 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; -import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; @@ -72,14 +70,14 @@ public class MainActivity extends AppCompatActivity { printStudentProfileList(); } - private void printStudentProfileList(){ + private void printStudentProfileList() { List studentProfiles = studentProfileDBHelper.getAllStudentProfile(getApplicationContext()); // by default sort by surname ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, showStudentProfileList(studentProfiles, profileNameDisplayMode)); // set tool bar subtitle text - toolbar.setSubtitle(studentProfiles.size() + " Profiles, by " + (profileNameDisplayMode ? "Surname" : "ID") ); + toolbar.setSubtitle(studentProfiles.size() + " Profiles, by " + (profileNameDisplayMode ? "Surname" : "ID")); studentProfileList.setAdapter(adapter); @@ -118,9 +116,9 @@ public class MainActivity extends AppCompatActivity { @Override public boolean onPrepareOptionsMenu(Menu menu) { // toggle to the next mode - if (profileNameDisplayMode){ + if (profileNameDisplayMode) { toggle.setTitle("By ID"); - } else{ + } else { toggle.setTitle("By Surname"); } return true; @@ -135,7 +133,7 @@ public class MainActivity extends AppCompatActivity { profileNameDisplayMode = false; printStudentProfileList(); return false; - } else if (id == R.id.toggle_profiles_display_mode && toggle.getTitle() == "By Surname"){ + } else if (id == R.id.toggle_profiles_display_mode && toggle.getTitle() == "By Surname") { profileNameDisplayMode = true; printStudentProfileList(); return false; @@ -143,7 +141,8 @@ public class MainActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } - protected String[] showStudentProfileList(List studentProfiles, boolean profileNameDisplayMode){ + + protected String[] showStudentProfileList(List studentProfiles, boolean profileNameDisplayMode) { // Create a new list for sorted profiles List sortedProfiles = new ArrayList<>(studentProfiles); @@ -151,7 +150,7 @@ public class MainActivity extends AppCompatActivity { String[] profileListToString = new String[sortedProfiles.size()]; // sort by surname - if (profileNameDisplayMode){ + if (profileNameDisplayMode) { // Sort the new list based on the "surname" field sortedProfiles.sort(new StudentProfileSurnameComparator()); @@ -187,7 +186,7 @@ public class MainActivity extends AppCompatActivity { }); } - protected void createAccessOpened(long profileID, AccessType accessType, LocalDateTime timestamp){ + protected void createAccessOpened(long profileID, AccessType accessType, LocalDateTime timestamp) { Access access = new Access(profileID, accessType, timestamp); accessDBHelper.insertAccess(access, getApplicationContext()); } diff --git a/app/src/main/java/com/example/coen390_assignment2/Views/ProfileActivity.java b/app/src/main/java/com/example/coen390_assignment2/Views/ProfileActivity.java index 0d01d49..ab4116e 100644 --- a/app/src/main/java/com/example/coen390_assignment2/Views/ProfileActivity.java +++ b/app/src/main/java/com/example/coen390_assignment2/Views/ProfileActivity.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.MenuItem; +import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; @@ -15,6 +16,7 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import com.example.coen390_assignment2.Controllers.AccessDBHelper; +import com.example.coen390_assignment2.Controllers.StudentProfileDBHelper; import com.example.coen390_assignment2.Models.Access; import com.example.coen390_assignment2.Models.AccessType; import com.example.coen390_assignment2.R; @@ -34,7 +36,9 @@ public class ProfileActivity extends AppCompatActivity { protected TextView surnameTextView, nameTextView, IDTextView, GPATextView, ProfileCreatedTextView; - protected AccessDBHelper dbHelper; + protected AccessDBHelper accessDBHelper; + + protected StudentProfileDBHelper studentProfileDBHelper; protected Toolbar toolbar; @@ -46,7 +50,8 @@ public class ProfileActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_profile); - dbHelper = new AccessDBHelper(getApplicationContext()); + accessDBHelper = new AccessDBHelper(getApplicationContext()); + studentProfileDBHelper = new StudentProfileDBHelper(getApplicationContext()); surnameTextView = findViewById(R.id.surnameTextView); nameTextView = findViewById(R.id.nameTextView); @@ -55,7 +60,7 @@ public class ProfileActivity extends AppCompatActivity { ProfileCreatedTextView = findViewById(R.id.ProfileCreatedTextView); accessListView = findViewById(R.id.accessListView); - toolbar = (Toolbar) findViewById(R.id.toolbar); + toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -65,7 +70,7 @@ public class ProfileActivity extends AppCompatActivity { @Override public void handleOnBackPressed() { Log.d("ProfileActivity", "handleOnBackPressed: Pressed"); - createAccessClosed(profileId, AccessType.CLOSED, LocalDateTime.now()); + createAccessClosed(profileId, LocalDateTime.now()); finish(); } }; @@ -82,6 +87,7 @@ public class ProfileActivity extends AppCompatActivity { gpa = intent.getFloatExtra("gpa", -1); creationDate = intent.getStringExtra("dateCreated"); + // set the text views surnameTextView.setText(surname); nameTextView.setText(name); IDTextView.setText(Long.toString(profileId)); @@ -89,7 +95,7 @@ public class ProfileActivity extends AppCompatActivity { ProfileCreatedTextView.setText(creationDate); if (profileId != -1 || gpa != -1) { - List accessList = dbHelper.getAccessFromProfileID(profileId, getApplicationContext()); + List accessList = accessDBHelper.getAccessFromProfileID(profileId, getApplicationContext()); ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, accessListFromProfileIDToString(accessList)); @@ -99,16 +105,29 @@ public class ProfileActivity extends AppCompatActivity { } } + // handle the case when user pressed on the back (up) button in the action/tool bar @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { - createAccessClosed(profileId, AccessType.CLOSED, LocalDateTime.now()); + createAccessClosed(profileId, LocalDateTime.now()); return false; } return super.onOptionsItemSelected(item); } + // Delete profile and return to Main Activity + public void onDeleteProfileButtonClick(View view) { + studentProfileDBHelper.deleteProfile(profileId, getApplicationContext()); + Log.d("ProfileActivity", "onDeleteProfileButtonClick: Deleted profile" + Long.toString(profileId)); + + Access access = new Access(profileId, AccessType.DELETED, LocalDateTime.now()); + accessDBHelper.insertAccess(access, getApplicationContext()); + Log.d("ProfileActivity", "onDeleteProfileButtonClick: Added access entry DELETE for profile ID: " + Long.toString(profileId)); + + finish(); + } + protected String[] accessListFromProfileIDToString(List accessList) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd @ HH:mm:ss"); @@ -123,8 +142,8 @@ public class ProfileActivity extends AppCompatActivity { return formattedAccessStrings.toArray(new String[0]); } - protected void createAccessClosed(long profileId, AccessType accessType, LocalDateTime timestamp) { - Access access = new Access(profileId, accessType, timestamp); - dbHelper.insertAccess(access, getApplicationContext()); + protected void createAccessClosed(long profileId, LocalDateTime timestamp) { + Access access = new Access(profileId, AccessType.CLOSED, timestamp); + accessDBHelper.insertAccess(access, getApplicationContext()); } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_profile.xml b/app/src/main/res/layout/activity_profile.xml index 4be2195..3e4ee00 100644 --- a/app/src/main/res/layout/activity_profile.xml +++ b/app/src/main/res/layout/activity_profile.xml @@ -1,5 +1,5 @@ - + android:theme="?attr/actionBarTheme"> - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + android:layout_below="@id/toolbar" + android:orientation="vertical"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +