Feat(main): Create profile\

This commit is contained in:
minhtrannhat 2023-11-05 14:31:15 -05:00
parent b320448a60
commit 6b11635544
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
10 changed files with 268 additions and 96 deletions

View File

@ -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<StudentProfile> getAllStudentProfile() {
public List<StudentProfile> getAllStudentProfile(Context context) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
List<StudentProfile> studentProfiles = new ArrayList<>();

View File

@ -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";
}
}

View File

@ -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";
}
}

View File

@ -0,0 +1,10 @@
package com.example.coen390_assignment2.Models;
import java.util.Comparator;
public class StudentProfileIDComparator implements Comparator<StudentProfile> {
@Override
public int compare(StudentProfile profile1, StudentProfile profile2) {
return Long.compare(profile1.getProfileID(), profile2.getProfileID());
}
}

View File

@ -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;
}
}

View File

@ -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<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") );
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<StudentProfile> studentProfiles, boolean profileNameDisplayMode){
// Create a new list for sorted profiles
List<StudentProfile> 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

View File

@ -1,56 +0,0 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/profile_surname_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginTop="16dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:hint="@string/surname_dialog_text"
android:inputType="textPersonName" />
<EditText
android:id="@+id/profile_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:hint="@string/name_dialog_text"
android:inputType="textPersonName" />
<EditText
android:id="@+id/profile_ID_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/ID_dialog_text"
android:inputType="number" />
<EditText
android:id="@+id/profile_GPA_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/GPA_dialog_text"
android:inputType="numberDecimal" />
</LinearLayout>

View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Views.InsertProfileDialogFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/profile_surname_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:hint="@string/surname_dialog_text"
android:inputType="textPersonName" />
<EditText
android:id="@+id/profile_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:ems="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profile_surname_edit_text"
android:fontFamily="sans-serif"
android:hint="@string/name_dialog_text"
android:inputType="textPersonName" />
<EditText
android:id="@+id/profile_ID_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:ems="10"
android:hint="@string/ID_dialog_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profile_name_edit_text"
android:inputType="number" />
<EditText
android:id="@+id/profile_GPA_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profile_ID_edit_text"
android:fontFamily="sans-serif"
android:hint="@string/GPA_dialog_text"
android:inputType="numberDecimal" />
<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/profile_GPA_edit_text"
app:layout_constraintTop_toBottomOf="@+id/profile_GPA_edit_text"
android:text="@string/cancel"/>
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="@+id/profile_GPA_edit_text"
app:layout_constraintTop_toBottomOf="@+id/profile_GPA_edit_text"
android:text="@string/save"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View File

@ -14,4 +14,6 @@
<string name="id_text">ID: </string>
<string name="name_text">Name: </string>
<string name="surname_text">Surname: </string>
<string name="cancel">Cancel</string>
<string name="save">Save</string>
</resources>