Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CS123-CanteeneoAndroid
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Willard Torres
CS123-CanteeneoAndroid
Commits
f7b8d3da
Commit
f7b8d3da
authored
Dec 06, 2016
by
Willard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Overhaul searching and filtering (done client side instead of server side)
parent
15839804
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
135 additions
and
71 deletions
+135
-71
CanteeneoApiInterface.java
app/src/main/java/com/testapp/CanteeneoApiInterface.java
+2
-7
DishAdapter.java
app/src/main/java/com/testapp/DishAdapter.java
+33
-5
NavDrawerActivity.java
app/src/main/java/com/testapp/NavDrawerActivity.java
+49
-50
Dish.java
app/src/main/java/com/testapp/entities/Dish.java
+33
-0
ic_refresh.xml
app/src/main/res/drawable/ic_refresh.xml
+9
-0
activity_nav_drawer.xml
app/src/main/res/layout/activity_nav_drawer.xml
+2
-2
nav_drawer.xml
app/src/main/res/menu/nav_drawer.xml
+4
-4
strings.xml
app/src/main/res/values/strings.xml
+3
-3
No files found.
app/src/main/java/com/testapp/CanteeneoApiInterface.java
View file @
f7b8d3da
...
@@ -28,13 +28,8 @@ import retrofit2.http.Query;
...
@@ -28,13 +28,8 @@ import retrofit2.http.Query;
public
interface
CanteeneoApiInterface
{
public
interface
CanteeneoApiInterface
{
@GET
(
"api/search/dish"
)
@GET
(
"api/dishes"
)
Call
<
List
<
Dish
>>
searchByName
(
@Query
(
"name"
)
String
name
,
Call
<
List
<
Dish
>>
getAllDishes
();
@Query
(
"type_filters"
)
String
types
,
@Query
(
"cuisine_filters"
)
String
cuisines
,
@Query
(
"location_filters"
)
String
location
,
@Query
(
"min_price"
)
int
min_price
,
@Query
(
"max_price"
)
int
max_price
);
@FormUrlEncoded
@FormUrlEncoded
@POST
(
"api/users/new"
)
@POST
(
"api/users/new"
)
...
...
app/src/main/java/com/testapp/DishAdapter.java
View file @
f7b8d3da
...
@@ -6,27 +6,31 @@ import android.view.ViewGroup;
...
@@ -6,27 +6,31 @@ import android.view.ViewGroup;
import
android.widget.BaseAdapter
;
import
android.widget.BaseAdapter
;
import
android.widget.ImageView
;
import
android.widget.ImageView
;
import
android.widget.TextView
;
import
android.widget.TextView
;
import
com.squareup.picasso.Picasso
;
import
com.squareup.picasso.Picasso
;
import
com.testapp.entities.Dish
;
import
com.testapp.entities.Dish
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
public
class
DishAdapter
extends
BaseAdapter
{
public
class
DishAdapter
extends
BaseAdapter
{
private
Activity
context
;
private
Activity
context
;
ArrayList
<
Dish
>
dishes
;
ArrayList
<
Dish
>
dishes
;
ArrayList
<
Dish
>
filteredDishes
;
public
DishAdapter
(
Activity
context
,
ArrayList
<
Dish
>
dishes
)
{
public
DishAdapter
(
Activity
context
,
ArrayList
<
Dish
>
dishes
)
{
this
.
context
=
context
;
this
.
context
=
context
;
this
.
dishes
=
dishes
;
this
.
dishes
=
dishes
;
filteredDishes
=
new
ArrayList
<>();
}
}
@Override
@Override
public
int
getCount
()
{
public
int
getCount
()
{
return
d
ishes
.
size
();
return
filteredD
ishes
.
size
();
}
}
@Override
@Override
public
Dish
getItem
(
int
position
)
{
public
Dish
getItem
(
int
position
)
{
return
d
ishes
.
get
(
position
);
return
filteredD
ishes
.
get
(
position
);
}
}
@Override
@Override
...
@@ -48,7 +52,7 @@ public class DishAdapter extends BaseAdapter {
...
@@ -48,7 +52,7 @@ public class DishAdapter extends BaseAdapter {
holder
=
(
DishViewHolder
)
convertView
.
getTag
();
holder
=
(
DishViewHolder
)
convertView
.
getTag
();
}
}
Dish
d
=
d
ishes
.
get
(
position
);
Dish
d
=
filteredD
ishes
.
get
(
position
);
holder
.
name
.
setText
(
d
.
getName
());
holder
.
name
.
setText
(
d
.
getName
());
holder
.
price
.
setText
(
d
.
getPrice
()+
""
);
holder
.
price
.
setText
(
d
.
getPrice
()+
""
);
Picasso
.
with
(
context
).
load
(
"http://"
+
parent
.
getResources
().
getString
(
R
.
string
.
server_ip
)
+
":5000/static/uploads/"
+
d
.
getImagePath
()).
fit
().
centerCrop
().
tag
(
context
).
into
(
holder
.
image
);
Picasso
.
with
(
context
).
load
(
"http://"
+
parent
.
getResources
().
getString
(
R
.
string
.
server_ip
)
+
":5000/static/uploads/"
+
d
.
getImagePath
()).
fit
().
centerCrop
().
tag
(
context
).
into
(
holder
.
image
);
...
@@ -56,9 +60,33 @@ public class DishAdapter extends BaseAdapter {
...
@@ -56,9 +60,33 @@ public class DishAdapter extends BaseAdapter {
return
convertView
;
return
convertView
;
}
}
public
void
filter
(
String
nameFilter
,
ArrayList
<
Integer
>
typeFilter
,
ArrayList
<
Integer
>
cuisineFilter
,
ArrayList
<
Integer
>
locationFilter
,
double
minPrice
,
double
maxPrice
)
{
filteredDishes
.
clear
();
for
(
Dish
dish:
dishes
)
{
boolean
skip
=
false
;
String
[]
keywords
=
nameFilter
.
toLowerCase
().
split
(
" "
);
for
(
String
keyword:
keywords
)
{
if
(
dish
.
getName
().
toLowerCase
().
indexOf
(
keyword
)
==
-
1
)
{
skip
=
true
;
break
;
}
}
if
(
skip
)
continue
;
if
(
typeFilter
.
indexOf
(
dish
.
getTypeId
())
==
-
1
)
continue
;
if
(
cuisineFilter
.
indexOf
(
dish
.
getCuisineId
())
==
-
1
)
continue
;
if
(
locationFilter
.
indexOf
(
dish
.
getLocationId
())
==
-
1
)
continue
;
if
(
dish
.
getPrice
()
<
minPrice
||
dish
.
getPrice
()
>
maxPrice
)
continue
;
filteredDishes
.
add
(
dish
);
}
}
static
class
DishViewHolder
{
static
class
DishViewHolder
{
TextView
name
;
TextView
name
;
TextView
price
;
TextView
price
;
ImageView
image
;
ImageView
image
;
}
}
}
}
\ No newline at end of file
app/src/main/java/com/testapp/NavDrawerActivity.java
View file @
f7b8d3da
...
@@ -15,6 +15,7 @@ import android.support.v7.app.ActionBarDrawerToggle;
...
@@ -15,6 +15,7 @@ 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.SearchView
;
import
android.support.v7.widget.Toolbar
;
import
android.support.v7.widget.Toolbar
;
import
android.view.Gravity
;
import
android.view.LayoutInflater
;
import
android.view.LayoutInflater
;
import
android.view.Menu
;
import
android.view.Menu
;
import
android.view.MenuItem
;
import
android.view.MenuItem
;
...
@@ -46,6 +47,8 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
...
@@ -46,6 +47,8 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
public
static
String
EXTRA_MESSAGE
=
"com.testapp.MESSAGE"
;
public
static
String
EXTRA_MESSAGE
=
"com.testapp.MESSAGE"
;
String
query
=
""
;
@Override
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
super
.
onCreate
(
savedInstanceState
);
...
@@ -72,8 +75,6 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
...
@@ -72,8 +75,6 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
NavigationView
navigationView
=
(
NavigationView
)
findViewById
(
R
.
id
.
nav_view
);
NavigationView
navigationView
=
(
NavigationView
)
findViewById
(
R
.
id
.
nav_view
);
navigationView
.
setNavigationItemSelectedListener
(
this
);
navigationView
.
setNavigationItemSelectedListener
(
this
);
Intent
intent
=
getIntent
();
populateFilter
(
AppUtils
.
service
.
getDishTypes
(),
R
.
id
.
type_checkboxes
);
populateFilter
(
AppUtils
.
service
.
getDishTypes
(),
R
.
id
.
type_checkboxes
);
populateFilter
(
AppUtils
.
service
.
getCuisines
(),
R
.
id
.
cuisine_checkboxes
);
populateFilter
(
AppUtils
.
service
.
getCuisines
(),
R
.
id
.
cuisine_checkboxes
);
populateFilter
(
AppUtils
.
service
.
getLocations
(),
R
.
id
.
location_checkboxes
);
populateFilter
(
AppUtils
.
service
.
getLocations
(),
R
.
id
.
location_checkboxes
);
...
@@ -96,6 +97,15 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
...
@@ -96,6 +97,15 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
}
}
});
});
Button
applyFilterButton
=
(
Button
)
findViewById
(
R
.
id
.
apply_filter_button
);
applyFilterButton
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
refreshDishes
();
((
DrawerLayout
)
NavDrawerActivity
.
this
.
findViewById
(
R
.
id
.
drawer_layout
)).
closeDrawer
(
Gravity
.
LEFT
);
}
});
RelativeLayout
loggedInHeader
=
(
RelativeLayout
)
findViewById
(
R
.
id
.
logged_in_header
);
RelativeLayout
loggedInHeader
=
(
RelativeLayout
)
findViewById
(
R
.
id
.
logged_in_header
);
if
(
AppUtils
.
isLoggedIn
())
{
if
(
AppUtils
.
isLoggedIn
())
{
loggedInHeader
.
setVisibility
(
View
.
VISIBLE
);
loggedInHeader
.
setVisibility
(
View
.
VISIBLE
);
...
@@ -122,6 +132,23 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
...
@@ -122,6 +132,23 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
}
else
{
}
else
{
loggedInHeader
.
setVisibility
(
View
.
GONE
);
loggedInHeader
.
setVisibility
(
View
.
GONE
);
}
}
Call
<
List
<
Dish
>>
call
=
AppUtils
.
service
.
getAllDishes
();
call
.
enqueue
(
new
Callback
<
List
<
Dish
>>()
{
@Override
public
void
onResponse
(
Call
<
List
<
Dish
>>
call
,
Response
<
List
<
Dish
>>
response
)
{
List
<
Dish
>
newDishes
=
response
.
body
();
dishes
.
clear
();
dishes
.
addAll
(
newDishes
);
refreshDishes
();
}
@Override
public
void
onFailure
(
Call
<
List
<
Dish
>>
call
,
Throwable
t
)
{
}
});
}
}
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
)
{
...
@@ -176,91 +203,63 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
...
@@ -176,91 +203,63 @@ public class NavDrawerActivity extends AppCompatActivity implements NavigationVi
@Override
@Override
public
boolean
onCreateOptionsMenu
(
Menu
menu
)
{
public
boolean
onCreateOptionsMenu
(
Menu
menu
)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater
().
inflate
(
R
.
menu
.
nav_drawer
,
menu
);
getMenuInflater
().
inflate
(
R
.
menu
.
nav_drawer
,
menu
);
MenuItem
searchItem
=
menu
.
findItem
(
R
.
id
.
action_search
);
MenuItem
searchItem
=
menu
.
findItem
(
R
.
id
.
action_search
);
SearchView
sv
=
(
SearchView
)
searchItem
.
getActionView
();
final
SearchView
sv
=
(
SearchView
)
searchItem
.
getActionView
();
sv
.
setOnQueryTextListener
(
new
SearchView
.
OnQueryTextListener
()
{
sv
.
setOnQueryTextListener
(
new
SearchView
.
OnQueryTextListener
()
{
@Override
@Override
public
boolean
onQueryTextSubmit
(
String
query
)
{
public
boolean
onQueryTextSubmit
(
String
query
)
{
searchDish
(
query
);
return
false
;
return
false
;
}
}
@Override
@Override
public
boolean
onQueryTextChange
(
String
newText
)
{
public
boolean
onQueryTextChange
(
String
newText
)
{
query
=
newText
;
refreshDishes
();
return
false
;
return
false
;
}
}
});
});
return
true
;
}
@Override
MenuItem
refreshItem
=
menu
.
findItem
(
R
.
id
.
action_refresh
);
public
boolean
onOptionsItemSelected
(
MenuItem
item
)
{
refreshItem
.
setOnMenuItemClickListener
(
new
MenuItem
.
OnMenuItemClickListener
()
{
// Handle action bar item clicks here. The action bar will
@Override
// automatically handle clicks on the Home/Up button, so long
public
boolean
onMenuItemClick
(
MenuItem
item
)
{
// as you specify a parent activity in AndroidManifest.xml.
refreshDishes
();
int
id
=
item
.
getItemId
();
return
false
;
}
//noinspection SimplifiableIfStatement
});
if
(
id
==
R
.
id
.
action_settings
)
{
return
true
;
}
return
super
.
onOptionsItemSelected
(
item
)
;
return
true
;
}
}
@SuppressWarnings
(
"StatementWithEmptyBody"
)
@SuppressWarnings
(
"StatementWithEmptyBody"
)
@Override
@Override
public
boolean
onNavigationItemSelected
(
MenuItem
item
)
{
public
boolean
onNavigationItemSelected
(
MenuItem
item
)
{
// Handle navigation view item clicks here.
int
id
=
item
.
getItemId
();
if
(
id
==
R
.
id
.
nav_all_items
)
{
// Handle the camera action
}
else
if
(
id
==
R
.
id
.
nav_filter
)
{
}
DrawerLayout
drawer
=
(
DrawerLayout
)
findViewById
(
R
.
id
.
drawer_layout
);
//drawer.closeDrawer(GravityCompat.START);
return
true
;
return
true
;
}
}
private
String
getChecked
(
int
layoutId
)
{
private
ArrayList
<
Integer
>
getChecked
(
int
layoutId
)
{
LinearLayout
checkboxes
=
(
LinearLayout
)
findViewById
(
layoutId
);
LinearLayout
checkboxes
=
(
LinearLayout
)
findViewById
(
layoutId
);
String
checked
=
""
;
ArrayList
<
Integer
>
checked
=
new
ArrayList
<>()
;
for
(
int
i
=
0
;
i
<
checkboxes
.
getChildCount
();
i
++)
{
for
(
int
i
=
0
;
i
<
checkboxes
.
getChildCount
();
i
++)
{
View
v
=
checkboxes
.
getChildAt
(
i
);
View
v
=
checkboxes
.
getChildAt
(
i
);
if
(
v
instanceof
CheckBox
)
{
if
(
v
instanceof
CheckBox
)
{
CheckBox
c
=
(
CheckBox
)
v
;
CheckBox
c
=
(
CheckBox
)
v
;
if
(
c
.
isChecked
())
{
if
(
c
.
isChecked
())
{
checked
+=
c
.
getId
(
);
checked
.
add
(
c
.
getId
()
);
}
}
}
}
}
}
return
checked
;
return
checked
;
}
}
private
void
searchDish
(
String
name
)
{
public
void
refreshDishes
()
{
Call
<
List
<
Dish
>>
call
=
AppUtils
.
service
.
searchByName
(
name
,
getChecked
(
R
.
id
.
type_checkboxes
),
getChecked
(
R
.
id
.
cuisine_checkboxes
),
getChecked
(
R
.
id
.
location_checkboxes
),
0
,
1000
);
adapter
.
filter
(
query
,
getChecked
(
R
.
id
.
type_checkboxes
),
getChecked
(
R
.
id
.
cuisine_checkboxes
),
getChecked
(
R
.
id
.
location_checkboxes
),
0
,
1000
);
call
.
enqueue
(
new
Callback
<
List
<
Dish
>>()
{
adapter
.
notifyDataSetChanged
();
@Override
System
.
out
.
println
(
adapter
.
filteredDishes
.
size
());
public
void
onResponse
(
Call
<
List
<
Dish
>>
call
,
Response
<
List
<
Dish
>>
response
)
{
List
<
Dish
>
newDishes
=
response
.
body
();
dishes
.
clear
();
dishes
.
addAll
(
newDishes
);
adapter
.
notifyDataSetChanged
();
}
@Override
public
void
onFailure
(
Call
<
List
<
Dish
>>
call
,
Throwable
t
)
{
}
});
}
}
}
}
app/src/main/java/com/testapp/entities/Dish.java
View file @
f7b8d3da
...
@@ -9,6 +9,15 @@ public class Dish {
...
@@ -9,6 +9,15 @@ public class Dish {
double
price
;
double
price
;
String
description
;
String
description
;
@SerializedName
(
"type_id"
)
int
typeId
;
@SerializedName
(
"cuisine_id"
)
int
cuisineId
;
@SerializedName
(
"location_id"
)
int
locationId
;
@SerializedName
(
"image_path"
)
@SerializedName
(
"image_path"
)
String
imagePath
;
String
imagePath
;
...
@@ -70,4 +79,28 @@ public class Dish {
...
@@ -70,4 +79,28 @@ public class Dish {
public
void
setDescription
(
String
description
)
{
public
void
setDescription
(
String
description
)
{
this
.
description
=
description
;
this
.
description
=
description
;
}
}
public
int
getTypeId
()
{
return
typeId
;
}
public
void
setTypeId
(
int
typeId
)
{
this
.
typeId
=
typeId
;
}
public
int
getCuisineId
()
{
return
cuisineId
;
}
public
void
setCuisineId
(
int
cuisineId
)
{
this
.
cuisineId
=
cuisineId
;
}
public
int
getLocationId
()
{
return
locationId
;
}
public
void
setLocationId
(
int
locationId
)
{
this
.
locationId
=
locationId
;
}
}
}
app/src/main/res/drawable/ic_refresh.xml
0 → 100644
View file @
f7b8d3da
<vector
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:width=
"24dp"
android:height=
"24dp"
android:viewportWidth=
"24.0"
android:viewportHeight=
"24.0"
>
<path
android:fillColor=
"#FFFFFFFF"
android:pathData=
"M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"
/>
</vector>
app/src/main/res/layout/activity_nav_drawer.xml
View file @
f7b8d3da
...
@@ -174,10 +174,10 @@
...
@@ -174,10 +174,10 @@
</LinearLayout>
</LinearLayout>
<Button
<Button
android:text=
"@string/
hint_search
"
android:text=
"@string/
apply_filter
"
android:layout_width=
"match_parent"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_height=
"wrap_content"
android:id=
"@+id/
search
_button"
android:id=
"@+id/
apply_filter
_button"
android:elevation=
"10dp"
android:elevation=
"10dp"
style=
"@style/Widget.AppCompat.Button"
/>
style=
"@style/Widget.AppCompat.Button"
/>
...
...
app/src/main/res/menu/nav_drawer.xml
View file @
f7b8d3da
...
@@ -5,14 +5,14 @@
...
@@ -5,14 +5,14 @@
<item
<item
android:id=
"@+id/action_search"
android:id=
"@+id/action_search"
android:orderInCategory=
"100"
android:orderInCategory=
"100"
android:title=
"@string/
hint_search
"
android:title=
"@string/
apply_filter
"
android:icon=
"@android:drawable/ic_menu_search"
android:icon=
"@android:drawable/ic_menu_search"
app:actionViewClass=
"android.support.v7.widget.SearchView"
app:actionViewClass=
"android.support.v7.widget.SearchView"
app:showAsAction=
"always"
/>
app:showAsAction=
"always"
/>
<item
<item
android:id=
"@+id/action_
settings
"
android:id=
"@+id/action_
refresh
"
android:orderInCategory=
"101"
android:orderInCategory=
"101"
android:icon=
"@drawable/ic_
menu_manage
"
android:icon=
"@drawable/ic_
refresh
"
android:title=
"@string/action_
settings
"
android:title=
"@string/action_
refresh
"
app:showAsAction=
"always"
/>
app:showAsAction=
"always"
/>
</menu>
</menu>
app/src/main/res/values/strings.xml
View file @
f7b8d3da
...
@@ -24,15 +24,15 @@
...
@@ -24,15 +24,15 @@
<string
name=
"drawer_4"
>
Price Range
</string>
<string
name=
"drawer_4"
>
Price Range
</string>
<string
name=
"drawer_5"
>
Locations
</string>
<string
name=
"drawer_5"
>
Locations
</string>
<string
name=
"drawer_6"
>
Cuisine
</string>
<string
name=
"drawer_6"
>
Cuisine
</string>
<string
name=
"
hint_search"
>
Search
</string>
<string
name=
"
apply_filter"
>
Apply Filters
</string>
<string
name=
"navigation_drawer_open"
>
Open navigation drawer
</string>
<string
name=
"navigation_drawer_open"
>
Open navigation drawer
</string>
<string
name=
"navigation_drawer_close"
>
Close navigation drawer
</string>
<string
name=
"navigation_drawer_close"
>
Close navigation drawer
</string>
<string
name=
"action_
settings"
>
Settings
</string>
<string
name=
"action_
refresh"
>
Refresh
</string>
<string
name=
"title_activity_search"
>
SearchActivity
</string>
<string
name=
"title_activity_search"
>
SearchActivity
</string>
<string
name=
"server_ip"
>
1
92.168.11.18
</string>
<string
name=
"server_ip"
>
1
0.0.2.2
</string>
<string-array
name=
"sampleEntries"
>
<string-array
name=
"sampleEntries"
>
<item>
Item 111
</item>
<item>
Item 111
</item>
...
...
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