Commit e54a0e15 authored by Daniel Cesanelli's avatar Daniel Cesanelli
Browse files

Agregado logo, ajustados colores, agregado chequeo de token al inicio y manejo...

Agregado logo, ajustados colores, agregado chequeo de token al inicio y manejo de sesion con view model
parent 7fe51f17
......@@ -3,65 +3,108 @@ package com.cesanelli.lajusta;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.cesanelli.lajusta.io.ApiAdapter;
import com.cesanelli.lajusta.io.models.Token;
import com.cesanelli.lajusta.io.models.User;
import com.cesanelli.lajusta.ui.viewmodel.UserViewModel;
import com.google.android.material.navigation.NavigationView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private SharedPreferences sharedpreferences;
private NavigationView navigationView;
private SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key == "JWT") {
String jwt = sharedPreferences.getString(key, "");
navigationView.getMenu().findItem(R.id.nav_login).setVisible(jwt == "");
navigationView.getMenu().findItem(R.id.nav_logout).setVisible(jwt != "");
Log.v("JWT CHANGED!!!", sharedPreferences.getString(key, ""));
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedpreferences.registerOnSharedPreferenceChangeListener(listener);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_quienes_somos, R.id.nav_news, R.id.nav_producers, R.id.nav_products, R.id.nav_cart, R.id.nav_login)
.setDrawerLayout(drawer)
.build();
UserViewModel userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
userViewModel.getToken().observe(this, token -> {
String jwt = sharedpreferences.getString("JWT", "");
navigationView.getMenu().findItem(R.id.nav_login).setVisible(jwt == "");
navigationView.getMenu().findItem(R.id.nav_logout).setVisible(jwt != "");
TextView email = (TextView) navigationView.getHeaderView(0).findViewById(R.id.text_email);
email.setText(jwt == "" ? "" : token.getUser().getFirstName() + " " + token.getUser().getLastName());
});
navigationView = findViewById(R.id.nav_view);
navigationView.getMenu().findItem(R.id.nav_logout).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
sharedpreferences.edit().putString("JWT", "").apply();
sharedpreferences.edit().putString("UserID", "").apply();
userViewModel.setToken(new Token());
return true;
}
});
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_quienes_somos, R.id.nav_news, R.id.nav_producers, R.id.nav_products, R.id.nav_cart, R.id.nav_login)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
checkToken();
}
private void checkToken() {
String jwt = sharedpreferences.getString("JWT", "");
String userId = sharedpreferences.getString("UserID", "");
UserViewModel userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
if (jwt != "" && userId != "") {
Call<User> call = ApiAdapter.getApiService().getUser(Integer.parseInt(userId), "Bearer " + jwt);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
Token token = new Token();
try {
if (response.isSuccessful()) {
token.setValue(jwt);
token.setUser(response.body());
}
} catch (Exception ex) {
sharedpreferences.edit().putString("JWT", "").apply();
sharedpreferences.edit().putString("UserID", "").apply();
} finally {
userViewModel.setToken(token);
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
sharedpreferences.edit().putString("JWT", "").apply();
}
});
} else {
userViewModel.setToken(new Token());
}
}
@Override
......@@ -71,22 +114,10 @@ public class MainActivity extends AppCompatActivity {
return true;
}
@Override
protected void onResume() {
sharedpreferences.registerOnSharedPreferenceChangeListener(listener);
super.onResume();
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
protected void onPause() {
sharedpreferences.unregisterOnSharedPreferenceChangeListener(listener);
super.onPause();
}
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Path;
......@@ -32,4 +33,7 @@ public interface ApiService {
@POST("user/signup")
Call<User> signup(@Body User body);
@GET("user/{id}")
Call<User> getUser(@Path("id") int id, @Header("Authorization") String authHeader);
}
......@@ -4,6 +4,10 @@ public class Token {
private String value;
private User user;
public Token(){
value = "";
user = new User();
}
public String getValue() {
return value;
}
......
......@@ -14,12 +14,15 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import com.cesanelli.lajusta.R;
import com.cesanelli.lajusta.io.ApiAdapter;
import com.cesanelli.lajusta.io.models.Token;
import com.cesanelli.lajusta.io.models.TokenBody;
import com.cesanelli.lajusta.ui.viewmodel.ProductListViewModel;
import com.cesanelli.lajusta.ui.viewmodel.UserViewModel;
import retrofit2.Call;
import retrofit2.Callback;
......@@ -71,8 +74,12 @@ public class LoginFragment extends Fragment {
public void onResponse(Call<Token> call, Response<Token> response) {
try {
if (response.isSuccessful()) {
Toast.makeText(getContext(),"¡Bienvenido "+ login_user.getText().toString()+"!",Toast.LENGTH_SHORT);
sharedpreferences.edit().putString("JWT", response.body().getValue()).apply();
Token token = response.body();
sharedpreferences.edit().putString("JWT", token.getValue()).apply();
sharedpreferences.edit().putString("UserID", Integer.toString(token.getUser().getId())).apply();
UserViewModel userViewModel = new ViewModelProvider(getActivity()).get(UserViewModel.class);
userViewModel.setToken(token);
Toast.makeText(getContext(),"¡Bienvenido "+ token.getUser().getName()+"!",Toast.LENGTH_SHORT).show();
Navigation.findNavController(v).navigate(R.id.nav_products);
}
} catch (Exception ex) {
......
......@@ -11,19 +11,19 @@ import com.cesanelli.lajusta.io.models.Product;
import java.util.List;
public class CartViewModel extends ViewModel {
final private MutableLiveData<Cart> cartView = new MutableLiveData<Cart>();
final private MutableLiveData<Cart> cart = new MutableLiveData<Cart>();
public CartViewModel() {
cartView.setValue(new Cart());
cart.setValue(new Cart());
}
public LiveData<Cart> getCart() {
return cartView;
return cart;
}
public void addProduct(Product product) {
Boolean exists = false;
Cart cart = this.cartView.getValue();
Cart cart = this.cart.getValue();
List<CartItem> items = cart.getItems();
for (CartItem cartItem : items) {
if (cartItem.getProduct().getId() == product.getId()) {
......@@ -36,12 +36,12 @@ public class CartViewModel extends ViewModel {
}
cart.setQuantity(cart.getQuantity() + 1);
cart.setTotal(cart.getTotal() + product.getBuyPrice());
cartView.setValue(cart);
this.cart.setValue(cart);
}
public void deleteProduct(Integer id) {
CartItem cartItem = null;
Cart cart = this.cartView.getValue();
Cart cart = this.cart.getValue();
List<CartItem> items = cart.getItems();
for (CartItem item : items) {
if (item.getProduct().getId() == id) {
......@@ -55,6 +55,6 @@ public class CartViewModel extends ViewModel {
cart.setQuantity(cart.getQuantity() - cartItem.getQuantity());
cart.setTotal(cart.getTotal() - (cartItem.getQuantity() * cartItem.getProduct().getBuyPrice()));
cartView.setValue(cartView.getValue());
this.cart.setValue(this.cart.getValue());
}
}
......@@ -9,13 +9,13 @@ import com.cesanelli.lajusta.io.models.Producer;
import java.util.List;
public class ProducerListViewModel extends ViewModel {
final private MutableLiveData<List<Producer>> selectedList = new MutableLiveData<List<Producer>>();
final private MutableLiveData<List<Producer>> producers = new MutableLiveData<List<Producer>>();
public LiveData<List<Producer>> getProducerList() {
return selectedList;
return producers;
}
public void setProducerList(List<Producer> producers) {
selectedList.setValue(producers);
this.producers.setValue(producers);
}
}
\ No newline at end of file
......@@ -7,13 +7,13 @@ import androidx.lifecycle.ViewModel;
import com.cesanelli.lajusta.io.models.Producer;
public class ProducerViewModel extends ViewModel {
final private MutableLiveData<Producer> selectedItem = new MutableLiveData<Producer>();
final private MutableLiveData<Producer> producer = new MutableLiveData<Producer>();
public LiveData<Producer> getProducer() {
return selectedItem;
return producer;
}
public void setProducer(Producer producer) {
selectedItem.setValue(producer);
this.producer.setValue(producer);
}
}
......@@ -6,17 +6,16 @@ import androidx.lifecycle.ViewModel;
import com.cesanelli.lajusta.io.models.Product;
import java.util.ArrayList;
import java.util.List;
public class ProductListViewModel extends ViewModel {
final private MutableLiveData<List<Product>> selectedList = new MutableLiveData<List<Product>>();
final private MutableLiveData<List<Product>> products = new MutableLiveData<List<Product>>();
public LiveData<List<Product>> getProductList() {
return selectedList;
return products;
}
public void setProductList(List<Product> products) {
selectedList.setValue(products);
this.products.setValue(products);
}
}
\ No newline at end of file
......@@ -7,13 +7,13 @@ import androidx.lifecycle.ViewModel;
import com.cesanelli.lajusta.io.models.Product;
public class ProductViewModel extends ViewModel {
final private MutableLiveData<Product> selectedItem = new MutableLiveData<Product>();
final private MutableLiveData<Product> product = new MutableLiveData<Product>();
public LiveData<Product> getProduct() {
return selectedItem;
return product;
}
public void setProduct(Product product) {
selectedItem.setValue(product);
this.product.setValue(product);
}
}
package com.cesanelli.lajusta.ui.viewmodel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.cesanelli.lajusta.io.models.Token;
public class UserViewModel extends ViewModel {
final private MutableLiveData<Token> token = new MutableLiveData<Token>();
public LiveData<Token> getToken() {
return token;
}
public void setToken(Token token) {
this.token.setValue(token);
}
}
This diff is collapsed.
......@@ -2,8 +2,8 @@
android:shape="rectangle">
<gradient
android:angle="135"
android:centerColor="#009688"
android:endColor="#00695C"
android:startColor="#4DB6AC"
android:centerColor="#1B5E20"
android:endColor="#1B5E20"
android:startColor="#1B5E20"
android:type="linear" />
</shape>
\ No newline at end of file
......@@ -2,15 +2,10 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="usuario" type="String" />
<variable name="email" type="String" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:background="?attr/colorPrimary"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
......@@ -21,24 +16,20 @@
<ImageView
android:id="@+id/img_usuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/text_usuario"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="120dp"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@{usuario}"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
android:textAlignment="center"
app:srcCompat="@drawable/ic_la_justa_vector"
app:tint="@color/white" />
<TextView
android:id="@+id/text_email"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{email}" />
android:text="@string/txt_username"
android:textAlignment="center"
android:textSize="22sp" />
</LinearLayout>
</layout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="verde">#FF1B5E20</color>
<color name="verde_claro">#FF88B24E</color>
<color name="naranja">#FFFF7043</color>
<color name="ambar">#FFFFCA28</color>
<color name="marron">#FF6D4C41</color>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="teal_700">#1B5E20</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>
\ No newline at end of file
......@@ -2,12 +2,12 @@
<!-- Base application theme. -->
<style name="Theme.LaJusta" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorPrimary">@color/verde</item>
<item name="colorPrimaryVariant">@color/verde_claro</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorSecondary">@color/verde</item>
<item name="colorSecondaryVariant">@color/verde_claro</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
......
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