diff --git a/app/build.gradle b/app/build.gradle index 42a7d2d..d6691bc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/com/embers/simpleto_do/MainActivity.kt b/app/src/main/java/com/embers/simpleto_do/MainActivity.kt index 7d1fdd8..a8bbba9 100644 --- a/app/src/main/java/com/embers/simpleto_do/MainActivity.kt +++ b/app/src/main/java/com/embers/simpleto_do/MainActivity.kt @@ -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() + private val TAG: String = this::class.java.name + + private var taskList = mutableListOf() + 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 = 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