From 3f5760f60ebccf57d9f5b5cdd21da0882cec12d3 Mon Sep 17 00:00:00 2001 From: Mathijs Saey Date: Fri, 28 Jul 2017 00:35:17 +0200 Subject: [PATCH] lel --- app/src/main/AndroidManifest.xml | 2 +- .../be/mathsaey/stoofnogaan/data/Task.java | 54 ++++++++++ .../mathsaey/stoofnogaan/data/TaskList.java | 100 ++++++++++++++++++ .../mathsaey/stoofnogaan/ui/MainActivity.java | 92 ++++------------ .../be/mathsaey/stoofnogaan/ui/TaskView.java | 65 ++++++------ app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/menu/menu_main.xml | 2 +- 7 files changed, 210 insertions(+), 107 deletions(-) create mode 100644 app/src/main/java/be/mathsaey/stoofnogaan/data/Task.java create mode 100644 app/src/main/java/be/mathsaey/stoofnogaan/data/TaskList.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fe4e954..654221f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - + diff --git a/app/src/main/java/be/mathsaey/stoofnogaan/data/Task.java b/app/src/main/java/be/mathsaey/stoofnogaan/data/Task.java new file mode 100644 index 0000000..ee46ebe --- /dev/null +++ b/app/src/main/java/be/mathsaey/stoofnogaan/data/Task.java @@ -0,0 +1,54 @@ +package be.mathsaey.stoofnogaan.data; + +public class Task { + private String text; + private boolean done; + + public Task() { + text = ""; + done = false; + } + + public Task(String s) { + this(); + text = s; + } + + public Task(String s, boolean b) { + this(s); + done = b; + } + + static public Task fromString(String s) { + Task res = new Task(); + + if (!s.isEmpty()) { + String[] split = s.split("\\t"); + res.done = Boolean.parseBoolean(split[0]); + res.text = split.length > 1 ? split[1] : ""; + } + return res; + } + + public String toString() { + return String.format("%s\t%s\n", done, text); + } + + public boolean getStatus() { + return this.done; + } + public void setStatus(boolean b) { + this.done = b; + } + + public String getText() { + return this.text; + } + public void setText(String s) { + this.text = s; + } + + public boolean isEmpty() { + return this.text.length() == 0; + } +} diff --git a/app/src/main/java/be/mathsaey/stoofnogaan/data/TaskList.java b/app/src/main/java/be/mathsaey/stoofnogaan/data/TaskList.java new file mode 100644 index 0000000..e218eba --- /dev/null +++ b/app/src/main/java/be/mathsaey/stoofnogaan/data/TaskList.java @@ -0,0 +1,100 @@ +package be.mathsaey.stoofnogaan.data; + +import android.content.Context; +import android.util.Log; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.PriorityQueue; + +import be.mathsaey.stoofnogaan.BuildConfig; + +public class TaskList implements Iterable { + private final String TASK_FILE = "tasks.txt"; + + private ArrayList list; + private Task last; + + public TaskList() { + list = new ArrayList(); + last = null; + } + + public TaskList(Context c) { + this(); + + try { + FileInputStream input = c.openFileInput(TASK_FILE); + InputStreamReader inputReader = new InputStreamReader(input); + BufferedReader bufferedReader = new BufferedReader(inputReader); + + String line; + + while ((line = bufferedReader.readLine()) != null) { + Task t = Task.fromString(line); + list.add(t); + last = t; + } + input.close(); + + } catch (FileNotFoundException e) { + // When the file is not found, just don't load. + } catch (IOException e) { + Log.d(BuildConfig.APPLICATION_ID, Log.getStackTraceString(e)); + } + } + + public void writeToFile(Context c) { + try { + FileOutputStream output = c.openFileOutput(TASK_FILE, Context.MODE_PRIVATE); + + for (Task t: list) { + String s = t.toString(); + output.write(s.getBytes()); + } + output.close(); + + } catch (FileNotFoundException e) { + Log.e(BuildConfig.APPLICATION_ID, "Could not find file for writing, that's weird..."); + } catch (IOException e) { + Log.e(BuildConfig.APPLICATION_ID, "Error when trying to write, this make me sad"); + } + } + + public boolean isFinished() { + for (Task t: list) { + if (!t.getStatus()) { + return false; + } + } + return true; + } + + public void add(Task t) { + list.add(t); + last = t; + } + + public void remove(Task t) { + list.remove(t); + + if (list.size() == 0) { + last = null; + } + } + + public boolean canAddNew() { + return (last == null) || (!last.isEmpty()); + } + + @Override + public Iterator iterator() { + return list.iterator(); + } +} diff --git a/app/src/main/java/be/mathsaey/stoofnogaan/ui/MainActivity.java b/app/src/main/java/be/mathsaey/stoofnogaan/ui/MainActivity.java index d97f453..fefc720 100644 --- a/app/src/main/java/be/mathsaey/stoofnogaan/ui/MainActivity.java +++ b/app/src/main/java/be/mathsaey/stoofnogaan/ui/MainActivity.java @@ -1,27 +1,19 @@ -package be.mathsaey.stoofnogaan; +package be.mathsaey.stoofnogaan.ui; import android.app.Activity; -import android.content.Context; import android.os.Bundle; -import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.LinearLayout; -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; +import be.mathsaey.stoofnogaan.R; +import be.mathsaey.stoofnogaan.data.Task; +import be.mathsaey.stoofnogaan.data.TaskList; public class MainActivity extends Activity { - // View which contains all the tasks private LinearLayout taskView; - private TaskView lastChild; - - private final String TASKS_FILE = "tasks.txt"; + private TaskList list; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,13 +21,13 @@ public class MainActivity extends Activity { setContentView(R.layout.activity_main); taskView = findViewById(R.id.tasks); - loadFromFile(); + loadTasks(); } @Override protected void onDestroy() { super.onDestroy(); - writeToFile(); + list.writeToFile(this); taskView.removeAllViews(); } @@ -46,64 +38,26 @@ public class MainActivity extends Activity { return true; } - public void onNewTaskButtonClick(View v) { - if (lastChild == null || !lastChild.isEmpty()) { - TaskView task = new TaskView(this); - taskView.addView(task); - lastChild = task; + public void loadTasks() { + list = new TaskList(this); + for (Task t: list) { + TaskView v = new TaskView(this, t); + taskView.addView(v); + } + } + + public void onNewTaskButtonClick(View _button) { + if (list.canAddNew()) { + Task t = new Task(); + TaskView v = new TaskView(this, t); + + list.add(t); + taskView.addView(v); } } public void deleteTask(TaskView v) { taskView.removeView(v); - - if (taskView.getChildCount() == 0){ - lastChild = null; - } - } - - private void writeToFile() { - try { - FileOutputStream output = openFileOutput(TASKS_FILE, Context.MODE_PRIVATE); - - for (int i = 0; i < taskView.getChildCount(); i++) { - TaskView v = (TaskView) taskView.getChildAt(i); - if (!v.isEmpty()) { - String s = v.toString() + "\n"; - output.write(s.getBytes()); - } - } - - output.close(); - } - catch (FileNotFoundException e) { - Log.e(BuildConfig.APPLICATION_ID, "Could not find file for writing, that's weird..."); - } - catch (IOException e) { - Log.e(BuildConfig.APPLICATION_ID, "Error when trying to write, deleting"); - - } - } - - private void loadFromFile() { - try { - FileInputStream input = openFileInput(TASKS_FILE); - - InputStreamReader streamReader = new InputStreamReader(input); - BufferedReader bufferedReader = new BufferedReader(streamReader); - String line; - - while ((line = bufferedReader.readLine()) != null) { - TaskView task = new TaskView(this, line); - taskView.addView(task); - lastChild = task; - } - input.close(); - } - catch (IOException e) {} - catch (Exception e) { - Log.w(BuildConfig.APPLICATION_ID, Log.getStackTraceString(e)); - } - + list.remove(v.getTask()); } } \ No newline at end of file diff --git a/app/src/main/java/be/mathsaey/stoofnogaan/ui/TaskView.java b/app/src/main/java/be/mathsaey/stoofnogaan/ui/TaskView.java index b0fb84e..100ee56 100644 --- a/app/src/main/java/be/mathsaey/stoofnogaan/ui/TaskView.java +++ b/app/src/main/java/be/mathsaey/stoofnogaan/ui/TaskView.java @@ -1,20 +1,27 @@ -package be.mathsaey.stoofnogaan; +package be.mathsaey.stoofnogaan.ui; -import android.util.Log; +import android.text.Editable; +import android.text.TextWatcher; import android.view.LayoutInflater; -import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.LinearLayout; +import android.widget.Toast; + +import be.mathsaey.stoofnogaan.R; +import be.mathsaey.stoofnogaan.data.Task; class TaskView extends LinearLayout { + private Task task; + private MainActivity parent; protected CheckBox checkBox; protected EditText textView; - public TaskView(MainActivity p) { + public TaskView(MainActivity p, Task t) { super(p); + task = t; LayoutInflater.from(p).inflate(R.layout.task, this); @@ -22,49 +29,37 @@ class TaskView extends LinearLayout { checkBox = findViewById(R.id.checkBox); textView = findViewById(R.id.textView); + checkBox.setChecked(t.getStatus()); + textView.setText(t.getText()); + addTextListeners(); } - public TaskView(MainActivity p, String s) { - this(p); - - if (!s.isEmpty()) { - Log.d(BuildConfig.APPLICATION_ID, s); - String[] split = s.split("\\t"); - checkBox.setChecked(split[0].equalsIgnoreCase("true")); - textView.setText(split[1]); - } + public void update() { + task.setText(textView.getText().toString()); + task.setStatus(checkBox.isChecked()); } - public boolean isEmpty() { - return textView.getText().length() == 0; - } - - public String toString() { - return String.format("%s\t%s", checkBox.isChecked(), textView.getText()); + public Task getTask() { + return this.task; } // Android is horrible, this code tries to figure out when the user finished editing. // When this is the case, check if the text field is empty, delete the taskview if this is // the case. private void addTextListeners() { - // When done is clicked... - // Or not because that is when things start to go boom -// textView.setOnEditorActionListener(new TextView.OnEditorActionListener() { -// @Override -// public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { -// if (i == EditorInfo.IME_ACTION_DONE && TaskView.this.isEmpty()) { -// parent.deleteTask(TaskView.this); -// return true; -// } -// return false; -// } -// }); - // Or when we lose focus - textView.setOnFocusChangeListener(new OnFocusChangeListener() { + // Update text when text is changed + textView.addTextChangedListener(new TextWatcher() { @Override - public void onFocusChange(View view, boolean b) { - if (!b && TaskView.this.isEmpty()) { + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} + + @Override + public void afterTextChanged(Editable editable) { + TaskView.this.update(); + if (task.isEmpty()) { parent.deleteTask(TaskView.this); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b3eeaba..8957dc4 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,7 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" - tools:context="be.mathsaey.stoofnogaan.MainActivity"> + tools:context=".MainActivity"> + xmlns:android="http://schemas.android.com/apk/res/android">