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
c7886e09
Commit
c7886e09
authored
Dec 28, 2020
by
Daniel Cesanelli
Browse files
Agregado filtro en productos
parent
129a9baa
Changes
5
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/com/cesanelli/lajusta/io/models/Category.java
View file @
c7886e09
...
...
@@ -2,6 +2,8 @@ package com.cesanelli.lajusta.io.models;
import
com.fasterxml.jackson.annotation.JsonIgnoreProperties
;
import
java.util.Objects
;
@JsonIgnoreProperties
public
class
Category
{
private
int
id
;
...
...
@@ -9,6 +11,14 @@ public class Category {
private
String
name
;
private
int
level
;
public
Category
()
{
}
public
Category
(
int
id
,
String
name
)
{
this
.
id
=
id
;
this
.
name
=
name
;
}
public
int
getId
()
{
return
id
;
}
...
...
@@ -40,4 +50,23 @@ public class Category {
public
void
setLevel
(
int
level
)
{
this
.
level
=
level
;
}
@Override
public
String
toString
()
{
if
(
name
.
length
()
==
0
)
return
name
;
return
name
.
toUpperCase
().
substring
(
0
,
1
)
+
name
.
toLowerCase
().
substring
(
1
);
}
@Override
public
boolean
equals
(
Object
o
)
{
if
(
this
==
o
)
return
true
;
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
return
false
;
Category
category
=
(
Category
)
o
;
return
id
==
category
.
id
;
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
id
);
}
}
app/src/main/java/com/cesanelli/lajusta/ui/ProductsFragment.java
View file @
c7886e09
...
...
@@ -4,6 +4,9 @@ import android.os.Bundle;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
android.widget.AdapterView
;
import
android.widget.ArrayAdapter
;
import
android.widget.Spinner
;
import
android.widget.Toast
;
import
androidx.annotation.NonNull
;
...
...
@@ -16,6 +19,7 @@ import androidx.recyclerview.widget.RecyclerView;
import
com.cesanelli.lajusta.R
;
import
com.cesanelli.lajusta.databinding.FragmentProductsListBinding
;
import
com.cesanelli.lajusta.io.models.Category
;
import
com.cesanelli.lajusta.io.models.Product
;
import
com.cesanelli.lajusta.ui.viewmodel.CartViewModel
;
import
com.cesanelli.lajusta.ui.viewmodel.ProductListViewModel
;
...
...
@@ -23,7 +27,7 @@ import com.cesanelli.lajusta.ui.viewmodel.ProductViewModel;
import
java.util.ArrayList
;
public
class
ProductsFragment
extends
Fragment
implements
ProductsRecyclerViewAdapter
.
OnProductListener
{
public
class
ProductsFragment
extends
Fragment
implements
ProductsRecyclerViewAdapter
.
OnProductListener
,
AdapterView
.
OnItemSelectedListener
{
private
ProductViewModel
productViewModel
;
private
ProductListViewModel
productListViewModel
;
private
CartViewModel
cartViewModel
;
...
...
@@ -68,6 +72,15 @@ public class ProductsFragment extends Fragment implements ProductsRecyclerViewAd
recyclerView
.
setAdapter
(
new
ProductsRecyclerViewAdapter
((
ArrayList
<
Product
>)
list
,
this
));
}
});
productListViewModel
.
getCategories
().
observe
(
getViewLifecycleOwner
(),
categories
->
{
if
(
categories
!=
null
)
{
Spinner
categoriesSpinner
=
(
Spinner
)
view
.
findViewById
(
R
.
id
.
categories
);
ArrayAdapter
adapter
=
new
ArrayAdapter
(
getContext
(),
android
.
R
.
layout
.
simple_spinner_item
,
categories
);
adapter
.
setDropDownViewResource
(
android
.
R
.
layout
.
simple_spinner_dropdown_item
);
categoriesSpinner
.
setAdapter
(
adapter
);
categoriesSpinner
.
setOnItemSelectedListener
(
this
);
}
});
}
@Override
...
...
@@ -79,7 +92,16 @@ public class ProductsFragment extends Fragment implements ProductsRecyclerViewAd
@Override
public
void
onProductAdd
(
Product
product
)
{
cartViewModel
.
addProduct
(
product
);
Toast
.
makeText
(
getContext
(),
"Se agregó "
+
product
.
getTitle
()
+
" al carrito."
,
Toast
.
LENGTH_SHORT
).
show
();
Toast
.
makeText
(
getContext
(),
"Se agregó "
+
product
.
getTitle
()
+
" al carrito."
,
Toast
.
LENGTH_SHORT
).
show
();
}
@Override
public
void
onItemSelected
(
AdapterView
<?>
parent
,
View
view
,
int
position
,
long
id
)
{
productListViewModel
.
setCategory
((
Category
)
parent
.
getItemAtPosition
(
position
));
}
@Override
public
void
onNothingSelected
(
AdapterView
<?>
parent
)
{
}
}
\ No newline at end of file
app/src/main/java/com/cesanelli/lajusta/ui/viewmodel/ProductListViewModel.java
View file @
c7886e09
...
...
@@ -5,10 +5,14 @@ import androidx.lifecycle.MutableLiveData;
import
androidx.lifecycle.ViewModel
;
import
com.cesanelli.lajusta.io.ApiAdapter
;
import
com.cesanelli.lajusta.io.models.Category
;
import
com.cesanelli.lajusta.io.models.Product
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
import
retrofit2.Call
;
import
retrofit2.Callback
;
...
...
@@ -17,6 +21,8 @@ import retrofit2.Response;
public
class
ProductListViewModel
extends
ViewModel
{
private
MutableLiveData
<
List
<
Product
>>
products
;
private
ArrayList
<
Product
>
productList
;
private
MutableLiveData
<
List
<
Category
>>
categories
;
public
LiveData
<
List
<
Product
>>
getProductList
()
{
if
(
products
==
null
)
{
...
...
@@ -27,11 +33,46 @@ public class ProductListViewModel extends ViewModel {
}
public
void
setProductList
(
List
<
Product
>
products
)
{
this
.
productList
=
(
ArrayList
<
Product
>)
products
;
Set
<
Category
>
categoriesList
=
new
HashSet
<
Category
>();
for
(
Product
p
:
products
)
{
categoriesList
.
addAll
(
Arrays
.
asList
(
p
.
getCategories
()));
}
ArrayList
<
Category
>
categories
=
new
ArrayList
<
Category
>();
categories
.
add
(
new
Category
(
0
,
""
));
categories
.
addAll
(
categoriesList
);
this
.
products
.
setValue
(
products
);
this
.
categories
.
setValue
(
categories
);
}
public
LiveData
<
List
<
Category
>>
getCategories
()
{
if
(
categories
==
null
)
{
categories
=
new
MutableLiveData
<
List
<
Category
>>();
}
return
categories
;
}
public
void
setCategory
(
Category
category
)
{
if
(
category
==
null
||
category
.
getId
()
==
0
)
{
this
.
products
.
setValue
(
productList
);
return
;
}
ArrayList
<
Product
>
filteredProducts
=
new
ArrayList
<
Product
>();
for
(
Product
p
:
productList
)
{
for
(
Category
c
:
p
.
getCategories
())
{
if
(
c
.
getId
()
==
category
.
getId
())
{
filteredProducts
.
add
(
p
);
break
;
}
}
}
this
.
products
.
setValue
(
filteredProducts
);
}
private
void
loadProducts
()
{
List
<
Product
>
products
=
new
ArrayList
<
Product
>();
Call
<
ArrayList
<
Product
>>
call
=
ApiAdapter
.
getApiService
().
getProducts
();
call
.
enqueue
(
new
Callback
<
ArrayList
<
Product
>>()
{
...
...
@@ -42,11 +83,13 @@ public class ProductListViewModel extends ViewModel {
setProductList
(
response
.
body
());
}
}
catch
(
Exception
ex
)
{
System
.
out
.
println
(
ex
.
toString
());
}
}
@Override
public
void
onFailure
(
Call
<
ArrayList
<
Product
>>
call
,
Throwable
t
)
{
System
.
out
.
println
(
t
.
toString
());
}
});
}
...
...
app/src/main/res/layout/fragment_products_list.xml
View file @
c7886e09
...
...
@@ -16,6 +16,18 @@
android:layout_height=
"match_parent"
tools:context=
".ui.CartFragment"
>
<Spinner
android:id=
"@+id/categories"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_marginStart=
"150dp"
android:layout_marginTop=
"10dp"
android:layout_marginEnd=
"20dp"
android:textAlignment=
"textEnd"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:id=
"@+id/listProducts"
android:layout_width=
"match_parent"
...
...
@@ -27,7 +39,7 @@
app:layout_constraintBottom_toTopOf=
"@+id/totalLayout"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_to
TopOf=
"parent
"
app:layout_constraintTop_to
BottomOf=
"@+id/categories
"
tools:context=
".ui.ProductsFragment"
tools:listitem=
"@layout/fragment_products"
/>
...
...
@@ -49,7 +61,7 @@
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:padding=
"10dp"
android:text=
"
C
antidad"
/>
android:text=
"
@string/c
antidad"
/>
<TextView
android:id=
"@+id/txtQuantity"
...
...
@@ -65,7 +77,7 @@
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:padding=
"10dp"
android:text=
"
Total $
"
/>
android:text=
"
@string/totalPesos
"
/>
<TextView
android:id=
"@+id/txtTotal"
...
...
@@ -75,6 +87,5 @@
android:padding=
"10dp"
android:text=
"@{String.valueOf(cart.total)}"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
\ No newline at end of file
app/src/main/res/values/strings.xml
View file @
c7886e09
...
...
@@ -21,4 +21,6 @@
<string
name=
"btn_login"
>
Login
</string>
<string
name=
"btn_register"
>
Registrar
</string>
<string
name=
"btn_buy"
>
Comprar
</string>
<string
name=
"cantidad"
>
Cantidad
</string>
<string
name=
"totalPesos"
>
Total $
</string>
</resources>
\ No newline at end of file
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