1+ package org .openimis .imispolicies ;
2+
3+ import android .app .AlertDialog ;
4+ import android .app .ProgressDialog ;
5+ import android .content .Intent ;
6+ import android .os .Bundle ;
7+ import android .text .Editable ;
8+ import android .text .TextWatcher ;
9+ import android .view .MenuItem ;
10+ import android .view .View ;
11+ import android .widget .ProgressBar ;
12+ import android .widget .TextView ;
13+
14+ import androidx .activity .EdgeToEdge ;
15+ import androidx .annotation .NonNull ;
16+ import androidx .appcompat .app .AppCompatActivity ;
17+ import androidx .core .graphics .Insets ;
18+ import androidx .core .view .ViewCompat ;
19+ import androidx .core .view .WindowInsetsCompat ;
20+
21+ import com .google .android .material .button .MaterialButton ;
22+ import com .google .android .material .textfield .TextInputEditText ;
23+ import com .google .android .material .textfield .TextInputLayout ;
24+
25+ import org .openimis .imispolicies .util .AndroidUtils ;
26+
27+ public class LoginActivity extends AppCompatActivity {
28+
29+ TextInputLayout layoutLoginName , layoutPassword ;
30+ MaterialButton btnLogin ;
31+ TextInputEditText txtLoginName , txtPassword ;
32+ String officerCode ;
33+ ClientAndroidInterface ca ;
34+ int page ;
35+ private ProgressDialog progressDialog ;
36+ ProgressBar progressBar ;
37+
38+ @ Override
39+ protected void onCreate (Bundle savedInstanceState ) {
40+ super .onCreate (savedInstanceState );
41+ setContentView (R .layout .activity_login );
42+ setTitle (getResources ().getString (R .string .Login ));
43+ if (getSupportActionBar () != null ) {
44+ getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
45+ }
46+ ca = new ClientAndroidInterface (this );
47+ progressDialog = new ProgressDialog (this );
48+ officerCode = ca .getOfficerCode ();
49+ page = getIntent ().getIntExtra ("Page" ,0 );
50+ initViews ();
51+ canSave ();
52+ setupListenners ();
53+
54+ btnLogin .setOnClickListener (new View .OnClickListener () {
55+ @ Override
56+ public void onClick (View view ) {
57+ String username = txtLoginName .getText ().toString ();
58+ String password = txtPassword .getText ().toString ();
59+
60+ boolean hasInternet = ca .CheckInternetAvailable ();
61+ if (!hasInternet ){
62+ ca .ShowDialog (getResources ().getString (R .string .NoInternet ));
63+ } else {
64+ progressBar .setVisibility (View .VISIBLE );
65+ boolean loggedIn = ca .LoginJI (username , password );
66+ if (loggedIn ){
67+ if (page == 0 ){
68+ Intent intent = new Intent (LoginActivity .this , SyncActivity .class );
69+ startActivity (intent );
70+ } else if (page == 1 ) {
71+ Intent intent = new Intent (LoginActivity .this , SearchActivity .class );
72+ startActivity (intent );
73+ } else if (page == 2 ) {
74+ Intent intent = new Intent (LoginActivity .this , Enrolment .class );
75+ startActivity (intent );
76+ } else if (page == 4 ) {
77+ ca .launchActivity ("Reports" );
78+ } else if (page == 5 ) {
79+ ca .launchActivity ("Enquire" );
80+ } else {
81+ Intent intent = new Intent (LoginActivity .this , MainActivity .class );
82+ startActivity (intent );
83+ }
84+ progressBar .setVisibility (View .GONE );
85+ finish ();
86+ } else {
87+ progressDialog .dismiss ();
88+ ca .ShowDialog (getResources ().getString (R .string .LoginFail ));
89+ }
90+ }
91+
92+ }
93+ });
94+ }
95+
96+ @ Override
97+ public boolean onOptionsItemSelected (@ NonNull MenuItem item ) {
98+ if (item .getItemId () == android .R .id .home ) {
99+ finish ();
100+ return true ;
101+ }
102+ return super .onOptionsItemSelected (item );
103+ }
104+
105+ private void initViews (){
106+ layoutPassword = findViewById (R .id .layoutPassword );
107+ layoutLoginName = findViewById (R .id .layoutLoginName );
108+ btnLogin = findViewById (R .id .btnLogin );
109+ txtLoginName = findViewById (R .id .txtLoginName );
110+ txtPassword = findViewById (R .id .txtPassword );
111+ progressBar = findViewById (R .id .loginProgressBar );
112+
113+ txtLoginName .setText (officerCode );
114+ }
115+
116+ private void canSave (){
117+ if (txtLoginName .getText ().toString ().isEmpty () ||
118+ txtPassword .getText ().toString ().isEmpty ()
119+ ){
120+ btnLogin .setEnabled (false );
121+ } else {
122+ btnLogin .setEnabled (true );
123+ }
124+ }
125+
126+ private void setupListenners (){
127+ txtLoginName .addTextChangedListener (new TextWatcher () {
128+ @ Override
129+ public void beforeTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {
130+
131+ }
132+
133+ @ Override
134+ public void onTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {
135+ canSave ();
136+ }
137+
138+ @ Override
139+ public void afterTextChanged (Editable editable ) {
140+
141+ }
142+ });
143+
144+ txtPassword .addTextChangedListener (new TextWatcher () {
145+ @ Override
146+ public void beforeTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {
147+
148+ }
149+
150+ @ Override
151+ public void onTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {
152+ canSave ();
153+ }
154+
155+ @ Override
156+ public void afterTextChanged (Editable editable ) {
157+
158+ }
159+ });
160+ }
161+ }
0 commit comments