Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LdS 2020 Grupo14
App La Justa
Commits
7fe51f17
Commit
7fe51f17
authored
Dec 16, 2020
by
Daniel Cesanelli
Browse files
Agregado Login/logout
parent
e8f445d2
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/com/cesanelli/lajusta/MainActivity.java
View file @
7fe51f17
...
...
@@ -5,6 +5,7 @@ import android.os.Bundle;
import
android.preference.PreferenceManager
;
import
android.util.Log
;
import
android.view.Menu
;
import
android.view.MenuItem
;
import
androidx.appcompat.app.AppCompatActivity
;
import
androidx.appcompat.widget.Toolbar
;
...
...
@@ -19,33 +20,48 @@ import com.google.android.material.navigation.NavigationView;
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
);
NavigationView
navigationView
=
findViewById
(
R
.
id
.
nav_view
);
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
();
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
)
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
);
SharedPreferences
sharedpreferences
=
PreferenceManager
.
getDefaultSharedPreferences
(
getApplicationContext
());
sharedpreferences
.
registerOnSharedPreferenceChangeListener
(
new
SharedPreferences
.
OnSharedPreferenceChangeListener
()
{
@Override
public
void
onSharedPreferenceChanged
(
SharedPreferences
sharedPreferences
,
String
key
)
{
if
(
key
==
"JWT"
){
Log
.
v
(
"JWT CHANGED!!!"
,
sharedPreferences
.
getString
(
key
,
""
));
}
}
});
}
@Override
...
...
@@ -55,10 +71,22 @@ 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
app/src/main/java/com/cesanelli/lajusta/ui/LoginFragment.java
View file @
7fe51f17
...
...
@@ -71,8 +71,9 @@ public class LoginFragment extends Fragment {
public
void
onResponse
(
Call
<
Token
>
call
,
Response
<
Token
>
response
)
{
try
{
if
(
response
.
isSuccessful
())
{
Log
.
v
(
"LOGIN"
,
response
.
body
().
getValue
()
);
Toast
.
makeText
(
getContext
(),
"¡Bienvenido "
+
login_user
.
getText
().
toString
()+
"!"
,
Toast
.
LENGTH_SHORT
);
sharedpreferences
.
edit
().
putString
(
"JWT"
,
response
.
body
().
getValue
()).
apply
();
Navigation
.
findNavController
(
v
).
navigate
(
R
.
id
.
nav_products
);
}
}
catch
(
Exception
ex
)
{
Toast
.
makeText
(
getContext
(),
ex
.
getMessage
(),
Toast
.
LENGTH_LONG
).
show
();
...
...
app/src/main/res/menu/activity_main_drawer.xml
View file @
7fe51f17
...
...
@@ -28,5 +28,10 @@
android:id=
"@+id/nav_login"
android:icon=
"@drawable/ic_menu_gallery"
android:title=
"@string/menu_login"
/>
<item
android:id=
"@+id/nav_logout"
android:icon=
"@drawable/ic_menu_gallery"
android:title=
"@string/menu_logout"
android:visible=
"false"
/>
</group>
</menu>
\ No newline at end of file
app/src/main/res/values/strings.xml
View file @
7fe51f17
...
...
@@ -11,6 +11,7 @@
<string
name=
"menu_products"
>
Productos
</string>
<string
name=
"menu_product"
>
Producto
</string>
<string
name=
"menu_login"
>
Login
</string>
<string
name=
"menu_logout"
>
Cerrar Sesión
</string>
<string
name=
"menu_register"
>
Registrar
</string>
<string
name=
"menu_cart"
>
Carrito de Compras
</string>
<string
name=
"menu_purchase"
>
Detalle Compra
</string>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment