This commit is contained in:
2017-07-28 00:35:17 +02:00
parent 5146adc2f6
commit 3f5760f60e
7 changed files with 210 additions and 107 deletions

View File

@@ -9,7 +9,7 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".MainActivity"> <activity android:name=".ui.MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

View File

@@ -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;
}
}

View File

@@ -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<Task> {
private final String TASK_FILE = "tasks.txt";
private ArrayList<Task> list;
private Task last;
public TaskList() {
list = new ArrayList<Task>();
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<Task> iterator() {
return list.iterator();
}
}

View File

@@ -1,27 +1,19 @@
package be.mathsaey.stoofnogaan; package be.mathsaey.stoofnogaan.ui;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.View; import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import java.io.BufferedReader; import be.mathsaey.stoofnogaan.R;
import java.io.FileInputStream; import be.mathsaey.stoofnogaan.data.Task;
import java.io.FileNotFoundException; import be.mathsaey.stoofnogaan.data.TaskList;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends Activity { public class MainActivity extends Activity {
// View which contains all the tasks // View which contains all the tasks
private LinearLayout taskView; private LinearLayout taskView;
private TaskView lastChild; private TaskList list;
private final String TASKS_FILE = "tasks.txt";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -29,13 +21,13 @@ public class MainActivity extends Activity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
taskView = findViewById(R.id.tasks); taskView = findViewById(R.id.tasks);
loadFromFile(); loadTasks();
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
writeToFile(); list.writeToFile(this);
taskView.removeAllViews(); taskView.removeAllViews();
} }
@@ -46,64 +38,26 @@ public class MainActivity extends Activity {
return true; return true;
} }
public void onNewTaskButtonClick(View v) { public void loadTasks() {
if (lastChild == null || !lastChild.isEmpty()) { list = new TaskList(this);
TaskView task = new TaskView(this); for (Task t: list) {
taskView.addView(task); TaskView v = new TaskView(this, t);
lastChild = task; 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) { public void deleteTask(TaskView v) {
taskView.removeView(v); taskView.removeView(v);
list.remove(v.getTask());
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));
}
} }
} }

View File

@@ -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.LayoutInflater;
import android.view.View;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast;
import be.mathsaey.stoofnogaan.R;
import be.mathsaey.stoofnogaan.data.Task;
class TaskView extends LinearLayout { class TaskView extends LinearLayout {
private Task task;
private MainActivity parent; private MainActivity parent;
protected CheckBox checkBox; protected CheckBox checkBox;
protected EditText textView; protected EditText textView;
public TaskView(MainActivity p) { public TaskView(MainActivity p, Task t) {
super(p); super(p);
task = t;
LayoutInflater.from(p).inflate(R.layout.task, this); LayoutInflater.from(p).inflate(R.layout.task, this);
@@ -22,49 +29,37 @@ class TaskView extends LinearLayout {
checkBox = findViewById(R.id.checkBox); checkBox = findViewById(R.id.checkBox);
textView = findViewById(R.id.textView); textView = findViewById(R.id.textView);
checkBox.setChecked(t.getStatus());
textView.setText(t.getText());
addTextListeners(); addTextListeners();
} }
public TaskView(MainActivity p, String s) { public void update() {
this(p); task.setText(textView.getText().toString());
task.setStatus(checkBox.isChecked());
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 boolean isEmpty() { public Task getTask() {
return textView.getText().length() == 0; return this.task;
}
public String toString() {
return String.format("%s\t%s", checkBox.isChecked(), textView.getText());
} }
// Android is horrible, this code tries to figure out when the user finished editing. // 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 // When this is the case, check if the text field is empty, delete the taskview if this is
// the case. // the case.
private void addTextListeners() { private void addTextListeners() {
// When done is clicked... // Update text when text is changed
// Or not because that is when things start to go boom textView.addTextChangedListener(new TextWatcher() {
// 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() {
@Override @Override
public void onFocusChange(View view, boolean b) { public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
if (!b && TaskView.this.isEmpty()) {
@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); parent.deleteTask(TaskView.this);
} }
} }

View File

@@ -5,7 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
tools:context="be.mathsaey.stoofnogaan.MainActivity"> tools:context=".MainActivity">
<LinearLayout <LinearLayout
android:id="@+id/tasks" android:id="@+id/tasks"

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu <menu
xmlns:android="http://schemas.android.com/apk/res/android"/> xmlns:android="http://schemas.android.com/apk/res/android">
<item <item
android:title="@string/settings_text" android:title="@string/settings_text"
android:icon="@android:drawable/ic_menu_preferences" android:icon="@android:drawable/ic_menu_preferences"