Commit 6d8f41a2 authored by Willard's avatar Willard

Add logout

parent 54833820
...@@ -39,6 +39,10 @@ public class AppUtils { ...@@ -39,6 +39,10 @@ public class AppUtils {
userId = -1; userId = -1;
} }
public static boolean isLoggedIn() {
return userId != -1;
}
public static String getUsername() { public static String getUsername() {
return username; return username;
} }
......
...@@ -116,6 +116,7 @@ public class LoginActivity extends AppCompatActivity { ...@@ -116,6 +116,7 @@ public class LoginActivity extends AppCompatActivity {
} else { } else {
showProgress(true); showProgress(true);
AppUtils.login(username, password, -1);
Call<Token> call = AppUtils.service.getToken(); Call<Token> call = AppUtils.service.getToken();
call.enqueue(new Callback<Token>() { call.enqueue(new Callback<Token>() {
@Override @Override
...@@ -126,7 +127,7 @@ public class LoginActivity extends AppCompatActivity { ...@@ -126,7 +127,7 @@ public class LoginActivity extends AppCompatActivity {
mPasswordView.setError(""); mPasswordView.setError("");
Toast.makeText(LoginActivity.this, "Invalid credentials!", Toast.LENGTH_LONG).show(); Toast.makeText(LoginActivity.this, "Invalid credentials!", Toast.LENGTH_LONG).show();
} else { } else {
Toast.makeText(LoginActivity.this, "Logged in as " + username + " successfully.", Toast.LENGTH_LONG).show(); Toast.makeText(LoginActivity.this, "Logged in as " + AppUtils.getUsername() + " successfully.", Toast.LENGTH_LONG).show();
AppUtils.login(username, password, response.body().getId()); AppUtils.login(username, password, response.body().getId());
Intent intent = new Intent(LoginActivity.this, NavDrawerActivity.class); Intent intent = new Intent(LoginActivity.this, NavDrawerActivity.class);
startActivity(intent); startActivity(intent);
......
package com.testapp; package com.testapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
...@@ -17,9 +20,13 @@ import android.view.Menu; ...@@ -17,9 +20,13 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ListView; import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.testapp.entities.Dish; import com.testapp.entities.Dish;
import com.testapp.entities.Filter; import com.testapp.entities.Filter;
...@@ -27,6 +34,7 @@ import com.testapp.entities.Filter; ...@@ -27,6 +34,7 @@ import com.testapp.entities.Filter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
...@@ -88,7 +96,32 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi ...@@ -88,7 +96,32 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
} }
}); });
RelativeLayout loggedInHeader = (RelativeLayout)findViewById(R.id.logged_in_header);
if(AppUtils.isLoggedIn()) {
loggedInHeader.setVisibility(View.VISIBLE);
((TextView)findViewById(R.id.greeting)).setText("Hello, " + AppUtils.getUsername());
findViewById(R.id.logout_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Activity context = NavDrawerActivity.this;
new AlertDialog.Builder(context)
.setTitle("Logout")
.setMessage("Are you sure you want to log out?")
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AppUtils.logout();
Intent i = new Intent(context, LoginActivity.class);
context.startActivity(i);
}
})
.setNegativeButton(R.string.no, null)
.show();
}
});
} else {
loggedInHeader.setVisibility(View.GONE);
}
} }
public <T extends Filter> void populateFilter(Call<List<T>> call, int layout_id) { public <T extends Filter> void populateFilter(Call<List<T>> call, int layout_id) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment