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;
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<Access> getAccessFromProfileID(long profileID, Context context) {

View File

@ -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<StudentProfile> 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();
}
}
}

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 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

View File

@ -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<StudentProfile> studentProfiles = studentProfileDBHelper.getAllStudentProfile(getApplicationContext());
// by default sort by surname
ArrayAdapter<String> 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<StudentProfile> studentProfiles, boolean profileNameDisplayMode){
protected String[] showStudentProfileList(List<StudentProfile> studentProfiles, boolean profileNameDisplayMode) {
// Create a new list for sorted profiles
List<StudentProfile> 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());
}

View File

@ -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<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));
@ -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<Access> 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());
}
}

View File

@ -1,5 +1,5 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -12,151 +12,170 @@
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme" />
android:theme="?attr/actionBarTheme">
<TextView
android:id="@+id/profileText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingTop="10dp"
android:paddingEnd="10dp"
android:text="@string/user_profile_text"
android:textSize="30sp" />
</androidx.appcompat.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/surnameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/surname_text"
android:textSize="20sp" />
<TextView
android:id="@+id/surnameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/name_text"
android:textSize="20sp" />
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/IDText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/id_text"
android:textSize="20sp" />
<TextView
android:id="@+id/IDTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/GPAText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/gpa_text"
android:textSize="20sp" />
<TextView
android:id="@+id/GPATextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/createdText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:paddingBottom="10dp"
android:text="@string/profile_created_text"
android:textSize="20sp" />
<TextView
android:id="@+id/ProfileCreatedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="@+id/AccessHistoryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingTop="10dp"
android:paddingEnd="10dp"
android:paddingBottom="10dp"
android:text="@string/access_history"
android:textSize="20sp" />
<ListView
android:id="@+id/accessListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="10dp"
android:paddingEnd="10dp" />
android:layout_below="@id/toolbar"
android:orientation="vertical">
</LinearLayout>
<TextView
android:id="@+id/profileText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingTop="10dp"
android:paddingEnd="10dp"
android:text="@string/user_profile_text"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/surnameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/surname_text"
android:textSize="20sp" />
<TextView
android:id="@+id/surnameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/name_text"
android:textSize="20sp" />
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/IDText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/id_text"
android:textSize="20sp" />
<TextView
android:id="@+id/IDTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/GPAText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/gpa_text"
android:textSize="20sp" />
<TextView
android:id="@+id/GPATextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/createdText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:paddingBottom="10dp"
android:text="@string/profile_created_text"
android:textSize="20sp" />
<TextView
android:id="@+id/ProfileCreatedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="@+id/AccessHistoryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingTop="10dp"
android:paddingEnd="10dp"
android:paddingBottom="10dp"
android:text="@string/access_history"
android:textSize="20sp" />
<ListView
android:id="@+id/accessListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="10dp"
android:paddingEnd="10dp" />
</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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
tools:context=".Views.InsertProfileDialogFragment">
<androidx.constraintlayout.widget.ConstraintLayout

View File

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