This commit is contained in:
minhtrannhat 2023-11-06 21:29:45 -05:00
parent fc7fb5bd88
commit 9c001d1943
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
8 changed files with 222 additions and 174 deletions

View File

@ -1,6 +1,5 @@
package com.example.coen390_assignment2.Controllers; package com.example.coen390_assignment2.Controllers;
import android.annotation.SuppressLint;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
@ -21,7 +20,6 @@ import java.util.List;
public class AccessDBHelper extends SQLiteOpenHelper { public class AccessDBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1; private static final int DATABASE_VERSION = 1;
private Context context = null;
public AccessDBHelper(@Nullable Context context) { public AccessDBHelper(@Nullable Context context) {
super(context, AccessContract.AccessEntry.DATABASE_NAME, null, DATABASE_VERSION); super(context, AccessContract.AccessEntry.DATABASE_NAME, null, DATABASE_VERSION);
@ -46,7 +44,7 @@ public class AccessDBHelper extends SQLiteOpenHelper {
onCreate(db); onCreate(db);
} }
public long insertAccess(Access access, Context context) { public void insertAccess(Access access, Context context) {
long id = -1; long id = -1;
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
@ -62,7 +60,6 @@ public class AccessDBHelper extends SQLiteOpenHelper {
} finally { } finally {
db.close(); db.close();
} }
return id;
} }
public List<Access> getAccessFromProfileID(long profileID, Context context) { public List<Access> getAccessFromProfileID(long profileID, Context context) {

View File

@ -9,7 +9,6 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.example.coen390_assignment2.Models.StudentProfile; import com.example.coen390_assignment2.Models.StudentProfile;
@ -22,7 +21,6 @@ import java.util.List;
public class StudentProfileDBHelper extends SQLiteOpenHelper { public class StudentProfileDBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1; private static final int DATABASE_VERSION = 1;
private Context context = null;
public StudentProfileDBHelper(@Nullable Context context) { public StudentProfileDBHelper(@Nullable Context context) {
super(context, StudentProfileContract.StudentProfileEntry.DATABASE_NAME, null, DATABASE_VERSION); super(context, StudentProfileContract.StudentProfileEntry.DATABASE_NAME, null, DATABASE_VERSION);
@ -44,7 +42,7 @@ public class StudentProfileDBHelper extends SQLiteOpenHelper {
onCreate(db); onCreate(db);
} }
public long insertStudentProfile(StudentProfile studentProfile, Context context) { public void insertStudentProfile(StudentProfile studentProfile, Context context) {
long id = -1; long id = -1;
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
@ -65,7 +63,6 @@ public class StudentProfileDBHelper extends SQLiteOpenHelper {
} finally { } finally {
db.close(); db.close();
} }
return id;
} }
public List<StudentProfile> getAllStudentProfile(Context context) { public List<StudentProfile> getAllStudentProfile(Context context) {
@ -98,4 +95,21 @@ public class StudentProfileDBHelper extends SQLiteOpenHelper {
} }
return studentProfiles; 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();
}
}
} }

View File

@ -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 EditText profile_surname_edit_text, profile_name_edit_text, profile_ID_edit_text, profile_GPA_edit_text;
private Button cancelButton, saveButton;
public InsertProfileDialogFragment() { public InsertProfileDialogFragment() {
// Required empty public constructor // 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_ID_edit_text = view.findViewById(R.id.profile_ID_edit_text);
profile_GPA_edit_text = view.findViewById(R.id.profile_GPA_edit_text); profile_GPA_edit_text = view.findViewById(R.id.profile_GPA_edit_text);
cancelButton = view.findViewById(R.id.cancelButton); Button cancelButton = view.findViewById(R.id.cancelButton);
saveButton = view.findViewById(R.id.saveButton); Button saveButton = view.findViewById(R.id.saveButton);
cancelButton.setOnClickListener(new View.OnClickListener() { cancelButton.setOnClickListener(new View.OnClickListener() {
@Override @Override

View File

@ -1,6 +1,5 @@
package com.example.coen390_assignment2.Views; package com.example.coen390_assignment2.Views;
import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
@ -10,7 +9,6 @@ import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
@ -143,6 +141,7 @@ public class MainActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
protected String[] showStudentProfileList(List<StudentProfile> studentProfiles, boolean profileNameDisplayMode) { protected String[] showStudentProfileList(List<StudentProfile> studentProfiles, boolean profileNameDisplayMode) {
// Create a new list for sorted profiles // Create a new list for sorted profiles
List<StudentProfile> sortedProfiles = new ArrayList<>(studentProfiles); List<StudentProfile> sortedProfiles = new ArrayList<>(studentProfiles);

View File

@ -5,6 +5,7 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
@ -15,6 +16,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import com.example.coen390_assignment2.Controllers.AccessDBHelper; 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.Access;
import com.example.coen390_assignment2.Models.AccessType; import com.example.coen390_assignment2.Models.AccessType;
import com.example.coen390_assignment2.R; import com.example.coen390_assignment2.R;
@ -34,7 +36,9 @@ public class ProfileActivity extends AppCompatActivity {
protected TextView surnameTextView, nameTextView, IDTextView, GPATextView, ProfileCreatedTextView; protected TextView surnameTextView, nameTextView, IDTextView, GPATextView, ProfileCreatedTextView;
protected AccessDBHelper dbHelper; protected AccessDBHelper accessDBHelper;
protected StudentProfileDBHelper studentProfileDBHelper;
protected Toolbar toolbar; protected Toolbar toolbar;
@ -46,7 +50,8 @@ public class ProfileActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile); setContentView(R.layout.activity_profile);
dbHelper = new AccessDBHelper(getApplicationContext()); accessDBHelper = new AccessDBHelper(getApplicationContext());
studentProfileDBHelper = new StudentProfileDBHelper(getApplicationContext());
surnameTextView = findViewById(R.id.surnameTextView); surnameTextView = findViewById(R.id.surnameTextView);
nameTextView = findViewById(R.id.nameTextView); nameTextView = findViewById(R.id.nameTextView);
@ -55,7 +60,7 @@ public class ProfileActivity extends AppCompatActivity {
ProfileCreatedTextView = findViewById(R.id.ProfileCreatedTextView); ProfileCreatedTextView = findViewById(R.id.ProfileCreatedTextView);
accessListView = findViewById(R.id.accessListView); accessListView = findViewById(R.id.accessListView);
toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -65,7 +70,7 @@ public class ProfileActivity extends AppCompatActivity {
@Override @Override
public void handleOnBackPressed() { public void handleOnBackPressed() {
Log.d("ProfileActivity", "handleOnBackPressed: Pressed"); Log.d("ProfileActivity", "handleOnBackPressed: Pressed");
createAccessClosed(profileId, AccessType.CLOSED, LocalDateTime.now()); createAccessClosed(profileId, LocalDateTime.now());
finish(); finish();
} }
}; };
@ -82,6 +87,7 @@ public class ProfileActivity extends AppCompatActivity {
gpa = intent.getFloatExtra("gpa", -1); gpa = intent.getFloatExtra("gpa", -1);
creationDate = intent.getStringExtra("dateCreated"); creationDate = intent.getStringExtra("dateCreated");
// set the text views
surnameTextView.setText(surname); surnameTextView.setText(surname);
nameTextView.setText(name); nameTextView.setText(name);
IDTextView.setText(Long.toString(profileId)); IDTextView.setText(Long.toString(profileId));
@ -89,7 +95,7 @@ public class ProfileActivity extends AppCompatActivity {
ProfileCreatedTextView.setText(creationDate); ProfileCreatedTextView.setText(creationDate);
if (profileId != -1 || gpa != -1) { if (profileId != -1 || gpa != -1) {
List<Access> accessList = dbHelper.getAccessFromProfileID(profileId, getApplicationContext()); List<Access> accessList = accessDBHelper.getAccessFromProfileID(profileId, getApplicationContext());
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, accessListFromProfileIDToString(accessList)); ArrayAdapter<String> 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 @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId(); int id = item.getItemId();
if (id == android.R.id.home) { if (id == android.R.id.home) {
createAccessClosed(profileId, AccessType.CLOSED, LocalDateTime.now()); createAccessClosed(profileId, LocalDateTime.now());
return false; return false;
} }
return super.onOptionsItemSelected(item); 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<Access> accessList) { protected String[] accessListFromProfileIDToString(List<Access> accessList) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd @ HH:mm:ss"); 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]); return formattedAccessStrings.toArray(new String[0]);
} }
protected void createAccessClosed(long profileId, AccessType accessType, LocalDateTime timestamp) { protected void createAccessClosed(long profileId, LocalDateTime timestamp) {
Access access = new Access(profileId, accessType, timestamp); Access access = new Access(profileId, AccessType.CLOSED, timestamp);
dbHelper.insertAccess(access, getApplicationContext()); accessDBHelper.insertAccess(access, getApplicationContext());
} }
} }

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -12,7 +12,16 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme" /> android:theme="?attr/actionBarTheme">
</androidx.appcompat.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
android:orientation="vertical">
<TextView <TextView
android:id="@+id/profileText" android:id="@+id/profileText"
@ -158,5 +167,15 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:paddingStart="10dp" android:paddingStart="10dp"
android:paddingEnd="10dp" /> android:paddingEnd="10dp" />
</LinearLayout> </LinearLayout>
<Button
android:id="@+id/delete_profile_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
android:onClick="onDeleteProfileButtonClick"
android:text="@string/delete_profile" />
</RelativeLayout>

View File

@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:padding="15dp"
tools:context=".Views.InsertProfileDialogFragment"> tools:context=".Views.InsertProfileDialogFragment">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout

View File

@ -17,4 +17,5 @@
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>
<string name="save">Save</string> <string name="save">Save</string>
<string name="access_history">Access History</string> <string name="access_history">Access History</string>
<string name="delete_profile">Delete Profile</string>
</resources> </resources>