Skip to content

Commit

Permalink
Merge pull request #32 from milan-codes/asc-order
Browse files Browse the repository at this point in the history
Fixed #28 #30 #31
  • Loading branch information
milan-codes authored Jun 7, 2020
2 parents 1575b1c + 9cfc831 commit b64cc1c
Show file tree
Hide file tree
Showing 20 changed files with 212 additions and 74 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "app.milanherke.mystudiez"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
versionCode 2
versionName "1.0.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
36 changes: 29 additions & 7 deletions app/src/main/java/app/milanherke/mystudiez/AddEditExamFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import app.milanherke.mystudiez.CalendarUtils.Companion.CalendarInteractions
import app.milanherke.mystudiez.SharedViewModel.RetrievingData
import com.google.firebase.database.DatabaseError
import kotlinx.android.synthetic.main.activity_main.*
Expand All @@ -36,6 +37,8 @@ class AddEditExamFragment : Fragment() {

private var exam: Exam? = null
private var subject: Subject? = null
private var examDate: Date? = null
private var reminder: Date? = null
private var listener: ExamSaved? = null
private var listOfSubjects: MutableMap<String, Subject>? = null
private var selectedSubjectId: String? = null
Expand Down Expand Up @@ -74,6 +77,14 @@ class AddEditExamFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
exam = arguments?.getParcelable(ARG_EXAM)

// Avoiding problems with smart-cast
val exam = exam
if (exam != null) {
examDate = exam.date
reminder = exam.reminder
}

subject = arguments?.getParcelable(ARG_SUBJECT)
}

