1
+ package com .selimhorri .pack .activity .admin .dept ;
2
+
3
+ import android .content .Intent ;
4
+ import android .os .Bundle ;
5
+ import android .view .Menu ;
6
+ import android .view .MenuItem ;
7
+ import android .widget .Button ;
8
+ import android .widget .EditText ;
9
+ import android .widget .Toast ;
10
+
11
+ import androidx .annotation .NonNull ;
12
+ import androidx .appcompat .app .AppCompatActivity ;
13
+
14
+ import com .selimhorri .pack .R ;
15
+ import com .selimhorri .pack .activity .HomeActivity ;
16
+ import com .selimhorri .pack .activity .admin .AdminIndexActivity ;
17
+ import com .selimhorri .pack .activity .admin .AdminInfoActivity ;
18
+ import com .selimhorri .pack .activity .admin .loc .AdminLocationEditActivity ;
19
+ import com .selimhorri .pack .pattern .builder .DepartmentBuilder ;
20
+ import com .selimhorri .pack .service .DepartmentService ;
21
+ import com .selimhorri .pack .service .LocationService ;
22
+ import com .selimhorri .pack .service .impl .DepartmentServiceImpl ;
23
+ import com .selimhorri .pack .service .impl .LocationServiceImpl ;
24
+
25
+ public class AdminDepartmentEditActivity extends AppCompatActivity {
26
+
27
+ private final DepartmentService departmentService ;
28
+ private final LocationService locationService ;
29
+ private EditText editTextDepartmentName ;
30
+ private EditText editTextLocation ;
31
+ private Button btnEditDepartment ;
32
+
33
+ public AdminDepartmentEditActivity () {
34
+ this .departmentService = new DepartmentServiceImpl (AdminDepartmentEditActivity .this );
35
+ this .locationService = new LocationServiceImpl (AdminDepartmentEditActivity .this );
36
+ }
37
+
38
+ @ Override
39
+ protected void onCreate (Bundle savedInstanceState ) {
40
+ super .onCreate (savedInstanceState );
41
+ setContentView (R .layout .activity_admin_edit_department );
42
+
43
+ this .initialize ();
44
+
45
+ final Bundle extras = super .getIntent ().getExtras ();
46
+ final Integer departmentId = extras .getInt ("departmentId" );
47
+
48
+ this .departmentService .findById (
49
+ departmentId ,
50
+ department -> {
51
+ this .editTextDepartmentName .setText (department .getDepartmentName ());
52
+ this .editTextLocation .setText (String .valueOf (department .getLocation ().getLocationId ()));
53
+ },
54
+ error -> Toast .makeText (AdminDepartmentEditActivity .this , error .toString (), Toast .LENGTH_SHORT ).show ()
55
+ );
56
+
57
+ this .btnEditDepartment .setOnClickListener (v -> {
58
+ final String departmentName = this .editTextDepartmentName .getText ().toString ().trim ();
59
+ final String locationIdString = this .editTextLocation .getText ().toString ().trim ();
60
+
61
+ if (isEmpty (departmentName , locationIdString ))
62
+ Toast .makeText (AdminDepartmentEditActivity .this , "Field(s) is/are empty, plz try again!" , Toast .LENGTH_SHORT ).show ();
63
+ else
64
+ this .locationService .findById (
65
+ Integer .parseInt (locationIdString ),
66
+ location ->
67
+ this .departmentService .update (
68
+ new DepartmentBuilder ()
69
+ .departmentId (departmentId )
70
+ .departmentName (departmentName )
71
+ .location (location )
72
+ .build (),
73
+ department -> {
74
+ if (department == null || department .getLocation () == null )
75
+ Toast .makeText (AdminDepartmentEditActivity .this , "Department does not exist!" , Toast .LENGTH_SHORT ).show ();
76
+ else {
77
+ super .startActivity (
78
+ new Intent (AdminDepartmentEditActivity .this , AdminLocationEditActivity .class )
79
+ .putExtra ("departmentId" , department .getDepartmentId ())
80
+ );
81
+ Toast .makeText (AdminDepartmentEditActivity .this , "Department updated successfully!" , Toast .LENGTH_SHORT ).show ();
82
+ }
83
+ },
84
+ error -> Toast .makeText (AdminDepartmentEditActivity .this , error .toString (), Toast .LENGTH_SHORT ).show ()
85
+ ),
86
+ error -> Toast .makeText (AdminDepartmentEditActivity .this , error .toString (), Toast .LENGTH_SHORT ).show ()
87
+ );
88
+
89
+ });
90
+
91
+ }
92
+
93
+ private void initialize () {
94
+ this .editTextDepartmentName = super .findViewById (R .id .editTextAdminDepartmentEditDepartmentName );
95
+ this .editTextLocation = super .findViewById (R .id .editTextAdminDepartmentEditLocationId );
96
+ this .btnEditDepartment = super .findViewById (R .id .buttonAdminDepartmentEditEditDepartment );
97
+ }
98
+
99
+ private static boolean isEmpty (final String departmentName , final String locationIdString ) {
100
+ return departmentName .isEmpty () || locationIdString .isEmpty ();
101
+ }
102
+
103
+ @ Override
104
+ public boolean onCreateOptionsMenu (Menu menu ) {
105
+ super .getMenuInflater ()
106
+ .inflate (R .menu .menu_admin , menu );
107
+ return true ;
108
+ }
109
+
110
+ @ Override
111
+ public boolean onOptionsItemSelected (@ NonNull MenuItem item ) {
112
+
113
+ switch (item .getItemId ()) {
114
+ case R .id .adminAccountInfo :
115
+ super .startActivity (new Intent (AdminDepartmentEditActivity .this , AdminInfoActivity .class ));
116
+ return true ;
117
+ case R .id .adminCategories :
118
+ super .startActivity (new Intent (this , AdminIndexActivity .class ));
119
+ return true ;
120
+ case R .id .adminNewDepartment :
121
+ super .startActivity (new Intent (AdminDepartmentEditActivity .this , AdminDepartmentAddActivity .class ));
122
+ return true ;
123
+ case R .id .adminAbout :
124
+ // super.startActivity(new Intent(AdminDepartmentAddActivity.this, AdminInfoActivity.class));
125
+ return true ;
126
+ case R .id .adminLogout :
127
+ super .getSharedPreferences ("admin" , MODE_PRIVATE )
128
+ .edit ()
129
+ .clear ()
130
+ .apply ();
131
+ super .startActivity (new Intent (AdminDepartmentEditActivity .this , HomeActivity .class ));
132
+ return true ;
133
+ default :
134
+ return super .onOptionsItemSelected (item );
135
+ }
136
+ }
137
+
138
+ }
0 commit comments