Commit f11d7c28 authored by Lukas Bersinger's avatar Lukas Bersinger
Browse files

refactor taskAddView to allow edits

parent 2b38fd9b
......@@ -10,7 +10,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".TaskTrackerApplication">
<activity android:name=".activities.AddTaskActivity"/>
<activity android:name=".activities.EditTaskActivity"/>
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......
package ch.ost.rj.mge.miniproject.tasktracker.activities
import android.app.DatePickerDialog
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doOnTextChanged
import ch.ost.rj.mge.miniproject.tasktracker.R
import ch.ost.rj.mge.miniproject.tasktracker.fragments.TaskFormFragment
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
import ch.ost.rj.mge.miniproject.tasktracker.repositories.TaskRepository
import java.text.DateFormat
import java.util.*
class AddTaskActivity : AppCompatActivity(), TaskFormFragment.OnTaskChangeListener {
class EditTaskActivity : AppCompatActivity(), TaskFormFragment.OnTaskChangeListener {
private lateinit var btnCancel: Button
private lateinit var btnCreate: Button
private var btnCancel: Button? = null
private var btnCreate: Button? = null
private val task = Task()
private var task = Task()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_task)
setContentView(R.layout.activity_edit_task)
val formFragment: TaskFormFragment =
getSupportFragmentManager().findFragmentById(R.id.fragment_task_form) as TaskFormFragment
val bundle = intent.extras
if (bundle != null) {
val taskId = bundle.getInt(KEY_TASK_ID)
if (taskId != 0) {
val taskToUpdate = TaskRepository.getTaskById(taskId)
if (taskToUpdate != null) {
task = taskToUpdate
formFragment.setTask(task)
}
}
}
btnCancel = findViewById(R.id.btnCancelCreateTask)
btnCancel.setOnClickListener {
btnCancel!!.setOnClickListener {
finish()
}
btnCreate = findViewById(R.id.btnCreateTask)
btnCreate.setOnClickListener {
createTask()
btnCreate = findViewById(R.id.btnSaveTask)
btnCreate!!.setOnClickListener {
saveTask()
}
updateValidity()
}
fun createTask() {
TaskRepository.createTask(task)
fun saveTask() {
if (task.isNew()) {
TaskRepository.createTask(task)
} else {
TaskRepository.updateTask(task)
}
finish()
}
fun updateValidity() {
btnCreate.isEnabled = task.isValid()
btnCreate?.isEnabled = task.isValid()
}
override fun onTitleChange(title: String) {
......@@ -61,4 +76,8 @@ class AddTaskActivity : AppCompatActivity(), TaskFormFragment.OnTaskChangeListen
task.estimatedTimeMinutes = (hours * 60) + minutes
updateValidity()
}
companion object {
val KEY_TASK_ID = "KEY_TASK_ID"
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ import ch.ost.rj.mge.miniproject.tasktracker.models.Task
import ch.ost.rj.mge.miniproject.tasktracker.repositories.TaskRepository
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(), TaskListFragment.OnItemClickListener {
private lateinit var listFragment: TaskListFragment
private lateinit var listEmptyFragment: TaskListEmptyFragment
......@@ -26,13 +26,19 @@ class MainActivity : AppCompatActivity() {
btnAddTask = findViewById(R.id.btnAddTask)
btnAddTask.setOnClickListener { _ ->
val intent = Intent(this, AddTaskActivity::class.java)
val intent = Intent(this, EditTaskActivity::class.java)
startActivity(intent)
}
taskListContainer = findViewById(R.id.fltTaskListContainer)
}
override fun onItemClick(task: Task) {
val intent = Intent(this, EditTaskActivity::class.java)
intent.putExtra(EditTaskActivity.KEY_TASK_ID, task.id)
startActivity(intent)
}
override fun onResume() {
super.onResume()
......
package ch.ost.rj.mge.miniproject.tasktracker.adapters
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
......@@ -11,7 +12,7 @@ import ch.ost.rj.mge.miniproject.tasktracker.models.Task
import java.text.DateFormat
class TaskListAdapter(private val dataset: List<Task>) :
class TaskListAdapter(private val dataset: List<Task>, private val clickListener: OnItemClickListener) :
RecyclerView.Adapter<TaskListAdapter.TaskListViewHolder>() {
class TaskListViewHolder(val cardView: CardView) : RecyclerView.ViewHolder(cardView)
......@@ -33,8 +34,7 @@ class TaskListAdapter(private val dataset: List<Task>) :
dueDateView.text = DateFormat.getDateInstance().format(activeTask.dueDate!!.time)
holder.cardView.setOnClickListener {
// Go To Edit Activity
Log.println(Log.DEBUG, "TaskListAdapter", "Opened Task with ID: " + activeTask.id.toString())
clickListener.onItemClick(activeTask)
}
}
......@@ -42,4 +42,7 @@ class TaskListAdapter(private val dataset: List<Task>) :
return dataset.size
}
interface OnItemClickListener {
fun onItemClick(task: Task)
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package ch.ost.rj.mge.miniproject.tasktracker.database
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
@Dao
......@@ -11,6 +12,12 @@ interface TaskDao {
@Query("SELECT * FROM tasks WHERE completed = 0 ORDER BY due_date")
fun getOpenTasksByDueDate(): List<Task>
@Query("SELECT * FROM tasks WHERE id=:id")
fun getTaskById(id: Int): Task?
@Insert
fun insert(task: Task)
@Update
fun update(task: Task)
}
\ No newline at end of file
......@@ -11,28 +11,37 @@ import android.widget.TextView
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import ch.ost.rj.mge.miniproject.tasktracker.R
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
import java.text.DateFormat
import java.util.*
import kotlin.math.floor
class TaskFormFragment : Fragment() {
private var changeListener: OnTaskChangeListener? = null
private lateinit var fldName: TextView
private lateinit var fldEstimateHours: TextView
private lateinit var fldEstimateMinutes: TextView
private lateinit var txtDueDate: TextView
private var estimateMinutes = 0L
private var estimateHours = 0L
private var selectedDate = Calendar.getInstance();
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val fragment = inflater.inflate(R.layout.fragment_task_form, container, false)
val fldName: TextView = fragment.findViewById(R.id.fldTaskName)
fldName = fragment.findViewById(R.id.fldTaskName)
fldName.doOnTextChanged { text, _, _, _ ->
changeListener?.onTitleChange(text.toString())
}
val fldEstimateHours: TextView = fragment.findViewById(R.id.fldEstimateHours)
fldEstimateHours = fragment.findViewById(R.id.fldEstimateHours)
fldEstimateHours.doOnTextChanged { text, _, _, _ ->
val input = text.toString().toLongOrNull()
......@@ -42,7 +51,7 @@ class TaskFormFragment : Fragment() {
}
}
val fldEstimateMinutes: TextView = fragment.findViewById(R.id.fldEstimateMinutes)
fldEstimateMinutes = fragment.findViewById(R.id.fldEstimateMinutes)
fldEstimateMinutes.doOnTextChanged { text, _, _, _ ->
val input = text.toString().toLongOrNull()
......@@ -52,26 +61,24 @@ class TaskFormFragment : Fragment() {
}
}
val txtDueDate: TextView = fragment.findViewById(R.id.txtDueDate)
txtDueDate = fragment.findViewById(R.id.txtDueDate)
val btnDatePicker: ImageButton = fragment.findViewById(R.id.btnDatePicker)
btnDatePicker.setOnClickListener {
val c = Calendar.getInstance()
val initialYear = c.get(Calendar.YEAR)
val initialMonth = c.get(Calendar.MONTH)
val initialDayOfMonth = c.get(Calendar.DAY_OF_MONTH)
val initialYear = selectedDate.get(Calendar.YEAR)
val initialMonth = selectedDate.get(Calendar.MONTH)
val initialDayOfMonth = selectedDate.get(Calendar.DAY_OF_MONTH)
val datePickerContext = context
if (datePickerContext != null) {
DatePickerDialog(
datePickerContext,
{ _, year, month, dayOfMonth ->
val date = Calendar.getInstance()
date.set(Calendar.YEAR, year)
date.set(Calendar.MONTH, month)
date.set(Calendar.DAY_OF_MONTH, dayOfMonth)
txtDueDate.text = DateFormat.getDateInstance().format(date.time)
changeListener?.onDueDateChange(date)
selectedDate.set(Calendar.YEAR, year)
selectedDate.set(Calendar.MONTH, month)
selectedDate.set(Calendar.DAY_OF_MONTH, dayOfMonth)
txtDueDate.text = DateFormat.getDateInstance().format(selectedDate.time)
changeListener?.onDueDateChange(selectedDate)
},
initialYear,
initialMonth,
......@@ -98,6 +105,27 @@ class TaskFormFragment : Fragment() {
changeListener = null
}
fun setTask(task: Task) {
if (task.name != null) {
fldName.text = task.name
} else {
fldName.text = ""
}
if (task.dueDate != null) {
selectedDate = task.dueDate
txtDueDate.text = DateFormat.getDateInstance().format(selectedDate.time)
} else {
selectedDate = Calendar.getInstance()
txtDueDate.text = "-"
}
estimateHours = floor(task.estimatedTimeMinutes / 60.0).toLong()
estimateMinutes = (task.estimatedTimeMinutes - (60 * estimateHours))
fldEstimateHours.text = estimateHours.toString()
fldEstimateMinutes.text = estimateMinutes.toString()
}
companion object {
@JvmStatic
fun newInstance() = TaskListEmptyFragment().apply { }
......
package ch.ost.rj.mge.miniproject.tasktracker.fragments
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
......@@ -12,20 +13,28 @@ import ch.ost.rj.mge.miniproject.tasktracker.adapters.TaskListAdapter
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
import ch.ost.rj.mge.miniproject.tasktracker.repositories.TaskRepository
/**
* A simple [Fragment] subclass.
* Use the [TaskListFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class TaskListFragment : Fragment() {
class TaskListFragment : Fragment(), TaskListAdapter.OnItemClickListener {
private var tasks: List<Task>? = null
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
private var changeListener: OnItemClickListener? = null
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnItemClickListener) {
changeListener = context
} else {
throw RuntimeException(context.toString() +
" must implement OnItemClickListener")
}
}
override fun onDetach() {
super.onDetach()
changeListener = null
}
override fun onCreateView(
......@@ -36,7 +45,7 @@ class TaskListFragment : Fragment() {
val fragment = inflater.inflate(R.layout.fragment_task_list, container, false)
viewManager = LinearLayoutManager(context)
viewAdapter = tasks?.let { TaskListAdapter(it) }!!
viewAdapter = tasks?.let { TaskListAdapter(it, this) }!!
recyclerView = fragment.findViewById(R.id.rvTaskList)
recyclerView.apply {
......@@ -48,13 +57,15 @@ class TaskListFragment : Fragment() {
return fragment
}
override fun onItemClick(task: Task) {
changeListener?.onItemClick(task)
}
interface OnItemClickListener {
fun onItemClick(task: Task)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment TaskListFragment.
*/
@JvmStatic
fun newInstance(tasks: List<Task>) = TaskListFragment().apply {
this.tasks = tasks
......
......@@ -41,4 +41,8 @@ class Task {
return true
}
fun isNew(): Boolean {
return id == 0;
}
}
......@@ -22,7 +22,15 @@ object TaskRepository {
return appDatabase.taskDao().getOpenTasksByDueDate()
}
fun getTaskById(id: Int): Task? {
return appDatabase.taskDao().getTaskById(id)
}
fun createTask(task: Task) {
appDatabase.taskDao().insert(task)
}
fun updateTask(task: Task) {
appDatabase.taskDao().update(task)
}
}
\ No newline at end of file
......@@ -4,10 +4,10 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.AddTaskActivity">
tools:context=".activities.EditTaskActivity">
<Button
android:id="@+id/btnCreateTask"
android:id="@+id/btnSaveTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
......@@ -28,7 +28,7 @@
app:layout_constraintStart_toStartOf="parent" />
<fragment
android:id="@+id/fragment"
android:id="@+id/fragment_task_form"
android:name="ch.ost.rj.mge.miniproject.tasktracker.fragments.TaskFormFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment