Commit 54833820 authored by Willard's avatar Willard

Abstract user login/logout, add header in navbar

parent 53a89ce2
...@@ -23,9 +23,33 @@ import retrofit2.converter.gson.GsonConverterFactory; ...@@ -23,9 +23,33 @@ import retrofit2.converter.gson.GsonConverterFactory;
public class AppUtils { public class AppUtils {
public static CanteeneoApiInterface service; public static CanteeneoApiInterface service;
private static Context c; private static Context c;
public static String username = ""; private static String username = "";
public static String password = ""; private static String password = "";
public static int userId = -1; private static int userId = -1;
public static void login(String _username, String _password, int _userId) {
username = _username;
password = _password;
userId = _userId;
}
public static void logout() {
username = "";
password = "";
userId = -1;
}
public static String getUsername() {
return username;
}
public static String getPassword() {
return password;
}
public static int getUserId() {
return userId;
}
public static boolean isNetworkAvailable(Context c) { public static boolean isNetworkAvailable(Context c) {
ConnectivityManager connectivity =(ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager connectivity =(ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
......
...@@ -59,7 +59,7 @@ public class DishReviewActivity extends AppCompatActivity { ...@@ -59,7 +59,7 @@ public class DishReviewActivity extends AppCompatActivity {
rv.setBody(body.getText().toString()); rv.setBody(body.getText().toString());
rv.setRating(rating.getRating()); rv.setRating(rating.getRating());
rv.setDishId(id); rv.setDishId(id);
rv.setUserId(AppUtils.userId); rv.setUserId(AppUtils.getUserId());
if(editing) { if(editing) {
editReview(reviewId, rv); editReview(reviewId, rv);
......
...@@ -61,7 +61,7 @@ public class DishReviewAdapter extends BaseAdapter { ...@@ -61,7 +61,7 @@ public class DishReviewAdapter extends BaseAdapter {
Button editButton = (Button)v.findViewById(R.id.edit_button); Button editButton = (Button)v.findViewById(R.id.edit_button);
Button deleteButton = (Button)v.findViewById(R.id.delete_button); Button deleteButton = (Button)v.findViewById(R.id.delete_button);
if(dr.getUserId() == AppUtils.userId) { if(dr.getUserId() == AppUtils.getUserId()) {
editButton.setVisibility(View.VISIBLE); editButton.setVisibility(View.VISIBLE);
editButton.setOnClickListener(new View.OnClickListener() { editButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
......
...@@ -141,7 +141,7 @@ public class DishViewActivity extends AppCompatActivity { ...@@ -141,7 +141,7 @@ public class DishViewActivity extends AppCompatActivity {
List<DishReview> newReviews = response.body(); List<DishReview> newReviews = response.body();
DishReview ownedReview = null; DishReview ownedReview = null;
for(DishReview review: newReviews) { for(DishReview review: newReviews) {
if(review.getUserId() == AppUtils.userId) { if(review.getUserId() == AppUtils.getUserId()) {
ownedReview = review; ownedReview = review;
break; break;
} }
......
...@@ -93,8 +93,8 @@ public class LoginActivity extends AppCompatActivity { ...@@ -93,8 +93,8 @@ public class LoginActivity extends AppCompatActivity {
mUsernameView.setError(null); mUsernameView.setError(null);
mPasswordView.setError(null); mPasswordView.setError(null);
String username = mUsernameView.getText().toString(); final String username = mUsernameView.getText().toString();
String password = mPasswordView.getText().toString(); final String password = mPasswordView.getText().toString();
boolean cancel = false; boolean cancel = false;
View focusView = null; View focusView = null;
...@@ -116,8 +116,6 @@ public class LoginActivity extends AppCompatActivity { ...@@ -116,8 +116,6 @@ public class LoginActivity extends AppCompatActivity {
} else { } else {
showProgress(true); showProgress(true);
AppUtils.username = username;
AppUtils.password = password;
Call<Token> call = AppUtils.service.getToken(); Call<Token> call = AppUtils.service.getToken();
call.enqueue(new Callback<Token>() { call.enqueue(new Callback<Token>() {
@Override @Override
...@@ -128,8 +126,8 @@ public class LoginActivity extends AppCompatActivity { ...@@ -128,8 +126,8 @@ 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 " + AppUtils.username + " successfully.", Toast.LENGTH_LONG).show(); Toast.makeText(LoginActivity.this, "Logged in as " + username + " successfully.", Toast.LENGTH_LONG).show();
AppUtils.userId = 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);
} }
......
...@@ -4,44 +4,32 @@ import android.content.Context; ...@@ -4,44 +4,32 @@ import android.content.Context;
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;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView; import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
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.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testapp.entities.Cuisine;
import com.testapp.entities.Dish; import com.testapp.entities.Dish;
import com.testapp.entities.DishType;
import com.testapp.entities.Filter; import com.testapp.entities.Filter;
import com.testapp.entities.Location;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class NavDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { public class NavDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
...@@ -99,6 +87,8 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi ...@@ -99,6 +87,8 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
startActivity(i); startActivity(i);
} }
}); });
} }
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) {
......
...@@ -60,7 +60,7 @@ public class StallReviewActivity extends AppCompatActivity { ...@@ -60,7 +60,7 @@ public class StallReviewActivity extends AppCompatActivity {
rv.setBody(body.getText().toString()); rv.setBody(body.getText().toString());
rv.setRating(rating.getRating()); rv.setRating(rating.getRating());
rv.setStallId(id); rv.setStallId(id);
rv.setUserId(AppUtils.userId); rv.setUserId(AppUtils.getUserId());
if(editing) { if(editing) {
editReview(reviewId, rv); editReview(reviewId, rv);
......
...@@ -61,7 +61,7 @@ public class StallReviewAdapter extends BaseAdapter { ...@@ -61,7 +61,7 @@ public class StallReviewAdapter extends BaseAdapter {
Button editButton = (Button)v.findViewById(R.id.edit_button); Button editButton = (Button)v.findViewById(R.id.edit_button);
Button deleteButton = (Button)v.findViewById(R.id.delete_button); Button deleteButton = (Button)v.findViewById(R.id.delete_button);
if(sr.getUserId() == AppUtils.userId) { if(sr.getUserId() == AppUtils.getUserId()) {
editButton.setVisibility(View.VISIBLE); editButton.setVisibility(View.VISIBLE);
editButton.setOnClickListener(new View.OnClickListener() { editButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
......
...@@ -178,7 +178,7 @@ public class StallViewActivity extends AppCompatActivity { ...@@ -178,7 +178,7 @@ public class StallViewActivity extends AppCompatActivity {
List<StallReview> newReviews = response.body(); List<StallReview> newReviews = response.body();
StallReview ownedReview = null; StallReview ownedReview = null;
for(StallReview review: newReviews) { for(StallReview review: newReviews) {
if(review.getUserId() == AppUtils.userId) { if(review.getUserId() == AppUtils.getUserId()) {
ownedReview = review; ownedReview = review;
break; break;
} }
......
...@@ -35,6 +35,34 @@ ...@@ -35,6 +35,34 @@
android:id="@+id/header" android:id="@+id/header"
/> />
<RelativeLayout
android:id="@+id/logged_in_header"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/logout_button"
android:text="Log Out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
style="@style/Widget.AppCompat.Button.Borderless.Colored"/>
<TextView
android:id="@+id/greeting"
android:text="Hello, Willard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"/>
</RelativeLayout>
<Space <Space
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="10dp" /> android:layout_height="10dp" />
......
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