I still don't know what I'm doing but less

This commit is contained in:
kdeschuy
2017-07-28 00:53:20 +02:00
14 changed files with 615 additions and 194 deletions

View File

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

Binary file not shown.

View File

@@ -1,111 +0,0 @@
package be.mathsaey.stoofnogaan;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends Activity {
// View which contains all the tasks
private LinearLayout taskView;
private TaskView lastChild;
private final String TASKS_FILE = "tasks.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
taskView = findViewById(R.id.tasks);
loadFromFile();
}
@Override
protected void onDestroy() {
super.onDestroy();
writeToFile();
taskView.removeAllViews();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onNewTaskButtonClick(View v) {
if (lastChild == null || !lastChild.isEmpty()) {
TaskView task = new TaskView(this);
taskView.addView(task);
lastChild = task;
}
}
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));
}
}
}

View File

@@ -1,77 +0,0 @@
package be.mathsaey.stoofnogaan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
class TaskView extends LinearLayout {
private MainActivity parent;
protected CheckBox checkBox;
protected EditText textView;
public TaskView(MainActivity p) {
super(p);
LayoutInflater.from(p).inflate(R.layout.task, this);
parent = p;
checkBox = findViewById(R.id.checkBox);
textView = findViewById(R.id.textView);
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 boolean isEmpty() {
return textView.getText().length() == 0;
}
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.
// 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() {
@Override
public void onFocusChange(View view, boolean b) {
if (!b && TaskView.this.isEmpty()) {
parent.deleteTask(TaskView.this);
}
}
});
}
}

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

@@ -0,0 +1,69 @@
package be.mathsaey.stoofnogaan.ui;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
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 TaskList list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
taskView = findViewById(R.id.tasks);
loadTasks();
Button button = (Button) findViewById(R.id.addButton);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Oswald-SemiBold.ttf");
button.setTypeface(custom_font);
}
@Override
protected void onDestroy() {
super.onDestroy();
list.writeToFile(this);
taskView.removeAllViews();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
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);
list.remove(v.getTask());
}
}

View File

@@ -0,0 +1,68 @@
package be.mathsaey.stoofnogaan.ui;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
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, Task t) {
super(p);
task = t;
LayoutInflater.from(p).inflate(R.layout.task, this);
parent = p;
checkBox = findViewById(R.id.checkBox);
textView = findViewById(R.id.textView);
checkBox.setChecked(t.getStatus());
textView.setText(t.getText());
addTextListeners();
}
public void update() {
task.setText(textView.getText().toString());
task.setStatus(checkBox.isChecked());
}
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() {
// Update text when text is changed
textView.addTextChangedListener(new TextWatcher() {
@Override
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);
}
}
});
}
}

View File

@@ -7,7 +7,7 @@
<!-- set the shadow color here -->
<stroke
android:width="2dp"
android:color="#3e1a01" />
android:color="#7c7656" />
<padding
android:bottom="2dp"
@@ -23,7 +23,7 @@
<item>
<shape>
<solid android:color="@color/colorBrown" />
<solid android:color="#dad3a9" />
<corners android:radius="3dp" />
</shape>
</item>

View File

@@ -7,8 +7,7 @@
android:orientation="vertical"
android:background="@color/colorLight"
android:padding="52dp"
android:paddingTop="102dp"
tools:context="be.mathsaey.stoofnogaan.MainActivity">
tools:context="ui.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
@@ -34,6 +33,7 @@
android:text="@string/task_new"
android:background="@drawable/button"
android:layout_alignParentBottom="true"
android:textColor="#7c7656"
android:paddingLeft="52dp"
android:paddingRight="52dp"
android:layout_centerHorizontal="true">

View File

@@ -3,7 +3,7 @@
<color name="colorWhite">#FFF</color>
<color name="colorLight">#FFF8E1</color>
<color name="colorOrange">#FF7F2A</color>
<color name="colorTeak">#5FD3BC</color>
<color name="colorTeal">#5FD3BC</color>
<color name="colorDropShadow">#E6DDBF</color>
<color name="colorBrown">#552200</color>
</resources>