mirror of
https://github.com/Xevion/simple-todo.git
synced 2025-12-06 23:16:24 -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.appcompat:appcompat:1.3.1'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
|
||||||
implementation 'androidx.recyclerview:recyclerview:1.2.1'
|
implementation 'androidx.recyclerview:recyclerview:1.2.1'
|
||||||
|
implementation 'commons-io:commons-io:2.6'
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.embers.simpleto_do
|
package com.embers.simpleto_do
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.view.KeyEvent
|
import android.view.KeyEvent
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
@@ -8,30 +9,88 @@ import android.widget.EditText
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
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() {
|
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?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
// Button is by default disabled (no text in textbox)
|
loadData()
|
||||||
val button = findViewById<Button>(R.id.button)
|
|
||||||
button.isEnabled = false
|
/**
|
||||||
|
* 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
|
// Setup RecyclerView
|
||||||
val editText = findViewById<EditText>(R.id.editTodoText)
|
editText = findViewById<EditText>(R.id.editTodoText)
|
||||||
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
|
recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
|
||||||
val adapter = TaskItemAdapter(taskList)
|
adapter = TaskItemAdapter(taskList, onLongClickListener)
|
||||||
recyclerView.adapter = adapter
|
recyclerView.adapter = adapter
|
||||||
recyclerView.layoutManager = LinearLayoutManager(this)
|
recyclerView.layoutManager = LinearLayoutManager(this)
|
||||||
|
button = findViewById(R.id.button)
|
||||||
|
|
||||||
|
refreshButton()
|
||||||
|
|
||||||
// Disable add task button when text is not inside EditText
|
// Disable add task button when text is not inside EditText
|
||||||
findViewById<EditText>(R.id.editTodoText).setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
|
findViewById<EditText>(R.id.editTodoText).setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
|
||||||
// Read only event after the action has been processed in the textbox
|
// Read only event after the action has been processed in the textbox
|
||||||
if (event.action == KeyEvent.ACTION_UP)
|
if (event.action == KeyEvent.ACTION_UP)
|
||||||
button.isEnabled = editText.text.isNotBlank()
|
refreshButton()
|
||||||
|
|
||||||
false
|
false
|
||||||
})
|
})
|
||||||
@@ -40,14 +99,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
// Check that the task name has text in it
|
// Check that the task name has text in it
|
||||||
if (editText.text.isBlank()) return@setOnClickListener
|
if (editText.text.isBlank()) return@setOnClickListener
|
||||||
|
|
||||||
// Add task name to task list
|
taskList.add(editText.text.toString()) // Add task name to task list
|
||||||
taskList.add(editText.text.toString())
|
editText.text.clear() // Clear text box
|
||||||
|
refreshButton() // Update button accordingly
|
||||||
// Clear text box
|
adapter.notifyItemInserted(taskList.size - 1) // Notify the adapter that data has changed
|
||||||
editText.text.clear()
|
saveData()
|
||||||
|
|
||||||
// Notify the adapter that data has changed
|
|
||||||
adapter.notifyItemInserted(taskList.size - 1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,10 +6,25 @@ import android.view.ViewGroup
|
|||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
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>() {
|
RecyclerView.Adapter<TaskItemAdapter.ViewHolder>() {
|
||||||
|
|
||||||
|
interface OnLongClickListener {
|
||||||
|
fun onItemLongClicked(position: Int)
|
||||||
|
}
|
||||||
|
|
||||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||||
val textView: TextView = itemView.findViewById(android.R.id.text1)
|
val textView: TextView = itemView.findViewById(android.R.id.text1)
|
||||||
|
|
||||||
|
init {
|
||||||
|
itemView.setOnLongClickListener {
|
||||||
|
longClickListener.onItemLongClicked(bindingAdapterPosition)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||||
|
|||||||
Reference in New Issue
Block a user