From 0749a4a486b2b15000a550c096df96fa3b815428 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Sun, 15 Nov 2020 02:36:07 -0500 Subject: [PATCH] throwing in the towel --- .../classscheduleapp/CourseActivity.kt | 69 ++++++++++------ .../classscheduleapp/CourseDatabase.kt | 23 ++++-- .../classscheduleapp/CourseDialogFragment.kt | 75 +++++++++++++++--- app/src/main/res/layout/dialog_course.xml | 78 +++++++++++++++++++ 4 files changed, 203 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseActivity.kt b/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseActivity.kt index dbcdd42..094d720 100644 --- a/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseActivity.kt +++ b/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseActivity.kt @@ -27,6 +27,7 @@ class CourseActivity : AppCompatActivity(), CourseDialogFragment.onCourseEntered override fun onCreate(savedInstanceState: Bundle?) { mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this) mDarkTheme = mSharedPrefs!!.getBoolean(SettingsFragment.PREFERENCE_THEME, false) + if (mDarkTheme) { setTheme(R.style.DarkTheme) } @@ -34,8 +35,6 @@ class CourseActivity : AppCompatActivity(), CourseDialogFragment.onCourseEntered super.onCreate(savedInstanceState) setContentView(R.layout.activity_course) - // mDialog = CourseDialogFragment() - mCourseListRecyclerView = findViewById(R.id.courseRecyclerView) mCourseDb = CourseDatabase.getInstance(applicationContext) mCourseListRecyclerView = findViewById(R.id.courseRecyclerView) @@ -63,31 +62,28 @@ class CourseActivity : AppCompatActivity(), CourseDialogFragment.onCourseEntered //Snackbar.make(findViewById(android.R.id.content), "*proceeds to make raspberry noises*", 5000).show() true } - R.id.edit -> { - // up comes a dialog box editing the desired list item - mDialog = CourseDialogFragment() - mDialog.getContext(CourseContext.EDIT, mCourseDb) - mDialog.show(supportFragmentManager, "") - true - } - R.id.delete -> { - // takes out item.itemId aka deletes record - true - } else -> super.onOptionsItemSelected(item) } } - override fun onCourseEntered(course: String) { + override fun onCourseEntered(course: Course) { // Returns subject entered in the SubjectDialogFragment dialog - if (course!!.isNotEmpty()) { - val c = Course(course) - if (mCourseDb.addCourse(c)) { - mCourseAdapter?.addCourse(c) - Toast.makeText(this@CourseActivity, course, Toast.LENGTH_LONG).show() + if (course != null) { + if (course.id == 0) { + if (mCourseDb.addCourse(course)) { + mCourseAdapter?.addCourse(course) + //Snackbar.make(this@CourseActivity, "$course entered!", 5000).show() + } else { + val message = resources.getString("Already exists!".toInt(), course) + Toast.makeText(this@CourseActivity, message, Toast.LENGTH_SHORT).show() + } } else { - val message = resources.getString("Already exists!".toInt(), course) - Toast.makeText(this@CourseActivity, message, Toast.LENGTH_SHORT).show() + if (course.id != 0) { + mCourseDb.updateCourse(course) + mCourseAdapter?.updateCourse(course) + } else { + // ?? + } } } } @@ -112,13 +108,13 @@ class CourseActivity : AppCompatActivity(), CourseDialogFragment.onCourseEntered fun bind(course: Course) { mCourse = course mTextView.text = course.name - mTextView.setBackgroundColor(Color.RED) + mTextView.setBackgroundColor(Color.MAGENTA) } override fun onClick(view: View) { - // Start QuestionActivity, indicating what subject was clicked mDialog = CourseDialogFragment() mDialog.getContext(CourseContext.VIEW, mCourseDb) + mDialog.getCourseId(mCourse!!.id) mDialog.show(supportFragmentManager, "") } @@ -152,7 +148,7 @@ class CourseActivity : AppCompatActivity(), CourseDialogFragment.onCourseEntered override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { // Process action item selection - return when (item.getItemId()) { + return when (item.itemId) { R.id.delete -> { // Delete from the database and remove from the RecyclerView mCourseDb.deleteCourse(mSelectedCourse) @@ -162,12 +158,21 @@ class CourseActivity : AppCompatActivity(), CourseDialogFragment.onCourseEntered mode.finish() true } + R.id.edit -> { + // up comes a dialog box editing the desired list item + mCourseAdapter?.takeCourseIndex() + mDialog = CourseDialogFragment() + mDialog.getContext(CourseContext.EDIT, mCourseDb) + mDialog.getCourseId(mSelectedCourse.id) + mDialog.show(supportFragmentManager, "") + true + } else -> false } } override fun onDestroyActionMode(mode: ActionMode) { - //mActionMode = null + mActionMode = null // CAB closing, need to deselect item if not deleted mCourseAdapter?.notifyItemChanged(mSelectedCoursePosition) @@ -183,6 +188,8 @@ class CourseActivity : AppCompatActivity(), CourseDialogFragment.onCourseEntered } private inner class CourseAdapter(private val mCourseList: MutableList) : RecyclerView.Adapter() { + private var mOldCourseIndex = 0 + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CourseHolder { val layoutInflater = LayoutInflater.from(applicationContext) return CourseHolder(layoutInflater, parent) @@ -196,6 +203,11 @@ class CourseActivity : AppCompatActivity(), CourseDialogFragment.onCourseEntered return mCourseList.size } + fun takeCourseIndex() { + val index = mSelectedCoursePosition + mOldCourseIndex = index + } + fun removeCourse(course: Course?) { // Find subject in the list val index = mCourseList.indexOf(course) @@ -218,5 +230,12 @@ class CourseActivity : AppCompatActivity(), CourseDialogFragment.onCourseEntered // Scroll to the top mCourseListRecyclerView?.scrollToPosition(0) } + + fun updateCourse(course: Course?) { + mCourseList.removeAt(mOldCourseIndex) + mCourseList.add(0, course!!) + notifyItemChanged(0) + mCourseListRecyclerView?.scrollToPosition(0) + } } } \ No newline at end of file diff --git a/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDatabase.kt b/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDatabase.kt index 6a46af5..ba460d6 100644 --- a/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDatabase.kt +++ b/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDatabase.kt @@ -58,15 +58,16 @@ class CourseDatabase(context: Context?) : SQLiteOpenHelper(context, DATABASE_NAM return courses } - fun getCourse(courseId: Long): Course { - val course = Course() + fun getCourse(courseid: Int): Course { + var course = Course() val db = this.readableDatabase - val sql - = "select * from " + CourseTable.TABLE + - "where " + CourseTable.COL_ID + " = ?" - val cursor = db.rawQuery(sql, arrayOf(courseId.toString())) + val sql = + "select * from " + CourseTable.TABLE + + " where " + CourseTable.COL_ID + " = ?" + val cursor = db.rawQuery(sql, arrayOf(java.lang.Float.toString(courseid.toFloat()))) if (cursor.moveToFirst()) { + course.id = cursor.getInt(0) course.name = cursor.getString(1) course.description = cursor.getString(2) course.location = cursor.getString(3) @@ -89,14 +90,20 @@ class CourseDatabase(context: Context?) : SQLiteOpenHelper(context, DATABASE_NAM values.put(CourseTable.COL_TIME, course.time) values.put(CourseTable.COL_INSTRUCTOR, course.instructor) val id = db.insert(CourseTable.TABLE, null, values) + course.id = id.toInt() return id != -1L } fun updateCourse(course: Course) { val db = writableDatabase val values = ContentValues() - - db.update(CourseTable.TABLE, values, CourseTable.COL_NAME + " = ?", arrayOf(course.name)) + values.put(CourseTable.COL_NAME, course.name) + values.put(CourseTable.COL_DESCRIPTION, course.description) + values.put(CourseTable.COL_LOCATION, course.location) + values.put(CourseTable.COL_DAYOFWEEK, course.dayofweek) + values.put(CourseTable.COL_TIME, course.time) + values.put(CourseTable.COL_INSTRUCTOR, course.instructor) + db.update(CourseTable.TABLE, values, CourseTable.COL_ID + " = " + course.id, null) } fun deleteCourse(course: Course) { diff --git a/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDialogFragment.kt b/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDialogFragment.kt index a41a534..2cd84ff 100644 --- a/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDialogFragment.kt +++ b/app/src/main/java/com/wyattjmiller/classscheduleapp/CourseDialogFragment.kt @@ -3,6 +3,8 @@ package com.wyattjmiller.classscheduleapp import android.app.Dialog import android.content.Context import android.os.Bundle +import android.text.Editable +import android.view.View import android.widget.Button import android.widget.EditText import android.widget.TextView @@ -20,10 +22,19 @@ class CourseDialogFragment : DialogFragment() { private lateinit var mEditTextCourseDescription: EditText private lateinit var mContext: CourseContext private lateinit var mCourseDb: CourseDatabase + + private lateinit var mCourseNameView: TextView + private lateinit var mCourseLocationView: TextView + private lateinit var mCourseTimeView: TextView + private lateinit var mCourseDateView: TextView + private lateinit var mCourseInstructor: TextView + private lateinit var mCourseDescriptionView: TextView + + private var mCourse: Course = Course() interface onCourseEnteredListener { - fun onCourseEntered(course: String) + fun onCourseEntered(course: Course) } private lateinit var mDoneButton: Button @@ -42,15 +53,59 @@ class CourseDialogFragment : DialogFragment() { mEditTextCourseTime = dialogView.findViewById(R.id.editTextCourseTime) mEditTextCourseDescription = dialogView.findViewById(R.id.editTextCourseDescription) + mCourseNameView = dialogView.findViewById(R.id.textviewCourseName2) + mCourseLocationView = dialogView.findViewById(R.id.textViewCourseLocation2) + mCourseDateView = dialogView.findViewById(R.id.textViewCourseDayOfWeek2) + mCourseTimeView = dialogView.findViewById(R.id.textViewCourseTime2) + mCourseInstructor = dialogView.findViewById(R.id.textViewCourseInstructor2) + mCourseDescriptionView = dialogView.findViewById(R.id.textViewCourseDescription2) + + mCourseNameView.visibility = View.GONE + mCourseLocationView.visibility = View.GONE + mCourseDateView.visibility = View.GONE + mCourseTimeView.visibility = View.GONE + mCourseInstructor.visibility = View.GONE + mCourseDescriptionView.visibility = View.GONE + when (mContext) { CourseContext.ADD -> { - // TODO: set title to add course and when pressing done, add to database + mTextViewTitle.text = "Add Course" } CourseContext.EDIT -> { - // TODO: set title to edit course and when pressing done, modify record to database + var course = mCourseDb.getCourse(mCourse.id) + + mTextViewTitle.text = "Edit Course" + mEditTextCourseName.setText(course.name) + mEditTextCourseLocation.setText(course.location) + mEditTextCourseInstructor.setText(course.instructor) + mEditTextDayOfWeek.setText(course.dayofweek) + mEditTextCourseTime.setText(course.time) + mEditTextCourseDescription.setText(course.description) } CourseContext.VIEW -> { - // TODO: set title to view course, make everything text, and when pressing done, close dialog + val course = mCourseDb.getCourse(mCourse.id) + + mEditTextCourseName.visibility = View.INVISIBLE + mEditTextCourseLocation.visibility = View.INVISIBLE + mEditTextCourseInstructor.visibility = View.INVISIBLE + mEditTextDayOfWeek.visibility = View.INVISIBLE + mEditTextCourseTime.visibility = View.INVISIBLE + mEditTextCourseDescription.visibility = View.INVISIBLE + + mCourseNameView.visibility = View.VISIBLE + mCourseLocationView.visibility = View.VISIBLE + mCourseDateView.visibility = View.VISIBLE + mCourseTimeView.visibility = View.VISIBLE + mCourseInstructor.visibility = View.VISIBLE + mCourseDescriptionView.visibility = View.VISIBLE + + mTextViewTitle.text = "View Course" + mCourseNameView.text = course.name + mCourseLocationView.text = course.location + mCourseDateView.text = course.dayofweek + mCourseTimeView.text = course.time + mCourseInstructor.text = course.instructor + mCourseDescriptionView.text = course.description } } @@ -68,8 +123,7 @@ class CourseDialogFragment : DialogFragment() { val valid = this.validityCheck() if (valid) { - val course = mCourse.name - mListener?.onCourseEntered(course!!.trim()) + mListener?.onCourseEntered(mCourse) dismiss() } } @@ -83,8 +137,7 @@ class CourseDialogFragment : DialogFragment() { val valid = this.validityCheck() if (valid) { - val course = mCourse.name - mListener?.onCourseEntered(course!!.trim()) + mListener?.onCourseEntered(mCourse) dismiss() } } @@ -102,6 +155,10 @@ class CourseDialogFragment : DialogFragment() { mCourseDb = database } + fun getCourseId(courseId: Int) { + mCourse.id = courseId + } + override fun onAttach(context: Context) { super.onAttach(context) mListener = context as onCourseEnteredListener @@ -118,7 +175,7 @@ class CourseDialogFragment : DialogFragment() { } companion object { - const val EXTRA_COURSE_ID = "com.wyattjmiller.classscheduleapp.course_id" + const val EXTRA_COURSE = "com.wyattjmiller.classscheduleapp.course" } } \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_course.xml b/app/src/main/res/layout/dialog_course.xml index bfccb1c..2a0a51e 100644 --- a/app/src/main/res/layout/dialog_course.xml +++ b/app/src/main/res/layout/dialog_course.xml @@ -31,6 +31,18 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/textViewTitle" /> + + + + + + @@ -108,6 +145,18 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/editTextCourseDayOfWeek" /> + + + +