Expand All @@ -90,6 +101,7 @@ class AddEditExamFragment : Fragment() {

// Avoiding problems with smart cast
val exam = exam
val examReminder = exam?.reminder
val subject = subject
val listOfSubjects = listOfSubjects

Expand Down Expand Up @@ -125,9 +137,8 @@ class AddEditExamFragment : Fragment() {
resources.getDrawable(R.drawable.circular_disabled_button, null)
new_exam_subject_btn.setTextColor(resources.getColor(R.color.colorTextSecondary, null))
new_exam_subject_btn.isEnabled = false
new_exam_date_btn.text = exam.date
new_exam_reminder_btn.text = exam.reminder

new_exam_date_btn.text = CalendarUtils.dateToString(exam.date, false)
new_exam_reminder_btn.text = if (examReminder == null) getString(R.string.add_edit_lesson_btn) else CalendarUtils.dateToString(examReminder, true)
} else if (exam == null && subject != null) {
// Fragment called from SubjectDetailsFragment
// User wants to create a new exam
Expand Down Expand Up @@ -184,12 +195,22 @@ class AddEditExamFragment : Fragment() {
}
}

val calendarListener : CalendarInteractions = object: CalendarInteractions {
override fun onDateSet(date: Date) {
examDate = date
}

override fun onTimeSet(date: Date) {
reminder = date
}
}

// User must set the date for the exam
new_exam_date_btn.setOnClickListener {
val cal = Calendar.getInstance()
DatePickerDialog(
context!!,
CalendarUtils.getDateSetListener(activity!!, R.id.new_exam_date_btn, cal),
CalendarUtils.getDateSetListener(activity!!, R.id.new_exam_date_btn, cal, calendarListener),
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)
Expand All @@ -201,7 +222,7 @@ class AddEditExamFragment : Fragment() {
val cal = Calendar.getInstance()
TimePickerDialog(
context,
CalendarUtils.getTimeSetListener(activity!!, R.id.new_exam_reminder_btn, cal, true),
CalendarUtils.getTimeSetListener(activity!!, R.id.new_exam_reminder_btn, cal, true, calendarListener),
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
true
Expand Down Expand Up @@ -285,8 +306,8 @@ class AddEditExamFragment : Fragment() {
new_exam_name.text.toString(),
new_exam_desc.text.toString(),
selectedSubjectId ?: (subject?.id ?: ""),
new_exam_date_btn.text.toString(),
if (new_exam_reminder_btn.text.toString() != getString(R.string.add_edit_lesson_btn)) new_exam_reminder_btn.text.toString() else ""
examDate!!,
reminder
)
exam.id = this.exam?.id ?: ""
return exam
Expand All @@ -299,6 +320,7 @@ class AddEditExamFragment : Fragment() {
*/
private fun requiredFieldsAreFilled(): Boolean {
if (new_exam_name.text.isNotEmpty()
&& new_exam_subject_btn.text != getString(R.string.add_edit_lesson_btn)
&& new_exam_subject_btn.text.isNotEmpty()
&& new_exam_date_btn.text != getString(R.string.add_edit_lesson_btn)
&& new_exam_date_btn.text.isNotEmpty()
Expand Down
38 changes: 30 additions & 8 deletions app/src/main/java/app/milanherke/mystudiez/AddEditTaskFragment.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package app.milanherke.mystudiez

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -13,6 +14,7 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import app.milanherke.mystudiez.CalendarUtils.Companion.CalendarInteractions
import app.milanherke.mystudiez.Fragments.TASK_DETAILS
import com.google.firebase.database.DatabaseError
import kotlinx.android.synthetic.main.activity_main.*
Expand All @@ -35,6 +37,8 @@ private const val ARG_SUBJECT = "subject"
class AddEditTaskFragment : Fragment() {
private var task: Task? = null
private var subject: Subject? = null
private var dueDate: Date? = null
private var reminder: Date? = null
private var listener: TaskSaved? = null
private var listOfSubjects: MutableMap<String, Subject>? = null
private var selectedSubjectId: String? = null
Expand Down Expand Up @@ -75,6 +79,14 @@ class AddEditTaskFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
task = arguments?.getParcelable(ARG_TASK)

// Avoiding problems with smart-cast
val task = task
if (task != null) {
dueDate = task.dueDate
reminder = task.reminder
}

subject = arguments?.getParcelable(ARG_SUBJECT)
}

Expand All @@ -91,6 +103,7 @@ class AddEditTaskFragment : Fragment() {

// Avoiding problems with smart cast
val task = task
val taskReminder = task?.reminder
val subject = subject
val listOfSubjects = listOfSubjects

Expand Down Expand Up @@ -128,9 +141,8 @@ class AddEditTaskFragment : Fragment() {
resources.getDrawable(R.drawable.circular_disabled_button, null)
new_task_subject_btn.setTextColor(resources.getColor(R.color.colorTextSecondary, null))
new_task_subject_btn.isEnabled = false
new_task_due_date_btn.text = task.dueDate
new_task_reminder_btn.text =
if (task.reminder.isEmpty()) getString(R.string.add_edit_lesson_btn) else task.reminder
new_task_due_date_btn.text = CalendarUtils.dateToString(task.dueDate, true)
new_task_reminder_btn.text = if (taskReminder == null) getString(R.string.add_edit_lesson_btn) else CalendarUtils.dateToString(taskReminder, true)

} else if (task == null && subject != null) {
// Fragment called from SubjectDetailsFragment
Expand Down Expand Up @@ -196,12 +208,22 @@ class AddEditTaskFragment : Fragment() {
showTaskTypesPopUp(it)
}

val calendarListener: CalendarInteractions = object: CalendarInteractions {
override fun onDateSet(date: Date) {
dueDate = date
}

override fun onTimeSet(date: Date) {
reminder = date
}
}

// User must set the due date
new_task_due_date_btn.setOnClickListener {
val cal = Calendar.getInstance()
DatePickerDialog(
context!!,
CalendarUtils.getDateSetListener(activity!!, R.id.new_task_due_date_btn, cal),
CalendarUtils.getDateSetListener(activity!!, R.id.new_task_due_date_btn, cal, calendarListener),
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)
Expand All @@ -213,7 +235,7 @@ class AddEditTaskFragment : Fragment() {
val cal = Calendar.getInstance()
TimePickerDialog(
context,
CalendarUtils.getTimeSetListener(activity!!, R.id.new_task_reminder_btn, cal, true),
CalendarUtils.getTimeSetListener(activity!!, R.id.new_task_reminder_btn, cal, true, calendarListener),
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
true
Expand Down Expand Up @@ -298,8 +320,8 @@ class AddEditTaskFragment : Fragment() {
new_task_desc.text.toString(),
if (taskType == 1 || taskType == 2) taskType else throw IllegalArgumentException("Parameter taskType ($taskType) must be one or two"),
selectedSubjectId ?: (subject?.id ?: ""),
new_task_due_date_btn.text.toString(),
if (new_task_reminder_btn.text.toString() != getString(R.string.add_edit_lesson_btn)) new_task_reminder_btn.text.toString() else ""
dueDate!!,
reminder
)
task.id = this.task?.id ?: ""
return task
Expand Down
47 changes: 39 additions & 8 deletions app/src/main/java/app/milanherke/mystudiez/CalendarUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class CalendarUtils {

companion object {

interface DateSet {
fun onSuccess(date: Date)
interface CalendarInteractions {
fun onDateSet(date: Date)
fun onTimeSet(date: Date)
}

/**
Expand All @@ -36,18 +37,18 @@ class CalendarUtils {
parentActivity: Activity,
@IdRes buttonId: Int,
cal: Calendar,
listener: DateSet? = null
listener: CalendarInteractions? = null
): DatePickerDialog.OnDateSetListener {
val button = parentActivity.findViewById<Button>(buttonId)

return DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
cal.set(Calendar.YEAR, year)
cal.set(Calendar.MONTH, month)
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val timeInMillis = roundToMidnight(cal.timeInMillis)
button.text =
SimpleDateFormat("dd'/'MM'/'yyyy", Locale.getDefault()).format(cal.time)

listener?.onSuccess(cal.time)
SimpleDateFormat("dd'/'MM'/'yyyy", Locale.getDefault()).format(Date(timeInMillis))
listener?.onDateSet(Date(timeInMillis))
}
}

Expand All @@ -57,29 +58,32 @@ class CalendarUtils {
* @param parentActivity Host activity of the fragment in which the function is used
* @param buttonId The selected time will be displayed on this button
* @param cal The calendar on which the time is set
* @param dateBefore Boolean - True if a [DatePickerDialog] is used before, otherwise false
* @return An OnTimeSetListener
*/
@SuppressLint("SetTextI18n")
fun getTimeSetListener(
parentActivity: Activity,
@IdRes buttonId: Int,
cal: Calendar,
dateBefore: Boolean
dateBefore: Boolean,
listener: CalendarInteractions? = null
): TimePickerDialog.OnTimeSetListener {
val button = parentActivity.findViewById<Button>(buttonId)

return TimePickerDialog.OnTimeSetListener { _, hour, minute ->
cal.set(Calendar.HOUR_OF_DAY, hour)
cal.set(Calendar.MINUTE, minute)

if (dateBefore) {
button.text =
"${button.text} ${SimpleDateFormat(
"HH:mm",
Locale.ENGLISH
).format(cal.time)}"
listener?.onTimeSet(cal.time)
} else {
button.text = SimpleDateFormat("HH:mm", Locale.ENGLISH).format(cal.time)
listener?.onTimeSet(cal.time)
}
}
}
Expand All @@ -104,5 +108,32 @@ class CalendarUtils {
else -> throw IllegalArgumentException("Parameter num $num must be between one and seven")
}
}

/**
* A simple function that takes in a unix timestamp and rounds it down to the previous midnight.
*
* @param time millisecond offset from the Epoch
* @return Date rounded down to midnight as a long value (milliseconds)
*/
fun roundToMidnight(time: Long): Long {
var timeStamp = time
timeStamp -= timeStamp % (24 * 60 * 60 * 1000) //subtract amount of time since midnight
return timeStamp
}

/**
* A simple function that formats a date object to dd/MM/yyyy
*
* @param date Date to be formatted
* @param includeTime Boolean - True if time (hours and minutes) should be included, otherwise false
* @return Formatted date
*/
fun dateToString(date: Date, includeTime: Boolean): String {
return if (includeTime) {
SimpleDateFormat("dd'/'MM'/'yyyy HH:mm", Locale.getDefault()).format(date)
} else {
SimpleDateFormat("dd'/'MM'/'yyyy", Locale.getDefault()).format(date)
}
}
}
}
7 changes: 4 additions & 3 deletions app/src/main/java/app/milanherke/mystudiez/Exam.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app.milanherke.mystudiez

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
import java.util.*

/**
* Data [Parcelable] subclass.
Expand All @@ -19,9 +20,9 @@ data class Exam(
var name: String,
var description: String,
var subjectId: String,
var date: String,
var reminder: String,
var date: Date,
var reminder: Date? = null,
var id: String = ""
) : Parcelable {
constructor() : this("", "", "", "", "", "")
constructor() : this("", "", "", Date(), null, "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ class ExamDetailsFragment : Fragment() {

// Avoiding problems with smart-cast
val exam = exam
val examReminder = exam?.reminder
val subject = subject

if (exam != null && subject != null) {
activity!!.toolbar.setTitle(R.string.exam_details_title)
exam_details_name_value.text = exam.name
exam_details_desc_value.text = exam.description
exam_details_subject_value.text = subject.name
exam_details_date_value.text = exam.date
exam_details_reminder_value.text = exam.reminder
exam_details_date_value.text = CalendarUtils.dateToString(exam.date, false)
exam_details_reminder_value.text = if (examReminder == null) getString(R.string.add_edit_lesson_btn) else CalendarUtils.dateToString(examReminder, true)
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/app/milanherke/mystudiez/ExamsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class ExamsFragment : Fragment(), ExamsRecyclerViewAdapter.OnExamClickListener {
viewModel.examsListLiveData.observe(
this,
Observer { list ->
examsAdapter.swapExamsList(list)
val sortedList = ArrayList(list.sortedWith(compareBy(Exam::date, Exam::name)))
examsAdapter.swapExamsList(sortedList)
if (exam_list != null && list.size != 0) Animations.runLayoutAnimation(exam_list)
}
)
Expand Down
Loading

0 comments on commit b64cc1c

Please sign in to comment.