1
1
package com .example .taskmanager .Activity ;
2
2
3
3
import androidx .appcompat .app .AppCompatActivity ;
4
+
4
5
import android .app .AlertDialog ;
5
6
import android .app .DatePickerDialog ;
6
7
import android .app .TimePickerDialog ;
7
8
import android .content .Intent ;
8
9
import android .os .Bundle ;
9
- import android .util .Log ;
10
10
import android .view .View ;
11
11
import android .widget .ArrayAdapter ;
12
12
import android .widget .Button ;
15
15
import android .widget .Spinner ;
16
16
import android .widget .TextView ;
17
17
import android .widget .TimePicker ;
18
- import android .widget .Toast ;
19
-
18
+ import com .example .taskmanager .Database .RoomDB ;
20
19
import com .example .taskmanager .Utility .TaskModel ;
21
20
import com .example .taskmanager .R ;
22
-
23
21
import java .time .LocalDate ;
24
22
import java .util .Calendar ;
25
23
@@ -32,23 +30,23 @@ public class ModifyTaskActivity extends AppCompatActivity {
32
30
private static TextView timeView ;
33
31
private static Button saveBtn ;
34
32
private static TextView priorityView ;
35
- String currentPriority = "Low" ;
36
33
String currentDate = "NotSpecified" ;
37
- String currentTime = "non" ;
34
+ TaskModel task ;
38
35
39
36
@ Override
40
37
protected void onCreate (Bundle savedInstanceState ) {
41
38
super .onCreate (savedInstanceState );
42
39
setContentView (R .layout .activity_modify_task );
40
+
43
41
//Declaring Views
44
- titleLb = ( TextView ) findViewById (R .id .titleLb );
45
- nameView = ( EditText ) findViewById (R .id .nameView );
46
- descriptionView = ( EditText ) findViewById (R .id .descriptionView );
47
- prioritySpinner = ( Spinner ) findViewById (R .id .prioritySpinner );
48
- dateView = ( TextView ) findViewById (R .id .dateView );
49
- timeView = ( TextView ) findViewById (R .id .timeView );
50
- saveBtn = ( Button ) findViewById (R .id .saveBtn );
51
- priorityView = ( TextView ) findViewById (R .id .priorityView );
42
+ titleLb = findViewById (R .id .titleLb );
43
+ nameView = findViewById (R .id .nameView );
44
+ descriptionView = findViewById (R .id .descriptionView );
45
+ prioritySpinner = findViewById (R .id .prioritySpinner );
46
+ dateView = findViewById (R .id .dateView );
47
+ timeView = findViewById (R .id .timeView );
48
+ saveBtn = findViewById (R .id .saveBtn );
49
+ priorityView = findViewById (R .id .priorityView );
52
50
53
51
// Adding items to priority Spinner
54
52
Spinner prioritySpinner = findViewById (R .id .prioritySpinner );
@@ -60,12 +58,13 @@ protected void onCreate(Bundle savedInstanceState) {
60
58
61
59
//Receive info from mainActivity
62
60
Intent i = getIntent ();
63
- TaskModel task = (TaskModel ) i .getSerializableExtra ("task" );
61
+ task = (TaskModel ) i .getSerializableExtra ("task" );
64
62
65
63
assert task != null ;
66
64
nameView .setText (task .getTitle ());
67
65
68
- prioritySpinner .setSelection (Integer .parseInt (task .getCurrentPriority ()));
66
+
67
+ prioritySpinner .setSelection (adapter .getPosition (task .getCurrentPriority ()));
69
68
priorityView .setText (prioritySpinner .getSelectedItem ().toString ());
70
69
dateView .setText (task .getCurrentDate ());
71
70
descriptionView .setText (task .getDescription ());
@@ -74,7 +73,7 @@ protected void onCreate(Bundle savedInstanceState) {
74
73
titleLb .setText (activityType + " Task" );
75
74
76
75
assert activityType != null ;
77
- if (activityType .equals ("View" )){
76
+ if (activityType .equals ("View" )) {
78
77
nameView .setEnabled (false );
79
78
prioritySpinner .setEnabled (false );
80
79
dateView .setEnabled (false );
@@ -83,8 +82,7 @@ protected void onCreate(Bundle savedInstanceState) {
83
82
saveBtn .setVisibility (View .GONE );
84
83
prioritySpinner .setVisibility (View .GONE );
85
84
priorityView .setVisibility (View .VISIBLE );
86
- }
87
- else {//Modify
85
+ } else {//Modify
88
86
nameView .setEnabled (true );
89
87
prioritySpinner .setEnabled (true );
90
88
dateView .setEnabled (true );
@@ -95,6 +93,7 @@ protected void onCreate(Bundle savedInstanceState) {
95
93
priorityView .setVisibility (View .GONE );
96
94
}
97
95
}
96
+
98
97
public void changeCalender (View v ) {
99
98
int currentMonth = LocalDate .now ().getMonthValue () - 1 ;
100
99
int currentDay = LocalDate .now ().getDayOfMonth ();
@@ -104,7 +103,7 @@ public void changeCalender(View v) {
104
103
@ Override
105
104
public void onDateSet (DatePicker view , int selectedYear , int selectedMonth , int selectedDayOfMonth ) {
106
105
// Construct selected date string
107
- currentDate = String .valueOf (selectedDayOfMonth ) + '-' + String . valueOf (selectedMonth + 1 ) + '-' + String . valueOf ( selectedYear ) ;
106
+ currentDate = String .valueOf (selectedDayOfMonth ) + '-' + (selectedMonth + 1 ) + '-' + selectedYear ;
108
107
109
108
// Compare selected date with current date
110
109
Calendar selectedCalendar = Calendar .getInstance ();
@@ -127,10 +126,11 @@ public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int
127
126
// Show the DatePickerDialog
128
127
datePickerDialog .show ();
129
128
}
130
- public void changeTime (View v ){
129
+
130
+ public void changeTime (View v ) {
131
131
Calendar calendar = Calendar .getInstance ();
132
132
int hour = calendar .get (Calendar .HOUR_OF_DAY );
133
- int minutes = calendar .get (calendar .MINUTE );
133
+ int minutes = calendar .get (Calendar .MINUTE );
134
134
TimePickerDialog timePickerDialog = new TimePickerDialog (this , new TimePickerDialog .OnTimeSetListener () {
135
135
@ Override
136
136
public void onTimeSet (TimePicker view , int hourOfDay , int minute ) {
@@ -141,10 +141,9 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
141
141
selectedTime .set (Calendar .MINUTE , minute );
142
142
143
143
String amPm ;
144
- if (hourOfDay >= 12 ){
144
+ if (hourOfDay >= 12 ) {
145
145
amPm = "PM" ;
146
- }
147
- else {
146
+ } else {
148
147
amPm = "AM" ;
149
148
}
150
149
@@ -153,39 +152,47 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
153
152
// Prompt user to select a future time
154
153
// Show message
155
154
AlertDialog .Builder builder = new AlertDialog .Builder (ModifyTaskActivity .this );
156
- builder .setTitle ("Incorrect Info" ).setMessage (hourOfDay + ":" + minute + amPm + " has passed buddy!" );
155
+ builder .setTitle ("Incorrect Info" ).setMessage (hourOfDay + ":" + minute + amPm + " has passed buddy!" );
157
156
builder .show ();
158
157
} else {
159
- ((TextView )findViewById (R .id .timeView )).setText ((hourOfDay % 12 )+ ":" + minute + " " + amPm );
158
+ ((TextView ) findViewById (R .id .timeView )).setText ((hourOfDay % 12 ) + ":" + minute + " " + amPm );
160
159
}
161
160
}
162
161
}, hour , minutes , false );
163
162
timePickerDialog .show ();
164
163
}
165
- public void returnToMain (View v ){
166
- Intent i = new Intent (this ,MainActivity .class );
167
- startActivity (i );
164
+
165
+ public void returnToMain (View v )
166
+ {
167
+ finish ();
168
168
}
169
- public void save (View v ){
169
+
170
+ public void save (View v ) {
170
171
String name = nameView .getText ().toString ().trim ();
171
172
String description = descriptionView .getText ().toString ();
172
- String priority = prioritySpinner .getSelectedItem ().toString ();
173
- Log .d ("success" ,priority );
174
- String date = dateView .getText ().toString ();
175
- Log .d ("success" ,date );
176
- String time = timeView .getText ().toString ();
177
- Log .d ("success" ,time );
178
- if (name .equals ("" )){
173
+
174
+ if (name .isEmpty ()) {
179
175
AlertDialog .Builder builder = new AlertDialog .Builder (ModifyTaskActivity .this );
180
176
builder .setTitle ("Incorrect Info" )
181
177
.setMessage ("Please enter task name" );
182
178
builder .show ();
183
- }
184
- else {
185
- String saved = "Your Task \" " + nameView .getText ().toString ().trim () + "\" has been saved Successfully!" ;
186
- Toast .makeText (this , saved , Toast .LENGTH_SHORT ).show ();
187
- Intent i = new Intent (this , MainActivity .class );
188
- startActivity (i );
179
+ } else if (description .isEmpty ()) {
180
+ AlertDialog .Builder builder = new AlertDialog .Builder (ModifyTaskActivity .this );
181
+ builder .setTitle ("Incorrect Info" )
182
+ .setMessage ("Please enter task description" );
183
+ builder .show ();
184
+ } else {
185
+ task .setTitle (name );
186
+ task .setDescription (description );
187
+ task .setCurrentDate (dateView .getText ().toString ());
188
+ task .setCurrentTime (timeView .getText ().toString ());
189
+ task .setCurrentPriority (prioritySpinner .getSelectedItem ().toString ());
190
+
191
+ RoomDB instance = RoomDB .getInstance (this );
192
+
193
+ new Thread (() -> instance .taskDAO ().updateTask (task )).start ();
194
+
195
+ finish ();
189
196
}
190
197
}
191
198
}
0 commit comments