From 6b1163554493d4c85ba1c359b23a7944e422f80e Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Sun, 5 Nov 2023 14:31:15 -0500 Subject: [PATCH] Feat(main): Create profile\ --- .../Controllers/StudentProfileDBHelper.java | 5 +- .../Models/AccessContract.java | 8 +- .../Models/StudentProfile.java | 2 +- .../Models/StudentProfileContract.java | 8 +- .../Models/StudentProfileIDComparator.java | 10 ++ .../Views/InsertProfileDialogFragment.java | 99 ++++++++++++++----- .../Views/MainActivity.java | 93 ++++++++++++++++- .../main/res/layout/dialog_profile_create.xml | 56 ----------- .../layout/fragment_insert_profile_dialog.xml | 81 +++++++++++++++ app/src/main/res/values/strings.xml | 2 + 10 files changed, 268 insertions(+), 96 deletions(-) create mode 100644 app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileIDComparator.java delete mode 100644 app/src/main/res/layout/dialog_profile_create.xml create mode 100644 app/src/main/res/layout/fragment_insert_profile_dialog.xml 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 c2a8fa9..f3f6a66 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,6 +9,7 @@ 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; @@ -43,7 +44,7 @@ public class StudentProfileDBHelper extends SQLiteOpenHelper { onCreate(db); } - public long insertStudentProfile(StudentProfile studentProfile) { + public long insertStudentProfile(StudentProfile studentProfile, Context context) { long id = -1; SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); @@ -65,7 +66,7 @@ public class StudentProfileDBHelper extends SQLiteOpenHelper { return id; } - public List getAllStudentProfile() { + public List getAllStudentProfile(Context context) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = null; List studentProfiles = new ArrayList<>(); diff --git a/app/src/main/java/com/example/coen390_assignment2/Models/AccessContract.java b/app/src/main/java/com/example/coen390_assignment2/Models/AccessContract.java index ec2f594..1840242 100644 --- a/app/src/main/java/com/example/coen390_assignment2/Models/AccessContract.java +++ b/app/src/main/java/com/example/coen390_assignment2/Models/AccessContract.java @@ -6,11 +6,11 @@ public final class AccessContract { private AccessContract(){} public static class AccessEntry implements BaseColumns{ - public static final String DATABASE_NAME = "access-entry-db"; + public static final String DATABASE_NAME = "access_entry_db"; public static final String TABLE_NAME = "access"; - public static final String COLUMN_NAME_ACCESS_ID = "access-id"; - public static final String COLUMN_NAME_PROFILE_ID = "profile-id"; - public static final String COLUMN_NAME_ACCESS_TYPE = "access-type"; + public static final String COLUMN_NAME_ACCESS_ID = "access_id"; + public static final String COLUMN_NAME_PROFILE_ID = "profile_id"; + public static final String COLUMN_NAME_ACCESS_TYPE = "access_type"; public static final String COLUMN_NAME_TIMESTAMP = "timestamp"; } } diff --git a/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfile.java b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfile.java index dea8390..de3b4f0 100644 --- a/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfile.java +++ b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfile.java @@ -52,4 +52,4 @@ public class StudentProfile { public LocalDate getProfileCreationDate() { return profileCreationDate; } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileContract.java b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileContract.java index cc3ff97..213be22 100644 --- a/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileContract.java +++ b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileContract.java @@ -11,12 +11,12 @@ public final class StudentProfileContract { /* Inner class that defines the table contents */ public static class StudentProfileEntry implements BaseColumns { - public static final String DATABASE_NAME = "student-profile-db"; + public static final String DATABASE_NAME = "student_profile_db"; public static final String TABLE_NAME = "profile"; - public static final String COLUMN_NAME_PROFILE_ID = "profile-id"; + public static final String COLUMN_NAME_PROFILE_ID = "profile_id"; public static final String COLUMN_NAME_SURNAME = "surname"; public static final String COLUMN_NAME_NAME = "name"; - public static final String COLUMN_NAME_PROFILE_GPA = "profile-gpa"; - public static final String COLUMN_NAME_PROFILE_CREATION_DATE = "profile-creation-date"; + public static final String COLUMN_NAME_PROFILE_GPA = "profile_gpa"; + public static final String COLUMN_NAME_PROFILE_CREATION_DATE = "profile_creation_date"; } } diff --git a/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileIDComparator.java b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileIDComparator.java new file mode 100644 index 0000000..40f96f3 --- /dev/null +++ b/app/src/main/java/com/example/coen390_assignment2/Models/StudentProfileIDComparator.java @@ -0,0 +1,10 @@ +package com.example.coen390_assignment2.Models; + +import java.util.Comparator; + +public class StudentProfileIDComparator implements Comparator { + @Override + public int compare(StudentProfile profile1, StudentProfile profile2) { + return Long.compare(profile1.getProfileID(), profile2.getProfileID()); + } +} 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 338b34e..8d0c328 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 @@ -1,40 +1,89 @@ package com.example.coen390_assignment2.Views; -import android.app.Dialog; -import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.DialogFragment; +import com.example.coen390_assignment2.Controllers.StudentProfileDBHelper; +import com.example.coen390_assignment2.Models.StudentProfile; import com.example.coen390_assignment2.R; +import java.time.LocalDate; + public class InsertProfileDialogFragment extends DialogFragment { - @NonNull - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - // Get the layout inflater. - LayoutInflater inflater = requireActivity().getLayoutInflater(); + private EditText profile_surname_edit_text, profile_name_edit_text, profile_ID_edit_text, profile_GPA_edit_text; - // Inflate and set the layout for the dialog. - // Pass null as the parent view because it's going in the dialog layout. - builder.setView(inflater.inflate(R.layout.dialog_profile_create, null)) - // Add action buttons - .setPositiveButton(R.string.save_text_dialog_fragment, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int id) { - // Sign in the user. + private Button cancelButton, saveButton; + + public InsertProfileDialogFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View view = inflater.inflate(R.layout.fragment_insert_profile_dialog, container, false); + + profile_surname_edit_text = view.findViewById(R.id.profile_surname_edit_text); + profile_name_edit_text = view.findViewById(R.id.profile_name_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); + + cancelButton = view.findViewById(R.id.cancelButton); + saveButton = view.findViewById(R.id.saveButton); + + cancelButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }); + + saveButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String surname = profile_surname_edit_text.getText().toString(); + String name = profile_name_edit_text.getText().toString(); + + String ID = profile_ID_edit_text.getText().toString(); + String GPA = profile_GPA_edit_text.getText().toString(); + + try { + float gpa = Float.parseFloat(GPA); + if ( + !(surname.isEmpty() || + name.isEmpty() || + ID.isEmpty() || + GPA.isEmpty() || + ID.length() != 8 || + (gpa <= 0.0f || gpa >= 4.3f))) { + + long id = Long.parseLong(ID); + + StudentProfile profile = new StudentProfile(surname, name, id, gpa, LocalDate.now()); + StudentProfileDBHelper dbHelper = new StudentProfileDBHelper(getActivity()); + dbHelper.insertStudentProfile(profile, getContext()); + + ((MainActivity) getActivity()).onStart(); + + dismiss(); + } else { + Toast.makeText(getContext(), "Invalid input!", Toast.LENGTH_LONG).show(); } - }) - .setNegativeButton(R.string.cancel_text_dialog_fragment, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - InsertProfileDialogFragment.this.getDialog().cancel(); - } - }); - return builder.create(); + } catch (NumberFormatException e) { + Toast.makeText(getContext(), "Invalid input!", Toast.LENGTH_LONG).show(); + } + } + }); + + return view; } } 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 134bddc..94a7fa1 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 @@ -2,38 +2,123 @@ package com.example.coen390_assignment2.Views; import android.os.Bundle; import android.view.Menu; +import android.view.MenuItem; import android.view.View; +import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.ListView; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; +import com.example.coen390_assignment2.Controllers.StudentProfileDBHelper; +import com.example.coen390_assignment2.Models.StudentProfile; +import com.example.coen390_assignment2.Models.StudentProfileIDComparator; +import com.example.coen390_assignment2.Models.StudentProfileSurnameComparator; import com.example.coen390_assignment2.R; +import java.util.ArrayList; +import java.util.List; + public class MainActivity extends AppCompatActivity { + + protected StudentProfileDBHelper studentProfileDBHelper; + + protected Toolbar toolbar; + protected Button showDialogButton; + + protected ListView studentProfileList; + + protected boolean profileNameDisplayMode; + + protected MenuItem toggle; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Toolbar toolbar = findViewById(R.id.toolbar); - Button showDialogButton = findViewById(R.id.add_profile_action_button); + toolbar = findViewById(R.id.toolbar); + showDialogButton = findViewById(R.id.add_profile_action_button); + studentProfileList = findViewById(R.id.profilesListView); // Initialize the ActionBar (Toolbar) setSupportActionBar(toolbar); - // TODO replace with actual database info - toolbar.setSubtitle("New Subtitle Text !!!!!!!!!!!!!!"); + studentProfileDBHelper = new StudentProfileDBHelper(getApplicationContext()); initAddProfileActionButton(showDialogButton); } + @Override + protected void onStart() { + super.onStart(); + + // surname when true, ID when false + profileNameDisplayMode = true; + + 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") ); + + studentProfileList.setAdapter(adapter); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.settings_main_activity, menu); + toggle = menu.findItem(R.id.toggle_profiles_display_mode); return true; } + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + // toggle to the next mode + if (profileNameDisplayMode){ + toggle.setTitle("By ID"); + } else{ + toggle.setTitle("By Surname"); + } + return true; + } + + protected String[] showStudentProfileList(List studentProfiles, boolean profileNameDisplayMode){ + // Create a new list for sorted profiles + List sortedProfiles = new ArrayList<>(studentProfiles); + + // Create a String array to hold the formatted strings + String[] profileListToString = new String[sortedProfiles.size()]; + + // sort by surname + if (profileNameDisplayMode){ + // Sort the new list based on the "surname" field + sortedProfiles.sort(new StudentProfileSurnameComparator()); + + for (int i = 0; i < sortedProfiles.size(); i++) { + StudentProfile profile = sortedProfiles.get(i); + String formattedString = i + ", " + profile.getSurname() + ", " + profile.getName(); + profileListToString[i] = formattedString; + } + } + + // sort by ID + else { + sortedProfiles.sort(new StudentProfileIDComparator()); + + for (int i = 0; i < sortedProfiles.size(); i++) { + StudentProfile profile = sortedProfiles.get(i); + String formattedString = i + ", " + profile.getProfileID(); + profileListToString[i] = formattedString; + } + } + + return profileListToString; + } + protected void initAddProfileActionButton(Button button) { button.setOnClickListener(new View.OnClickListener() { @Override diff --git a/app/src/main/res/layout/dialog_profile_create.xml b/app/src/main/res/layout/dialog_profile_create.xml deleted file mode 100644 index 6381abe..0000000 --- a/app/src/main/res/layout/dialog_profile_create.xml +++ /dev/null @@ -1,56 +0,0 @@ - - - - - - - - - - diff --git a/app/src/main/res/layout/fragment_insert_profile_dialog.xml b/app/src/main/res/layout/fragment_insert_profile_dialog.xml new file mode 100644 index 0000000..c33b294 --- /dev/null +++ b/app/src/main/res/layout/fragment_insert_profile_dialog.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + +