mirror of
https://github.com/Xevion/simple-todo.git
synced 2025-12-06 01:16:23 -06:00
Added task removal, task saving/loading
- Also refactored/moved around code for better viewing/functionality
This commit is contained in:
@@ -31,6 +31,7 @@ dependencies {
|
||||
implementation 'androidx.appcompat:appcompat:1.3.1'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
|
||||
implementation 'androidx.recyclerview:recyclerview:1.2.1'
|
||||
implementation 'commons-io:commons-io:2.6'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.embers.simpleto_do
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.KeyEvent
|
||||
import android.view.View
|
||||
import android.widget.Button
|
||||
@@ -8,30 +9,88 @@ import android.widget.EditText
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.apache.commons.io.FileUtils
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.nio.charset.Charset
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private val taskList = mutableListOf<String>()
|
||||
private val TAG: String = this::class.java.name
|
||||
|
||||
private var taskList = mutableListOf<String>()
|
||||
private lateinit var editText: EditText
|
||||
private lateinit var recyclerView: RecyclerView
|
||||
private lateinit var adapter : TaskItemAdapter
|
||||
private lateinit var button: Button
|
||||
|
||||
private fun getDataFile() : File {
|
||||
return File(filesDir, "data.txt")
|
||||
}
|
||||
|
||||
private fun saveData(data: List<String> = taskList) {
|
||||
val file: File = getDataFile()
|
||||
try {
|
||||
FileUtils.writeLines(file, data)
|
||||
Log.i(TAG, "Successfully wrote ${taskList.size} tasks to ${file.absolutePath}")
|
||||
} catch (exception: IOException) {
|
||||
exception.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load data from the relevant data file.
|
||||
*
|
||||
*/
|
||||
private fun loadData() {
|
||||
val file: File = getDataFile()
|
||||
try {
|
||||
taskList = FileUtils.readLines(file, Charset.defaultCharset())
|
||||
Log.i(TAG, "Successfully read ${taskList.size} tasks from ${file.absolutePath}")
|
||||
} catch (exception: IOException) {
|
||||
exception.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the enabled state of the 'Add' button depending on whether the text field has text.
|
||||
*
|
||||
*/
|
||||
private fun refreshButton() {
|
||||
button.isEnabled = editText.text.isNotBlank()
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
// Button is by default disabled (no text in textbox)
|
||||
val button = findViewById<Button>(R.id.button)
|
||||
button.isEnabled = false
|
||||
loadData()
|
||||
|
||||
/**
|
||||
* Callback to remove a task at a given location in the list (on long click action)
|
||||
*/
|
||||
val onLongClickListener = object : TaskItemAdapter.OnLongClickListener {
|
||||
override fun onItemLongClicked(position: Int) {
|
||||
taskList.removeAt(position)
|
||||
adapter.notifyItemRemoved(position)
|
||||
saveData()
|
||||
}
|
||||
}
|
||||
|
||||
// Setup RecyclerView
|
||||
val editText = findViewById<EditText>(R.id.editTodoText)
|
||||
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
|
||||
val adapter = TaskItemAdapter(taskList)
|
||||
editText = findViewById<EditText>(R.id.editTodoText)
|
||||
recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
|
||||
adapter = TaskItemAdapter(taskList, onLongClickListener)
|
||||
recyclerView.adapter = adapter
|
||||
recyclerView.layoutManager = LinearLayoutManager(this)
|
||||
button = findViewById(R.id.button)
|
||||
|
||||
refreshButton()
|
||||
|
||||
// Disable add task button when text is not inside EditText
|
||||
findViewById<EditText>(R.id.editTodoText).setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
|
||||
// Read only event after the action has been processed in the textbox
|
||||
if (event.action == KeyEvent.ACTION_UP)
|
||||
button.isEnabled = editText.text.isNotBlank()
|
||||
refreshButton()
|
||||
|
||||
false
|
||||
})
|
||||
@@ -40,14 +99,11 @@ class MainActivity : AppCompatActivity() {
|
||||
// Check that the task name has text in it
|
||||
if (editText.text.isBlank()) return@setOnClickListener
|
||||
|
||||
// Add task name to task list
|
||||
taskList.add(editText.text.toString())
|
||||
|
||||
// Clear text box
|
||||
editText.text.clear()
|
||||
|
||||
// Notify the adapter that data has changed
|
||||
adapter.notifyItemInserted(taskList.size - 1)
|
||||
taskList.add(editText.text.toString()) // Add task name to task list
|
||||
editText.text.clear() // Clear text box
|
||||
refreshButton() // Update button accordingly
|
||||
adapter.notifyItemInserted(taskList.size - 1) // Notify the adapter that data has changed
|
||||
saveData()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,25 @@ import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
class TaskItemAdapter(private val taskList: List<String>) :
|
||||
class TaskItemAdapter(
|
||||
private val taskList: List<String>,
|
||||
private val longClickListener: OnLongClickListener
|
||||
) :
|
||||
RecyclerView.Adapter<TaskItemAdapter.ViewHolder>() {
|
||||
|
||||
interface OnLongClickListener {
|
||||
fun onItemLongClicked(position: Int)
|
||||
}
|
||||
|
||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
val textView: TextView = itemView.findViewById(android.R.id.text1)
|
||||
|
||||
init {
|
||||
itemView.setOnLongClickListener {
|
||||
longClickListener.onItemLongClicked(bindingAdapterPosition)
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
|
||||
Reference in New Issue
Block a user