Skip to content

Commit 75b8d5c

Browse files
committed
feat: task name validation; use current time checkbox
1 parent e0cfddf commit 75b8d5c

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

app/src/main/java/tech/akpmakes/android/taskkeeper/MainActivity.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,23 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
206206
data.getLongExtra("whenTime", 0)
207207
);
208208

209+
if(evt.getName().length() == 0) {
210+
Snackbar.make(findViewById(android.R.id.content), "Your task could not be saved. Task name is required.",
211+
Snackbar.LENGTH_LONG).show();
212+
return;
213+
}
214+
209215
if (mDBQuery != null) {
210216
if(data.hasExtra("whenKey")) {
211217
mDBQuery.getRef().child(data.getStringExtra("whenKey")).setValue(evt);
212218
} else {
213219
mDBQuery.getRef().push().setValue(evt);
214220
}
215221

216-
Snackbar.make(findViewById(android.R.id.content), "Event saved successfully!",
222+
Snackbar.make(findViewById(android.R.id.content), "Task saved successfully!",
217223
Snackbar.LENGTH_LONG).show();
218224
} else {
219-
Snackbar.make(findViewById(android.R.id.content), "Your event could not be saved. Please try again.",
225+
Snackbar.make(findViewById(android.R.id.content), "Your task could not be saved. Please try again.",
220226
Snackbar.LENGTH_LONG).show();
221227
}
222228
}

app/src/main/java/tech/akpmakes/android/taskkeeper/TaskViewActivity.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import android.app.Activity;
44
import android.content.Intent;
55
import android.os.Bundle;
6+
import android.os.Handler;
67
import android.support.v7.app.AppCompatActivity;
78
import android.view.View;
89
import android.widget.Button;
10+
import android.widget.CheckBox;
11+
import android.widget.CompoundButton;
912
import android.widget.EditText;
1013
import android.widget.TextView;
1114

@@ -22,6 +25,7 @@ public class TaskViewActivity extends AppCompatActivity implements DatePickerDia
2225
Calendar whenTime;
2326
TextView taskDate;
2427
TextView taskTime;
28+
CheckBox useCurrentTime;
2529
String whenKey;
2630

2731
@Override
@@ -31,16 +35,29 @@ protected void onCreate(Bundle savedInstanceState) {
3135
final EditText taskName = findViewById(R.id.task_name_content);
3236
taskDate = findViewById(R.id.task_when_date);
3337
taskTime = findViewById(R.id.task_when_time);
38+
useCurrentTime = findViewById(R.id.useCurrentTime);
3439
Button taskSave = findViewById(R.id.btn_save_task);
3540

3641
whenTime = Calendar.getInstance();
3742

43+
final Handler handler = new Handler();
44+
handler.postDelayed(new Runnable() {
45+
@Override
46+
public void run() {
47+
if(useCurrentTime.isChecked()) {
48+
updateTime();
49+
}
50+
handler.postDelayed( this, 250 );
51+
}
52+
}, 0);
53+
3854
Intent i = getIntent();
3955
if (i.hasExtra("whenName")) {
4056
taskName.setText(i.getStringExtra("whenName"));
4157
}
4258

4359
if (i.hasExtra("whenTime")) {
60+
useCurrentTime.setChecked(false);
4461
whenTime.setTimeInMillis(i.getLongExtra("whenTime", 0));
4562
}
4663

@@ -88,8 +105,15 @@ public void onClick(View view) {
88105
taskSave.setOnClickListener(new View.OnClickListener() {
89106
@Override
90107
public void onClick(View view) {
108+
if(useCurrentTime.isChecked()) {
109+
updateTime();
110+
}
91111
String name = taskName.getText().toString();
92112
Long when = whenTime.getTimeInMillis();
113+
if (name.length() == 0) {
114+
taskName.setError("Task name is required!");
115+
return;
116+
}
93117
WhenEvent evt = new WhenEvent(name, when);
94118
Intent i = new Intent();
95119
i.putExtra("whenName", evt.getName());
@@ -101,6 +125,17 @@ public void onClick(View view) {
101125
finish();
102126
}
103127
});
128+
129+
useCurrentTime.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
130+
@Override
131+
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
132+
if (isChecked) {
133+
134+
} else {
135+
136+
}
137+
}
138+
});
104139
}
105140

106141
@Override
@@ -109,6 +144,8 @@ public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayO
109144
whenTime.set(Calendar.MONTH, monthOfYear);
110145
whenTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
111146

147+
useCurrentTime.setChecked(false);
148+
112149
updateDateTimeUI();
113150
}
114151

@@ -118,6 +155,14 @@ public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int seco
118155
whenTime.set(Calendar.MINUTE, minute);
119156
whenTime.set(Calendar.SECOND, second);
120157

158+
useCurrentTime.setChecked(false);
159+
160+
updateDateTimeUI();
161+
}
162+
163+
private void updateTime() {
164+
Calendar now = Calendar.getInstance();
165+
whenTime = now;
121166
updateDateTimeUI();
122167
}
123168

app/src/main/res/layout/activity_task_view.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
android:orientation="horizontal"
3333
app:layout_constraintEnd_toEndOf="parent"
3434
app:layout_constraintStart_toStartOf="parent"
35-
app:layout_constraintTop_toBottomOf="@+id/task_name_content">
35+
app:layout_constraintTop_toBottomOf="@+id/task_name_content"
36+
android:id="@+id/linearLayout">
3637

3738
<TextView
3839
android:id="@+id/task_when_date"
@@ -71,4 +72,15 @@
7172
app:layout_constraintHorizontal_bias="0.498"
7273
app:layout_constraintStart_toStartOf="parent" />
7374

75+
<CheckBox
76+
android:id="@+id/useCurrentTime"
77+
android:layout_width="wrap_content"
78+
android:layout_height="wrap_content"
79+
android:layout_marginStart="8dp"
80+
android:layout_marginTop="8dp"
81+
android:checked="true"
82+
android:text="Use Current Time"
83+
app:layout_constraintStart_toStartOf="parent"
84+
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
85+
7486
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)